sqlite: handle ?NNN parameters as positional

PR-URL: https://github.com/nodejs/node/pull/59350
Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
This commit is contained in:
Edy Silva 2025-08-22 09:56:58 -03:00 committed by GitHub
parent b85e77bf27
commit 3c1521cfa0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View File

@ -1941,7 +1941,9 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
}
for (int i = anon_start; i < args.Length(); ++i) {
while (sqlite3_bind_parameter_name(statement_, anon_idx) != nullptr) {
while (1) {
const char* param = sqlite3_bind_parameter_name(statement_, anon_idx);
if (param == nullptr || param[0] == '?') break;
anon_idx++;
}

View File

@ -240,6 +240,36 @@ suite('StatementSync.prototype.run()', () => {
stmt.run({ k: 3, v: 30 }), { changes: 1, lastInsertRowid: 3 }
);
});
test('SQLite defaults unbound ?NNN parameters', (t) => {
const db = new DatabaseSync(nextDb());
t.after(() => { db.close(); });
const setup = db.exec(
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER NOT NULL) STRICT;'
);
t.assert.strictEqual(setup, undefined);
const stmt = db.prepare('INSERT INTO data (key, val) VALUES (?1, ?3)');
t.assert.throws(() => {
stmt.run(1);
}, {
code: 'ERR_SQLITE_ERROR',
message: 'NOT NULL constraint failed: data.val',
errcode: 1299,
errstr: 'constraint failed',
});
});
test('binds ?NNN params by position', (t) => {
const db = new DatabaseSync(nextDb());
t.after(() => { db.close(); });
const setup = db.exec(
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER NOT NULL) STRICT;'
);
t.assert.strictEqual(setup, undefined);
const stmt = db.prepare('INSERT INTO data (key, val) VALUES (?1, ?2)');
t.assert.deepStrictEqual(stmt.run(1, 2), { changes: 1, lastInsertRowid: 1 });
});
});
suite('StatementSync.prototype.sourceSQL', () => {