forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrino.pegjs
4837 lines (4453 loc) · 129 KB
/
trino.pegjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
const reservedMap = {
'ALTER': true,
'ALL': true,
'ADD': true,
'AND': true,
'AS': true,
'ASC': true,
'BETWEEN': true,
'BY': true,
'CALL': true,
'CASE': true,
'CREATE': true,
'CONTAINS': true,
'CURRENT_DATE': true,
'CURRENT_TIME': true,
'CURRENT_TIMESTAMP': true,
'CURRENT_USER': true,
'DELETE': true,
'DESC': true,
'DISTINCT': true,
'DROP': true,
'ELSE': true,
'END': true,
'EXISTS': true,
'EXPLAIN': true,
'FALSE': true,
'FROM': true,
'FULL': true,
'GROUP': true,
'HAVING': true,
'IN': true,
'INNER': true,
'INSERT': true,
'INTO': true,
'IS': true,
'JOIN': true,
'JSON': true,
// 'KEY': true,
'LEFT': true,
'LIKE': true,
'LIMIT': true,
'NOT': true,
'NULL': true,
'NULLS': true,
'OFFSET': true,
'ON': true,
'OR': true,
'ORDER': true,
'OUTER': true,
'RECURSIVE': true,
'RENAME': true,
// 'REPLACE': true,
'RIGHT': true,
'ROWS': true,
'SELECT': true,
'SESSION_USER': true,
'SET': true,
'SHOW': true,
'SYSTEM_USER': true,
'TABLE': true,
'THEN': true,
'TRUE': true,
'TRUNCATE': true,
'UNION': true,
'UPDATE': true,
'USING': true,
// 'VALUES': true,
'WITH': true,
'WHEN': true,
'WHERE': true,
'WINDOW': true,
'GLOBAL': true,
'SESSION': true,
'LOCAL': true,
'PERSIST': true,
'PERSIST_ONLY': true,
};
const reservedFunctionName = {
avg: true,
sum: true,
count: true,
max: true,
min: true,
group_concat: true,
std: true,
variance: true,
current_date: true,
current_time: true,
current_timestamp: true,
current_user: true,
user: true,
session_user: true,
system_user: true
}
function getLocationObject() {
return options.includeLocations ? {loc: location()} : {}
}
function createUnaryExpr(op, e) {
return {
type: 'unary_expr',
operator: op,
expr: e
};
}
function createBinaryExpr(op, left, right) {
return {
type: 'binary_expr',
operator: op,
left: left,
right: right,
...getLocationObject()
};
}
function isBigInt(numberStr) {
const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER)
const num = BigInt(numberStr)
if (num < previousMaxSafe) return false
return true
}
function createList(head, tail, po = 3) {
const result = [head];
for (let i = 0; i < tail.length; i++) {
delete tail[i][po].tableList
delete tail[i][po].columnList
result.push(tail[i][po]);
}
return result;
}
function createBinaryExprChain(head, tail) {
let result = head;
for (let i = 0; i < tail.length; i++) {
result = createBinaryExpr(tail[i][1], result, tail[i][3]);
}
return result;
}
function queryTableAlias(tableName) {
const alias = tableAlias[tableName]
if (alias) return alias
if (tableName) return tableName
return null
}
function columnListTableAlias(columnList) {
const newColumnsList = new Set()
const symbolChar = '::'
for(let column of columnList.keys()) {
const columnInfo = column.split(symbolChar)
if (!columnInfo) {
newColumnsList.add(column)
break
}
if (columnInfo && columnInfo[1]) columnInfo[1] = queryTableAlias(columnInfo[1])
newColumnsList.add(columnInfo.join(symbolChar))
}
return Array.from(newColumnsList)
}
function refreshColumnList(columnList) {
const columns = columnListTableAlias(columnList)
columnList.clear()
columns.forEach(col => columnList.add(col))
}
function commonStrToLiteral(strOrLiteral) {
return typeof strOrLiteral === 'string' ? { type: 'same', value: strOrLiteral } : strOrLiteral
}
function checkLambdaExprType(expr) {
const type = expr.type || (expr.ast && expr.ast.type)
if (type === 'aggr_func') throw new Error('Aggregations are not supported in lambda expressions')
if (type === 'select') throw new Error('Subqueries are not supported in lambda expressions')
if (type === 'binary_expr') {
checkLambdaExprType(expr.left)
checkLambdaExprType(expr.right)
}
return true
}
const cmpPrefixMap = {
'+': true,
'-': true,
'*': true,
'/': true,
'>': true,
'<': true,
'!': true,
'=': true,
//between
'B': true,
'b': true,
//for is or in
'I': true,
'i': true,
//for like
'L': true,
'l': true,
//for not
'N': true,
'n': true
};
// used for dependency analysis
let varList = [];
const tableList = new Set();
const columnList = new Set();
const tableAlias = {};
}
start
= __ n:multiple_stmt {
return n
}
cmd_stmt
= drop_stmt
/ create_stmt
/ truncate_stmt
/ rename_stmt
/ call_stmt
/ use_stmt
/ alter_stmt
/ set_stmt
/ lock_stmt
/ show_stmt
/ deallocate_stmt
create_stmt
= create_table_stmt
/ create_constraint_trigger
/ create_extension_stmt
/ create_index_stmt
/ create_sequence
/ create_db_stmt
/ create_domain_stmt
/ create_type_stmt
/ create_view_stmt
alter_stmt
= alter_table_stmt
/ alter_schema_stmt
/ alter_domain_type_stmt
/ alter_function_stmt
/ alter_aggregate_stmt
crud_stmt
= union_stmt
/ update_stmt
/ replace_insert_stmt
/ insert_no_columns_stmt
/ delete_stmt
/ cmd_stmt
/ proc_stmts
multiple_stmt
= head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* {
/*
// is in reality: { tableList: any[]; columnList: any[]; ast: T; }
export type AstStatement<T> = T;
=> AstStatement<crud_stmt[]> */
const headAst = head && head.ast || head
const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst;
for (let i = 0; i < tail.length; i++) {
if(!tail[i][3] || tail[i][3].length === 0) continue;
cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]);
}
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: cur
}
}
set_op
= KW_UNION __ a:KW_ALL? {
// => 'union' | 'union all'
return a ? 'union all' : 'union'
}
union_stmt
= head:select_stmt tail:(__ set_op __ select_stmt)* __ ob: order_by_clause? __ l:limit_clause? {
/* export interface union_stmt_node extends select_stmt_node {
_next: union_stmt_node;
set_op: 'union' | 'union all';
}
=> AstStatement<union_stmt_node>
*/
let cur = head
for (let i = 0; i < tail.length; i++) {
cur._next = tail[i][3]
cur.set_op = tail[i][1]
cur = cur._next
}
if(ob) head._orderby = ob
if(l && l.value && l.value.length > 0) head._limit = l
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: head
}
}
if_not_exists_stmt
= 'IF'i __ KW_NOT __ KW_EXISTS {
// => 'IF NOT EXISTS'
return 'IF NOT EXISTS'
}
create_extension_stmt
= a:KW_CREATE __
e:'EXTENSION'i __
ife: if_not_exists_stmt? __
n:(ident_name / literal_string) __
w:KW_WITH? __
s:('SCHEMA'i __ ident_name / literal_string)? __
v:('VERSION'i __ (ident_name / literal_string))? __
f:(KW_FROM __ (ident_name / literal_string))? {
/*
export type nameOrLiteral = literal_string | { type: 'same', value: string; };
=> {
type: 'create';
keyword: 'extension';
if_not_exists?: 'if not exists';
extension: nameOrLiteral;
with: 'with';
schema: nameOrLiteral;
version: nameOrLiteral;
from: nameOrLiteral;
}
*/
return {
type: 'create',
keyword: e.toLowerCase(),
if_not_exists:ife,
extension: commonStrToLiteral(n),
with: w && w[0].toLowerCase(),
schema: commonStrToLiteral(s && s[2].toLowerCase()), // <== wont that be a bug ?
version: commonStrToLiteral(v && v[2]),
from: commonStrToLiteral(f && f[2]),
}
}
create_db_definition
= head:create_option_character_set tail:(__ create_option_character_set)* {
// => create_option_character_set[]
return createList(head, tail, 1)
}
create_db_stmt
= a:KW_CREATE __
k:(KW_DATABASE / KW_SCHEMA) __
ife:if_not_exists_stmt? __
t:proc_func_name __
c:create_db_definition? {
const keyword = k.toLowerCase()
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
keyword,
if_not_exists:ife,
[keyword]: { db: t.schema, schema: t.name },
create_definitions: c,
}
}
}
view_with
= KW_WITH __ c:("CASCADED"i / "LOCAL"i) __ "CHECK"i __ "OPTION" {
// => string
return `with ${c.toLowerCase()} check option`
}
/ KW_WITH __ "CHECK"i __ "OPTION" {
// => string
return 'with check option'
}
with_view_option
= 'check_option'i __ KW_ASSIGIN_EQUAL __ t:("CASCADED"i / "LOCAL"i) {
// => {type: string; value: string; symbol: string; }
return { type: 'check_option', value: t, symbol: '=' }
}
/ k:('security_barrier'i / 'security_invoker'i) __ KW_ASSIGIN_EQUAL __ t:literal_bool {
// => {type: string; value: string; symbol: string; }
return { type: k.toLowerCase(), value: t.value ? 'true' : 'false', symbol: '=' }
}
with_view_options
= head:with_view_option tail:(__ COMMA __ with_view_option)* {
// => with_view_option[]
return createList(head, tail);
}
create_view_stmt
= a:KW_CREATE __ or:(KW_OR __ KW_REPLACE)? __ tp:(KW_TEMP / KW_TEMPORARY)? __ r:KW_RECURSIVE? __
KW_VIEW __ v:table_name __ c:(LPAREN __ column_list __ RPAREN)? __ wo:(KW_WITH __ LPAREN __ with_view_options __ RPAREN)? __
KW_AS __ s:select_stmt_nake __ w:view_with? {
/*
export type create_view_stmt = {
type: 'create',
keyword: 'view',
replace?: 'or replace',
temporary?: 'temporary' | 'temp',
recursive?: 'recursive',
view: table_name,
columns?: column_list,
select: select_stmt_nake,
with_options?: with_options,
with?: string,
}
=> AstStatement<create_view_stmt>
*/
v.view = v.table
delete v.table
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
keyword: 'view',
replace: or && 'or replace',
temporary: tp && tp[0].toLowerCase(),
recursive: r && r.toLowerCase(),
columns: c && c[2],
select: s,
view: v,
with_options: wo && wo[4],
with: w,
}
}
}
create_type_stmt
= a:KW_CREATE __ k:'TYPE'i __ s:table_name __ as:KW_AS __ r:KW_ENUM __ LPAREN __ e:expr_list? __ RPAREN {
/*
export type create_type_stmt = {
type: 'create',
keyword: 'type',
name: { schema: string; name: string },
as?: string,
resource?: string,
create_definitions?: any
}
=> AstStatement<create_type_stmt>
*/
e.parentheses = true
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
keyword: k.toLowerCase(),
name: { schema: s.db, name: s.table },
as: as && as[0] && as[0].toLowerCase(),
resource: r.toLowerCase(),
create_definitions: e,
},
...getLocationObject(),
}
}
/ a:KW_CREATE __ k:'TYPE'i __ s:table_name {
// => AstStatement<create_type_stmt>
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
keyword: k.toLowerCase(),
name: { schema: s.db, name: s.table },
}
}
}
create_domain_stmt
= a:KW_CREATE __ k:'DOMAIN'i __ s:table_name __ as:KW_AS? __ d:data_type __ ce:collate_expr? __ de:default_expr? __ ccc: create_constraint_check? {
/*
export type create_domain_stmt = {
type: 'create',
keyword: 'domain',
domain: { schema: string; name: string },
as?: string,
target: data_type,
create_definitions?: any[]
}
=> AstStatement<create_domain_stmt>
*/
if (ccc) ccc.type = 'constraint'
const definitions = [ce, de, ccc].filter(v => v)
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
keyword: k.toLowerCase(),
domain: { schema: s.db, name: s.table },
as: as && as[0] && as[0].toLowerCase(),
target: d,
create_definitions: definitions,
},
...getLocationObject(),
}
}
create_table_stmt
= a:KW_CREATE __
tp:KW_TEMPORARY? __
KW_TABLE __
ife:if_not_exists_stmt? __
t:table_ref_list __
c:create_table_definition __
to:table_options? __
ir: (KW_IGNORE / KW_REPLACE)? __
as: KW_AS? __
qe: union_stmt? {
/*
export type create_table_stmt_node = create_table_stmt_node_simple | create_table_stmt_node_like;
export interface create_table_stmt_node_base {
type: 'create';
keyword: 'table';
temporary?: 'temporary';
if_not_exists?: 'if not exists';
table: table_ref_list;
}
export interface create_table_stmt_node_simple extends create_table_stmt_node_base{
ignore_replace?: 'ignore' | 'replace';
as?: 'as';
query_expr?: union_stmt_node;
create_definitions?: create_table_definition;
table_options?: table_options;
}
=> AstStatement<create_table_stmt_node>
*/
if(t) t.forEach(tt => tableList.add(`create::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
keyword: 'table',
temporary: tp && tp[0].toLowerCase(),
if_not_exists:ife,
table: t,
ignore_replace: ir && ir[0].toLowerCase(),
as: as && as[0].toLowerCase(),
query_expr: qe && qe.ast,
create_definitions: c,
table_options: to
},
...getLocationObject(),
}
}
/ a:KW_CREATE __
tp:KW_TEMPORARY? __
KW_TABLE __
ife:if_not_exists_stmt? __
t:table_ref_list __
lt:create_like_table {
/*
export interface create_table_stmt_node_like extends create_table_stmt_node_base{
like: create_like_table;
}
=> AstStatement<create_table_stmt_node>;
*/
if(t) t.forEach(tt => tableList.add(`create::${[tt.db, tt.schema].filter(Boolean).join('.') || null}::${tt.table}`));
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
keyword: 'table',
temporary: tp && tp[0].toLowerCase(),
if_not_exists:ife,
table: t,
like: lt
}
}
}
create_sequence
= a:KW_CREATE __
tp:(KW_TEMPORARY / KW_TEMP)? __
KW_SEQUENCE __
ife:if_not_exists_stmt? __
t:table_name __ as:(KW_AS __ alias_ident)?__
c:create_sequence_definition_list? {
/*
export type create_sequence_stmt = {
type: 'create',
keyword: 'sequence',
temporary?: 'temporary' | 'temp',
if_not_exists?: 'if not exists',
table: table_ref_list,
create_definitions?: create_sequence_definition_list
}
=> AstStatement<create_sequence_stmt>
*/
t.as = as && as[2]
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
keyword: 'sequence',
temporary: tp && tp[0].toLowerCase(),
if_not_exists:ife,
sequence: [t],
create_definitions: c,
}
}
}
sequence_definition_increment
= k:'INCREMENT'i __ b:KW_BY? __ n:literal_numeric {
/*
export type sequence_definition = { "resource": "sequence", prefix?: string,value: literal | column_ref }
=> sequence_definition
*/
return {
resource: 'sequence',
prefix: b ? `${k.toLowerCase()} by` : k.toLowerCase(),
value: n
}
}
sequence_definition_minval
= k:'MINVALUE'i __ n:literal_numeric {
// => sequence_definition
return {
resource: 'sequence',
prefix: k.toLowerCase(),
value: n
}
}
/ 'NO'i __ 'MINVALUE'i {
// => sequence_definition
return {
resource: 'sequence',
value: {
type: 'origin',
value: 'no minvalue'
}
}
}
sequence_definition_maxval
= k:'MAXVALUE'i __ n:literal_numeric {
// => sequence_definition
return {
resource: 'sequence',
prefix: k.toLowerCase(),
value: n
}
}
/ 'NO'i __ 'MAXVALUE'i {
// => sequence_definition
return {
resource: 'sequence',
value: {
type: 'origin',
value: 'no maxvalue'
}
}
}
sequence_definition_start
= k:'START'i __ w:KW_WITH? __ n:literal_numeric {
// => sequence_definition
return {
resource: 'sequence',
prefix: w ? `${k.toLowerCase()} with` : k.toLowerCase(),
value: n
}
}
sequence_definition_cache
= k:'CACHE'i __ n:literal_numeric {
// => sequence_definition
return {
resource: 'sequence',
prefix: k.toLowerCase(),
value: n
}
}
sequence_definition_cycle
= n:'NO'i? __ 'CYCLE'i {
// => sequence_definition
return {
resource: 'sequence',
value: {
type: 'origin',
value: n ? 'no cycle' : 'cycle'
}
}
}
sequence_definition_owned
= 'OWNED'i __ KW_BY __ 'NONE'i {
// => sequence_definition
return {
resource: 'sequence',
prefix: 'owned by',
value: {
type: 'origin',
value: 'none'
}
}
}
/ n:'OWNED'i __ KW_BY __ col:column_ref {
// => sequence_definition
return {
resource: 'sequence',
prefix: 'owned by',
value: col
}
}
create_sequence_definition
= sequence_definition_increment
/ sequence_definition_minval
/ sequence_definition_maxval
/ sequence_definition_start
/ sequence_definition_cache
/ sequence_definition_cycle
/ sequence_definition_owned
create_sequence_definition_list
= head: create_sequence_definition tail:(__ create_sequence_definition)* {
// => create_sequence_definition[]
return createList(head, tail, 1)
}
create_index_stmt
= a:KW_CREATE __
kw:KW_UNIQUE? __
t:KW_INDEX __
co:KW_CONCURRENTLY? __
n:ident? __
on:KW_ON __
ta:table_name __
um:index_type? __
LPAREN __ cols:column_order_list __ RPAREN __
wr:(KW_WITH __ LPAREN __ index_options_list __ RPAREN)? __
ts:(KW_TABLESPACE __ ident_name)? __
w:where_clause? __ {
/*
export interface create_index_stmt_node {
type: 'create';
index_type?: 'unique';
keyword: 'index';
concurrently?: 'concurrently';
index: string;
on_kw: string;
table: table_name;
index_using?: index_type;
index_columns: column_order[];
with?: index_option[];
with_before_where: true;
tablespace?: {type: 'origin'; value: string; }
where?: where_clause;
}
=> AstStatement<create_index_stmt_node>
*/
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: a[0].toLowerCase(),
index_type: kw && kw.toLowerCase(),
keyword: t.toLowerCase(),
concurrently: co && co.toLowerCase(),
index: n,
on_kw: on[0].toLowerCase(),
table: ta,
index_using: um,
index_columns: cols,
with: wr && wr[4],
with_before_where: true,
tablespace: ts && { type: 'origin', value: ts[2] },
where: w,
}
}
}
column_order_list
= head:column_order tail:(__ COMMA __ column_order)* {
// => column_order[]
return createList(head, tail)
}
column_order
= c:expr __
ca:collate_expr? __
op:ident? __
o:(KW_ASC / KW_DESC)? __
nf:('NULLS'i __ ('FIRST'i / 'LAST'i))? {
/*
=> {
collate: collate_expr;
opclass: ident;
order: 'asc' | 'desc';
nulls: 'nulls last' | 'nulls first';
}
*/
return {
...c,
collate: ca,
opclass: op,
order_by: o && o.toLowerCase(),
nulls: nf && `${nf[0].toLowerCase()} ${nf[2].toLowerCase()}`,
}
}
create_like_table_simple
= KW_LIKE __ t: table_ref_list {
// => { type: 'like'; table: table_ref_list; }
return {
type: 'like',
table: t
}
}
create_like_table
= create_like_table_simple
/ LPAREN __ e:create_like_table __ RPAREN {
// => create_like_table_simple & { parentheses?: boolean; }
e.parentheses = true;
return e;
}
create_table_definition
= LPAREN __ head:create_definition tail:(__ COMMA __ create_definition)* __ RPAREN {
// => create_definition[]
return createList(head, tail);
}
create_definition
= create_column_definition
/ create_index_definition
/ create_fulltext_spatial_index_definition
/ create_constraint_definition
column_definition_opt
= column_constraint
/ a:('AUTO_INCREMENT'i) {
// => { auto_increment: 'auto_increment'; }
return { auto_increment: a.toLowerCase() }
}
/ 'UNIQUE'i __ k:('KEY'i)? {
// => { unique: 'unique' | 'unique key'; }
const sql = ['unique']
if (k) sql.push(k)
return { unique: sql.join(' ').toLowerCase('') }
}
/ p:('PRIMARY'i)? __ 'KEY'i {
// => { unique: 'key' | 'primary key'; }
const sql = []
if (p) sql.push('primary')
sql.push('key')
return { primary_key: sql.join(' ').toLowerCase('') }
}
/ co:keyword_comment {
// => { comment: keyword_comment; }
return { comment: co }
}
/ ca:collate_expr {
// => { collate: collate_expr; }
return { collate: ca }
}
/ cf:column_format {
// => { column_format: column_format; }
return { column_format: cf }
}
/ s:storage {
// => { storage: storage }
return { storage: s }
}
/ re:reference_definition {
// => { reference_definition: reference_definition; }
return { reference_definition: re }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
// => { character_set: collate_expr }
return { character_set: { type: t, value: v, symbol: s }}
}
column_definition_opt_list
= head:column_definition_opt __ tail:(__ column_definition_opt)* {
/*
=> {
nullable?: column_constraint['nullable'];
default_val?: column_constraint['default_val'];
auto_increment?: 'auto_increment';
unique?: 'unique' | 'unique key';
primary?: 'key' | 'primary key';
comment?: keyword_comment;
collate?: collate_expr;
column_format?: column_format;
storage?: storage;
reference_definition?: reference_definition;
}
*/
let opt = head
for (let i = 0; i < tail.length; i++) {
opt = { ...opt, ...tail[i][1] }
}
return opt
}
create_column_definition
= c:column_ref __
d:data_type __
cdo:column_definition_opt_list? {
/*
=> {
column: column_ref;
definition: data_type;
nullable: column_constraint['nullable'];
default_val: column_constraint['default_val'];
auto_increment?: 'auto_increment';
unique?: 'unique' | 'unique key';
primary?: 'key' | 'primary key';
comment?: keyword_comment;
collate?: collate_expr;
column_format?: column_format;
storage?: storage;
reference_definition?: reference_definition;
resource: 'column';
}
*/
columnList.add(`create::${c.table}::${c.column}`)
return {
column: c,
definition: d,
resource: 'column',
...(cdo || {})
}
}
column_constraint
= n:(literal_not_null / literal_null) __ df:default_expr? {
// => { nullable: literal_null | literal_not_null; default_val: default_expr; }
if (n && !n.value) n.value = 'null'
return {
default_val: df,
nullable: n
}
}
/ df:default_expr __ n:(literal_not_null / literal_null)? {
// => { nullable: literal_null | literal_not_null; default_val: default_expr; }
if (n && !n.value) n.value = 'null'
return {
default_val: df,
nullable: n
}
}
collate_expr
= KW_COLLATE __ ca:ident_name __ s:KW_ASSIGIN_EQUAL __ t:ident {
return {
type: 'collate',
keyword: 'collate',
collate: {
name: ca,
symbol: s,
value: t
}
}
}
/ KW_COLLATE __ s:KW_ASSIGIN_EQUAL? __ ca:ident {
return {
type: 'collate',
keyword: 'collate',
collate: {
name: ca,