Skip to content

Commit d028d90

Browse files
committed
sqlite: handle ?NNN parameters as positional
1 parent f441317 commit d028d90

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

19441944
for (int i = anon_start; i < args.Length(); ++i){
1945-
while (sqlite3_bind_parameter_name(statement_, anon_idx) != nullptr){
1945+
while (1){
1946+
constchar* param = sqlite3_bind_parameter_name(statement_, anon_idx);
1947+
if (param == nullptr || param[0] == '?') break;
19461948
anon_idx++;
19471949
}
19481950

‎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+
constdb=newDatabaseSync(nextDb());
246+
t.after(()=>{db.close();});
247+
constsetup=db.exec(
248+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER NOT NULL) STRICT;'
249+
);
250+
t.assert.strictEqual(setup,undefined);
251+
conststmt=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+
constdb=newDatabaseSync(nextDb());
265+
t.after(()=>{db.close();});
266+
constsetup=db.exec(
267+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER NOT NULL) STRICT;'
268+
);
269+
t.assert.strictEqual(setup,undefined);
270+
conststmt=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)