From 6b2b98bf6a6bf207df21a19b6e7e327345ee576f Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Mon, 16 Dec 2024 16:19:11 +0100 Subject: [PATCH] Change bind to async function --- cpp/PreparedStatementHostObject.cpp | 38 ++++++++++++++++++-- example/src/tests/preparedStatements.spec.ts | 8 ++--- src/index.ts | 16 ++++++--- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/cpp/PreparedStatementHostObject.cpp b/cpp/PreparedStatementHostObject.cpp index f2f9daed..ffb7754f 100644 --- a/cpp/PreparedStatementHostObject.cpp +++ b/cpp/PreparedStatementHostObject.cpp @@ -30,13 +30,45 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt, const jsi::Value &js_params = args[0]; std::vector params = to_variant_vec(rt, js_params); + + auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise"); + auto promise = promiseCtr.callAsConstructor( + rt, HOSTFN("executor") { + auto resolve = std::make_shared(rt, args[0]); + auto reject = std::make_shared(rt, args[1]); + auto task = [&rt, this, resolve, reject, + invoker = this->_js_call_invoker, params]() { + try { #ifdef OP_SQLITE_USE_LIBSQL - opsqlite_libsql_bind_statement(_stmt, ¶ms); + opsqlite_libsql_bind_statement(_stmt, ¶ms); #else - opsqlite_bind_statement(_stmt, ¶ms); + opsqlite_bind_statement(_stmt, ¶ms); #endif + invoker->invokeAsync([&rt, resolve] { + resolve->asObject(rt).asFunction(rt).call(rt, {}); + }); + } catch (const std::runtime_error &e) { + invoker->invokeAsync([&rt, e, reject] { + auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error"); + auto error = errorCtr.callAsConstructor( + rt, jsi::String::createFromUtf8(rt, e.what())); + reject->asObject(rt).asFunction(rt).call(rt, error); + }); + } catch (const std::exception &e) { + invoker->invokeAsync([&rt, e, reject] { + auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error"); + auto error = errorCtr.callAsConstructor( + rt, jsi::String::createFromUtf8(rt, e.what())); + reject->asObject(rt).asFunction(rt).call(rt, error); + }); + } + }; + + _thread_pool->queueWork(task); - return {}; + return {}; + })); + return promise; }); } diff --git a/example/src/tests/preparedStatements.spec.ts b/example/src/tests/preparedStatements.spec.ts index f97e23ab..be066fe3 100644 --- a/example/src/tests/preparedStatements.spec.ts +++ b/example/src/tests/preparedStatements.spec.ts @@ -62,12 +62,12 @@ export function preparedStatementsTests() { it('prepared statement, rebind select', async () => { const statement = db.prepareStatement('SELECT * FROM User WHERE id = ?;'); - statement.bind([1]); + await statement.bind([1]); let results = await statement.execute(); expect(results.rows[0]!.name === 'Oscar'); - statement.bind([2]); + await statement.bind([2]); results = await statement.execute(); expect(results.rows[0]!.name === 'Pablo'); }); @@ -76,10 +76,10 @@ export function preparedStatementsTests() { const statement = db.prepareStatement( 'INSERT INTO "User" (id, name) VALUES(?,?);', ); - statement.bind([4, 'Juan']); + await statement.bind([4, 'Juan']); await statement.execute(); - statement.bind([5, 'Pedro']); + await statement.bind([5, 'Pedro']); await statement.execute(); }); }); diff --git a/src/index.ts b/src/index.ts index 53d41848..a5158cc5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -132,7 +132,7 @@ export interface PendingTransaction { } export type PreparedStatementObj = { - bind: (params: any[]) => void; + bind: (params: any[]) => Promise; execute: () => Promise; }; @@ -288,7 +288,10 @@ function enhanceDB(db: DB, options: any): DB { return p; }); - let intermediateResult = db.executeSync(query, sanitizedParams); + let intermediateResult = db.executeSync( + query, + sanitizedParams as Scalar[] + ); let rows: any[] = []; for (let i = 0; i < (intermediateResult.rawRows?.length ?? 0); i++) { let row: Record = {}; @@ -323,7 +326,10 @@ function enhanceDB(db: DB, options: any): DB { return p; }); - let intermediateResult = await db.execute(query, sanitizedParams); + let intermediateResult = await db.execute( + query, + sanitizedParams as Scalar[] + ); let rows: any[] = []; for (let i = 0; i < (intermediateResult.rawRows?.length ?? 0); i++) { @@ -351,7 +357,7 @@ function enhanceDB(db: DB, options: any): DB { const stmt = db.prepareStatement(query); return { - bind: (params: any[]) => { + bind: async (params: any[]) => { const sanitizedParams = params.map((p) => { if (ArrayBuffer.isView(p)) { return p.buffer; @@ -360,7 +366,7 @@ function enhanceDB(db: DB, options: any): DB { return p; }); - stmt.bind(sanitizedParams); + await stmt.bind(sanitizedParams); }, execute: stmt.execute, };