mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
properly prepare complex arrays
`arrayString` duplicated too much of `prepareValue`'s logic, and so didn't receive bugfixes for handling dates with timestamps. Defer to `prepareValue` whenever possible. This change enforces double-quote escaping of all array elements, regardless of whether escaping is necessary. This has the side-effect of properly escaping JSON arrays.
This commit is contained in:
parent
364cf4b3ca
commit
c41eedc3e0
@ -22,10 +22,7 @@ function arrayString(val) {
|
||||
if(i > 0) {
|
||||
result = result + ',';
|
||||
}
|
||||
if(val[i] instanceof Date) {
|
||||
result = result + JSON.stringify(val[i]);
|
||||
}
|
||||
else if(typeof val[i] === 'undefined') {
|
||||
if(val[i] === null || typeof val[i] === 'undefined') {
|
||||
result = result + 'NULL';
|
||||
}
|
||||
else if(Array.isArray(val[i])) {
|
||||
@ -33,8 +30,7 @@ function arrayString(val) {
|
||||
}
|
||||
else
|
||||
{
|
||||
result = result +
|
||||
(val[i] === null ? 'NULL' : JSON.stringify(val[i]));
|
||||
result = result + JSON.stringify(prepareValue(val[i]));
|
||||
}
|
||||
}
|
||||
result = result + '}';
|
||||
|
||||
@ -95,9 +95,24 @@ test('prepareValue: string prepared properly', function() {
|
||||
assert.strictEqual(out, 'big bad wolf');
|
||||
});
|
||||
|
||||
test('prepareValue: array prepared properly', function() {
|
||||
test('prepareValue: simple array prepared properly', function() {
|
||||
var out = utils.prepareValue([1, null, 3, undefined, [5, 6, "squ,awk"]]);
|
||||
assert.strictEqual(out, '{1,NULL,3,NULL,{5,6,"squ,awk"}}');
|
||||
assert.strictEqual(out, '{"1",NULL,"3",NULL,{"5","6","squ,awk"}}');
|
||||
});
|
||||
|
||||
test('prepareValue: complex array prepared properly', function() {
|
||||
var out = utils.prepareValue([{ x: 42 }, { y: 84 }]);
|
||||
assert.strictEqual(out, '{"{\\"x\\":42}","{\\"y\\":84}"}');
|
||||
});
|
||||
|
||||
test('prepareValue: date array prepared properly', function() {
|
||||
helper.setTimezoneOffset(-330);
|
||||
|
||||
var date = new Date(2014, 1, 1, 11, 11, 1, 7);
|
||||
var out = utils.prepareValue([date]);
|
||||
assert.strictEqual(out, '{"2014-02-01T11:11:01.007+05:30"}');
|
||||
|
||||
helper.resetTimezoneOffset();
|
||||
});
|
||||
|
||||
test('prepareValue: arbitrary objects prepared properly', function() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user