also is there a way to return the value from a `re...
# exposed
u
also is there a way to return the value from a
replace()
statement?
c
Hi @Up Could you confirm which
replace()
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:
Copy code
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.
u
nevermind on this, I've since restructured my approach to use DAO as much as possible. on that note, is there no DAO upsert? right now I'm having to do something like
Copy code
XXX.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)