Skip to content

compare prepared statement text with previous to prevent incorrect queries #1814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ Client.prototype._attachListeners = function (con) {
// it again on the same client
con.on('parseComplete', function (msg) {
if (self.activeQuery.name) {
con.parsedStatements[self.activeQuery.name] = true
con.parsedStatements[self.activeQuery.name] = self.activeQuery.text
}
})

Expand Down
6 changes: 5 additions & 1 deletion lib/native/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ NativeQuery.prototype.submit = function (client) {
// check if the client has already executed this named query
// if so...just execute it again - skip the planning phase
if (client.namedQueries[this.name]) {
if (this.text && client.namedQueries[this.name] !== this.text) {
const err = new Error(`Prepared statements must be unique - '${this.name}' was used for a different statement`)
return after(err)
}
return client.native.execute(this.name, values, after)
}
// plan the named query the first time, then execute it
return client.native.prepare(this.name, this.text, values.length, function (err) {
if (err) return after(err)
client.namedQueries[self.name] = true
client.namedQueries[self.name] = self.text
return self.native.execute(self.name, values, after)
})
} else if (this.values) {
Expand Down
4 changes: 4 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ Query.prototype.submit = function (connection) {
if (typeof this.text !== 'string' && typeof this.name !== 'string') {
return new Error('A query must have either text or a name. Supplying neither is unsupported.')
}
const previous = connection.parsedStatements[this.name]
if (this.text && previous && this.text !== previous) {
return new Error(`Prepared statements must be unique - '${this.name}' was used for a different statement`)
}
if (this.values && !Array.isArray(this.values)) {
return new Error('Query values must be an array')
}
Expand Down
11 changes: 11 additions & 0 deletions test/integration/client/prepared-statement-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ var suite = new helper.Suite()

q.on('end', () => done())
})

suite.test('with same name, but with different text', function (done) {
client.query(new Query({
text: 'select name from person where age >= $1 and name LIKE $2',
name: queryName,
values: [30, '%n%']
}), assert.calls(err => {
assert.equal(err.message, `Prepared statements must be unique - '${queryName}' was used for a different statement`)
done()
}))
})
})()

;(function () {
Expand Down