Skip to content

Commit 6773b83

Browse files
committed
sqlite: bind undefined to null
1 parent c9acf34 commit 6773b83

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/node_sqlite.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,8 @@ bool StatementSync::BindValue(const Local<Value>& value, const int index) {
22932293
buf.data(),
22942294
static_cast<sqlite3_uint64>(buf.length()),
22952295
SQLITE_TRANSIENT);
2296+
} else if (value->IsUndefined()) {
2297+
r = sqlite3_bind_null(statement_, index);
22962298
} else if (value->IsBigInt()) {
22972299
bool lossless;
22982300
int64_t as_int = value.As<BigInt>()->Int64Value(&lossless);

test/parallel/test-sqlite-data-types.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ suite('data binding and mapping', () => {
4646
stmt.run(4, 99n, 0xf, '', new Uint8Array()),
4747
{ changes: 1, lastInsertRowid: 4 },
4848
);
49+
t.assert.deepStrictEqual(
50+
stmt.run(5, undefined, undefined, undefined, undefined),
51+
{ changes: 1, lastInsertRowid: 5 },
52+
);
4953

5054
const query = db.prepare('SELECT * FROM types WHERE key = ?');
5155
t.assert.deepStrictEqual(query.get(1), {
@@ -80,6 +84,21 @@ suite('data binding and mapping', () => {
8084
text: '',
8185
buf: new Uint8Array(),
8286
});
87+
t.assert.deepStrictEqual(query.get(5), {
88+
__proto__: null,
89+
key: 5,
90+
int: null,
91+
double: null,
92+
text: null,
93+
buf: null,
94+
});
95+
96+
const insertNamedParams = db.prepare('INSERT INTO types (key, int, double, text, buf) VALUES ($key, $int, $double, $text, $buf)');
97+
const params = { key: 6, int: undefined, double: undefined, text: undefined, buf: undefined };
98+
t.assert.deepStrictEqual(insertNamedParams.run(params), { changes: 1, lastInsertRowid: 6 });
99+
t.assert.deepStrictEqual(insertNamedParams.run({ key: 7 }), { changes: 1, lastInsertRowid: 7 });
100+
t.assert.deepStrictEqual(query.get(6), { __proto__: null, key: 6, int: null, double: null, text: null, buf: null });
101+
t.assert.deepStrictEqual(query.get(7), { __proto__: null, key: 7, int: null, double: null, text: null, buf: null });
83102
});
84103

85104
test('large strings are bound correctly', (t) => {
@@ -126,7 +145,6 @@ suite('data binding and mapping', () => {
126145
t.assert.strictEqual(setup, undefined);
127146

128147
[
129-
undefined,
130148
() => {},
131149
Symbol(),
132150
/foo/,

0 commit comments

Comments
 (0)