Skip to content

Commit f24c817

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

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/node_sqlite.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ bool StatementSync::BindValue(const Local<Value>& value, const int index) {
22842284
SQLITE_TRANSIENT,
22852285
SQLITE_UTF8);
22862286
}
2287-
} else if (value->IsNull()) {
2287+
} else if (value->IsNull() || value->IsUndefined()) {
22882288
r = sqlite3_bind_null(statement_, index);
22892289
} else if (value->IsArrayBufferView()) {
22902290
ArrayBufferViewContents<uint8_t> buf(value);

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ suite('data binding and mapping', () => {
8080
text: '',
8181
buf: new Uint8Array(),
8282
});
83+
84+
const nulls = { int: null, double: null, text: null, buf: null };
85+
const undefObj = {
86+
int: undefined,
87+
double: undefined,
88+
text: undefined,
89+
buf: undefined,
90+
};
91+
const insertAnon = db.prepare('INSERT INTO types VALUES (?, ?, ?, ?, ?)');
92+
const insertNamed = db.prepare(
93+
'INSERT INTO types VALUES ($key, $int, $double, $text, $buf)'
94+
);
95+
t.assert.deepStrictEqual(
96+
insertAnon.run(6, undefined, undefined, undefined, undefined),
97+
{ lastInsertRowid: 6, changes: 1 },
98+
);
99+
t.assert.deepStrictEqual(
100+
insertNamed.run({ key: 7, ...undefObj }),
101+
{ lastInsertRowid: 7, changes: 1 },
102+
);
103+
t.assert.deepStrictEqual(
104+
insertNamed.run({ key: 8 }),
105+
{ lastInsertRowid: 8, changes: 1 },
106+
);
107+
t.assert.deepStrictEqual(query.get(6), { __proto__: null, key: 6, ...nulls });
108+
t.assert.deepStrictEqual(query.get(7), { __proto__: null, key: 7, ...nulls });
109+
t.assert.deepStrictEqual(query.get(8), { __proto__: null, key: 8, ...nulls });
83110
});
84111

85112
test('large strings are bound correctly', (t) => {
@@ -126,7 +153,6 @@ suite('data binding and mapping', () => {
126153
t.assert.strictEqual(setup, undefined);
127154

128155
[
129-
undefined,
130156
() => {},
131157
Symbol(),
132158
/foo/,

0 commit comments

Comments
 (0)