Skip to content

Commit 7d45baf

Browse files
authored
Permit JS booleans to be bound to queries, as integer 0/1 values (rhashimoto#272)
* Permit boolean values to be bound to statements, as 0/1 * Add test for boolean binding
1 parent fa72fb0 commit 7d45baf

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/sqlite-api.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export function Factory(Module) {
118118
}
119119
case 'string':
120120
return sqlite3.bind_text(stmt, i, value);
121+
case "boolean":
122+
return sqlite3.bind_int(stmt, i, value ? 1 : 0);
121123
default:
122124
if (value instanceof Uint8Array || Array.isArray(value)) {
123125
return sqlite3.bind_blob(stmt, i, value);

test/api_statements.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,27 @@ export function api_statements(context) {
238238
}
239239
});
240240

241+
it('should bind boolean', async function() {
242+
let rc;
243+
const sql = 'SELECT ?';
244+
const storeValue = true;
245+
const expectedRetrievedValue = 1;
246+
247+
for await (const stmt of i(sqlite3.statements(db, sql))) {
248+
// Comlink intercepts the 'bind' property so use an alias.
249+
rc = await sqlite3.bind$(stmt, 1, storeValue);
250+
expect(rc).toEqual(SQLite.SQLITE_OK);
251+
252+
while ((rc = await sqlite3.step(stmt)) !== SQLite.SQLITE_DONE) {
253+
expect(rc).toEqual(SQLite.SQLITE_ROW);
254+
255+
expect(await sqlite3.column_count(stmt)).toEqual(1);
256+
expect(await sqlite3.column_type(stmt, 0)).toEqual(SQLite.SQLITE_INTEGER);
257+
expect(await sqlite3.column_int(stmt, 0)).toEqual(expectedRetrievedValue);
258+
}
259+
}
260+
});
261+
241262
it('should bind collection array', async function() {
242263
let rc;
243264
const sql = 'VALUES (?, ?, ?, ?, ?)';

0 commit comments

Comments
 (0)