diff --git a/lib/result.js b/lib/result.js index c9a777eca..8ec3de01f 100644 --- a/lib/result.js +++ b/lib/result.js @@ -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) { diff --git a/test/integration/gh-issues/130.js b/test/integration/gh-issues/130-tests.js similarity index 100% rename from test/integration/gh-issues/130.js rename to test/integration/gh-issues/130-tests.js diff --git a/test/integration/gh-issues/131.js b/test/integration/gh-issues/131-tests.js similarity index 90% rename from test/integration/gh-issues/131.js rename to test/integration/gh-issues/131-tests.js index 74f35c121..eee5086a6 100644 --- a/test/integration/gh-issues/131.js +++ b/test/integration/gh-issues/131-tests.js @@ -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(); })) }) diff --git a/test/integration/gh-issues/507-tests.js b/test/integration/gh-issues/507-tests.js new file mode 100644 index 000000000..ef75effc6 --- /dev/null +++ b/test/integration/gh-issues/507-tests.js @@ -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() + }) + })) +})