Skip to content

Commit 5f592a1

Browse files
committed
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.
1 parent 8abf59a commit 5f592a1

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

lib/result.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ Result.prototype.addRow = function(row) {
6565
};
6666

6767
var inlineParser = function(fieldName, i) {
68-
return "\nthis['" + fieldName + "'] = " +
69-
"rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
68+
return "\nthis['" +
69+
//fields containing single quotes will break
70+
//the evaluated javascript unless they are escaped
71+
//see https://github.com/brianc/node-postgres/issues/507
72+
fieldName.replace("'", "\\'") +
73+
"'] = " +
74+
"rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
7075
};
7176

7277
Result.prototype.addFields = function(fieldDescriptions) {

test/integration/gh-issues/131.js renamed to test/integration/gh-issues/131-tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var helper = require(__dirname + "/../test-helper");
22
var pg = helper.pg;
33

44
test('parsing array results', function() {
5-
pg.connect(helper.config, assert.calls(function(err, client) {
5+
pg.connect(helper.config, assert.calls(function(err, client, done) {
66
assert.isNull(err);
77
client.query("CREATE TEMP TABLE why(names text[], numbors integer[], decimals double precision[])");
88
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() {
1212
assert.equal(result.rows[0].decimals[0], 0.1);
1313
assert.equal(result.rows[0].decimals[1], 0.05);
1414
assert.equal(result.rows[0].decimals[2], 3.654);
15+
done()
1516
pg.end();
1617
}))
1718
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var helper = require(__dirname + "/../test-helper");
2+
var pg = helper.pg;
3+
4+
test('parsing array results', function() {
5+
pg.connect(helper.config, assert.success(function(client, done) {
6+
client.query('CREATE TEMP TABLE test_table(bar integer, "baz\'s" integer)')
7+
client.query('INSERT INTO test_table(bar, "baz\'s") VALUES(1, 1), (2, 2)')
8+
client.query('SELECT * FROM test_table', function(err, res) {
9+
assert.equal(res.rows[0]["baz's"], 1)
10+
assert.equal(res.rows[1]["baz's"], 2)
11+
done()
12+
pg.end()
13+
})
14+
}))
15+
})

0 commit comments

Comments
 (0)