Skip to content
Open
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
3 changes: 2 additions & 1 deletion packages/pg/lib/native/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ 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]) {
// check for undefined, not falsy: the text can be empty
if (client.namedQueries[this.name] !== undefined) {
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)
Expand Down
7 changes: 6 additions & 1 deletion packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ class Query extends EventEmitter {
}

hasBeenParsed(connection) {
return this.name && (connection.parsedStatements[this.name] || connection.submittedNamedStatements[this.name])
// check for undefined, not falsy: the text can be empty
return (
this.name &&
(connection.parsedStatements[this.name] !== undefined ||
connection.submittedNamedStatements[this.name] !== undefined)
)
}

handlePortalSuspended(connection) {
Expand Down
12 changes: 12 additions & 0 deletions packages/pg/test/integration/client/empty-query-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ suite.test('callback supported', function (done) {
client.end(done)
})
})

suite.test('a named empty statement can run more than once', async function () {
const client = helper.client()
try {
for (let i = 0; i < 2; i++) {
const result = await client.query({ text: '', name: 'empty' })
assert.empty(result.rows)
}
} finally {
await client.end()
}
})
Loading