Skip to content

Commit

Permalink
Change bind to async function
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Dec 16, 2024
1 parent 8745181 commit 6b2b98b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
38 changes: 35 additions & 3 deletions cpp/PreparedStatementHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,45 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,

const jsi::Value &js_params = args[0];
std::vector<JSVariant> 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<jsi::Value>(rt, args[0]);
auto reject = std::make_shared<jsi::Value>(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, &params);
opsqlite_libsql_bind_statement(_stmt, &params);
#else
opsqlite_bind_statement(_stmt, &params);
opsqlite_bind_statement(_stmt, &params);
#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;
});
}

Expand Down
8 changes: 4 additions & 4 deletions example/src/tests/preparedStatements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand All @@ -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();
});
});
Expand Down
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export interface PendingTransaction {
}

export type PreparedStatementObj = {
bind: (params: any[]) => void;
bind: (params: any[]) => Promise<void>;
execute: () => Promise<QueryResult>;
};

Expand Down Expand Up @@ -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<string, Scalar> = {};
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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;
Expand All @@ -360,7 +366,7 @@ function enhanceDB(db: DB, options: any): DB {
return p;
});

stmt.bind(sanitizedParams);
await stmt.bind(sanitizedParams);
},
execute: stmt.execute,
};
Expand Down

0 comments on commit 6b2b98b

Please sign in to comment.