-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicanhazsql.p6
211 lines (176 loc) · 3.86 KB
/
icanhazsql.p6
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
grammar LolSql {
rule TOP { ^ <statement> $ {*} }
rule statement {
'HAI!'
[
| <select>
| <update>
]
KTHNXBYE
{*}
}
rule select {
<from>
<columns>
<where>?
{*}
}
rule update {
<from>
<set>
<where>?
{*}
}
rule from {
I\'M IN UR \`<ident>\` {*}
}
rule where {
I CAN HAZ \`$<column>=<ident>\`
$<comp>=[ LIEK | '=' | '<' | '<=' | '>' | '>=' ]
<value>
{*}
}
rule set {
PLZ MAKES
$<sets>=[ \`<ident>\` LIEKS <value> ]**','
{*}
}
rule columns {
SELECTIN UR <column>**',' {*}
}
rule column {
\`$<name>=<ident>\` [AZ \`$<as>=<ident>\`]? {*}
}
rule value { <str_value> | <number> }
rule str_value { \' [ '\\\'' | <-[']>+ ]* \' }
rule number { \d+[\.\d+]? }
}
class Column {
has $.name;
has $.as;
has $.new_value;
}
class Where {
has $.column;
has $.comparative;
has $.value;
method to_sql() {
return ($.column, $.comparative, $.value).join(' ');
}
}
class Select {
has $.table_name;
has Column @.columns;
has Where $.where;
method to_sql {
return [~] gather {
take 'SELECT ';
take @.columns.map({
.name ~ (.as ?? ' AS ' ~ .as !! '')
}).join(', ');
take ' FROM ' ~ $.table_name;
if $.where {
take ' WHERE ' ~ $.where.to_sql;
}
take "\n";
}
}
}
class Update {
has $.table_name;
has Column @.columns;
has Where $.where;
method to_sql {
return [~] gather {
take 'UPDATE ' ~ $.table_name ~ ' SET ';
take @.columns.map({
.name ~ ' = ' ~ .new_value
}).join(', ');
if $.where {
take ' WHERE ' ~ $.where.to_sql;
}
take "\n";
}
}
}
class LolSqlToTree {
method TOP($/) {
make $<statement>.ast;
}
method statement($/) {
if $<select> {
make $<select>.ast;
}
elsif $<update> {
make $<update>.ast;
}
}
method select($/) {
my %optionals;
if $<where> {
%optionals<where> = $<where>[0].ast;
}
make Select.new(
table_name => $<from>.ast,
columns => $<columns>.ast,
|%optionals
);
}
method update($/) {
my %optionals;
if $<where> {
%optionals<where> = $<where>[0].ast;
}
make Update.new(
table_name => $<from>.ast,
columns => $<set>.ast,
|%optionals
);
}
method from($/) {
make ~$<ident>;
}
method columns($/) {
my Column @columns;
for $<column>.list -> $col {
push @columns, $col.ast;
}
make @columns;
}
method column($/) {
make Column.new(
name => ~$<name>,
as => $<as>?? ~$<as> !! undef
);
}
method set($/) {
my Column @columns;
for $<set>.list -> $set {
push @columns, Column.new(
name => $<ident>.trim,
new_value => $<value>.trim
);
}
make @columns;
}
method where($/) {
my $comp = $<comp>.trim;
if $comp eq 'LIEK' { $comp = 'LIKE' }
make Where.new(
column => $<column>.trim,
comparative => $comp,
value => $<value>.trim
);
}
}
loop {
my $expr = $*IN.get;
my $match = LolSql.parse($expr,
:action(LolSqlToTree.new));
if $match {
my $tree = $match.ast;
say $tree.to_sql;
} else {
say "PARSE FAIL! UR DOIN IT RONG";
}
}