From f7de9ce820715146644c076f31959b68a323cf30 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Sat, 15 Jul 2017 11:14:15 -0500 Subject: [PATCH] Non-array query values cause query to end with an error. This is a small change and is _kinda_ backwards compatible since the old behavior was to throw an error, but if someone was relying on anything with `.map` working as values it would break them, so it's in a major semver bump. --- lib/native/query.js | 7 +++- lib/query.js | 6 +++ .../client/error-handling-tests.js | 13 ++++++ test/native/error-tests.js | 41 ------------------- 4 files changed, 24 insertions(+), 43 deletions(-) delete mode 100644 test/native/error-tests.js diff --git a/lib/native/query.js b/lib/native/query.js index f9dacfa1..a35e5304 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -146,8 +146,11 @@ NativeQuery.prototype.submit = function(client) { client.namedQueries[self.name] = true; return self.native.execute(self.name, values, after); }); - } - else if(this.values) { + } else if(this.values) { + if (!Array.isArray(this.values)) { + const err = new Error('Query values must be an array') + return after(err) + } var vals = this.values.map(utils.prepareValue); client.native.query(this.text, vals, after); } else { diff --git a/lib/query.js b/lib/query.js index a8e76547..26b930fb 100644 --- a/lib/query.js +++ b/lib/query.js @@ -133,6 +133,12 @@ Query.prototype.submit = function(connection) { connection.emit('readyForQuery') return } + if (this.values && !Array.isArray(this.values)) { + const err = new Error('Query values must be an array') + connection.emit('error', err) + connection.emit('readyForQuery') + return + } if(this.requiresPreparation()) { this.prepare(connection); } else { diff --git a/test/integration/client/error-handling-tests.js b/test/integration/client/error-handling-tests.js index a798a659..e4d77903 100644 --- a/test/integration/client/error-handling-tests.js +++ b/test/integration/client/error-handling-tests.js @@ -17,6 +17,19 @@ var createErorrClient = function() { const suite = new helper.Suite('error handling') +suite.test('sending non-array argument as values causes an error callback', (done) => { + const client = new Client() + client.connect(() => { + client.query('select $1::text as name', 'foo', (err) => { + assert(err instanceof Error) + client.query('SELECT $1::text as name', ['foo'], (err, res) => { + assert.equal(res.rows[0].name, 'foo') + client.end(done) + }) + }) + }) +}) + suite.test('re-using connections results in error callback', (done) => { const client = new Client() client.connect(() => { diff --git a/test/native/error-tests.js b/test/native/error-tests.js deleted file mode 100644 index a6f87644..00000000 --- a/test/native/error-tests.js +++ /dev/null @@ -1,41 +0,0 @@ -"use strict"; -var helper = require(__dirname + "/../test-helper"); -var Client = require(__dirname + "/../../lib/native"); - - -var connect = function(callback) { - var client = new Client(helper.config); - client.connect(); - assert.emits(client, 'connect', function() { - callback(client); - }) -} - -test('parameterized query with non-array for second value', function() { - test('inline', function() { - connect(function(client) { - client.end(); - assert.emits(client, 'end', function() { - assert.throws(function() { - client.query("SELECT *", "LKSDJF") - }); - }); - }); - }); - - test('config', function() { - connect(function(client) { - client.end(); - assert.emits(client, 'end', function() { - assert.throws(function() { - client.query({ - text: "SELECT *", - values: "ALSDKFJ" - }); - }); - }); - }); - }); -}); - -