parent
27bee1d0bc
commit
5d821c3acb
10
lib/utils.js
10
lib/utils.js
@ -8,6 +8,14 @@
|
||||
|
||||
var defaults = require('./defaults');
|
||||
|
||||
function escapeElement(elementRepresentation) {
|
||||
var escaped = elementRepresentation
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/"/g, '\\"');
|
||||
|
||||
return '"' + escaped + '"';
|
||||
}
|
||||
|
||||
// convert a JS array to a postgres array literal
|
||||
// uses comma separator so won't work for types like box that use
|
||||
// a different array separator.
|
||||
@ -25,7 +33,7 @@ function arrayString(val) {
|
||||
}
|
||||
else
|
||||
{
|
||||
result = result + JSON.stringify(prepareValue(val[i]));
|
||||
result += escapeElement(prepareValue(val[i]));
|
||||
}
|
||||
}
|
||||
result = result + '}';
|
||||
|
@ -1,6 +1,36 @@
|
||||
var helper = require(__dirname + "/test-helper");
|
||||
var pg = helper.pg;
|
||||
|
||||
test('serializing arrays', function() {
|
||||
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
||||
assert.isNull(err);
|
||||
|
||||
test('nulls', function() {
|
||||
client.query('SELECT $1::text[] as array', [[null]], assert.success(function(result) {
|
||||
var array = result.rows[0].array;
|
||||
assert.lengthIs(array, 1);
|
||||
assert.isNull(array[0]);
|
||||
}));
|
||||
});
|
||||
|
||||
test('elements containing JSON-escaped characters', function() {
|
||||
var param = '\\"\\"';
|
||||
|
||||
for (var i = 1; i <= 0x1f; i++) {
|
||||
param += String.fromCharCode(i);
|
||||
}
|
||||
|
||||
client.query('SELECT $1::text[] as array', [[param]], assert.success(function(result) {
|
||||
var array = result.rows[0].array;
|
||||
assert.lengthIs(array, 1);
|
||||
assert.equal(array[0], param);
|
||||
}));
|
||||
|
||||
done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
test('parsing array results', function() {
|
||||
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
||||
assert.isNull(err);
|
||||
|
Loading…
Reference in New Issue
Block a user