Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: brianc/node-postgres
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.8.1
Choose a base ref
...
head repository: brianc/node-postgres
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.8.2
Choose a head ref
  • 3 commits
  • 6 files changed
  • 3 contributors

Commits on Mar 6, 2019

  1. Update license date in readme (#1823)

    blrhc authored and brianc committed Mar 6, 2019
    Copy the full SHA
    4c6c0e9 View commit details
  2. compare prepared statement text with previous to prevent incorrect qu…

    …eries (#1814)
    
    * compare prepared statement text with previous to prevent incorrect queries - fixes #1813
    
    * make suggested changes to error message
    Rich-Harris authored and brianc committed Mar 6, 2019
    Copy the full SHA
    e6a878c View commit details

Commits on Mar 7, 2019

  1. Bump version

    brianc committed Mar 7, 2019
    Copy the full SHA
    bae9fd7 View commit details
Showing with 23 additions and 4 deletions.
  1. +1 −1 README.md
  2. +1 −1 lib/client.js
  3. +5 −1 lib/native/query.js
  4. +4 −0 lib/query.js
  5. +1 −1 package.json
  6. +11 −0 test/integration/client/prepared-statement-tests.js
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ The causes and solutions to common errors can be found among the [Frequently Ask

## License

Copyright (c) 2010-2018 Brian Carlson (brian.m.carlson@gmail.com)
Copyright (c) 2010-2019 Brian Carlson (brian.m.carlson@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
@@ -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
}
})

6 changes: 5 additions & 1 deletion lib/native/query.js
Original file line number Diff line number Diff line change
@@ -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) {
4 changes: 4 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
@@ -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')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg",
"version": "7.8.1",
"version": "7.8.2",
"description": "PostgreSQL client - pure javascript & libpq with the same API",
"keywords": [
"database",
11 changes: 11 additions & 0 deletions test/integration/client/prepared-statement-tests.js
Original file line number Diff line number Diff line change
@@ -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 () {