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.
This commit is contained in:
Brian M. Carlson 2017-07-15 11:14:15 -05:00 committed by Brian C
parent 27450d07e6
commit f7de9ce820
4 changed files with 24 additions and 43 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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(() => {

View File

@ -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"
});
});
});
});
});
});