From 672defa80599cb140909bcf1093b3b8bd9cdc239 Mon Sep 17 00:00:00 2001 From: Eliezer Graber Date: Thu, 11 Sep 2025 15:11:38 -0400 Subject: [PATCH] Fix row count returned for insert/update/delete --- .../androidx/driver/AndroidxSqliteDriver.kt | 23 ++++++++-- .../driver/AndroidxSqliteDriverTest.kt | 46 +++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/library/src/commonMain/kotlin/com/eygraber/sqldelight/androidx/driver/AndroidxSqliteDriver.kt b/library/src/commonMain/kotlin/com/eygraber/sqldelight/androidx/driver/AndroidxSqliteDriver.kt index 249038f..4c06015 100644 --- a/library/src/commonMain/kotlin/com/eygraber/sqldelight/androidx/driver/AndroidxSqliteDriver.kt +++ b/library/src/commonMain/kotlin/com/eygraber/sqldelight/androidx/driver/AndroidxSqliteDriver.kt @@ -348,6 +348,14 @@ public class AndroidxSqliteDriver( ): QueryResult { createOrMigrateIfNeeded() + fun SQLiteConnection.getTotalChangedRows() = + prepare("SELECT changes()").use { statement -> + when { + statement.step() -> statement.getLong(0) + else -> 0 + } + } + val transaction = currentTransaction() if(transaction == null) { val writerConnection = connectionPool.acquireWriterConnection() @@ -362,7 +370,10 @@ public class AndroidxSqliteDriver( ) }, binders = binders, - result = { execute() }, + result = { + execute() + writerConnection.getTotalChangedRows() + }, ) } finally { connectionPool.releaseWriterConnection() @@ -379,7 +390,10 @@ public class AndroidxSqliteDriver( ) }, binders = binders, - result = { execute() }, + result = { + execute() + connection.getTotalChangedRows() + }, ) } } @@ -564,7 +578,7 @@ private fun SQLiteConnection.reportForeignKeyViolations( } internal interface AndroidxStatement : SqlPreparedStatement { - fun execute(): Long + fun execute() fun executeQuery(mapper: (SqlCursor) -> QueryResult): R fun reset() fun close() @@ -601,12 +615,11 @@ private class AndroidxPreparedStatement( override fun executeQuery(mapper: (SqlCursor) -> QueryResult): R = throw UnsupportedOperationException() - override fun execute(): Long { + override fun execute() { var cont = true while(cont) { cont = statement.step() } - return statement.getColumnCount().toLong() } override fun toString() = sql diff --git a/library/src/commonTest/kotlin/com/eygraber/sqldelight/androidx/driver/AndroidxSqliteDriverTest.kt b/library/src/commonTest/kotlin/com/eygraber/sqldelight/androidx/driver/AndroidxSqliteDriverTest.kt index a9f03ba..8a38dbf 100644 --- a/library/src/commonTest/kotlin/com/eygraber/sqldelight/androidx/driver/AndroidxSqliteDriverTest.kt +++ b/library/src/commonTest/kotlin/com/eygraber/sqldelight/androidx/driver/AndroidxSqliteDriverTest.kt @@ -282,4 +282,50 @@ abstract class AndroidxSqliteDriverTest { } } } + + @Test + fun `row count is correctly returned after an insert`() { + val rowCount = driver.execute(null, "INSERT INTO test VALUES (?, ?)", 2) { + bindLong(0, 1) + bindString(1, "42") + }.value + + assertEquals(1, rowCount) + } + + @Test + fun `row count is correctly returned after an update`() { + val rowCount = driver.execute(null, "UPDATE test SET value = ?", 1) { + bindString(0, "42") + }.value + + assertEquals(0, rowCount) + + driver.execute(null, "INSERT INTO test VALUES (?, ?)", 2) { + bindLong(0, 1) + bindString(1, "41") + } + + val rowCount2 = driver.execute(null, "UPDATE test SET value = ?", 1) { + bindString(0, "42") + }.value + + assertEquals(1, rowCount2) + } + + @Test + fun `row count is correctly returned after a delete`() { + val rowCount = driver.execute(null, "DELETE FROM test", 0).value + + assertEquals(0, rowCount) + + driver.execute(null, "INSERT INTO test VALUES (?, ?)", 2) { + bindLong(0, 1) + bindString(1, "41") + } + + val rowCount2 = driver.execute(null, "DELETE FROM test", 0).value + + assertEquals(1, rowCount2) + } }