sql-parser 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sql-parser.rb +13 -0
- data/lib/sql-parser/parser.racc +315 -0
- data/lib/sql-parser/parser.racc.rb +1424 -0
- data/lib/sql-parser/parser.rex +100 -0
- data/lib/sql-parser/parser.rex.rb +273 -0
- data/lib/sql-parser/sql_visitor.rb +351 -0
- data/lib/sql-parser/statement.rb +492 -0
- data/lib/sql-parser/version.rb +5 -0
- metadata +103 -0
@@ -0,0 +1,492 @@
|
|
1
|
+
module SQLParser
|
2
|
+
|
3
|
+
module Statement
|
4
|
+
|
5
|
+
class Node
|
6
|
+
|
7
|
+
def accept(visitor)
|
8
|
+
klass = self.class.ancestors.find do |ancestor|
|
9
|
+
visitor.respond_to?("visit_#{demodulize(ancestor.name)}")
|
10
|
+
end
|
11
|
+
|
12
|
+
if klass
|
13
|
+
visitor.__send__("visit_#{demodulize(klass.name)}", self)
|
14
|
+
else
|
15
|
+
raise "No visitor for #{self.class.name}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_sql
|
20
|
+
SQLVisitor.new.visit(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def demodulize(str)
|
26
|
+
str.split('::')[-1]
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
class DirectSelect < Node
|
32
|
+
|
33
|
+
def initialize(query_expression, order_by)
|
34
|
+
@query_expression = query_expression
|
35
|
+
@order_by = order_by
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_reader :query_expression
|
39
|
+
attr_reader :order_by
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
class OrderBy < Node
|
44
|
+
|
45
|
+
def initialize(sort_specification)
|
46
|
+
@sort_specification = Array(sort_specification)
|
47
|
+
end
|
48
|
+
|
49
|
+
attr_reader :sort_specification
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
class Subquery < Node
|
54
|
+
|
55
|
+
def initialize(query_specification)
|
56
|
+
@query_specification = query_specification
|
57
|
+
end
|
58
|
+
|
59
|
+
attr_reader :query_specification
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
class Select < Node
|
64
|
+
def initialize(list, table_expression = nil)
|
65
|
+
@list = list
|
66
|
+
@table_expression = table_expression
|
67
|
+
end
|
68
|
+
|
69
|
+
attr_reader :list
|
70
|
+
attr_reader :table_expression
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
class SelectList < Node
|
75
|
+
|
76
|
+
def initialize(columns)
|
77
|
+
@columns = Array(columns)
|
78
|
+
end
|
79
|
+
|
80
|
+
attr_reader :columns
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
class Distinct < Node
|
85
|
+
|
86
|
+
def initialize(column)
|
87
|
+
@column = column
|
88
|
+
end
|
89
|
+
|
90
|
+
attr_reader :column
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
class All < Node
|
95
|
+
end
|
96
|
+
|
97
|
+
class TableExpression < Node
|
98
|
+
|
99
|
+
def initialize(from_clause, where_clause = nil, group_by_clause = nil, having_clause = nil)
|
100
|
+
@from_clause = from_clause
|
101
|
+
@where_clause = where_clause
|
102
|
+
@group_by_clause = group_by_clause
|
103
|
+
@having_clause = having_clause
|
104
|
+
end
|
105
|
+
|
106
|
+
attr_reader :from_clause
|
107
|
+
attr_reader :where_clause
|
108
|
+
attr_reader :group_by_clause
|
109
|
+
attr_reader :having_clause
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
class FromClause < Node
|
114
|
+
|
115
|
+
def initialize(tables)
|
116
|
+
@tables = Array(tables)
|
117
|
+
end
|
118
|
+
|
119
|
+
attr_reader :tables
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
class OrderClause < Node
|
124
|
+
|
125
|
+
def initialize(columns)
|
126
|
+
@columns = Array(columns)
|
127
|
+
end
|
128
|
+
|
129
|
+
attr_reader :columns
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
class OrderSpecification < Node
|
134
|
+
|
135
|
+
def initialize(column)
|
136
|
+
@column = column
|
137
|
+
end
|
138
|
+
|
139
|
+
attr_reader :column
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
class Ascending < OrderSpecification
|
144
|
+
end
|
145
|
+
|
146
|
+
class Descending < OrderSpecification
|
147
|
+
end
|
148
|
+
|
149
|
+
class HavingClause < Node
|
150
|
+
|
151
|
+
def initialize(search_condition)
|
152
|
+
@search_condition = search_condition
|
153
|
+
end
|
154
|
+
|
155
|
+
attr_reader :search_condition
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
class GroupByClause < Node
|
160
|
+
|
161
|
+
def initialize(columns)
|
162
|
+
@columns = Array(columns)
|
163
|
+
end
|
164
|
+
|
165
|
+
attr_reader :columns
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
class WhereClause < Node
|
170
|
+
|
171
|
+
def initialize(search_condition)
|
172
|
+
@search_condition = search_condition
|
173
|
+
end
|
174
|
+
|
175
|
+
attr_reader :search_condition
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
class On < Node
|
180
|
+
|
181
|
+
def initialize(search_condition)
|
182
|
+
@search_condition = search_condition
|
183
|
+
end
|
184
|
+
|
185
|
+
attr_reader :search_condition
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
class SearchCondition < Node
|
190
|
+
|
191
|
+
def initialize(left, right)
|
192
|
+
@left = left
|
193
|
+
@right = right
|
194
|
+
end
|
195
|
+
|
196
|
+
attr_reader :left
|
197
|
+
attr_reader :right
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
class Using < Node
|
202
|
+
|
203
|
+
def initialize(columns)
|
204
|
+
@columns = Array(columns)
|
205
|
+
end
|
206
|
+
|
207
|
+
attr_reader :columns
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
class Or < SearchCondition
|
212
|
+
end
|
213
|
+
|
214
|
+
class And < SearchCondition
|
215
|
+
end
|
216
|
+
|
217
|
+
class Exists < Node
|
218
|
+
|
219
|
+
def initialize(table_subquery)
|
220
|
+
@table_subquery = table_subquery
|
221
|
+
end
|
222
|
+
|
223
|
+
attr_reader :table_subquery
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
class ComparisonPredicate < Node
|
228
|
+
|
229
|
+
def initialize(left, right)
|
230
|
+
@left = left
|
231
|
+
@right = right
|
232
|
+
end
|
233
|
+
|
234
|
+
attr_reader :left
|
235
|
+
attr_reader :right
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
class Is < ComparisonPredicate
|
240
|
+
end
|
241
|
+
|
242
|
+
class Like < ComparisonPredicate
|
243
|
+
end
|
244
|
+
|
245
|
+
class In < ComparisonPredicate
|
246
|
+
end
|
247
|
+
|
248
|
+
class InValueList < Node
|
249
|
+
|
250
|
+
def initialize(values)
|
251
|
+
@values = values
|
252
|
+
end
|
253
|
+
|
254
|
+
attr_reader :values
|
255
|
+
|
256
|
+
end
|
257
|
+
|
258
|
+
class Between < Node
|
259
|
+
|
260
|
+
def initialize(left, min, max)
|
261
|
+
@left = left
|
262
|
+
@min = min
|
263
|
+
@max = max
|
264
|
+
end
|
265
|
+
|
266
|
+
attr_reader :left
|
267
|
+
attr_reader :min
|
268
|
+
attr_reader :max
|
269
|
+
|
270
|
+
end
|
271
|
+
|
272
|
+
class GreaterOrEquals < ComparisonPredicate
|
273
|
+
end
|
274
|
+
|
275
|
+
class LessOrEquals < ComparisonPredicate
|
276
|
+
end
|
277
|
+
|
278
|
+
class Greater < ComparisonPredicate
|
279
|
+
end
|
280
|
+
|
281
|
+
class Less < ComparisonPredicate
|
282
|
+
end
|
283
|
+
|
284
|
+
class Equals < ComparisonPredicate
|
285
|
+
end
|
286
|
+
|
287
|
+
class Aggregate < Node
|
288
|
+
|
289
|
+
def initialize(column)
|
290
|
+
@column = column
|
291
|
+
end
|
292
|
+
|
293
|
+
attr_reader :column
|
294
|
+
|
295
|
+
end
|
296
|
+
|
297
|
+
class Sum < Aggregate
|
298
|
+
end
|
299
|
+
|
300
|
+
class Minimum < Aggregate
|
301
|
+
end
|
302
|
+
|
303
|
+
class Maximum < Aggregate
|
304
|
+
end
|
305
|
+
|
306
|
+
class Average < Aggregate
|
307
|
+
end
|
308
|
+
|
309
|
+
class Count < Aggregate
|
310
|
+
end
|
311
|
+
|
312
|
+
class JoinedTable < Node
|
313
|
+
|
314
|
+
def initialize(left, right)
|
315
|
+
@left = left
|
316
|
+
@right = right
|
317
|
+
end
|
318
|
+
|
319
|
+
attr_reader :left
|
320
|
+
attr_reader :right
|
321
|
+
|
322
|
+
end
|
323
|
+
|
324
|
+
class CrossJoin < JoinedTable
|
325
|
+
end
|
326
|
+
|
327
|
+
class QualifiedJoin < JoinedTable
|
328
|
+
|
329
|
+
def initialize(left, right, search_condition)
|
330
|
+
super(left, right)
|
331
|
+
@search_condition = search_condition
|
332
|
+
end
|
333
|
+
|
334
|
+
attr_reader :search_condition
|
335
|
+
|
336
|
+
end
|
337
|
+
|
338
|
+
class InnerJoin < QualifiedJoin
|
339
|
+
end
|
340
|
+
|
341
|
+
class LeftJoin < QualifiedJoin
|
342
|
+
end
|
343
|
+
|
344
|
+
class LeftOuterJoin < QualifiedJoin
|
345
|
+
end
|
346
|
+
|
347
|
+
class RightJoin < QualifiedJoin
|
348
|
+
end
|
349
|
+
|
350
|
+
class RightOuterJoin < QualifiedJoin
|
351
|
+
end
|
352
|
+
|
353
|
+
class FullJoin < QualifiedJoin
|
354
|
+
end
|
355
|
+
|
356
|
+
class FullOuterJoin < QualifiedJoin
|
357
|
+
end
|
358
|
+
|
359
|
+
class QualifiedColumn < Node
|
360
|
+
|
361
|
+
def initialize(table, column)
|
362
|
+
@table = table
|
363
|
+
@column = column
|
364
|
+
end
|
365
|
+
|
366
|
+
attr_reader :table
|
367
|
+
attr_reader :column
|
368
|
+
|
369
|
+
end
|
370
|
+
|
371
|
+
class Identifier < Node
|
372
|
+
|
373
|
+
def initialize(name)
|
374
|
+
@name = name
|
375
|
+
end
|
376
|
+
|
377
|
+
attr_reader :name
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
class Table < Identifier
|
382
|
+
end
|
383
|
+
|
384
|
+
class Column < Identifier
|
385
|
+
end
|
386
|
+
|
387
|
+
class As < Node
|
388
|
+
|
389
|
+
def initialize(value, column)
|
390
|
+
@value = value
|
391
|
+
@column = column
|
392
|
+
end
|
393
|
+
|
394
|
+
attr_reader :value
|
395
|
+
attr_reader :column
|
396
|
+
|
397
|
+
end
|
398
|
+
|
399
|
+
class Arithmetic < Node
|
400
|
+
|
401
|
+
def initialize(left, right)
|
402
|
+
@left = left
|
403
|
+
@right = right
|
404
|
+
end
|
405
|
+
|
406
|
+
attr_reader :left
|
407
|
+
attr_reader :right
|
408
|
+
|
409
|
+
end
|
410
|
+
|
411
|
+
class Multiply < Arithmetic
|
412
|
+
end
|
413
|
+
|
414
|
+
class Divide < Arithmetic
|
415
|
+
end
|
416
|
+
|
417
|
+
class Add < Arithmetic
|
418
|
+
end
|
419
|
+
|
420
|
+
class Subtract < Arithmetic
|
421
|
+
end
|
422
|
+
|
423
|
+
class Unary < Node
|
424
|
+
|
425
|
+
def initialize(value)
|
426
|
+
@value = value
|
427
|
+
end
|
428
|
+
|
429
|
+
attr_reader :value
|
430
|
+
|
431
|
+
end
|
432
|
+
|
433
|
+
class Not < Unary
|
434
|
+
end
|
435
|
+
|
436
|
+
class UnaryPlus < Unary
|
437
|
+
end
|
438
|
+
|
439
|
+
class UnaryMinus < Unary
|
440
|
+
end
|
441
|
+
|
442
|
+
class CurrentUser < Node
|
443
|
+
end
|
444
|
+
|
445
|
+
class True < Node
|
446
|
+
end
|
447
|
+
|
448
|
+
class False < Node
|
449
|
+
end
|
450
|
+
|
451
|
+
class Null < Node
|
452
|
+
end
|
453
|
+
|
454
|
+
class Literal < Node
|
455
|
+
|
456
|
+
def initialize(value)
|
457
|
+
@value = value
|
458
|
+
end
|
459
|
+
|
460
|
+
attr_reader :value
|
461
|
+
|
462
|
+
end
|
463
|
+
|
464
|
+
class DateTime < Literal
|
465
|
+
end
|
466
|
+
|
467
|
+
class Date < Literal
|
468
|
+
end
|
469
|
+
|
470
|
+
class String < Literal
|
471
|
+
end
|
472
|
+
|
473
|
+
class ApproximateFloat < Node
|
474
|
+
|
475
|
+
def initialize(mantissa, exponent)
|
476
|
+
@mantissa = mantissa
|
477
|
+
@exponent = exponent
|
478
|
+
end
|
479
|
+
|
480
|
+
attr_reader :mantissa
|
481
|
+
attr_reader :exponent
|
482
|
+
|
483
|
+
end
|
484
|
+
|
485
|
+
class Float < Literal
|
486
|
+
end
|
487
|
+
|
488
|
+
class Integer < Literal
|
489
|
+
end
|
490
|
+
|
491
|
+
end
|
492
|
+
end
|