Skip to content

Commit 13bac30

Browse files
committed
Add proper uniqueness to prepared statements
1 parent 0692e59 commit 13bac30

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

lib/connection.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,28 @@ export default function Connection(options = {}) {
8484
q.reject(err)
8585
}
8686

87-
function send(query, { str, args = [] }) {
87+
function send(query, { sig, str, args = [] }) {
8888
timeout && clearTimeout(timer)
8989
!connection.ready || backend.query
9090
? queries.push(query)
9191
: (backend.query = query)
9292

93-
const buffer = statements.has(str)
94-
? prepared(statements.get(str), str, args)
95-
: prepare(str, args)
93+
const buffer = statements.has(sig)
94+
? prepared(statements.get(sig), args)
95+
: prepare(sig, str, args)
9696

9797
connection.ready
9898
? socket.write(buffer)
9999
: (messages.push(buffer), socket.connect())
100100
}
101101

102-
function prepared(name, str, args) {
102+
function prepared(name, args) {
103103
return frontend.Bind(name, args)
104104
}
105105

106-
function prepare(str, args) {
107-
const name = 'p' + id++
108-
statements.set(str, name)
106+
function prepare(sig, str, args) {
107+
const name = sig ? 'p' + id++ : ''
108+
sig && statements.set(sig, name)
109109
return Buffer.concat([
110110
frontend.Parse(name, str, args),
111111
frontend.Bind(name, args)

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export default function Postgres(url, options) {
302302
.then(() => clearTimeout(destroy))
303303
}
304304

305-
function parse(xs, args) {
305+
function parse(xs, args = []) {
306306
const xargs = []
307307
let str = xs[0]
308308
let arg
@@ -318,6 +318,7 @@ export default function Postgres(url, options) {
318318
}
319319

320320
return {
321+
sig: args.length === xargs.length ? xargs.map(x => x.type) + str : null,
321322
str: str.trim(),
322323
args: xargs
323324
}

0 commit comments

Comments
 (0)