From 02c47f50711a147eef920ec10549761135bdc3e1 Mon Sep 17 00:00:00 2001 From: Jens Kristian Geyti Date: Wed, 10 Feb 2016 20:52:43 +0000 Subject: [PATCH] Support querying tables with column names with multiple apostrophes (issue #934). Includes integration test. --- lib/result.js | 5 ++++- test/integration/client/query-column-names-tests.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/integration/client/query-column-names-tests.js diff --git a/lib/result.js b/lib/result.js index bf05381d6..dc2ce0e03 100644 --- a/lib/result.js +++ b/lib/result.js @@ -70,7 +70,10 @@ var inlineParser = function(fieldName, i) { //fields containing single quotes will break //the evaluated javascript unless they are escaped //see https://github.com/brianc/node-postgres/issues/507 - fieldName.replace("'", "\\'") + + //Addendum: However, we need to make sure to replace all + //occurences of apostrophes, not just the first one. + //See https://github.com/brianc/node-postgres/issues/934 + fieldName.replace(/'/g, "\\'") + "'] = " + "rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);"; }; diff --git a/test/integration/client/query-column-names-tests.js b/test/integration/client/query-column-names-tests.js new file mode 100644 index 000000000..811d673a0 --- /dev/null +++ b/test/integration/client/query-column-names-tests.js @@ -0,0 +1,13 @@ +var helper = require(__dirname + '/../test-helper'); +var pg = helper.pg; + +test('support for complex column names', function() { + pg.connect(helper.config, assert.success(function(client, done) { + client.query("CREATE TEMP TABLE t ( \"complex''column\" TEXT )"); + client.query('SELECT * FROM t', assert.success(function(res) { + done(); + assert.strictEqual(res.fields[0].name, "complex''column"); + pg.end(); + })); + })); +}); \ No newline at end of file