Skip to content

Commit e2b6bdc

Browse files
geeksilva97targos
authored andcommitted
sqlite: handle ?NNN parameters as positional
PR-URL: #59350 Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
1 parent 93a368d commit e2b6bdc

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/node_sqlite.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,9 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
19331933
}
19341934

19351935
for (int i = anon_start; i < args.Length(); ++i) {
1936-
while (sqlite3_bind_parameter_name(statement_, anon_idx) != nullptr) {
1936+
while (1) {
1937+
const char* param = sqlite3_bind_parameter_name(statement_, anon_idx);
1938+
if (param == nullptr || param[0] == '?') break;
19371939
anon_idx++;
19381940
}
19391941

test/parallel/test-sqlite-statement-sync.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,36 @@ suite('StatementSync.prototype.run()', () => {
240240
stmt.run({ k: 3, v: 30 }), { changes: 1, lastInsertRowid: 3 }
241241
);
242242
});
243+
244+
test('SQLite defaults unbound ?NNN parameters', (t) => {
245+
const db = new DatabaseSync(nextDb());
246+
t.after(() => { db.close(); });
247+
const setup = db.exec(
248+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER NOT NULL) STRICT;'
249+
);
250+
t.assert.strictEqual(setup, undefined);
251+
const stmt = db.prepare('INSERT INTO data (key, val) VALUES (?1, ?3)');
252+
253+
t.assert.throws(() => {
254+
stmt.run(1);
255+
}, {
256+
code: 'ERR_SQLITE_ERROR',
257+
message: 'NOT NULL constraint failed: data.val',
258+
errcode: 1299,
259+
errstr: 'constraint failed',
260+
});
261+
});
262+
263+
test('binds ?NNN params by position', (t) => {
264+
const db = new DatabaseSync(nextDb());
265+
t.after(() => { db.close(); });
266+
const setup = db.exec(
267+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER NOT NULL) STRICT;'
268+
);
269+
t.assert.strictEqual(setup, undefined);
270+
const stmt = db.prepare('INSERT INTO data (key, val) VALUES (?1, ?2)');
271+
t.assert.deepStrictEqual(stmt.run(1, 2), { changes: 1, lastInsertRowid: 1 });
272+
});
243273
});
244274

245275
suite('StatementSync.prototype.sourceSQL', () => {

0 commit comments

Comments
 (0)