sqyrl 0.0.2

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/index.mjs ADDED
@@ -0,0 +1,1459 @@
1
+ import { createRequire } from "node:module";
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
21
+ value: mod,
22
+ enumerable: true
23
+ }) : target, mod));
24
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
25
+ //#endregion
26
+ //#region src/output.ts
27
+ function outputSql(ast) {
28
+ const parts = [];
29
+ parts.push(`SELECT ${ast.columns.map(outputColumn).join(", ")}`);
30
+ parts.push(`FROM ${outputTableRef(ast.from)}`);
31
+ if (ast.where) parts.push(`WHERE ${outputWhereExpr(ast.where)}`);
32
+ if (ast.limit !== null) parts.push(`LIMIT ${ast.limit}`);
33
+ return parts.join(" ");
34
+ }
35
+ function outputColumn(col) {
36
+ let expr;
37
+ switch (col.type) {
38
+ case "wildcard":
39
+ expr = "*";
40
+ break;
41
+ case "qualified_wildcard":
42
+ expr = `${col.table}.*`;
43
+ break;
44
+ case "qualified":
45
+ expr = `${col.table}.${col.name}`;
46
+ break;
47
+ case "simple":
48
+ expr = col.name;
49
+ break;
50
+ }
51
+ return col.alias ? `${expr} AS ${col.alias}` : expr;
52
+ }
53
+ function outputTableRef(ref) {
54
+ const name = ref.schema ? `${ref.schema}.${ref.name}` : ref.name;
55
+ return ref.alias ? `${name} AS ${ref.alias}` : name;
56
+ }
57
+ function outputColumnRef(ref) {
58
+ return ref.table ? `${ref.table}.${ref.name}` : ref.name;
59
+ }
60
+ function outputWhereExpr(expr, parentType) {
61
+ switch (expr.type) {
62
+ case "comparison": return `${outputColumnRef(expr.column)} = '${expr.value}'`;
63
+ case "and": {
64
+ const s = `${outputWhereExpr(expr.left, "and")} AND ${outputWhereExpr(expr.right, "and")}`;
65
+ return parentType === "or" ? `(${s})` : s;
66
+ }
67
+ case "or": {
68
+ const s = `${outputWhereExpr(expr.left, "or")} OR ${outputWhereExpr(expr.right, "or")}`;
69
+ return parentType === "and" ? `(${s})` : s;
70
+ }
71
+ }
72
+ }
73
+ //#endregion
74
+ //#region src/parse.ts
75
+ var import_sql_ohm_bundle = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exports, module) => {
76
+ const { makeRecipe } = __require("ohm-js");
77
+ module.exports = makeRecipe([
78
+ "grammar",
79
+ { source: "SQL {\n Statement = SelectStatement \";\"?\n\n SelectStatement\n = select ColumnList from TableRef WhereClause? LimitClause?\n\n // --- Columns ---\n\n ColumnList = NonemptyListOf<ColumnEntry, \",\">\n\n ColumnEntry\n = ColumnExpr as identifier -- aliased\n | ColumnExpr -- plain\n\n ColumnExpr\n = qualifiedWildcard -- qualifiedWild\n | wildcard -- wild\n | qualifiedName -- qualified\n | identifier -- simple\n\n qualifiedWildcard = identifier \".\" \"*\"\n qualifiedName = identifier \".\" identifier\n wildcard = \"*\"\n\n // --- FROM ---\n\n TableRef\n = TableName as identifier -- aliased\n | TableName identifier -- implicitAlias\n | TableName -- plain\n\n TableName\n = identifier \".\" identifier -- qualified\n | identifier -- simple\n\n // --- WHERE ---\n\n WhereClause = where WhereExpr\n\n WhereExpr = WhereOrExpr\n\n WhereOrExpr\n = WhereOrExpr or WhereAndExpr -- or\n | WhereAndExpr\n\n WhereAndExpr\n = WhereAndExpr and WherePrimary -- and\n | WherePrimary\n\n WherePrimary\n = \"(\" WhereExpr \")\" -- paren\n | WhereComparison\n\n WhereComparison = ColumnRef \"=\" stringLiteral\n\n ColumnRef\n = identifier \".\" identifier -- qualified\n | identifier -- simple\n\n stringLiteral = \"'\" (~\"'\" any)* \"'\"\n\n // --- LIMIT ---\n\n LimitClause = limit integer\n\n // --- Terminals ---\n\n identifier = ~keyword letter (alnum | \"_\")*\n integer = digit+\n\n // --- Keywords (case-insensitive) ---\n\n select = caseInsensitive<\"select\"> ~identPart\n from = caseInsensitive<\"from\"> ~identPart\n as = caseInsensitive<\"as\"> ~identPart\n where = caseInsensitive<\"where\"> ~identPart\n and = caseInsensitive<\"and\"> ~identPart\n or = caseInsensitive<\"or\"> ~identPart\n limit = caseInsensitive<\"limit\"> ~identPart\n\n keyword = select | from | as | where | and | or | limit\n identPart = alnum | \"_\"\n\n // --- Comments & whitespace ---\n\n space += lineComment\n lineComment = \"--\" (~\"\\n\" any)*\n}" },
80
+ "SQL",
81
+ null,
82
+ "Statement",
83
+ {
84
+ Statement: [
85
+ "define",
86
+ { sourceInterval: [8, 40] },
87
+ null,
88
+ [],
89
+ [
90
+ "seq",
91
+ { sourceInterval: [20, 40] },
92
+ [
93
+ "app",
94
+ { sourceInterval: [20, 35] },
95
+ "SelectStatement",
96
+ []
97
+ ],
98
+ [
99
+ "opt",
100
+ { sourceInterval: [36, 40] },
101
+ [
102
+ "terminal",
103
+ { sourceInterval: [36, 39] },
104
+ ";"
105
+ ]
106
+ ]
107
+ ]
108
+ ],
109
+ SelectStatement: [
110
+ "define",
111
+ { sourceInterval: [44, 123] },
112
+ null,
113
+ [],
114
+ [
115
+ "seq",
116
+ { sourceInterval: [66, 123] },
117
+ [
118
+ "app",
119
+ { sourceInterval: [66, 72] },
120
+ "select",
121
+ []
122
+ ],
123
+ [
124
+ "app",
125
+ { sourceInterval: [73, 83] },
126
+ "ColumnList",
127
+ []
128
+ ],
129
+ [
130
+ "app",
131
+ { sourceInterval: [84, 88] },
132
+ "from",
133
+ []
134
+ ],
135
+ [
136
+ "app",
137
+ { sourceInterval: [89, 97] },
138
+ "TableRef",
139
+ []
140
+ ],
141
+ [
142
+ "opt",
143
+ { sourceInterval: [98, 110] },
144
+ [
145
+ "app",
146
+ { sourceInterval: [98, 109] },
147
+ "WhereClause",
148
+ []
149
+ ]
150
+ ],
151
+ [
152
+ "opt",
153
+ { sourceInterval: [111, 123] },
154
+ [
155
+ "app",
156
+ { sourceInterval: [111, 122] },
157
+ "LimitClause",
158
+ []
159
+ ]
160
+ ]
161
+ ]
162
+ ],
163
+ ColumnList: [
164
+ "define",
165
+ { sourceInterval: [149, 194] },
166
+ null,
167
+ [],
168
+ [
169
+ "app",
170
+ { sourceInterval: [162, 194] },
171
+ "NonemptyListOf",
172
+ [[
173
+ "app",
174
+ { sourceInterval: [177, 188] },
175
+ "ColumnEntry",
176
+ []
177
+ ], [
178
+ "terminal",
179
+ { sourceInterval: [190, 193] },
180
+ ","
181
+ ]]
182
+ ]
183
+ ],
184
+ ColumnEntry_aliased: [
185
+ "define",
186
+ { sourceInterval: [216, 252] },
187
+ null,
188
+ [],
189
+ [
190
+ "seq",
191
+ { sourceInterval: [216, 240] },
192
+ [
193
+ "app",
194
+ { sourceInterval: [216, 226] },
195
+ "ColumnExpr",
196
+ []
197
+ ],
198
+ [
199
+ "app",
200
+ { sourceInterval: [227, 229] },
201
+ "as",
202
+ []
203
+ ],
204
+ [
205
+ "app",
206
+ { sourceInterval: [230, 240] },
207
+ "identifier",
208
+ []
209
+ ]
210
+ ]
211
+ ],
212
+ ColumnEntry_plain: [
213
+ "define",
214
+ { sourceInterval: [259, 293] },
215
+ null,
216
+ [],
217
+ [
218
+ "app",
219
+ { sourceInterval: [259, 269] },
220
+ "ColumnExpr",
221
+ []
222
+ ]
223
+ ],
224
+ ColumnEntry: [
225
+ "define",
226
+ { sourceInterval: [198, 293] },
227
+ null,
228
+ [],
229
+ [
230
+ "alt",
231
+ { sourceInterval: [216, 293] },
232
+ [
233
+ "app",
234
+ { sourceInterval: [216, 240] },
235
+ "ColumnEntry_aliased",
236
+ []
237
+ ],
238
+ [
239
+ "app",
240
+ { sourceInterval: [259, 269] },
241
+ "ColumnEntry_plain",
242
+ []
243
+ ]
244
+ ]
245
+ ],
246
+ ColumnExpr_qualifiedWild: [
247
+ "define",
248
+ { sourceInterval: [314, 350] },
249
+ null,
250
+ [],
251
+ [
252
+ "app",
253
+ { sourceInterval: [314, 331] },
254
+ "qualifiedWildcard",
255
+ []
256
+ ]
257
+ ],
258
+ ColumnExpr_wild: [
259
+ "define",
260
+ { sourceInterval: [357, 384] },
261
+ null,
262
+ [],
263
+ [
264
+ "app",
265
+ { sourceInterval: [357, 365] },
266
+ "wildcard",
267
+ []
268
+ ]
269
+ ],
270
+ ColumnExpr_qualified: [
271
+ "define",
272
+ { sourceInterval: [391, 423] },
273
+ null,
274
+ [],
275
+ [
276
+ "app",
277
+ { sourceInterval: [391, 404] },
278
+ "qualifiedName",
279
+ []
280
+ ]
281
+ ],
282
+ ColumnExpr_simple: [
283
+ "define",
284
+ { sourceInterval: [430, 459] },
285
+ null,
286
+ [],
287
+ [
288
+ "app",
289
+ { sourceInterval: [430, 440] },
290
+ "identifier",
291
+ []
292
+ ]
293
+ ],
294
+ ColumnExpr: [
295
+ "define",
296
+ { sourceInterval: [297, 459] },
297
+ null,
298
+ [],
299
+ [
300
+ "alt",
301
+ { sourceInterval: [314, 459] },
302
+ [
303
+ "app",
304
+ { sourceInterval: [314, 331] },
305
+ "ColumnExpr_qualifiedWild",
306
+ []
307
+ ],
308
+ [
309
+ "app",
310
+ { sourceInterval: [357, 365] },
311
+ "ColumnExpr_wild",
312
+ []
313
+ ],
314
+ [
315
+ "app",
316
+ { sourceInterval: [391, 404] },
317
+ "ColumnExpr_qualified",
318
+ []
319
+ ],
320
+ [
321
+ "app",
322
+ { sourceInterval: [430, 440] },
323
+ "ColumnExpr_simple",
324
+ []
325
+ ]
326
+ ]
327
+ ],
328
+ qualifiedWildcard: [
329
+ "define",
330
+ { sourceInterval: [463, 501] },
331
+ null,
332
+ [],
333
+ [
334
+ "seq",
335
+ { sourceInterval: [483, 501] },
336
+ [
337
+ "app",
338
+ { sourceInterval: [483, 493] },
339
+ "identifier",
340
+ []
341
+ ],
342
+ [
343
+ "terminal",
344
+ { sourceInterval: [494, 497] },
345
+ "."
346
+ ],
347
+ [
348
+ "terminal",
349
+ { sourceInterval: [498, 501] },
350
+ "*"
351
+ ]
352
+ ]
353
+ ],
354
+ qualifiedName: [
355
+ "define",
356
+ { sourceInterval: [504, 549] },
357
+ null,
358
+ [],
359
+ [
360
+ "seq",
361
+ { sourceInterval: [524, 549] },
362
+ [
363
+ "app",
364
+ { sourceInterval: [524, 534] },
365
+ "identifier",
366
+ []
367
+ ],
368
+ [
369
+ "terminal",
370
+ { sourceInterval: [535, 538] },
371
+ "."
372
+ ],
373
+ [
374
+ "app",
375
+ { sourceInterval: [539, 549] },
376
+ "identifier",
377
+ []
378
+ ]
379
+ ]
380
+ ],
381
+ wildcard: [
382
+ "define",
383
+ { sourceInterval: [552, 575] },
384
+ null,
385
+ [],
386
+ [
387
+ "terminal",
388
+ { sourceInterval: [572, 575] },
389
+ "*"
390
+ ]
391
+ ],
392
+ TableRef_aliased: [
393
+ "define",
394
+ { sourceInterval: [613, 648] },
395
+ null,
396
+ [],
397
+ [
398
+ "seq",
399
+ { sourceInterval: [613, 636] },
400
+ [
401
+ "app",
402
+ { sourceInterval: [613, 622] },
403
+ "TableName",
404
+ []
405
+ ],
406
+ [
407
+ "app",
408
+ { sourceInterval: [623, 625] },
409
+ "as",
410
+ []
411
+ ],
412
+ [
413
+ "app",
414
+ { sourceInterval: [626, 636] },
415
+ "identifier",
416
+ []
417
+ ]
418
+ ]
419
+ ],
420
+ TableRef_implicitAlias: [
421
+ "define",
422
+ { sourceInterval: [655, 696] },
423
+ null,
424
+ [],
425
+ [
426
+ "seq",
427
+ { sourceInterval: [655, 675] },
428
+ [
429
+ "app",
430
+ { sourceInterval: [655, 664] },
431
+ "TableName",
432
+ []
433
+ ],
434
+ [
435
+ "app",
436
+ { sourceInterval: [665, 675] },
437
+ "identifier",
438
+ []
439
+ ]
440
+ ]
441
+ ],
442
+ TableRef_plain: [
443
+ "define",
444
+ { sourceInterval: [703, 736] },
445
+ null,
446
+ [],
447
+ [
448
+ "app",
449
+ { sourceInterval: [703, 712] },
450
+ "TableName",
451
+ []
452
+ ]
453
+ ],
454
+ TableRef: [
455
+ "define",
456
+ { sourceInterval: [598, 736] },
457
+ null,
458
+ [],
459
+ [
460
+ "alt",
461
+ { sourceInterval: [613, 736] },
462
+ [
463
+ "app",
464
+ { sourceInterval: [613, 636] },
465
+ "TableRef_aliased",
466
+ []
467
+ ],
468
+ [
469
+ "app",
470
+ { sourceInterval: [655, 675] },
471
+ "TableRef_implicitAlias",
472
+ []
473
+ ],
474
+ [
475
+ "app",
476
+ { sourceInterval: [703, 712] },
477
+ "TableRef_plain",
478
+ []
479
+ ]
480
+ ]
481
+ ],
482
+ TableName_qualified: [
483
+ "define",
484
+ { sourceInterval: [756, 795] },
485
+ null,
486
+ [],
487
+ [
488
+ "seq",
489
+ { sourceInterval: [756, 781] },
490
+ [
491
+ "app",
492
+ { sourceInterval: [756, 766] },
493
+ "identifier",
494
+ []
495
+ ],
496
+ [
497
+ "terminal",
498
+ { sourceInterval: [767, 770] },
499
+ "."
500
+ ],
501
+ [
502
+ "app",
503
+ { sourceInterval: [771, 781] },
504
+ "identifier",
505
+ []
506
+ ]
507
+ ]
508
+ ],
509
+ TableName_simple: [
510
+ "define",
511
+ { sourceInterval: [802, 838] },
512
+ null,
513
+ [],
514
+ [
515
+ "app",
516
+ { sourceInterval: [802, 812] },
517
+ "identifier",
518
+ []
519
+ ]
520
+ ],
521
+ TableName: [
522
+ "define",
523
+ { sourceInterval: [740, 838] },
524
+ null,
525
+ [],
526
+ [
527
+ "alt",
528
+ { sourceInterval: [756, 838] },
529
+ [
530
+ "app",
531
+ { sourceInterval: [756, 781] },
532
+ "TableName_qualified",
533
+ []
534
+ ],
535
+ [
536
+ "app",
537
+ { sourceInterval: [802, 812] },
538
+ "TableName_simple",
539
+ []
540
+ ]
541
+ ]
542
+ ],
543
+ WhereClause: [
544
+ "define",
545
+ { sourceInterval: [862, 891] },
546
+ null,
547
+ [],
548
+ [
549
+ "seq",
550
+ { sourceInterval: [876, 891] },
551
+ [
552
+ "app",
553
+ { sourceInterval: [876, 881] },
554
+ "where",
555
+ []
556
+ ],
557
+ [
558
+ "app",
559
+ { sourceInterval: [882, 891] },
560
+ "WhereExpr",
561
+ []
562
+ ]
563
+ ]
564
+ ],
565
+ WhereExpr: [
566
+ "define",
567
+ { sourceInterval: [895, 918] },
568
+ null,
569
+ [],
570
+ [
571
+ "app",
572
+ { sourceInterval: [907, 918] },
573
+ "WhereOrExpr",
574
+ []
575
+ ]
576
+ ],
577
+ WhereOrExpr_or: [
578
+ "define",
579
+ { sourceInterval: [940, 974] },
580
+ null,
581
+ [],
582
+ [
583
+ "seq",
584
+ { sourceInterval: [940, 967] },
585
+ [
586
+ "app",
587
+ { sourceInterval: [940, 951] },
588
+ "WhereOrExpr",
589
+ []
590
+ ],
591
+ [
592
+ "app",
593
+ { sourceInterval: [952, 954] },
594
+ "or",
595
+ []
596
+ ],
597
+ [
598
+ "app",
599
+ { sourceInterval: [955, 967] },
600
+ "WhereAndExpr",
601
+ []
602
+ ]
603
+ ]
604
+ ],
605
+ WhereOrExpr: [
606
+ "define",
607
+ { sourceInterval: [922, 993] },
608
+ null,
609
+ [],
610
+ [
611
+ "alt",
612
+ { sourceInterval: [940, 993] },
613
+ [
614
+ "app",
615
+ { sourceInterval: [940, 967] },
616
+ "WhereOrExpr_or",
617
+ []
618
+ ],
619
+ [
620
+ "app",
621
+ { sourceInterval: [981, 993] },
622
+ "WhereAndExpr",
623
+ []
624
+ ]
625
+ ]
626
+ ],
627
+ WhereAndExpr_and: [
628
+ "define",
629
+ { sourceInterval: [1016, 1053] },
630
+ null,
631
+ [],
632
+ [
633
+ "seq",
634
+ { sourceInterval: [1016, 1045] },
635
+ [
636
+ "app",
637
+ { sourceInterval: [1016, 1028] },
638
+ "WhereAndExpr",
639
+ []
640
+ ],
641
+ [
642
+ "app",
643
+ { sourceInterval: [1029, 1032] },
644
+ "and",
645
+ []
646
+ ],
647
+ [
648
+ "app",
649
+ { sourceInterval: [1033, 1045] },
650
+ "WherePrimary",
651
+ []
652
+ ]
653
+ ]
654
+ ],
655
+ WhereAndExpr: [
656
+ "define",
657
+ { sourceInterval: [997, 1072] },
658
+ null,
659
+ [],
660
+ [
661
+ "alt",
662
+ { sourceInterval: [1016, 1072] },
663
+ [
664
+ "app",
665
+ { sourceInterval: [1016, 1045] },
666
+ "WhereAndExpr_and",
667
+ []
668
+ ],
669
+ [
670
+ "app",
671
+ { sourceInterval: [1060, 1072] },
672
+ "WherePrimary",
673
+ []
674
+ ]
675
+ ]
676
+ ],
677
+ WherePrimary_paren: [
678
+ "define",
679
+ { sourceInterval: [1095, 1124] },
680
+ null,
681
+ [],
682
+ [
683
+ "seq",
684
+ { sourceInterval: [1095, 1112] },
685
+ [
686
+ "terminal",
687
+ { sourceInterval: [1095, 1098] },
688
+ "("
689
+ ],
690
+ [
691
+ "app",
692
+ { sourceInterval: [1099, 1108] },
693
+ "WhereExpr",
694
+ []
695
+ ],
696
+ [
697
+ "terminal",
698
+ { sourceInterval: [1109, 1112] },
699
+ ")"
700
+ ]
701
+ ]
702
+ ],
703
+ WherePrimary: [
704
+ "define",
705
+ { sourceInterval: [1076, 1146] },
706
+ null,
707
+ [],
708
+ [
709
+ "alt",
710
+ { sourceInterval: [1095, 1146] },
711
+ [
712
+ "app",
713
+ { sourceInterval: [1095, 1112] },
714
+ "WherePrimary_paren",
715
+ []
716
+ ],
717
+ [
718
+ "app",
719
+ { sourceInterval: [1131, 1146] },
720
+ "WhereComparison",
721
+ []
722
+ ]
723
+ ]
724
+ ],
725
+ WhereComparison: [
726
+ "define",
727
+ { sourceInterval: [1150, 1195] },
728
+ null,
729
+ [],
730
+ [
731
+ "seq",
732
+ { sourceInterval: [1168, 1195] },
733
+ [
734
+ "app",
735
+ { sourceInterval: [1168, 1177] },
736
+ "ColumnRef",
737
+ []
738
+ ],
739
+ [
740
+ "terminal",
741
+ { sourceInterval: [1178, 1181] },
742
+ "="
743
+ ],
744
+ [
745
+ "app",
746
+ { sourceInterval: [1182, 1195] },
747
+ "stringLiteral",
748
+ []
749
+ ]
750
+ ]
751
+ ],
752
+ ColumnRef_qualified: [
753
+ "define",
754
+ { sourceInterval: [1215, 1254] },
755
+ null,
756
+ [],
757
+ [
758
+ "seq",
759
+ { sourceInterval: [1215, 1240] },
760
+ [
761
+ "app",
762
+ { sourceInterval: [1215, 1225] },
763
+ "identifier",
764
+ []
765
+ ],
766
+ [
767
+ "terminal",
768
+ { sourceInterval: [1226, 1229] },
769
+ "."
770
+ ],
771
+ [
772
+ "app",
773
+ { sourceInterval: [1230, 1240] },
774
+ "identifier",
775
+ []
776
+ ]
777
+ ]
778
+ ],
779
+ ColumnRef_simple: [
780
+ "define",
781
+ { sourceInterval: [1261, 1297] },
782
+ null,
783
+ [],
784
+ [
785
+ "app",
786
+ { sourceInterval: [1261, 1271] },
787
+ "identifier",
788
+ []
789
+ ]
790
+ ],
791
+ ColumnRef: [
792
+ "define",
793
+ { sourceInterval: [1199, 1297] },
794
+ null,
795
+ [],
796
+ [
797
+ "alt",
798
+ { sourceInterval: [1215, 1297] },
799
+ [
800
+ "app",
801
+ { sourceInterval: [1215, 1240] },
802
+ "ColumnRef_qualified",
803
+ []
804
+ ],
805
+ [
806
+ "app",
807
+ { sourceInterval: [1261, 1271] },
808
+ "ColumnRef_simple",
809
+ []
810
+ ]
811
+ ]
812
+ ],
813
+ stringLiteral: [
814
+ "define",
815
+ { sourceInterval: [1301, 1336] },
816
+ null,
817
+ [],
818
+ [
819
+ "seq",
820
+ { sourceInterval: [1317, 1336] },
821
+ [
822
+ "terminal",
823
+ { sourceInterval: [1317, 1320] },
824
+ "'"
825
+ ],
826
+ [
827
+ "star",
828
+ { sourceInterval: [1321, 1332] },
829
+ [
830
+ "seq",
831
+ { sourceInterval: [1322, 1330] },
832
+ [
833
+ "not",
834
+ { sourceInterval: [1322, 1326] },
835
+ [
836
+ "terminal",
837
+ { sourceInterval: [1323, 1326] },
838
+ "'"
839
+ ]
840
+ ],
841
+ [
842
+ "app",
843
+ { sourceInterval: [1327, 1330] },
844
+ "any",
845
+ []
846
+ ]
847
+ ]
848
+ ],
849
+ [
850
+ "terminal",
851
+ { sourceInterval: [1333, 1336] },
852
+ "'"
853
+ ]
854
+ ]
855
+ ],
856
+ LimitClause: [
857
+ "define",
858
+ { sourceInterval: [1360, 1387] },
859
+ null,
860
+ [],
861
+ [
862
+ "seq",
863
+ { sourceInterval: [1374, 1387] },
864
+ [
865
+ "app",
866
+ { sourceInterval: [1374, 1379] },
867
+ "limit",
868
+ []
869
+ ],
870
+ [
871
+ "app",
872
+ { sourceInterval: [1380, 1387] },
873
+ "integer",
874
+ []
875
+ ]
876
+ ]
877
+ ],
878
+ identifier: [
879
+ "define",
880
+ { sourceInterval: [1415, 1458] },
881
+ null,
882
+ [],
883
+ [
884
+ "seq",
885
+ { sourceInterval: [1428, 1458] },
886
+ [
887
+ "not",
888
+ { sourceInterval: [1428, 1436] },
889
+ [
890
+ "app",
891
+ { sourceInterval: [1429, 1436] },
892
+ "keyword",
893
+ []
894
+ ]
895
+ ],
896
+ [
897
+ "app",
898
+ { sourceInterval: [1437, 1443] },
899
+ "letter",
900
+ []
901
+ ],
902
+ [
903
+ "star",
904
+ { sourceInterval: [1444, 1458] },
905
+ [
906
+ "alt",
907
+ { sourceInterval: [1445, 1456] },
908
+ [
909
+ "app",
910
+ { sourceInterval: [1445, 1450] },
911
+ "alnum",
912
+ []
913
+ ],
914
+ [
915
+ "terminal",
916
+ { sourceInterval: [1453, 1456] },
917
+ "_"
918
+ ]
919
+ ]
920
+ ]
921
+ ]
922
+ ],
923
+ integer: [
924
+ "define",
925
+ { sourceInterval: [1461, 1480] },
926
+ null,
927
+ [],
928
+ [
929
+ "plus",
930
+ { sourceInterval: [1474, 1480] },
931
+ [
932
+ "app",
933
+ { sourceInterval: [1474, 1479] },
934
+ "digit",
935
+ []
936
+ ]
937
+ ]
938
+ ],
939
+ select: [
940
+ "define",
941
+ { sourceInterval: [1526, 1571] },
942
+ null,
943
+ [],
944
+ [
945
+ "seq",
946
+ { sourceInterval: [1535, 1571] },
947
+ [
948
+ "app",
949
+ { sourceInterval: [1535, 1560] },
950
+ "caseInsensitive",
951
+ [[
952
+ "terminal",
953
+ { sourceInterval: [1551, 1559] },
954
+ "select"
955
+ ]]
956
+ ],
957
+ [
958
+ "not",
959
+ { sourceInterval: [1561, 1571] },
960
+ [
961
+ "app",
962
+ { sourceInterval: [1562, 1571] },
963
+ "identPart",
964
+ []
965
+ ]
966
+ ]
967
+ ]
968
+ ],
969
+ from: [
970
+ "define",
971
+ { sourceInterval: [1574, 1619] },
972
+ null,
973
+ [],
974
+ [
975
+ "seq",
976
+ { sourceInterval: [1583, 1619] },
977
+ [
978
+ "app",
979
+ { sourceInterval: [1583, 1606] },
980
+ "caseInsensitive",
981
+ [[
982
+ "terminal",
983
+ { sourceInterval: [1599, 1605] },
984
+ "from"
985
+ ]]
986
+ ],
987
+ [
988
+ "not",
989
+ { sourceInterval: [1609, 1619] },
990
+ [
991
+ "app",
992
+ { sourceInterval: [1610, 1619] },
993
+ "identPart",
994
+ []
995
+ ]
996
+ ]
997
+ ]
998
+ ],
999
+ as: [
1000
+ "define",
1001
+ { sourceInterval: [1622, 1667] },
1002
+ null,
1003
+ [],
1004
+ [
1005
+ "seq",
1006
+ { sourceInterval: [1631, 1667] },
1007
+ [
1008
+ "app",
1009
+ { sourceInterval: [1631, 1652] },
1010
+ "caseInsensitive",
1011
+ [[
1012
+ "terminal",
1013
+ { sourceInterval: [1647, 1651] },
1014
+ "as"
1015
+ ]]
1016
+ ],
1017
+ [
1018
+ "not",
1019
+ { sourceInterval: [1657, 1667] },
1020
+ [
1021
+ "app",
1022
+ { sourceInterval: [1658, 1667] },
1023
+ "identPart",
1024
+ []
1025
+ ]
1026
+ ]
1027
+ ]
1028
+ ],
1029
+ where: [
1030
+ "define",
1031
+ { sourceInterval: [1670, 1715] },
1032
+ null,
1033
+ [],
1034
+ [
1035
+ "seq",
1036
+ { sourceInterval: [1679, 1715] },
1037
+ [
1038
+ "app",
1039
+ { sourceInterval: [1679, 1703] },
1040
+ "caseInsensitive",
1041
+ [[
1042
+ "terminal",
1043
+ { sourceInterval: [1695, 1702] },
1044
+ "where"
1045
+ ]]
1046
+ ],
1047
+ [
1048
+ "not",
1049
+ { sourceInterval: [1705, 1715] },
1050
+ [
1051
+ "app",
1052
+ { sourceInterval: [1706, 1715] },
1053
+ "identPart",
1054
+ []
1055
+ ]
1056
+ ]
1057
+ ]
1058
+ ],
1059
+ and: [
1060
+ "define",
1061
+ { sourceInterval: [1718, 1763] },
1062
+ null,
1063
+ [],
1064
+ [
1065
+ "seq",
1066
+ { sourceInterval: [1727, 1763] },
1067
+ [
1068
+ "app",
1069
+ { sourceInterval: [1727, 1749] },
1070
+ "caseInsensitive",
1071
+ [[
1072
+ "terminal",
1073
+ { sourceInterval: [1743, 1748] },
1074
+ "and"
1075
+ ]]
1076
+ ],
1077
+ [
1078
+ "not",
1079
+ { sourceInterval: [1753, 1763] },
1080
+ [
1081
+ "app",
1082
+ { sourceInterval: [1754, 1763] },
1083
+ "identPart",
1084
+ []
1085
+ ]
1086
+ ]
1087
+ ]
1088
+ ],
1089
+ or: [
1090
+ "define",
1091
+ { sourceInterval: [1766, 1811] },
1092
+ null,
1093
+ [],
1094
+ [
1095
+ "seq",
1096
+ { sourceInterval: [1775, 1811] },
1097
+ [
1098
+ "app",
1099
+ { sourceInterval: [1775, 1796] },
1100
+ "caseInsensitive",
1101
+ [[
1102
+ "terminal",
1103
+ { sourceInterval: [1791, 1795] },
1104
+ "or"
1105
+ ]]
1106
+ ],
1107
+ [
1108
+ "not",
1109
+ { sourceInterval: [1801, 1811] },
1110
+ [
1111
+ "app",
1112
+ { sourceInterval: [1802, 1811] },
1113
+ "identPart",
1114
+ []
1115
+ ]
1116
+ ]
1117
+ ]
1118
+ ],
1119
+ limit: [
1120
+ "define",
1121
+ { sourceInterval: [1814, 1859] },
1122
+ null,
1123
+ [],
1124
+ [
1125
+ "seq",
1126
+ { sourceInterval: [1823, 1859] },
1127
+ [
1128
+ "app",
1129
+ { sourceInterval: [1823, 1847] },
1130
+ "caseInsensitive",
1131
+ [[
1132
+ "terminal",
1133
+ { sourceInterval: [1839, 1846] },
1134
+ "limit"
1135
+ ]]
1136
+ ],
1137
+ [
1138
+ "not",
1139
+ { sourceInterval: [1849, 1859] },
1140
+ [
1141
+ "app",
1142
+ { sourceInterval: [1850, 1859] },
1143
+ "identPart",
1144
+ []
1145
+ ]
1146
+ ]
1147
+ ]
1148
+ ],
1149
+ keyword: [
1150
+ "define",
1151
+ { sourceInterval: [1863, 1918] },
1152
+ null,
1153
+ [],
1154
+ [
1155
+ "alt",
1156
+ { sourceInterval: [1873, 1918] },
1157
+ [
1158
+ "app",
1159
+ { sourceInterval: [1873, 1879] },
1160
+ "select",
1161
+ []
1162
+ ],
1163
+ [
1164
+ "app",
1165
+ { sourceInterval: [1882, 1886] },
1166
+ "from",
1167
+ []
1168
+ ],
1169
+ [
1170
+ "app",
1171
+ { sourceInterval: [1889, 1891] },
1172
+ "as",
1173
+ []
1174
+ ],
1175
+ [
1176
+ "app",
1177
+ { sourceInterval: [1894, 1899] },
1178
+ "where",
1179
+ []
1180
+ ],
1181
+ [
1182
+ "app",
1183
+ { sourceInterval: [1902, 1905] },
1184
+ "and",
1185
+ []
1186
+ ],
1187
+ [
1188
+ "app",
1189
+ { sourceInterval: [1908, 1910] },
1190
+ "or",
1191
+ []
1192
+ ],
1193
+ [
1194
+ "app",
1195
+ { sourceInterval: [1913, 1918] },
1196
+ "limit",
1197
+ []
1198
+ ]
1199
+ ]
1200
+ ],
1201
+ identPart: [
1202
+ "define",
1203
+ { sourceInterval: [1921, 1944] },
1204
+ null,
1205
+ [],
1206
+ [
1207
+ "alt",
1208
+ { sourceInterval: [1933, 1944] },
1209
+ [
1210
+ "app",
1211
+ { sourceInterval: [1933, 1938] },
1212
+ "alnum",
1213
+ []
1214
+ ],
1215
+ [
1216
+ "terminal",
1217
+ { sourceInterval: [1941, 1944] },
1218
+ "_"
1219
+ ]
1220
+ ]
1221
+ ],
1222
+ space: [
1223
+ "extend",
1224
+ { sourceInterval: [1984, 2004] },
1225
+ null,
1226
+ [],
1227
+ [
1228
+ "app",
1229
+ { sourceInterval: [1993, 2004] },
1230
+ "lineComment",
1231
+ []
1232
+ ]
1233
+ ],
1234
+ lineComment: [
1235
+ "define",
1236
+ { sourceInterval: [2007, 2038] },
1237
+ null,
1238
+ [],
1239
+ [
1240
+ "seq",
1241
+ { sourceInterval: [2021, 2038] },
1242
+ [
1243
+ "terminal",
1244
+ { sourceInterval: [2021, 2025] },
1245
+ "--"
1246
+ ],
1247
+ [
1248
+ "star",
1249
+ { sourceInterval: [2026, 2038] },
1250
+ [
1251
+ "seq",
1252
+ { sourceInterval: [2027, 2036] },
1253
+ [
1254
+ "not",
1255
+ { sourceInterval: [2027, 2032] },
1256
+ [
1257
+ "terminal",
1258
+ { sourceInterval: [2028, 2032] },
1259
+ "\n"
1260
+ ]
1261
+ ],
1262
+ [
1263
+ "app",
1264
+ { sourceInterval: [2033, 2036] },
1265
+ "any",
1266
+ []
1267
+ ]
1268
+ ]
1269
+ ]
1270
+ ]
1271
+ ]
1272
+ }
1273
+ ]);
1274
+ })))(), 1);
1275
+ const semantics = import_sql_ohm_bundle.default.createSemantics();
1276
+ semantics.addOperation("toAST()", {
1277
+ Statement(select, _semi) {
1278
+ return select.toAST();
1279
+ },
1280
+ SelectStatement(_select, columns, _from, tableRef, whereClause, limitClause) {
1281
+ const cols = columns.toAST();
1282
+ const from = tableRef.toAST();
1283
+ const whereIter = whereClause.children;
1284
+ const where = whereIter.length > 0 ? whereIter[0].toAST() : null;
1285
+ const limitIter = limitClause.children;
1286
+ return {
1287
+ type: "select",
1288
+ columns: cols,
1289
+ from,
1290
+ where,
1291
+ limit: limitIter.length > 0 ? limitIter[0].toAST() : null
1292
+ };
1293
+ },
1294
+ ColumnList(list) {
1295
+ return list.asIteration().children.map((c) => c.toAST());
1296
+ },
1297
+ ColumnEntry_aliased(expr, _as, alias) {
1298
+ return {
1299
+ ...expr.toAST(),
1300
+ alias: alias.sourceString
1301
+ };
1302
+ },
1303
+ ColumnEntry_plain(expr) {
1304
+ return expr.toAST();
1305
+ },
1306
+ ColumnExpr_qualifiedWild(qw) {
1307
+ return qw.toAST();
1308
+ },
1309
+ ColumnExpr_wild(w) {
1310
+ return w.toAST();
1311
+ },
1312
+ ColumnExpr_qualified(qn) {
1313
+ return qn.toAST();
1314
+ },
1315
+ ColumnExpr_simple(ident) {
1316
+ return {
1317
+ type: "simple",
1318
+ name: ident.sourceString
1319
+ };
1320
+ },
1321
+ qualifiedWildcard(table, _dot, _star) {
1322
+ return {
1323
+ type: "qualified_wildcard",
1324
+ table: table.sourceString
1325
+ };
1326
+ },
1327
+ qualifiedName(table, _dot, col) {
1328
+ return {
1329
+ type: "qualified",
1330
+ table: table.sourceString,
1331
+ name: col.sourceString
1332
+ };
1333
+ },
1334
+ wildcard(_star) {
1335
+ return { type: "wildcard" };
1336
+ },
1337
+ TableRef_aliased(tableName, _as, alias) {
1338
+ return {
1339
+ type: "table",
1340
+ ...tableName.toAST(),
1341
+ alias: alias.sourceString
1342
+ };
1343
+ },
1344
+ TableRef_implicitAlias(tableName, alias) {
1345
+ return {
1346
+ type: "table",
1347
+ ...tableName.toAST(),
1348
+ alias: alias.sourceString
1349
+ };
1350
+ },
1351
+ TableRef_plain(tableName) {
1352
+ return {
1353
+ type: "table",
1354
+ ...tableName.toAST()
1355
+ };
1356
+ },
1357
+ TableName_qualified(schema, _dot, name) {
1358
+ return {
1359
+ schema: schema.sourceString,
1360
+ name: name.sourceString
1361
+ };
1362
+ },
1363
+ TableName_simple(name) {
1364
+ return { name: name.sourceString };
1365
+ },
1366
+ WhereClause(_where, expr) {
1367
+ return expr.toAST();
1368
+ },
1369
+ WhereExpr(orExpr) {
1370
+ return orExpr.toAST();
1371
+ },
1372
+ WhereOrExpr_or(left, _or, right) {
1373
+ return {
1374
+ type: "or",
1375
+ left: left.toAST(),
1376
+ right: right.toAST()
1377
+ };
1378
+ },
1379
+ WhereAndExpr_and(left, _and, right) {
1380
+ return {
1381
+ type: "and",
1382
+ left: left.toAST(),
1383
+ right: right.toAST()
1384
+ };
1385
+ },
1386
+ WherePrimary_paren(_open, expr, _close) {
1387
+ return expr.toAST();
1388
+ },
1389
+ WhereComparison(colRef, _eq, value) {
1390
+ return {
1391
+ type: "comparison",
1392
+ operator: "=",
1393
+ column: colRef.toAST(),
1394
+ value: value.toAST()
1395
+ };
1396
+ },
1397
+ ColumnRef_qualified(table, _dot, name) {
1398
+ return {
1399
+ type: "column_ref",
1400
+ table: table.sourceString,
1401
+ name: name.sourceString
1402
+ };
1403
+ },
1404
+ ColumnRef_simple(name) {
1405
+ return {
1406
+ type: "column_ref",
1407
+ name: name.sourceString
1408
+ };
1409
+ },
1410
+ stringLiteral(_open, chars, _close) {
1411
+ return chars.sourceString;
1412
+ },
1413
+ LimitClause(_limit, integer) {
1414
+ return integer.toAST();
1415
+ },
1416
+ integer(_digits) {
1417
+ return parseInt(this.sourceString, 10);
1418
+ }
1419
+ });
1420
+ function parseSql(expr) {
1421
+ const matchResult = import_sql_ohm_bundle.default.match(expr);
1422
+ if (matchResult.failed()) throw new Error(matchResult.message);
1423
+ return semantics(matchResult).toAST();
1424
+ }
1425
+ //#endregion
1426
+ //#region src/sanitise.ts
1427
+ function sanitiseSql({ ast, schema, table, col, value }) {
1428
+ const newClause = {
1429
+ type: "comparison",
1430
+ operator: "=",
1431
+ column: {
1432
+ type: "column_ref",
1433
+ table: `${schema}.${table}`,
1434
+ name: col
1435
+ },
1436
+ value
1437
+ };
1438
+ return {
1439
+ ...ast,
1440
+ where: ast.where ? {
1441
+ type: "and",
1442
+ left: newClause,
1443
+ right: ast.where
1444
+ } : newClause
1445
+ };
1446
+ }
1447
+ //#endregion
1448
+ //#region src/index.ts
1449
+ function sqyrl(expr, { schema, table, column, value }) {
1450
+ return outputSql(sanitiseSql({
1451
+ ast: parseSql(expr),
1452
+ schema,
1453
+ table,
1454
+ col: column,
1455
+ value
1456
+ }));
1457
+ }
1458
+ //#endregion
1459
+ export { outputSql, parseSql, sanitiseSql, sqyrl };