Skip to content

Commit a4de4dc

Browse files
committed
Merge branch 'main' into oscar/android-16kb-pages
# Conflicts: # example/ios/Podfile.lock
2 parents ed3b875 + 14336a6 commit a4de4dc

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

cpp/PreparedStatementHostObject.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
7474
});
7575
}
7676

77+
if (name == "bindSync") {
78+
return HOSTFN("bindSync") {
79+
if (_stmt == nullptr) {
80+
throw std::runtime_error("statement has been freed");
81+
}
82+
83+
const jsi::Value &js_params = args[0];
84+
std::vector<JSVariant> params = to_variant_vec(rt, js_params);
85+
try {
86+
#ifdef OP_SQLITE_USE_LIBSQL
87+
opsqlite_libsql_bind_statement(_stmt, &params);
88+
#else
89+
opsqlite_bind_statement(_stmt, &params);
90+
#endif
91+
} catch (const std::runtime_error &e) {
92+
throw std::runtime_error(e.what());
93+
} catch (const std::exception &e) {
94+
throw std::runtime_error(e.what());
95+
}
96+
return {};
97+
});
98+
}
99+
77100
if (name == "execute") {
78101
return HOSTFN("execute") {
79102
if (_stmt == nullptr) {

docs/docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const statement = db.prepareStatement('SELECT * FROM User WHERE name = ?;');
7070

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

7577
await statement.bind(['Carlos']);

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"node": ">=18"
6464
},
6565
"op-sqlite": {
66-
"libsql": true,
66+
"libsql": false,
6767
"sqlcipher": false,
6868
"iosSqlite": false,
6969
"fts5": true,

example/src/tests/preparedStatements.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,21 @@ export function preparedStatementsTests() {
8282
await statement.bind([5, 'Pedro']);
8383
await statement.execute();
8484
});
85+
86+
it('prepared statement, bindsync', async () => {
87+
const statement = db.prepareStatement(
88+
'INSERT INTO "User" (id, name) VALUES(?,?);',
89+
);
90+
91+
statement.bindSync([4, 'Juan']);
92+
await statement.execute();
93+
94+
statement.bind([5, 'Pedro']);
95+
await statement.execute();
96+
97+
const selectStatement = db.prepareStatement('SELECT * FROM User;');
98+
let results = await selectStatement.execute();
99+
expect(results.rows.length).to.equal(5);
100+
});
85101
});
86102
}

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ type PendingTransaction = {
9797

9898
export type PreparedStatement = {
9999
bind: (params: any[]) => Promise<void>;
100+
bindSync: (params: any[]) => void;
100101
execute: () => Promise<QueryResult>;
101102
};
102103

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

519520
return {
520-
bind: async (params: Scalar[]) => {
521-
const sanitizedParams = params.map((p) => {
522-
if (ArrayBuffer.isView(p)) {
523-
return p.buffer;
524-
}
521+
bindSync: (params: Scalar[]) => {
522+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
525523

526-
return p;
527-
});
524+
stmt.bindSync(sanitizedParams!);
525+
},
526+
bind: async (params: Scalar[]) => {
527+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
528528

529-
await stmt.bind(sanitizedParams);
529+
await stmt.bind(sanitizedParams!);
530530
},
531531
execute: stmt.execute,
532532
};

0 commit comments

Comments
 (0)