Skip to content

Commit 5fb17fb

Browse files
committed
More efficient handling for prepared statements
1 parent 13bac30 commit 5fb17fb

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/index.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -304,38 +304,42 @@ export default function Postgres(url, options) {
304304

305305
function parse(xs, args = []) {
306306
const xargs = []
307+
, types = []
308+
307309
let str = xs[0]
308310
let arg
309311

310312
for (let i = 1; i < xs.length; i++) {
311313
arg = args[i - 1]
312314
str += (arg.rows
313-
? parseRows(arg.rows, xargs)
315+
? parseRows(arg.rows, xargs, types)
314316
: arg.row
315-
? parseRow(arg.row, xargs)
316-
: parseValue(arg, xargs)
317+
? parseRow(arg.row, xargs, types)
318+
: parseValue(arg, xargs, types)
317319
) + xs[i]
318320
}
319321

320322
return {
321-
sig: args.length === xargs.length ? xargs.map(x => x.type) + str : null,
323+
sig: !xargs.dynamic && types + str,
322324
str: str.trim(),
323325
args: xargs
324326
}
325327
}
326328

327-
function parseRows(rows, xargs) {
328-
return rows.map(row => parseRow(row, xargs)).join(',')
329+
function parseRows(rows, xargs, types) {
330+
xargs.dynamic = true
331+
return rows.map(row => parseRow(row, xargs, types)).join(',')
329332
}
330333

331-
function parseRow(row, xargs) {
332-
return '(' + row.map(x => parseValue(x, xargs)).join(',') + ')'
334+
function parseRow(row, xargs, types) {
335+
return '(' + row.map(x => parseValue(x, xargs, types)).join(',') + ')'
333336
}
334337

335-
function parseValue(x, xargs) {
338+
function parseValue(x, xargs, types) {
336339
const value = x.value ? x.value : x
337340
, type = x.type || (Array.isArray(value) ? typeArrayMap[inferType(value)] : inferType(value))
338341

342+
types.push(type)
339343
return '$' + xargs.push({
340344
type,
341345
value: type

0 commit comments

Comments
 (0)