|
1 | | -import type { CFXParameters, CachedStatement } from '../types'; |
2 | | -import { getCached, setCached } from '../cache/statementCache'; |
| 1 | +import type { CFXParameters } from '../types'; |
3 | 2 |
|
4 | | -const NAMED_PARAM_RE = /(?<!["'])[:@]([a-zA-Z][a-zA-Z0-9_]*)/g; |
5 | | - |
6 | | -function convertNamedToPositional( |
7 | | - query: string, |
8 | | - params: Record<string, unknown> |
9 | | -): [string, unknown[]] { |
10 | | - const cached = getCached(query); |
11 | | - if (cached) { |
12 | | - return [cached.sql, cached.params.map((k) => params[k] ?? null)]; |
13 | | - } |
14 | | - |
15 | | - const paramNames: string[] = []; |
16 | | - const sql = query.replace(NAMED_PARAM_RE, (_match, name) => { |
17 | | - paramNames.push(name); |
18 | | - return '?'; |
19 | | - }); |
20 | | - |
21 | | - setCached(query, { sql, params: paramNames }); |
22 | | - return [sql, paramNames.map((k) => params[k] ?? null)]; |
23 | | -} |
24 | | - |
25 | | -function normalizeObjectParams(params: Record<string, unknown>, count: number): unknown[] { |
26 | | - const result: unknown[] = []; |
27 | | - for (let i = 0; i < count; i++) { |
28 | | - result.push(params[String(i + 1)] ?? null); |
29 | | - } |
30 | | - return result; |
31 | | -} |
32 | | - |
33 | | -function countPlaceholders(query: string): number { |
34 | | - let count = 0; |
35 | | - for (let i = 0; i < query.length; i++) { |
36 | | - if (query[i] === '?' && query[i + 1] !== '?') count++; |
37 | | - } |
38 | | - return count; |
39 | | -} |
| 3 | +const convertNamedPlaceholders: (query: string, params: Record<string, any>) => [string, any[]] = |
| 4 | + require('named-placeholders')(); |
40 | 5 |
|
41 | 6 | export function parseArguments( |
42 | 7 | query: string, |
43 | 8 | parameters?: CFXParameters |
44 | 9 | ): [string, unknown[]] { |
45 | | - if (parameters === null || parameters === undefined || typeof parameters === 'function') { |
46 | | - return [query, []]; |
47 | | - } |
48 | | - |
49 | | - if (!Array.isArray(parameters) && typeof parameters === 'object') { |
| 10 | + if (convertNamedPlaceholders && parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { |
50 | 11 | if (query.includes(':') || query.includes('@')) { |
51 | | - return convertNamedToPositional(query, parameters as Record<string, unknown>); |
| 12 | + [query, parameters] = convertNamedPlaceholders(query, parameters as Record<string, unknown>); |
52 | 13 | } |
| 14 | + } |
53 | 15 |
|
54 | | - const count = countPlaceholders(query); |
55 | | - return [query, normalizeObjectParams(parameters as Record<string, unknown>, count)]; |
| 16 | + if (!parameters || typeof parameters === 'function') { |
| 17 | + return [query, []]; |
56 | 18 | } |
57 | 19 |
|
58 | | - if (Array.isArray(parameters)) { |
59 | | - const count = countPlaceholders(query); |
60 | | - const arr = parameters as unknown[]; |
| 20 | + const placeholders = query.match(/\?(?!\?)/g)?.length ?? 0; |
61 | 21 |
|
62 | | - if (arr.length < count) { |
63 | | - const padded = [...arr]; |
64 | | - while (padded.length < count) padded.push(null); |
65 | | - return [query, padded]; |
| 22 | + if (parameters && !Array.isArray(parameters)) { |
| 23 | + const arr: unknown[] = []; |
| 24 | + for (let i = 0; i < placeholders; i++) { |
| 25 | + arr[i] = (parameters as Record<string, unknown>)[i + 1] ?? null; |
66 | 26 | } |
| 27 | + return [query, arr]; |
| 28 | + } |
67 | 29 |
|
68 | | - if (arr.length > count && count > 0) { |
69 | | - throw new Error( |
70 | | - `[swiftdb] Too many parameters (${arr.length}) for query with ${count} placeholders` |
71 | | - ); |
| 30 | + if (placeholders) { |
| 31 | + if ((parameters as unknown[]).length === 0) { |
| 32 | + const arr: unknown[] = []; |
| 33 | + for (let i = 0; i < placeholders; i++) arr[i] = null; |
| 34 | + return [query, arr]; |
72 | 35 | } |
73 | 36 |
|
74 | | - return [query, arr]; |
| 37 | + const diff = placeholders - (parameters as unknown[]).length; |
| 38 | + if (diff > 0) { |
| 39 | + const padded = [...(parameters as unknown[])]; |
| 40 | + for (let i = 0; i < diff; i++) padded.push(null); |
| 41 | + return [query, padded]; |
| 42 | + } else if (diff < 0) { |
| 43 | + throw new Error(`Expected ${placeholders} parameters, but received ${(parameters as unknown[]).length}.`); |
| 44 | + } |
75 | 45 | } |
76 | 46 |
|
77 | | - return [query, []]; |
| 47 | + return [query, parameters as unknown[]]; |
78 | 48 | } |
0 commit comments