Skip to content

Commit 3478130

Browse files
geeksilva97richardlau
authored andcommitted
sqlite: handle ?NNN parameters as positional
PR-URL: #59350 Reviewed-By: Zeyu "Alex" Yang <[email protected]>
1 parent 1b4885a commit 3478130

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
@@ -1932,7 +1932,9 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
19321932
}
19331933

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

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)