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
6 changes: 4 additions & 2 deletions packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ class Query extends EventEmitter {
valueMapper: utils.prepareValue,
})
} catch (err) {
// we should close parse to avoid leaking connections
connection.close({ type: 'S', name: this.name })
// only close the unnamed statement. a named one stays prepared and is used again
if (!this.name) {
connection.close({ type: 'S', name: this.name })
}
connection.sync()

this.handleError(err, connection)
Expand Down
57 changes: 57 additions & 0 deletions packages/pg/test/integration/client/prepared-statement-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,60 @@ const suite = new helper.Suite()

suite.test('cleanup', () => client.end())
})()
;(function () {
const name = 'bind-throw'
const text = 'SELECT $1::text AS v'
const badValue = {
toPostgres() {
throw new Error('bad value')
},
}

suite.test(
'a named statement still runs after its values fail to serialize',
!helper.args.native &&
async function () {
const client = helper.client()
try {
await assert.rejects(client.query({ name, text, values: [badValue] }), /bad value/)
const result = await client.query({ name, text, values: ['ok'] })
assert.equal(result.rows[0].v, 'ok')
} finally {
await client.end()
}
}
)

suite.test(
'an already prepared statement still runs after its values fail to serialize',
!helper.args.native &&
async function () {
const client = helper.client()
try {
await client.query({ name, text, values: ['first'] })
await assert.rejects(client.query({ name, text, values: [badValue] }), /bad value/)
const result = await client.query({ name, text, values: ['ok'] })
assert.equal(result.rows[0].v, 'ok')
} finally {
await client.end()
}
}
)

suite.test(
'a pipelined query with the same name still runs after the first fails to serialize',
!helper.args.native &&
async function () {
const client = helper.client(undefined, { pipeline: true })
try {
const failed = client.query({ name, text, values: [badValue] })
const next = client.query({ name, text, values: ['ok'] })
await assert.rejects(failed, /bad value/)
const result = await next
assert.equal(result.rows[0].v, 'ok')
} finally {
await client.end()
}
}
)
})()
Loading