Skip to content

Commit 819bf48

Browse files
Copilotmathiasrw
andauthored
Let SELECT * INTO SQL() handle NULL values to fix #42 (#2196)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]> Co-authored-by: Mathias Wulff <[email protected]>
1 parent 8e625c2 commit 819bf48

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

src/830into.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,28 @@ 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') {
46+
// Check if value should be escaped as a string
47+
var shouldEscape =
48+
(col.typeid &&
49+
(col.typeid === 'STRING' ||
50+
col.typeid === 'VARCHAR' ||
51+
col.typeid === 'NVARCHAR' ||
52+
col.typeid === 'CHAR' ||
53+
col.typeid === 'NCHAR')) ||
54+
typeof val == 'string';
55+
if (shouldEscape) {
5356
val = "'" + escapeqq(val) + "'";
5457
}
55-
}
56-
return val;
57-
});
58+
return val;
59+
})
60+
.join(',');
5861
s += ');\n';
5962
}
6063
// if(filename === '') {

test/test42.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 42 - NULL values in INTO SQL()', function () {
7+
it('1. Should output NULL for null values', () => {
8+
var data = [
9+
{a: 1, b: 'test', c: null, d: 3},
10+
{a: 2, b: null, c: 4, d: null},
11+
{a: null, b: 'value', c: null, d: null},
12+
];
13+
var res = alasql('SELECT * INTO SQL({tableid:"test_table"}) FROM ?', [data]);
14+
15+
var expected =
16+
"INSERT INTO test_table(a,b,c,d) VALUES (1,'test',NULL,3);\n" +
17+
'INSERT INTO test_table(a,b,c,d) VALUES (2,NULL,4,NULL);\n' +
18+
"INSERT INTO test_table(a,b,c,d) VALUES (NULL,'value',NULL,NULL);\n";
19+
20+
assert.deepEqual(res, expected);
21+
});
22+
23+
it('2. Should handle undefined values as NULL', () => {
24+
var data = [
25+
{a: 1, b: 'test'},
26+
{a: 2, b: undefined, c: 4},
27+
];
28+
var res = alasql('SELECT * INTO SQL({tableid:"test_table"}) FROM ?', [data]);
29+
30+
var expected =
31+
"INSERT INTO test_table(a,b) VALUES (1,'test');\n" +
32+
'INSERT INTO test_table(a,b) VALUES (2,NULL);\n';
33+
34+
assert.deepEqual(res, expected);
35+
});
36+
37+
it('3. Should handle mixed NULL, undefined, and empty strings', () => {
38+
var data = [{a: 1, b: '', c: null, d: undefined}];
39+
var res = alasql('SELECT * INTO SQL({tableid:"test_table"}) FROM ?', [data]);
40+
41+
var expected = "INSERT INTO test_table(a,b,c,d) VALUES (1,'',NULL,NULL);\n";
42+
43+
assert.deepEqual(res, expected);
44+
});
45+
});

0 commit comments

Comments
 (0)