Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cpp/PreparedStatementHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
});
}

if (name == "bindSync") {
return HOSTFN("bindSync") {
if (_stmt == nullptr) {
throw std::runtime_error("statement has been freed");
}

const jsi::Value &js_params = args[0];
std::vector<JSVariant> params = to_variant_vec(rt, js_params);
try {
#ifdef OP_SQLITE_USE_LIBSQL
opsqlite_libsql_bind_statement(_stmt, &params);
#else
opsqlite_bind_statement(_stmt, &params);
#endif
} catch (const std::runtime_error &e) {
throw std::runtime_error(e.what());
} catch (const std::exception &e) {
throw std::runtime_error(e.what());
}
return {};
});
}

if (name == "execute") {
return HOSTFN("execute") {
if (_stmt == nullptr) {
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const statement = db.prepareStatement('SELECT * FROM User WHERE name = ?;');

// bind the variables in the order they appear
await statement.bind(['Oscar']);
// Or use the bindsync version
statement.bindSync(['Luis']);
let results1 = await statement.execute();

await statement.bind(['Carlos']);
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ SPEC CHECKSUMS:
GCDWebServer: 2c156a56c8226e2d5c0c3f208a3621ccffbe3ce4
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
hermes-engine: 06a9c6900587420b90accc394199527c64259db4
op-sqlite: 94ed545f045bdcc18e9daeda43467fe30a5835fe
op-sqlite: e775e8b295b1371840bf8abaa8b634fce73cf0bd
RCT-Folly: bf5c0376ffe4dd2cf438dcf86db385df9fdce648
RCTDeprecation: fb7d408617e25d7f537940000d766d60149c5fea
RCTRequired: 9aaf0ffcc1f41f0c671af863970ef25c422a9920
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"node": ">=18"
},
"op-sqlite": {
"libsql": true,
"libsql": false,
"sqlcipher": false,
"iosSqlite": false,
"fts5": true,
Expand Down
16 changes: 16 additions & 0 deletions example/src/tests/preparedStatements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,21 @@ export function preparedStatementsTests() {
await statement.bind([5, 'Pedro']);
await statement.execute();
});

it('prepared statement, bindsync', async () => {
const statement = db.prepareStatement(
'INSERT INTO "User" (id, name) VALUES(?,?);',
);

statement.bindSync([4, 'Juan']);
await statement.execute();

statement.bind([5, 'Pedro']);
await statement.execute();

const selectStatement = db.prepareStatement('SELECT * FROM User;');
let results = await selectStatement.execute();
expect(results.rows.length).to.equal(5);
});
});
}
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type PendingTransaction = {

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

Expand Down Expand Up @@ -517,16 +518,15 @@ function enhanceDB(db: InternalDB, options: DBParams): DB {
const stmt = db.prepareStatement(query);

return {
bind: async (params: Scalar[]) => {
const sanitizedParams = params.map((p) => {
if (ArrayBuffer.isView(p)) {
return p.buffer;
}
bindSync: (params: Scalar[]) => {
const sanitizedParams = sanitizeArrayBuffersInArray(params);

return p;
});
stmt.bindSync(sanitizedParams!);
},
bind: async (params: Scalar[]) => {
const sanitizedParams = sanitizeArrayBuffersInArray(params);

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