Up
03/28/2026, 2:35 PMreplace() statement?Chantal Loncle
03/31/2026, 4:19 PMreplace() overload you're interested in returning values from?
The variant that takes a query as its arg returns the affected row count automatically.
The variant that takes column-value mappings works exactly like an insert() or upsert(). So you can access different values directly from that statement, like the affected row count and any values retrieved after statement execution:
transaction(db) {
val stmt = TestTable.replace {
it[username] = "name1"
it[session] = "session1".toByteArray()
}
val affectedRowCount = stmt.insertedCount
val insertedServerId = stmt.resultedValues
?.singleOrNull()
?.getOrNull(TestTable.serverID)
}
The content of resultedValues is filled with whatever the underlying database sends back after the statement is run. For MySQL, this generally tends to be values included in the statement, as well as any generated key values.Up
03/31/2026, 6:11 PMXXX.findById(id)?.apply {
block()
} ?: XXX.new(id) {
block()
}
but that generates 2 SQL queries rather than a single INSERT ... ON CONFLICT DO UPDATE SET ... (postgres)