-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql.pegjs
151 lines (117 loc) · 2.97 KB
/
sql.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
start = tables:(w create_table w ";" w)* {
return tables.map(t => t[1]);
}
create_table "CREATE TABLE" =
CREATE w1 TABLE w1 name:identifier lparenw modifiers:table_modifier_seq rparenw {
return {
name,
columns: modifiers
.filter(c => c.__type == "column_definition"),
constraints: modifiers
.filter(c => c.__type == "table_constraint")
}
}
column_type "column type" =
VARCHAR lparenw size:integer rparenw {
return "VARCHAR(" + size + ")"
} /
DECIMAL lparenw a:integer comma b:integer rparenw {
return "DECIMAL(" + a + "," + b + ")"
} /
TEXT /
INT /
DATETIME /
DATE
column_modifier "column modifier" =
NOT w1 NULL {
return "NOT_NULL"
} /
AUTO w1 INCREMENT {
return "AUTO_INCREMENT"
}
identifier_seq = head:identifier tail:(comma i:identifier w { return i })* {
return [head, ...tail]
}
table_constraint "table constraint" =
PRIMARY w1 KEY lparenw columns:identifier_seq rparenw {
return {
__type: "table_constraint",
type: "PRIMARY_KEY",
columns: columns
}
} /
FOREIGN w1 KEY lparenw selfColumn:identifier w rparen w1 REFERENCES w1 foreignTable:identifier lparenw foreignColumn:identifier rparenw mandatory:(w1 MANDATORY)? {
return {
__type: "table_constraint",
type: "FOREIGN_KEY",
selfColumn,
foreignTable,
foreignColumn,
foreignMandatory: mandatory !== null,
}
}
column_definition "column definition" =
name:identifier w1 type:column_type modifiers:(w column_modifier w)* {
return {
__type: "column_definition",
name,
type,
modifiers: modifiers.map(m => m[1])
}
}
table_modifier = column_definition / table_constraint
table_modifier_seq = head:table_modifier tail:(comma i:table_modifier { return i })* comma? {
return [head, ...tail]
}
identifier "identifier" =
[A-Za-z0-9_]+ { return text(); }
integer "integer" =
[0-9]+ { return text(); }
w1 "whitespace" =
[ \t\n\r]+
w "whitespace" =
[ \t\n\r]*
CREATE "create" =
"CREATE" / "create"
TABLE "table" =
"TABLE" / "table"
VARCHAR "varchar" =
"VARCHAR" / "varchar"
DECIMAL "decimal" =
"DECIMAL" / "decimal"
TEXT "text" =
"TEXT" / "text"
INT "int" =
"INT" / "int"
DATETIME "datetime" =
"DATETIME" / "datetime"
DATE "date" =
"DATE" / "date"
NOT "not" =
"NOT" / "not"
NULL "null" =
"NULL" / "null"
AUTO "auto" =
"AUTO" / "auto"
INCREMENT "increment" =
"INCREMENT" / "increment"
PRIMARY "primary" =
"PRIMARY" / "primary"
FOREIGN "foreign" =
"FOREIGN" / "foreign"
KEY "key" =
"KEY" / "key"
REFERENCES "references" =
"REFERENCES" / "references"
MANDATORY "mandatory" =
"MANDATORY" / "mandatory"
lparen "left parentheses" =
"("
lparenw "left parentheses" =
w lparen w
rparen "right parentheses" =
")"
rparenw "right parentheses" =
w rparen w
comma "comma" =
w "," w