sqlstack 1.0.17 → 1.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/adapters/sqlite.d.ts.map +1 -1
- package/dist/cjs/adapters/sqlite.js +91 -7
- package/dist/cjs/adapters/sqlite.js.map +1 -1
- package/dist/cjs/binders/sqlBinder.d.ts.map +1 -1
- package/dist/cjs/binders/sqlBinder.js +761 -0
- package/dist/cjs/binders/sqlBinder.js.map +1 -1
- package/dist/cjs/core/errors.d.ts +5 -0
- package/dist/cjs/core/errors.d.ts.map +1 -1
- package/dist/cjs/core/errors.js +10 -1
- package/dist/cjs/core/errors.js.map +1 -1
- package/dist/cjs/decorators/query.d.ts.map +1 -1
- package/dist/cjs/decorators/query.js +8 -1
- package/dist/cjs/decorators/query.js.map +1 -1
- package/dist/esm/adapters/sqlite.js +91 -7
- package/dist/esm/adapters/sqlite.js.map +1 -1
- package/dist/esm/binders/sqlBinder.js +761 -0
- package/dist/esm/binders/sqlBinder.js.map +1 -1
- package/dist/esm/core/errors.js +8 -0
- package/dist/esm/core/errors.js.map +1 -1
- package/dist/esm/decorators/query.js +9 -2
- package/dist/esm/decorators/query.js.map +1 -1
- package/package.json +1 -1
- package/readme.md +285 -96
|
@@ -281,6 +281,767 @@ function createBinder(dialect) {
|
|
|
281
281
|
i = updateEnd;
|
|
282
282
|
continue;
|
|
283
283
|
}
|
|
284
|
+
// Check for :insert(...) syntax (case-insensitive)
|
|
285
|
+
const insertMatch = /^:insert\s*\(/i.exec(sql.slice(i));
|
|
286
|
+
if (insertMatch) {
|
|
287
|
+
if (isArrayArgs) {
|
|
288
|
+
throw new Error(':insert() syntax only works with named parameters');
|
|
289
|
+
}
|
|
290
|
+
// Parse the column list inside parentheses
|
|
291
|
+
let j = i + insertMatch[0].length; // Skip ':insert('
|
|
292
|
+
const columnStart = j;
|
|
293
|
+
let parenDepth = 1;
|
|
294
|
+
let inString = false;
|
|
295
|
+
let stringChar = '';
|
|
296
|
+
// Find matching closing parenthesis
|
|
297
|
+
while (j < len && parenDepth > 0) {
|
|
298
|
+
const c = sql[j];
|
|
299
|
+
const cNext = j + 1 < len ? sql[j + 1] : '';
|
|
300
|
+
if (!inString) {
|
|
301
|
+
if (c === "'" || c === '"') {
|
|
302
|
+
inString = true;
|
|
303
|
+
stringChar = c;
|
|
304
|
+
}
|
|
305
|
+
else if (c === '(') {
|
|
306
|
+
parenDepth++;
|
|
307
|
+
}
|
|
308
|
+
else if (c === ')') {
|
|
309
|
+
parenDepth--;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
if (c === stringChar) {
|
|
314
|
+
// Check for escaped quote
|
|
315
|
+
if (cNext === stringChar) {
|
|
316
|
+
j++; // Skip escaped quote
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
inString = false;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (parenDepth > 0)
|
|
324
|
+
j++;
|
|
325
|
+
}
|
|
326
|
+
if (parenDepth > 0) {
|
|
327
|
+
throw new Error('Unclosed :insert() parentheses');
|
|
328
|
+
}
|
|
329
|
+
// Extract column list (between the parentheses)
|
|
330
|
+
const columnList = sql.slice(columnStart, j).trim();
|
|
331
|
+
const insertEnd = j + 1; // Include closing ')'
|
|
332
|
+
// Parse column list (split by comma, respecting quotes and parentheses)
|
|
333
|
+
const items = [];
|
|
334
|
+
let current = '';
|
|
335
|
+
let inSQ = false; // single quote string
|
|
336
|
+
let inDQ = false; // double quote identifier
|
|
337
|
+
let itemParenDepth = 0;
|
|
338
|
+
for (let k = 0; k < columnList.length; k++) {
|
|
339
|
+
const ch = columnList[k];
|
|
340
|
+
const next = k + 1 < columnList.length ? columnList[k + 1] : '';
|
|
341
|
+
if (inSQ) {
|
|
342
|
+
current += ch;
|
|
343
|
+
if (ch === "'" && next === "'") {
|
|
344
|
+
current += next;
|
|
345
|
+
k++;
|
|
346
|
+
}
|
|
347
|
+
else if (ch === "'") {
|
|
348
|
+
inSQ = false;
|
|
349
|
+
}
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (inDQ) {
|
|
353
|
+
current += ch;
|
|
354
|
+
if (ch === '"' && next === '"') {
|
|
355
|
+
current += next;
|
|
356
|
+
k++;
|
|
357
|
+
}
|
|
358
|
+
else if (ch === '"') {
|
|
359
|
+
inDQ = false;
|
|
360
|
+
}
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (ch === "'") {
|
|
364
|
+
current += ch;
|
|
365
|
+
inSQ = true;
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
if (ch === '"') {
|
|
369
|
+
current += ch;
|
|
370
|
+
inDQ = true;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (ch === '(') {
|
|
374
|
+
current += ch;
|
|
375
|
+
itemParenDepth++;
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
if (ch === ')') {
|
|
379
|
+
current += ch;
|
|
380
|
+
itemParenDepth--;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
if (ch === ',' && itemParenDepth === 0) {
|
|
384
|
+
const trimmed = current.trim();
|
|
385
|
+
if (trimmed.length > 0) {
|
|
386
|
+
items.push(trimmed);
|
|
387
|
+
}
|
|
388
|
+
current = '';
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
current += ch;
|
|
392
|
+
}
|
|
393
|
+
// Add the last item
|
|
394
|
+
const trimmed = current.trim();
|
|
395
|
+
if (trimmed.length > 0) {
|
|
396
|
+
items.push(trimmed);
|
|
397
|
+
}
|
|
398
|
+
if (items.length === 0) {
|
|
399
|
+
throw new Error(':insert() requires at least one column name');
|
|
400
|
+
}
|
|
401
|
+
// Build column list and VALUES clause
|
|
402
|
+
const obj = args;
|
|
403
|
+
const columnNames = [];
|
|
404
|
+
const valueParts = [];
|
|
405
|
+
const insertParams = [];
|
|
406
|
+
let hasParameterized = false;
|
|
407
|
+
for (const item of items) {
|
|
408
|
+
const trimmed = item.trim();
|
|
409
|
+
// Check if this is a literal expression (contains = not inside quotes)
|
|
410
|
+
let isLiteral = false;
|
|
411
|
+
let eqIndex = -1;
|
|
412
|
+
let inString = false;
|
|
413
|
+
let stringChar = '';
|
|
414
|
+
for (let k = 0; k < trimmed.length; k++) {
|
|
415
|
+
const ch = trimmed[k];
|
|
416
|
+
const next = k + 1 < trimmed.length ? trimmed[k + 1] : '';
|
|
417
|
+
if (!inString && (ch === "'" || ch === '"')) {
|
|
418
|
+
inString = true;
|
|
419
|
+
stringChar = ch;
|
|
420
|
+
}
|
|
421
|
+
else if (inString) {
|
|
422
|
+
if (ch === stringChar) {
|
|
423
|
+
if (next === stringChar) {
|
|
424
|
+
k++; // Skip escaped quote
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
inString = false;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
else if (ch === '=') {
|
|
432
|
+
eqIndex = k;
|
|
433
|
+
isLiteral = true;
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (isLiteral && eqIndex >= 0) {
|
|
438
|
+
// Literal SQL expression - extract column name and value
|
|
439
|
+
const colName = trimmed.slice(0, eqIndex).trim();
|
|
440
|
+
const literalValue = trimmed.slice(eqIndex + 1).trim();
|
|
441
|
+
columnNames.push(colName);
|
|
442
|
+
valueParts.push(literalValue);
|
|
443
|
+
}
|
|
444
|
+
else {
|
|
445
|
+
// Parameterized column - check if defined in args
|
|
446
|
+
hasParameterized = true;
|
|
447
|
+
if (Object.prototype.hasOwnProperty.call(obj, trimmed) && obj[trimmed] !== undefined) {
|
|
448
|
+
columnNames.push(trimmed);
|
|
449
|
+
valueParts.push(placeholder(pgIndex));
|
|
450
|
+
insertParams.push(obj[trimmed]);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
// Only throw error if all parameterized columns are undefined (literals are always included)
|
|
455
|
+
if (hasParameterized && columnNames.length === 0) {
|
|
456
|
+
throw new Error('All columns in :insert() are undefined');
|
|
457
|
+
}
|
|
458
|
+
if (columnNames.length === 0) {
|
|
459
|
+
throw new Error(':insert() requires at least one column name');
|
|
460
|
+
}
|
|
461
|
+
// Replace :insert(...) with column list and VALUES clause
|
|
462
|
+
out.push('(', columnNames.join(', '), ') VALUES (', valueParts.join(', '), ')');
|
|
463
|
+
params.push(...insertParams);
|
|
464
|
+
i = insertEnd;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
// Check for :batch_insert(...) syntax (case-insensitive)
|
|
468
|
+
const batchInsertMatch = /^:batch_insert\s*\(/i.exec(sql.slice(i));
|
|
469
|
+
if (batchInsertMatch) {
|
|
470
|
+
if (isArrayArgs) {
|
|
471
|
+
throw new Error(':batch_insert() syntax only works with named parameters');
|
|
472
|
+
}
|
|
473
|
+
// Parse the argument list inside parentheses
|
|
474
|
+
let j = i + batchInsertMatch[0].length; // Skip ':batch_insert('
|
|
475
|
+
const argStart = j;
|
|
476
|
+
let parenDepth = 1;
|
|
477
|
+
let inString = false;
|
|
478
|
+
let stringChar = '';
|
|
479
|
+
// Find matching closing parenthesis
|
|
480
|
+
while (j < len && parenDepth > 0) {
|
|
481
|
+
const c = sql[j];
|
|
482
|
+
const cNext = j + 1 < len ? sql[j + 1] : '';
|
|
483
|
+
if (!inString) {
|
|
484
|
+
if (c === "'" || c === '"') {
|
|
485
|
+
inString = true;
|
|
486
|
+
stringChar = c;
|
|
487
|
+
}
|
|
488
|
+
else if (c === '(') {
|
|
489
|
+
parenDepth++;
|
|
490
|
+
}
|
|
491
|
+
else if (c === ')') {
|
|
492
|
+
parenDepth--;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
if (c === stringChar) {
|
|
497
|
+
// Check for escaped quote
|
|
498
|
+
if (cNext === stringChar) {
|
|
499
|
+
j++; // Skip escaped quote
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
inString = false;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (parenDepth > 0)
|
|
507
|
+
j++;
|
|
508
|
+
}
|
|
509
|
+
if (parenDepth > 0) {
|
|
510
|
+
throw new Error('Unclosed :batch_insert() parentheses');
|
|
511
|
+
}
|
|
512
|
+
// Extract argument list (between the parentheses)
|
|
513
|
+
const argList = sql.slice(argStart, j).trim();
|
|
514
|
+
const batchInsertEnd = j + 1; // Include closing ')'
|
|
515
|
+
// Parse argument list (split by comma, respecting quotes and parentheses)
|
|
516
|
+
const items = [];
|
|
517
|
+
let current = '';
|
|
518
|
+
let inSQ = false; // single quote string
|
|
519
|
+
let inDQ = false; // double quote identifier
|
|
520
|
+
let itemParenDepth = 0;
|
|
521
|
+
for (let k = 0; k < argList.length; k++) {
|
|
522
|
+
const ch2 = argList[k];
|
|
523
|
+
const next2 = k + 1 < argList.length ? argList[k + 1] : '';
|
|
524
|
+
if (inSQ) {
|
|
525
|
+
current += ch2;
|
|
526
|
+
if (ch2 === "'" && next2 === "'") {
|
|
527
|
+
current += next2;
|
|
528
|
+
k++;
|
|
529
|
+
}
|
|
530
|
+
else if (ch2 === "'") {
|
|
531
|
+
inSQ = false;
|
|
532
|
+
}
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
if (inDQ) {
|
|
536
|
+
current += ch2;
|
|
537
|
+
if (ch2 === '"' && next2 === '"') {
|
|
538
|
+
current += next2;
|
|
539
|
+
k++;
|
|
540
|
+
}
|
|
541
|
+
else if (ch2 === '"') {
|
|
542
|
+
inDQ = false;
|
|
543
|
+
}
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
if (ch2 === "'") {
|
|
547
|
+
current += ch2;
|
|
548
|
+
inSQ = true;
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
if (ch2 === '"') {
|
|
552
|
+
current += ch2;
|
|
553
|
+
inDQ = true;
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (ch2 === '(') {
|
|
557
|
+
current += ch2;
|
|
558
|
+
itemParenDepth++;
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
if (ch2 === ')') {
|
|
562
|
+
current += ch2;
|
|
563
|
+
itemParenDepth--;
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
if (ch2 === ',' && itemParenDepth === 0) {
|
|
567
|
+
const trimmedItem = current.trim();
|
|
568
|
+
if (trimmedItem.length > 0) {
|
|
569
|
+
items.push(trimmedItem);
|
|
570
|
+
}
|
|
571
|
+
current = '';
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
574
|
+
current += ch2;
|
|
575
|
+
}
|
|
576
|
+
// Add the last item
|
|
577
|
+
const trimmedItem = current.trim();
|
|
578
|
+
if (trimmedItem.length > 0) {
|
|
579
|
+
items.push(trimmedItem);
|
|
580
|
+
}
|
|
581
|
+
if (items.length === 0) {
|
|
582
|
+
throw new Error(':batch_insert() requires at least one argument');
|
|
583
|
+
}
|
|
584
|
+
const obj = args;
|
|
585
|
+
const batchArgs = [];
|
|
586
|
+
const resolvePath = (root, path) => {
|
|
587
|
+
const segments = path.split('.').map((s) => s.trim()).filter((s) => s.length > 0);
|
|
588
|
+
if (segments.length === 0) {
|
|
589
|
+
throw new Error(`Invalid path "${path}" in :batch_insert()`);
|
|
590
|
+
}
|
|
591
|
+
const nestedArrayError = () => {
|
|
592
|
+
throw new Error('Nested arrays are not supported in :batch_insert()');
|
|
593
|
+
};
|
|
594
|
+
const descend = (value, idx) => {
|
|
595
|
+
if (idx >= segments.length) {
|
|
596
|
+
if (Array.isArray(value)) {
|
|
597
|
+
// At leaf: array-of-arrays is not allowed
|
|
598
|
+
for (const v of value) {
|
|
599
|
+
if (Array.isArray(v))
|
|
600
|
+
nestedArrayError();
|
|
601
|
+
}
|
|
602
|
+
return { isArray: true, values: value.slice() };
|
|
603
|
+
}
|
|
604
|
+
return { isArray: false, values: [value] };
|
|
605
|
+
}
|
|
606
|
+
const key = segments[idx];
|
|
607
|
+
if (Array.isArray(value)) {
|
|
608
|
+
// We are traversing through an array: map remaining path over each element
|
|
609
|
+
const rowValues = [];
|
|
610
|
+
for (const element of value) {
|
|
611
|
+
const sub = descend(element, idx);
|
|
612
|
+
if (sub.isArray)
|
|
613
|
+
nestedArrayError();
|
|
614
|
+
rowValues.push(sub.values[0]);
|
|
615
|
+
}
|
|
616
|
+
return { isArray: true, values: rowValues };
|
|
617
|
+
}
|
|
618
|
+
let nextVal;
|
|
619
|
+
if (value && typeof value === 'object' && Object.prototype.hasOwnProperty.call(value, key)) {
|
|
620
|
+
nextVal = value[key];
|
|
621
|
+
}
|
|
622
|
+
else {
|
|
623
|
+
nextVal = undefined;
|
|
624
|
+
}
|
|
625
|
+
return descend(nextVal, idx + 1);
|
|
626
|
+
};
|
|
627
|
+
return descend(root, 0);
|
|
628
|
+
};
|
|
629
|
+
for (const item of items) {
|
|
630
|
+
const path = item.trim();
|
|
631
|
+
const firstSeg = path.split('.')[0];
|
|
632
|
+
if (!Object.prototype.hasOwnProperty.call(obj, firstSeg)) {
|
|
633
|
+
throw new Error(`Path "${path}" in :batch_insert() refers to a missing key`);
|
|
634
|
+
}
|
|
635
|
+
const { isArray, values } = resolvePath(obj, path);
|
|
636
|
+
// Undefined is not allowed for any value
|
|
637
|
+
for (const v of values) {
|
|
638
|
+
if (v === undefined) {
|
|
639
|
+
throw new Error(`Path "${path}" in :batch_insert() resolved to undefined`);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
batchArgs.push({ path, isArray, values });
|
|
643
|
+
}
|
|
644
|
+
const arrayArgs = batchArgs.filter((b) => b.isArray);
|
|
645
|
+
if (arrayArgs.length === 0) {
|
|
646
|
+
throw new Error(':batch_insert() requires at least one array argument');
|
|
647
|
+
}
|
|
648
|
+
const rowCount = arrayArgs[0].values.length;
|
|
649
|
+
if (rowCount === 0) {
|
|
650
|
+
throw new Error(':batch_insert() array arguments must not be empty');
|
|
651
|
+
}
|
|
652
|
+
for (const argInfo of arrayArgs) {
|
|
653
|
+
if (argInfo.values.length !== rowCount) {
|
|
654
|
+
throw new Error(':batch_insert() arrays must share the same length');
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
// Build VALUES clause
|
|
658
|
+
const valueTuples = [];
|
|
659
|
+
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
|
|
660
|
+
const placeholders = [];
|
|
661
|
+
for (const argInfo of batchArgs) {
|
|
662
|
+
const value = argInfo.isArray ? argInfo.values[rowIndex] : argInfo.values[0];
|
|
663
|
+
placeholders.push(placeholder(pgIndex));
|
|
664
|
+
params.push(value);
|
|
665
|
+
}
|
|
666
|
+
valueTuples.push(`(${placeholders.join(', ')})`);
|
|
667
|
+
}
|
|
668
|
+
out.push('VALUES ', valueTuples.join(', '));
|
|
669
|
+
i = batchInsertEnd;
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
// Check for :filter(...) syntax (case-insensitive)
|
|
673
|
+
const filterMatch = /^:filter\s*\(/i.exec(sql.slice(i));
|
|
674
|
+
if (filterMatch) {
|
|
675
|
+
if (isArrayArgs) {
|
|
676
|
+
throw new Error(':filter() syntax only works with named parameters');
|
|
677
|
+
}
|
|
678
|
+
// Parse the condition list inside parentheses
|
|
679
|
+
let j = i + filterMatch[0].length; // Skip ':filter('
|
|
680
|
+
const conditionStart = j;
|
|
681
|
+
let parenDepth = 1;
|
|
682
|
+
let inString = false;
|
|
683
|
+
let stringChar = '';
|
|
684
|
+
// Find matching closing parenthesis
|
|
685
|
+
while (j < len && parenDepth > 0) {
|
|
686
|
+
const c = sql[j];
|
|
687
|
+
const cNext = j + 1 < len ? sql[j + 1] : '';
|
|
688
|
+
if (!inString) {
|
|
689
|
+
if (c === "'" || c === '"') {
|
|
690
|
+
inString = true;
|
|
691
|
+
stringChar = c;
|
|
692
|
+
}
|
|
693
|
+
else if (c === '(') {
|
|
694
|
+
parenDepth++;
|
|
695
|
+
}
|
|
696
|
+
else if (c === ')') {
|
|
697
|
+
parenDepth--;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
else {
|
|
701
|
+
if (c === stringChar) {
|
|
702
|
+
// Check for escaped quote
|
|
703
|
+
if (cNext === stringChar) {
|
|
704
|
+
j++; // Skip escaped quote
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
inString = false;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
if (parenDepth > 0)
|
|
712
|
+
j++;
|
|
713
|
+
}
|
|
714
|
+
if (parenDepth > 0) {
|
|
715
|
+
throw new Error('Unclosed :filter() parentheses');
|
|
716
|
+
}
|
|
717
|
+
// Extract condition list (between the parentheses)
|
|
718
|
+
const conditionList = sql.slice(conditionStart, j).trim();
|
|
719
|
+
const filterEnd = j + 1; // Include closing ')'
|
|
720
|
+
// Parse condition list (split by comma, respecting quotes and parentheses)
|
|
721
|
+
const items = [];
|
|
722
|
+
let current = '';
|
|
723
|
+
let inSQ = false; // single quote string
|
|
724
|
+
let inDQ = false; // double quote identifier
|
|
725
|
+
let itemParenDepth = 0;
|
|
726
|
+
for (let k = 0; k < conditionList.length; k++) {
|
|
727
|
+
const ch = conditionList[k];
|
|
728
|
+
const next = k + 1 < conditionList.length ? conditionList[k + 1] : '';
|
|
729
|
+
if (inSQ) {
|
|
730
|
+
current += ch;
|
|
731
|
+
if (ch === "'" && next === "'") {
|
|
732
|
+
current += next;
|
|
733
|
+
k++;
|
|
734
|
+
}
|
|
735
|
+
else if (ch === "'") {
|
|
736
|
+
inSQ = false;
|
|
737
|
+
}
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
if (inDQ) {
|
|
741
|
+
current += ch;
|
|
742
|
+
if (ch === '"' && next === '"') {
|
|
743
|
+
current += next;
|
|
744
|
+
k++;
|
|
745
|
+
}
|
|
746
|
+
else if (ch === '"') {
|
|
747
|
+
inDQ = false;
|
|
748
|
+
}
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
if (ch === "'") {
|
|
752
|
+
current += ch;
|
|
753
|
+
inSQ = true;
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
if (ch === '"') {
|
|
757
|
+
current += ch;
|
|
758
|
+
inDQ = true;
|
|
759
|
+
continue;
|
|
760
|
+
}
|
|
761
|
+
if (ch === '(') {
|
|
762
|
+
current += ch;
|
|
763
|
+
itemParenDepth++;
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
if (ch === ')') {
|
|
767
|
+
current += ch;
|
|
768
|
+
itemParenDepth--;
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
if (ch === ',' && itemParenDepth === 0) {
|
|
772
|
+
const trimmed = current.trim();
|
|
773
|
+
if (trimmed.length > 0) {
|
|
774
|
+
items.push(trimmed);
|
|
775
|
+
}
|
|
776
|
+
current = '';
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
current += ch;
|
|
780
|
+
}
|
|
781
|
+
// Add the last item
|
|
782
|
+
const trimmed = current.trim();
|
|
783
|
+
if (trimmed.length > 0) {
|
|
784
|
+
items.push(trimmed);
|
|
785
|
+
}
|
|
786
|
+
if (items.length === 0) {
|
|
787
|
+
throw new Error(':filter() requires at least one condition');
|
|
788
|
+
}
|
|
789
|
+
// Build WHERE/HAVING conditions
|
|
790
|
+
const obj = args;
|
|
791
|
+
const conditions = [];
|
|
792
|
+
const filterParams = [];
|
|
793
|
+
let hasParameterized = false;
|
|
794
|
+
// Helper function to expand :or(:param) patterns
|
|
795
|
+
const expandOr = (expr) => {
|
|
796
|
+
// Find all :or(:param) patterns
|
|
797
|
+
const orPattern = /:or\s*\(\s*:(\w+)\s*\)/gi;
|
|
798
|
+
const matches = [];
|
|
799
|
+
let match;
|
|
800
|
+
while ((match = orPattern.exec(expr)) !== null) {
|
|
801
|
+
matches.push({
|
|
802
|
+
fullMatch: match[0],
|
|
803
|
+
paramName: match[1],
|
|
804
|
+
index: match.index
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
if (matches.length === 0) {
|
|
808
|
+
return { condition: '', params: [] };
|
|
809
|
+
}
|
|
810
|
+
// Check if all parameters are defined
|
|
811
|
+
let shouldInclude = true;
|
|
812
|
+
const replacements = [];
|
|
813
|
+
for (const m of matches) {
|
|
814
|
+
if (!Object.prototype.hasOwnProperty.call(obj, m.paramName) || obj[m.paramName] === undefined) {
|
|
815
|
+
shouldInclude = false;
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
const arr = obj[m.paramName];
|
|
819
|
+
if (!Array.isArray(arr) || arr.length === 0) {
|
|
820
|
+
shouldInclude = false;
|
|
821
|
+
break;
|
|
822
|
+
}
|
|
823
|
+
// Find the column expression for this :or() pattern
|
|
824
|
+
// Look backwards from the match to find the column name and operator
|
|
825
|
+
const beforeOr = expr.slice(0, m.index);
|
|
826
|
+
const afterOr = expr.slice(m.index + m.fullMatch.length);
|
|
827
|
+
// Try to match column name and operator pattern
|
|
828
|
+
// Match patterns like "col =", "col >", "col <=", "col LIKE", etc.
|
|
829
|
+
const beforeTrimmed = beforeOr.trim();
|
|
830
|
+
const operatorMatch = beforeTrimmed.match(/(\w+)\s+([=<>!]+|>=|<=|!=|<>|LIKE|IN|BETWEEN|IS(?:\s+NOT)?)\s*$/i);
|
|
831
|
+
if (operatorMatch) {
|
|
832
|
+
const colName = operatorMatch[1];
|
|
833
|
+
const operator = operatorMatch[2];
|
|
834
|
+
const parts = [];
|
|
835
|
+
const params = [];
|
|
836
|
+
for (const val of arr) {
|
|
837
|
+
if (val === undefined)
|
|
838
|
+
continue;
|
|
839
|
+
parts.push(`${colName} ${operator} ${placeholder(pgIndex)}`);
|
|
840
|
+
params.push(val);
|
|
841
|
+
}
|
|
842
|
+
if (parts.length > 0) {
|
|
843
|
+
// Replace the column/operator and :or() together
|
|
844
|
+
// Find the operator text in the original expr by searching backwards
|
|
845
|
+
const operatorText = operatorMatch[0];
|
|
846
|
+
// Find the last occurrence of this pattern before m.index
|
|
847
|
+
let operatorStart = -1;
|
|
848
|
+
for (let pos = m.index - operatorText.length; pos >= 0; pos--) {
|
|
849
|
+
if (expr.slice(pos, pos + operatorText.length) === operatorText) {
|
|
850
|
+
operatorStart = pos;
|
|
851
|
+
break;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
if (operatorStart === -1) {
|
|
855
|
+
// Fallback: use m.index - operatorText.length
|
|
856
|
+
operatorStart = Math.max(0, m.index - operatorText.length);
|
|
857
|
+
}
|
|
858
|
+
replacements.push({
|
|
859
|
+
index: operatorStart,
|
|
860
|
+
length: m.index + m.fullMatch.length - operatorStart,
|
|
861
|
+
replacement: parts.join(' OR '),
|
|
862
|
+
params
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
else {
|
|
866
|
+
shouldInclude = false;
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
else {
|
|
871
|
+
// Fallback: use the entire before/after context
|
|
872
|
+
const parts = [];
|
|
873
|
+
const params = [];
|
|
874
|
+
for (const val of arr) {
|
|
875
|
+
if (val === undefined)
|
|
876
|
+
continue;
|
|
877
|
+
parts.push(`${beforeTrimmed}${placeholder(pgIndex)}${afterOr.trim()}`);
|
|
878
|
+
params.push(val);
|
|
879
|
+
}
|
|
880
|
+
if (parts.length > 0) {
|
|
881
|
+
replacements.push({
|
|
882
|
+
index: m.index,
|
|
883
|
+
length: m.fullMatch.length,
|
|
884
|
+
replacement: parts.join(' OR '),
|
|
885
|
+
params
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
else {
|
|
889
|
+
shouldInclude = false;
|
|
890
|
+
break;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
if (!shouldInclude || replacements.length === 0) {
|
|
895
|
+
return { condition: '', params: [] };
|
|
896
|
+
}
|
|
897
|
+
// Replace from end to start to preserve indices
|
|
898
|
+
let resultExpr = expr;
|
|
899
|
+
const allParams = [];
|
|
900
|
+
for (const rep of replacements.reverse()) {
|
|
901
|
+
const before = resultExpr.slice(0, rep.index);
|
|
902
|
+
const after = resultExpr.slice(rep.index + rep.length);
|
|
903
|
+
resultExpr = before + rep.replacement + after;
|
|
904
|
+
allParams.unshift(...rep.params);
|
|
905
|
+
}
|
|
906
|
+
// Check if the original expression was already wrapped in parentheses
|
|
907
|
+
const trimmedExpr = expr.trim();
|
|
908
|
+
const alreadyWrapped = trimmedExpr.startsWith('(') && trimmedExpr.endsWith(')');
|
|
909
|
+
const wrappedCondition = alreadyWrapped ? resultExpr : `(${resultExpr})`;
|
|
910
|
+
return { condition: wrappedCondition, params: allParams };
|
|
911
|
+
};
|
|
912
|
+
for (const item of items) {
|
|
913
|
+
const trimmed = item.trim();
|
|
914
|
+
// Check for :or(:param) pattern first
|
|
915
|
+
const orResult = expandOr(trimmed);
|
|
916
|
+
if (orResult.condition) {
|
|
917
|
+
conditions.push(orResult.condition);
|
|
918
|
+
filterParams.push(...orResult.params);
|
|
919
|
+
continue;
|
|
920
|
+
}
|
|
921
|
+
// Check if this is a simple column name (no operators, no :param)
|
|
922
|
+
let hasOperator = false;
|
|
923
|
+
let hasParam = false;
|
|
924
|
+
let inString = false;
|
|
925
|
+
let stringChar = '';
|
|
926
|
+
for (let k = 0; k < trimmed.length; k++) {
|
|
927
|
+
const ch = trimmed[k];
|
|
928
|
+
const next = k + 1 < trimmed.length ? trimmed[k + 1] : '';
|
|
929
|
+
if (!inString && (ch === "'" || ch === '"')) {
|
|
930
|
+
inString = true;
|
|
931
|
+
stringChar = ch;
|
|
932
|
+
}
|
|
933
|
+
else if (inString) {
|
|
934
|
+
if (ch === stringChar) {
|
|
935
|
+
if (next === stringChar) {
|
|
936
|
+
k++; // Skip escaped quote
|
|
937
|
+
}
|
|
938
|
+
else {
|
|
939
|
+
inString = false;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
else {
|
|
944
|
+
// Check for operators (outside strings)
|
|
945
|
+
if ((ch === '>' || ch === '<' || ch === '=' || ch === '!') ||
|
|
946
|
+
(ch === 'L' && trimmed.slice(k, k + 4).toUpperCase() === 'LIKE') ||
|
|
947
|
+
(ch === 'I' && trimmed.slice(k, k + 2).toUpperCase() === 'IN') ||
|
|
948
|
+
(ch === 'B' && trimmed.slice(k, k + 7).toUpperCase() === 'BETWEEN') ||
|
|
949
|
+
(ch === 'I' && trimmed.slice(k, k + 2).toUpperCase() === 'IS')) {
|
|
950
|
+
hasOperator = true;
|
|
951
|
+
}
|
|
952
|
+
// Check for :param pattern
|
|
953
|
+
if (ch === ':' && next && /[a-zA-Z_]/.test(next)) {
|
|
954
|
+
hasParam = true;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
if (!hasOperator && !hasParam) {
|
|
959
|
+
// Simple column name → equality
|
|
960
|
+
hasParameterized = true;
|
|
961
|
+
if (Object.prototype.hasOwnProperty.call(obj, trimmed) && obj[trimmed] !== undefined) {
|
|
962
|
+
conditions.push(`${trimmed} = ${placeholder(pgIndex)}`);
|
|
963
|
+
filterParams.push(obj[trimmed]);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
else if (hasParam) {
|
|
967
|
+
// Parameterized expression → extract and bind parameters
|
|
968
|
+
hasParameterized = true;
|
|
969
|
+
// Extract all :param patterns and check if they're defined
|
|
970
|
+
let shouldInclude = true;
|
|
971
|
+
const paramMatches = [];
|
|
972
|
+
let searchStart = 0;
|
|
973
|
+
while (true) {
|
|
974
|
+
const paramMatch = /:([a-zA-Z_][a-zA-Z0-9_]*)/g.exec(trimmed.slice(searchStart));
|
|
975
|
+
if (!paramMatch)
|
|
976
|
+
break;
|
|
977
|
+
const paramName = paramMatch[1];
|
|
978
|
+
const fullIndex = searchStart + paramMatch.index;
|
|
979
|
+
paramMatches.push({ name: paramName, index: fullIndex });
|
|
980
|
+
if (!Object.prototype.hasOwnProperty.call(obj, paramName) || obj[paramName] === undefined) {
|
|
981
|
+
shouldInclude = false;
|
|
982
|
+
break;
|
|
983
|
+
}
|
|
984
|
+
searchStart = fullIndex + paramMatch[0].length;
|
|
985
|
+
}
|
|
986
|
+
if (shouldInclude && paramMatches.length > 0) {
|
|
987
|
+
// Replace :param with placeholders (from end to start to preserve indices)
|
|
988
|
+
let conditionExpr = trimmed;
|
|
989
|
+
const orderedParams = [];
|
|
990
|
+
for (const pm of paramMatches.reverse()) {
|
|
991
|
+
const paramValue = obj[pm.name];
|
|
992
|
+
const before = conditionExpr.slice(0, pm.index);
|
|
993
|
+
const after = conditionExpr.slice(pm.index + pm.name.length + 1);
|
|
994
|
+
// Check if this is an array parameter (for IN clauses)
|
|
995
|
+
if (Array.isArray(paramValue)) {
|
|
996
|
+
if (paramValue.length === 0) {
|
|
997
|
+
shouldInclude = false;
|
|
998
|
+
break;
|
|
999
|
+
}
|
|
1000
|
+
// Expand array: replace :ids with ?, ?, ?
|
|
1001
|
+
const placeholders = paramValue.map(() => placeholder(pgIndex)).join(', ');
|
|
1002
|
+
conditionExpr = before + placeholders + after;
|
|
1003
|
+
orderedParams.unshift(...paramValue);
|
|
1004
|
+
}
|
|
1005
|
+
else {
|
|
1006
|
+
conditionExpr = before + placeholder(pgIndex) + after;
|
|
1007
|
+
orderedParams.unshift(paramValue);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
if (shouldInclude) {
|
|
1011
|
+
// Add params in original order
|
|
1012
|
+
const finalParams = [];
|
|
1013
|
+
for (const pm of paramMatches.reverse()) {
|
|
1014
|
+
const paramValue = obj[pm.name];
|
|
1015
|
+
if (Array.isArray(paramValue)) {
|
|
1016
|
+
finalParams.push(...paramValue);
|
|
1017
|
+
}
|
|
1018
|
+
else {
|
|
1019
|
+
finalParams.push(paramValue);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
conditions.push(conditionExpr);
|
|
1023
|
+
filterParams.push(...finalParams);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
else {
|
|
1028
|
+
// Literal expression → include as-is
|
|
1029
|
+
conditions.push(trimmed);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
// Only throw error if all parameterized conditions are undefined (literals are always included)
|
|
1033
|
+
if (hasParameterized && conditions.length === 0) {
|
|
1034
|
+
throw new Error('All conditions in :filter() are undefined');
|
|
1035
|
+
}
|
|
1036
|
+
if (conditions.length === 0) {
|
|
1037
|
+
throw new Error(':filter() requires at least one condition');
|
|
1038
|
+
}
|
|
1039
|
+
// Replace :filter(...) with conditions joined by AND
|
|
1040
|
+
out.push(conditions.join(' AND '));
|
|
1041
|
+
params.push(...filterParams);
|
|
1042
|
+
i = filterEnd;
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
284
1045
|
// Attempt to parse a parameter token
|
|
285
1046
|
let j = i + 1;
|
|
286
1047
|
const start = j;
|