Skip to content

Commit 223d09a

Browse files
committed
find matching prepared statement type and value
Deduplicate positional parameters, to simplify prepared statements positional parameters in case there are duplicates. For example `${organization_id}` a few times in a more complex nested statement.
1 parent e7dfa14 commit 223d09a

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/types.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,25 @@ export function handleValue(x, parameters, types, options) {
7676
let value = x instanceof Parameter ? x.value : x
7777
if (value === undefined) {
7878
x instanceof Parameter
79-
? x.value = options.transform.undefined
79+
? value = x.value = options.transform.undefined
8080
: value = x = options.transform.undefined
8181

8282
if (value === undefined)
8383
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
8484
}
8585

86-
return '$' + (types.push(
86+
const type =
8787
x instanceof Parameter
88-
? (parameters.push(x.value), x.array
89-
? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)
88+
? x.array
89+
? x.array[x.type || inferType(value)] || x.type || firstIsString(value)
9090
: x.type
91-
)
92-
: (parameters.push(x), inferType(x))
93-
))
91+
: inferType(value)
92+
93+
for (let i = 0; i < parameters.length; i++)
94+
if (parameters[i] === value && types[i] === type) return '$' + (i + 1)
95+
96+
parameters.push(value)
97+
return '$' + types.push(type)
9498
}
9599

96100
const defaultHandlers = typeHandlers(types)

0 commit comments

Comments
 (0)