diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index e138d120197e33..d6ce58e6492470 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -611,19 +611,8 @@ bool StatementSync::BindParams(const FunctionCallbackInfo& 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; @@ -638,11 +627,17 @@ bool StatementSync::BindParams(const FunctionCallbackInfo& 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; } diff --git a/test/parallel/test-sqlite-named-parameters.js b/test/parallel/test-sqlite-named-parameters.js index d8091a0fd14b13..28d8d587885fd4 100644 --- a/test/parallel/test-sqlite-named-parameters.js +++ b/test/parallel/test-sqlite-named-parameters.js @@ -13,7 +13,7 @@ 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( @@ -21,12 +21,11 @@ suite('named parameters', () => { ); 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, }); });