Fix exception caused by column names with single quotes

Also rename some test files so they match the Makefile regex.  They will be included in the test suite from now on.
This commit is contained in:
Brian M. Carlson 2014-01-22 08:38:29 -06:00
parent 8abf59a632
commit 5f592a1240
4 changed files with 24 additions and 3 deletions

View File

@ -65,8 +65,13 @@ Result.prototype.addRow = function(row) {
};
var inlineParser = function(fieldName, i) {
return "\nthis['" + fieldName + "'] = " +
"rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
return "\nthis['" +
//fields containing single quotes will break
//the evaluated javascript unless they are escaped
//see https://github.com/brianc/node-postgres/issues/507
fieldName.replace("'", "\\'") +
"'] = " +
"rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
};
Result.prototype.addFields = function(fieldDescriptions) {

View File

@ -2,7 +2,7 @@ var helper = require(__dirname + "/../test-helper");
var pg = helper.pg;
test('parsing array results', function() {
pg.connect(helper.config, assert.calls(function(err, client) {
pg.connect(helper.config, assert.calls(function(err, client, done) {
assert.isNull(err);
client.query("CREATE TEMP TABLE why(names text[], numbors integer[], decimals double precision[])");
client.query('INSERT INTO why(names, numbors, decimals) VALUES(\'{"aaron", "brian","a b c" }\', \'{1, 2, 3}\', \'{.1, 0.05, 3.654}\')').on('error', console.log);
@ -12,6 +12,7 @@ test('parsing array results', function() {
assert.equal(result.rows[0].decimals[0], 0.1);
assert.equal(result.rows[0].decimals[1], 0.05);
assert.equal(result.rows[0].decimals[2], 3.654);
done()
pg.end();
}))
})

View File

@ -0,0 +1,15 @@
var helper = require(__dirname + "/../test-helper");
var pg = helper.pg;
test('parsing array results', function() {
pg.connect(helper.config, assert.success(function(client, done) {
client.query('CREATE TEMP TABLE test_table(bar integer, "baz\'s" integer)')
client.query('INSERT INTO test_table(bar, "baz\'s") VALUES(1, 1), (2, 2)')
client.query('SELECT * FROM test_table', function(err, res) {
assert.equal(res.rows[0]["baz's"], 1)
assert.equal(res.rows[1]["baz's"], 2)
done()
pg.end()
})
}))
})