From 5f592a1240b01ede0d34ed2c1f4d23054106f2b4 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Wed, 22 Jan 2014 08:38:29 -0600 Subject: [PATCH] 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. --- lib/result.js | 9 +++++++-- .../gh-issues/{130.js => 130-tests.js} | 0 .../gh-issues/{131.js => 131-tests.js} | 3 ++- test/integration/gh-issues/507-tests.js | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) rename test/integration/gh-issues/{130.js => 130-tests.js} (100%) rename test/integration/gh-issues/{131.js => 131-tests.js} (90%) create mode 100644 test/integration/gh-issues/507-tests.js diff --git a/lib/result.js b/lib/result.js index c9a777e..8ec3de0 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 74f35c1..eee5086 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 0000000..ef75eff --- /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() + }) + })) +})