Skip to content

Commit 72b26e9

Browse files
committed
properly preparing statement with callback in createOrGetStatement()
1 parent b656d28 commit 72b26e9

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,25 @@ class SQLite3Driver internal constructor(private val db: Sqlite3.Database) : Sql
5252
internal fun _endTransactionForTests(successful: Boolean) = endTransaction(successful)
5353
}
5454

55-
private fun createOrGetStatement(identifier: Int?, sql: String): Sqlite3.Statement {
55+
private suspend fun createOrGetStatement(identifier: Int?, sql: String): Sqlite3.Statement {
56+
57+
val preparedStatement = suspendCoroutine { cont ->
58+
lateinit var statement: Sqlite3.Statement
59+
val callback: (Any) -> Unit = {
60+
if (it is Throwable) {
61+
cont.resumeWithException(SQLite3JsException(it))
62+
} else {
63+
cont.resume(statement)
64+
}
65+
}
66+
statement = db.prepare(sql, callback)
67+
}
68+
5669
val res = if (identifier == null) {
57-
db.prepare(sql)
70+
preparedStatement
5871
} else {
5972
statements.getOrPut(identifier) {
60-
val res2 = db.prepare(sql)
61-
return@getOrPut res2
73+
return@getOrPut preparedStatement
6274
}
6375
}
6476
return res
@@ -85,24 +97,19 @@ class SQLite3Driver internal constructor(private val db: Sqlite3.Database) : Sql
8597
}
8698

8799
override fun execute(identifier: Int?, sql: String, parameters: Int, binders: (SqlPreparedStatement.() -> Unit)?): QueryResult<Long> = QueryResult.AsyncValue {
88-
try {
89-
val statement = createOrGetStatement(identifier, sql)
90-
statement.bind(parameters, binders)
91-
suspendCoroutine { cont ->
92-
val callback: (Any) -> Unit = {
93-
if (it is Throwable) {
94-
cont.resumeWithException(SQLite3JsException(it))
95-
} else {
96-
cont.resume(Unit)
97-
}
100+
val statement = createOrGetStatement(identifier, sql)
101+
statement.bind(parameters, binders)
102+
suspendCoroutine { cont ->
103+
val callback: (Any) -> Unit = {
104+
if (it is Throwable) {
105+
cont.resumeWithException(SQLite3JsException(it))
106+
} else {
107+
cont.resume(Unit)
98108
}
99-
statement.run(callback)
100109
}
101-
return@AsyncValue 0
102-
} catch (e: Throwable) {
103-
println("exception $e")
104-
return@AsyncValue 0
110+
statement.run(callback)
105111
}
112+
return@AsyncValue 0
106113
}
107114

108115
override fun newTransaction(): QueryResult<Transacter.Transaction> = QueryResult.AsyncValue {
@@ -161,22 +168,16 @@ class SQLite3Driver internal constructor(private val db: Sqlite3.Database) : Sql
161168
if (parameters > 0) {
162169
val bound = SQLite3PreparedStatement(parameters)
163170
binders(bound)
164-
println("binders")
165171
suspendCoroutine { cont ->
166172
val callback: (Any?) -> Unit = {
167173
if (it == null || it !is Throwable) {
168-
println("bind resume")
169174
cont.resume(it)
170175
} else {
171-
println("bind resume exception")
172176
cont.resumeWithException(SQLite3JsException(it))
173177
}
174178
}
175-
println("bind")
176179
bind(bound.parameters.toTypedArray(), callback = callback)
177-
println("bind end")
178180
}
179-
println("bind after susp")
180181
}
181182
}
182183
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SQLite3DriverTest {
5353
println("db test.db created")
5454
block(driver)
5555
driver.close()
56-
println("deleteing db")
56+
println("deleting db")
5757
js("require('fs').unlinkSync('test.db')")
5858
} catch (e: Exception) {
5959
e.printStackTrace()
@@ -260,7 +260,7 @@ class SQLite3DriverTest {
260260
driver.await(13, "INSERT INTO nonexisting_table VALUES (?, ?);", 2, binders)
261261
}
262262

263-
// driver.newTransaction().await()
263+
driver.newTransaction().await()
264264
val success = try {
265265
insert {
266266
bindLong(0, 3)
@@ -274,7 +274,7 @@ class SQLite3DriverTest {
274274
try {
275275
assertFalse(success)
276276
} finally {
277-
// (driver as SQLite3Driver)._endTransactionForTests(success)
277+
(driver as SQLite3Driver)._endTransactionForTests(success)
278278
}
279279
}
280280

0 commit comments

Comments
 (0)