Skip to content

Commit 9592162

Browse files
committed
Fixing statement finalization
1 parent 9246009 commit 9592162

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

src/jsMain/kotlin/cz/sazel/sqldelight/node/sqlite3/SQLite3Cursor.kt

+6-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ internal class SQLite3Cursor(val statementInit: suspend () -> Sqlite3.Statement)
2424
reset()
2525
initialized = true
2626
}
27-
row = fetchRow() ?: return@AsyncValue false
27+
row = fetchRow()
28+
if (row == null) {
29+
_close()
30+
return@AsyncValue false
31+
}
2832
counter++
2933
true
3034
}
@@ -47,12 +51,7 @@ internal class SQLite3Cursor(val statementInit: suspend () -> Sqlite3.Statement)
4751

4852
@Suppress("FunctionNaming")
4953
internal suspend fun _close() {
50-
suspendCoroutine { cont ->
51-
val callback: (Error?) -> Unit = { err ->
52-
if (err == null) cont.resume(Unit) else cont.resumeWithException(SQLite3JsException(err))
53-
}
54-
statement.finalize(callback)
55-
}
54+
statement.finalizeSuspending()
5655
}
5756

5857
private suspend fun reset() {
@@ -61,7 +60,6 @@ internal class SQLite3Cursor(val statementInit: suspend () -> Sqlite3.Statement)
6160
cont.resume(Unit)
6261
}
6362
statement.reset(callback)
64-
6563
}
6664
}
6765

src/jsMain/kotlin/cz/sazel/sqldelight/node/sqlite3/SQLite3Driver.kt

+20-18
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,30 @@ private fun initSqlite3Database(
2525
internal suspend fun SQLite3Driver.withSchema(schema: SqlSchema<QueryResult.AsyncValue<Unit>>? = null) =
2626
this.also { schema?.create(it)?.await() }
2727

28+
internal suspend fun (Sqlite3.Statement).finalizeSuspending() {
29+
suspendCoroutine<Any?> { cont ->
30+
val callback: (err: Error?) -> Unit = {
31+
if (it == null) {
32+
cont.resume(it)
33+
} else {
34+
cont.resumeWithException(SQLite3JsException(it))
35+
}
36+
}
37+
finalize(callback)
38+
}
39+
}
40+
2841
class SQLite3Driver internal constructor(private val db: Sqlite3.Database) : SqlDriver {
2942
private val listeners = mutableMapOf<String, MutableSet<Query.Listener>>()
30-
private val statements = mutableMapOf<Int, Sqlite3.Statement>()
3143
private var transaction: Transaction? = null
3244

3345
internal inner class Transaction(
3446
override val enclosingTransaction: Transacter.Transaction?,
3547
) : Transacter.Transaction() {
48+
internal val statements = mutableMapOf<Int, Sqlite3.Statement>()
3649
override fun endTransaction(successful: Boolean): QueryResult<Unit> = QueryResult.AsyncValue {
3750
if (enclosingTransaction == null) {
51+
statements.onEach { it.value.finalizeSuspending() }
3852
val sql = if (successful) "END TRANSACTION" else "ROLLBACK TRANSACTION"
3953
suspendCoroutine<Any?> { cont ->
4054
val callback: (Any?) -> Unit = {
@@ -73,9 +87,9 @@ class SQLite3Driver internal constructor(private val db: Sqlite3.Database) : Sql
7387
val res = if (identifier == null) {
7488
preparedStatement
7589
} else {
76-
statements.getOrPut(identifier) {
90+
transaction?.statements?.getOrPut(identifier) {
7791
return@getOrPut preparedStatement
78-
}
92+
} ?: preparedStatement
7993
}
8094
return res
8195
}
@@ -96,6 +110,9 @@ class SQLite3Driver internal constructor(private val db: Sqlite3.Database) : Sql
96110
}
97111
statement.run(callback)
98112
}
113+
if (transaction == null) {
114+
statement.finalizeSuspending()
115+
}
99116
return@AsyncValue 0
100117
}
101118

@@ -138,21 +155,6 @@ class SQLite3Driver internal constructor(private val db: Sqlite3.Database) : Sql
138155

139156
internal fun _endTransactionForTests(successful: Boolean) = transaction?._endTransactionForTests(successful)
140157

141-
internal suspend fun _finalizeAllStatements() {
142-
statements.onEach { statement ->
143-
suspendCoroutine<Any?> { cont ->
144-
val callback: (err: Error?) -> Unit = {
145-
if (it == null) {
146-
cont.resume(it)
147-
} else {
148-
cont.resumeWithException(SQLite3JsException(it))
149-
}
150-
}
151-
statement.value.finalize(callback)
152-
}
153-
}
154-
}
155-
156158
override fun addListener(vararg queryKeys: String, listener: Query.Listener) {
157159
queryKeys.forEach {
158160
listeners.getOrPut(it) { mutableSetOf() }.add(listener)

src/jsTest/kotlin/cz/sazel/sqldelight/node/sqlite3/SQLite3DriverTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class SQLite3DriverTest {
5252
val driver = initSqlite3SqlDriver("test.db", schema = schema)
5353
println("db test.db created")
5454
block(driver)
55-
driver._finalizeAllStatements()
5655
driver.close()
5756
println("deleting db")
5857
js("require('fs').unlinkSync('test.db')")

0 commit comments

Comments
 (0)