@@ -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}
0 commit comments