Skip to content

src: allow unknown sqlite parameters #55931

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

Closed
Closed
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
23 changes: 9 additions & 14 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -611,19 +611,8 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
Utf8Value utf8_key(env()->isolate(), key);
int r = sqlite3_bind_parameter_index(statement_, *utf8_key);
if (r == 0) {
if (allow_bare_named_params_) {
auto lookup = bare_named_params_->find(std::string(*utf8_key));
if (lookup != bare_named_params_->end()) {
r = sqlite3_bind_parameter_index(statement_,
lookup->second.c_str());
}
}

if (r == 0) {
THROW_ERR_INVALID_STATE(
env(), "Unknown named parameter '%s'", *utf8_key);
return false;
}
// Ignore unknown named parameter
continue;
}

Local<Value> value;
Expand All @@ -638,11 +627,17 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
anon_start++;
}

for (int i = anon_start; i < args.Length(); ++i) {
int param_count = sqlite3_bind_parameter_count(statement_);

for (int i = anon_start; i < args.Length() && anon_idx <= param_count; ++i) {
while (sqlite3_bind_parameter_name(statement_, anon_idx) != nullptr) {
anon_idx++;
}

if (anon_idx > param_count) {
break; // Ignore extra parameters
}

if (!BindValue(args[i], anon_idx)) {
return false;
}
Expand Down
13 changes: 6 additions & 7 deletions test/parallel/test-sqlite-named-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ function nextDb() {
}

suite('named parameters', () => {
test('throws on unknown named parameters', (t) => {
test('unknown named parameters are supported', (t) => {
const db = new DatabaseSync(nextDb());
t.after(() => { db.close(); });
const setup = db.exec(
'CREATE TABLE types(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
);
t.assert.strictEqual(setup, undefined);

t.assert.throws(() => {
const stmt = db.prepare('INSERT INTO types (key, val) VALUES ($k, $v)');
stmt.run({ $k: 1, $unknown: 1 });
}, {
code: 'ERR_INVALID_STATE',
message: /Unknown named parameter '\$unknown'/,
const stmt = db.prepare('INSERT INTO types (key, val) VALUES ($k, $v)');
t.assert.deepStrictEqual(stmt.run({ $k: 1, $unknown: 1, $v: 1 }, true),
{
changes: 1,
lastInsertRowid: 1,
});
});

Expand Down