Skip to content

Commit cc04534

Browse files
Copilotmathiasrw
andcommitted
Fix NULL values in INTO SQL() - handle null and undefined properly
Co-authored-by: mathiasrw <[email protected]>
1 parent d1b867d commit cc04534

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/830into.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,31 @@ alasql.into.SQL = function (filename, opts, data, columns, cb) {
3636
})
3737
.join(',');
3838
s += ') VALUES (';
39-
s += columns.map(function (col) {
40-
var val = data[i][col.columnid];
41-
if (col.typeid) {
42-
if (
43-
col.typeid === 'STRING' ||
44-
col.typeid === 'VARCHAR' ||
45-
col.typeid === 'NVARCHAR' ||
46-
col.typeid === 'CHAR' ||
47-
col.typeid === 'NCHAR'
48-
) {
49-
val = "'" + escapeqq(val) + "'";
39+
s += columns
40+
.map(function (col) {
41+
var val = data[i][col.columnid];
42+
// Handle null and undefined values
43+
if (val === null || val === undefined) {
44+
return 'NULL';
5045
}
51-
} else {
52-
if (typeof val == 'string') {
53-
val = "'" + escapeqq(val) + "'";
46+
if (col.typeid) {
47+
if (
48+
col.typeid === 'STRING' ||
49+
col.typeid === 'VARCHAR' ||
50+
col.typeid === 'NVARCHAR' ||
51+
col.typeid === 'CHAR' ||
52+
col.typeid === 'NCHAR'
53+
) {
54+
val = "'" + escapeqq(val) + "'";
55+
}
56+
} else {
57+
if (typeof val == 'string') {
58+
val = "'" + escapeqq(val) + "'";
59+
}
5460
}
55-
}
56-
return val;
57-
});
61+
return val;
62+
})
63+
.join(',');
5864
s += ');\n';
5965
}
6066
// if(filename === '') {

0 commit comments

Comments
 (0)