Skip to content

Commit ca68425

Browse files
committed
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.
1 parent 27450d0 commit ca68425

File tree

4 files changed

+24
-43
lines changed

4 files changed

+24
-43
lines changed

lib/native/query.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,11 @@ NativeQuery.prototype.submit = function(client) {
146146
client.namedQueries[self.name] = true;
147147
return self.native.execute(self.name, values, after);
148148
});
149-
}
150-
else if(this.values) {
149+
} else if(this.values) {
150+
if (!Array.isArray(this.values)) {
151+
const err = new Error('Query values must be an array')
152+
return after(err)
153+
}
151154
var vals = this.values.map(utils.prepareValue);
152155
client.native.query(this.text, vals, after);
153156
} else {

lib/query.js

+6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ Query.prototype.submit = function(connection) {
133133
connection.emit('readyForQuery')
134134
return
135135
}
136+
if (this.values && !Array.isArray(this.values)) {
137+
const err = new Error('Query values must be an array')
138+
connection.emit('error', err)
139+
connection.emit('readyForQuery')
140+
return
141+
}
136142
if(this.requiresPreparation()) {
137143
this.prepare(connection);
138144
} else {

test/integration/client/error-handling-tests.js

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ var createErorrClient = function() {
1717

1818
const suite = new helper.Suite('error handling')
1919

20+
suite.test('sending non-array argument as values causes an error callback', (done) => {
21+
const client = new Client()
22+
client.connect(() => {
23+
client.query('select $1::text as name', 'foo', (err) => {
24+
assert(err instanceof Error)
25+
client.query('SELECT $1::text as name', ['foo'], (err, res) => {
26+
assert.equal(res.rows[0].name, 'foo')
27+
client.end(done)
28+
})
29+
})
30+
})
31+
})
32+
2033
suite.test('re-using connections results in error callback', (done) => {
2134
const client = new Client()
2235
client.connect(() => {

test/native/error-tests.js

-41
This file was deleted.

0 commit comments

Comments
 (0)