Skip to content

Commit 5fef539

Browse files
authored
Merge pull request #198 from rhashimoto/clear-bindings
Fix #187. Add clear_bindings().
2 parents 3d5aa74 + 001852b commit 5fef539

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

docs/assets/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/interfaces/SQLiteAPI.html

Lines changed: 7 additions & 2 deletions
Large diffs are not rendered by default.

src/sqlite-api.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ export function Factory(Module) {
236236
};
237237
})();
238238

239+
sqlite3.clear_bindings = (function() {
240+
const fname = 'sqlite3_clear_bindings';
241+
const f = Module.cwrap(fname, ...decl('n:n'));
242+
return function(stmt) {
243+
verifyStatement(stmt);
244+
const result = f(stmt);
245+
return check(fname, result, mapStmtToDB.get(stmt));
246+
};
247+
})();
248+
239249
sqlite3.close = (function() {
240250
const fname = 'sqlite3_close';
241251
const f = Module.cwrap(fname, ...decl('n:n'), { async });

src/types/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ declare interface SQLiteAPI {
330330
*/
331331
changes(db): number;
332332

333+
/**
334+
* Reset all bindings on a prepared statement.
335+
* @see https://www.sqlite.org/c3ref/clear_bindings.html
336+
* @param stmt prepared statement pointer
337+
* @returns `SQLITE_OK` (throws exception on error)
338+
*/
339+
clear_bindings(stmt: number): number;
340+
333341
/**
334342
* Close database connection
335343
* @see https://www.sqlite.org/c3ref/close.html

test/api_statements.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,41 @@ export function api_statements(context) {
366366
rc = await sqlite3.finalize(stmt);
367367
expect(rc).toEqual(SQLite.SQLITE_OK);
368368
});
369+
370+
it('should clear bindings', async function() {
371+
let rc;
372+
373+
const sql = 'SELECT ?';
374+
for await (const stmt of i(sqlite3.statements(db, sql))) {
375+
{
376+
rc = await sqlite3.bind_int(stmt, 1, 42);
377+
expect(rc).toEqual(SQLite.SQLITE_OK);
378+
379+
rc = await sqlite3.reset(stmt);
380+
expect(rc).toEqual(SQLite.SQLITE_OK);
381+
382+
rc = await sqlite3.step(stmt);
383+
expect(rc).toEqual(SQLite.SQLITE_ROW);
384+
385+
const value = await sqlite3.column(stmt, 0);
386+
expect(value).toEqual(42);
387+
}
388+
389+
{
390+
rc = await sqlite3.clear_bindings(stmt);
391+
expect(rc).toEqual(SQLite.SQLITE_OK);
392+
393+
rc = await sqlite3.reset(stmt);
394+
expect(rc).toEqual(SQLite.SQLITE_OK);
395+
396+
rc = await sqlite3.step(stmt);
397+
expect(rc).toEqual(SQLite.SQLITE_ROW);
398+
399+
const value = await sqlite3.column(stmt, 0);
400+
expect(value).not.toEqual(42);
401+
}
402+
}
403+
});
369404
});
370405
}
371406

0 commit comments

Comments
 (0)