Skip to content

Commit

Permalink
Merge pull request #198 from rhashimoto/clear-bindings
Browse files Browse the repository at this point in the history
Fix #187. Add clear_bindings().
  • Loading branch information
rhashimoto authored Jul 26, 2024
2 parents 3d5aa74 + 001852b commit 5fef539
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/assets/search.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions docs/interfaces/SQLiteAPI.html

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/sqlite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ export function Factory(Module) {
};
})();

sqlite3.clear_bindings = (function() {
const fname = 'sqlite3_clear_bindings';
const f = Module.cwrap(fname, ...decl('n:n'));
return function(stmt) {
verifyStatement(stmt);
const result = f(stmt);
return check(fname, result, mapStmtToDB.get(stmt));
};
})();

sqlite3.close = (function() {
const fname = 'sqlite3_close';
const f = Module.cwrap(fname, ...decl('n:n'), { async });
Expand Down
8 changes: 8 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,14 @@ declare interface SQLiteAPI {
*/
changes(db): number;

/**
* Reset all bindings on a prepared statement.
* @see https://www.sqlite.org/c3ref/clear_bindings.html
* @param stmt prepared statement pointer
* @returns `SQLITE_OK` (throws exception on error)
*/
clear_bindings(stmt: number): number;

/**
* Close database connection
* @see https://www.sqlite.org/c3ref/close.html
Expand Down
35 changes: 35 additions & 0 deletions test/api_statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,41 @@ export function api_statements(context) {
rc = await sqlite3.finalize(stmt);
expect(rc).toEqual(SQLite.SQLITE_OK);
});

it('should clear bindings', async function() {
let rc;

const sql = 'SELECT ?';
for await (const stmt of i(sqlite3.statements(db, sql))) {
{
rc = await sqlite3.bind_int(stmt, 1, 42);
expect(rc).toEqual(SQLite.SQLITE_OK);

rc = await sqlite3.reset(stmt);
expect(rc).toEqual(SQLite.SQLITE_OK);

rc = await sqlite3.step(stmt);
expect(rc).toEqual(SQLite.SQLITE_ROW);

const value = await sqlite3.column(stmt, 0);
expect(value).toEqual(42);
}

{
rc = await sqlite3.clear_bindings(stmt);
expect(rc).toEqual(SQLite.SQLITE_OK);

rc = await sqlite3.reset(stmt);
expect(rc).toEqual(SQLite.SQLITE_OK);

rc = await sqlite3.step(stmt);
expect(rc).toEqual(SQLite.SQLITE_ROW);

const value = await sqlite3.column(stmt, 0);
expect(value).not.toEqual(42);
}
}
});
});
}

Expand Down

0 comments on commit 5fef539

Please sign in to comment.