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