Skip to content

Unexpected identifier with pg versions > 2.3.1 #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}))
})
Expand Down
15 changes: 15 additions & 0 deletions test/integration/gh-issues/507-tests.js
Original file line number Diff line number Diff line change
@@ -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()
})
}))
})