Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #187. Add clear_bindings(). #198

Merged
merged 1 commit into from
Jul 26, 2024
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
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
Loading