-
Notifications
You must be signed in to change notification settings - Fork 6
/
nodes.rb
515 lines (422 loc) · 10 KB
/
nodes.rb
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
module Rgviz
class Query
attr_accessor :select
attr_accessor :where
attr_accessor :group_by
attr_accessor :pivot
attr_accessor :order_by
attr_accessor :limit
attr_accessor :offset
attr_accessor :labels
attr_accessor :formats
attr_accessor :options
def select?
@select && @select.columns && @select.columns.length > 0
end
def accept(visitor)
if visitor.visit_query self
select.accept visitor if select
where.accept visitor if where
group_by.accept visitor if group_by
pivot.accept visitor if pivot
order_by.accept visitor if order_by
labels.each{|x| x.accept visitor} if labels
formats.each{|x| x.accept visitor} if formats
end
visitor.end_visit_query self
end
def to_s
str = ''
if select
if select.columns.empty?
str << "select * "
else
str << "select #{select.to_s} "
end
end
str << "where #{where} " if where
str << "group by #{group_by} " if group_by
str << "pivot #{pivot} " if pivot
str << "order by #{order_by} " if order_by
str << "limit #{limit} " if limit
str << "offset #{offset} " if offset
str << "label #{labels.map(&:to_s).join(', ')} " if labels
str << "format #{formats.map(&:to_s).join(', ')} " if formats
str << "options #{options} " if options
str.strip
end
end
class ColumnsContainer
attr_accessor :columns
def initialize
@columns = []
end
def to_s
@columns.map(&:to_s).join(', ')
end
end
class Select < ColumnsContainer
def accept(visitor)
if visitor.visit_select(self)
columns.each{|x| x.accept visitor}
end
visitor.end_visit_select self
end
end
class GroupBy < ColumnsContainer
def accept(visitor)
if visitor.visit_group_by(self)
columns.each{|x| x.accept visitor}
end
visitor.end_visit_group_by self
end
end
class Pivot < ColumnsContainer
def accept(visitor)
if visitor.visit_pivot(self)
columns.each{|x| x.accept visitor}
end
visitor.end_visit_pivot self
end
end
class OrderBy
attr_accessor :sorts
def initialize
@sorts = []
end
def accept(visitor)
if visitor.visit_order_by(self)
sorts.each{|x| x.accept visitor}
end
visitor.end_visit_order_by self
end
def to_s
@sorts.map(&:to_s).join(', ')
end
end
class Sort
Asc = Token::Asc
Desc = Token::Desc
attr_accessor :column
attr_accessor :order
def initialize(column, order)
@column = column
@order = order
end
def accept(visitor)
if visitor.visit_sort(self)
column.accept visitor
end
visitor.end_visit_sort self
end
def to_s
"#{column} #{order}"
end
end
class Where
attr_accessor :expression
def initialize(expression)
@expression = expression
end
def accept(visitor)
if visitor.visit_where(self)
expression.accept visitor
end
visitor.end_visit_where self
end
def to_s
@expression.to_s
end
end
class Label
attr_accessor :column
attr_accessor :label
def initialize(column, label)
@column = column
@label = label
end
def accept(visitor)
if visitor.visit_label(self)
column.accept visitor
end
visitor.end_visit_label self
end
def to_s
"#{column} `#{label}`"
end
end
class Format
attr_accessor :column
attr_accessor :pattern
def initialize(column, pattern)
@column = column
@pattern = pattern
end
def accept(visitor)
if visitor.visit_format(self)
column.accept visitor
end
visitor.end_visit_format self
end
def to_s
"#{column} `#{pattern}`"
end
end
class Options
attr_accessor :no_values
attr_accessor :no_format
def to_s
s = ''
s += 'no_values ' if @no_values
s += 'no_format ' if @no_format
s.strip
end
end
class LogicalExpression
And = Token::And
Or = Token::Or
attr_accessor :operator
attr_accessor :operands
def initialize(operator, operands)
@operator = operator
@operands = operands
end
def accept(visitor)
if visitor.visit_logical_expression(self)
operands.each{|x| x.accept visitor}
end
visitor.end_visit_logical_expression self
end
def to_s
operands.map(&:to_s).join(" #{operator} ")
end
end
class BinaryExpression
Contains = Token::Contains
EndsWith = :'ends with'
Eq = Token::EQ
Gt = Token::GT
Gte = Token::GTE
Like = Token::Like
Lt = Token::LT
Lte = Token::LTE
Matches = Token::Matches
Neq = Token::NEQ
StartsWith = :'starts with'
attr_accessor :operator
attr_accessor :left
attr_accessor :right
def initialize(left, operator, right)
@left = left
@operator = operator
@right = right
end
def accept(visitor)
if visitor.visit_binary_expression(self)
left.accept visitor
right.accept visitor
end
visitor.end_visit_binary_expression self
end
def to_s
"#{left} #{operator} #{right}"
end
end
class UnaryExpression
Not = Token::Not
IsNull = :'is null'
IsNotNull = :'is not null'
attr_accessor :operand
attr_accessor :operator
def initialize(operator, operand)
@operator = operator
@operand = operand
end
def accept(visitor)
if visitor.visit_unary_expression(self)
operand.accept visitor
end
visitor.end_visit_unary_expression self
end
def to_s
if operator == Not
"not #{operand}"
else
"#{operand} #{operator}"
end
end
end
class IdColumn
attr_accessor :name
def initialize(name)
@name = name
end
def accept(visitor)
visitor.visit_id_column(self)
visitor.end_visit_id_column self
end
def hash
@name.hash
end
def eql?(other)
other.class == IdColumn && other.name == @name
end
alias_method :==, :eql?
def to_s
@name
end
end
class ValueColumn
attr_accessor :value
def initialize(value)
@value = value
end
def hash
@value.hash
end
def eql?(other)
other.class == self.class && other.value == @value
end
alias_method :==, :eql?
def to_s
value.to_s
end
end
class NumberColumn < ValueColumn
def accept(visitor)
visitor.visit_number_column(self)
visitor.end_visit_number_column self
end
end
class StringColumn < ValueColumn
def accept(visitor)
visitor.visit_string_column(self)
visitor.end_visit_string_column self
end
def to_s
"'#{value}'"
end
end
class BooleanColumn < ValueColumn
def accept(visitor)
visitor.visit_boolean_column(self)
visitor.end_visit_boolean_column self
end
end
class DateColumn < ValueColumn
def accept(visitor)
visitor.visit_date_column(self)
visitor.end_visit_date_column self
end
def to_s
"date '#{value.to_s}'"
end
end
class DateTimeColumn < ValueColumn
def accept(visitor)
visitor.visit_date_time_column(self)
visitor.end_visit_date_time_column self
end
def to_s
"datetime '" + value.strftime("%Y-%m-%d %H:%M:%S") + "'"
end
end
class TimeOfDayColumn < ValueColumn
def accept(visitor)
visitor.visit_time_of_day_column(self)
visitor.end_visit_time_of_day_column self
end
def to_s
"timeofday '" + value.strftime("%H:%M:%S") + "'"
end
end
class ScalarFunctionColumn
Concat = Token::Concat
DateDiff = Token::DateDiff
Day = Token::Day
DayOfWeek = Token::DayOfWeek
Difference = Token::MINUS
Hour = Token::Hour
Lower = Token::Lower
Millisecond = Token::Millisecond
Minute = Token::Minute
Month = Token::Month
Now = Token::Now
Product = Token::STAR
Quarter = Token::Quarter
Quotient = Token::SLASH
Round = Token::Round
Floor = Token::Floor
Second = Token::Second
Sum = Token::PLUS
ToDate = Token::ToDate
Upper = Token::Upper
Year = Token::Year
attr_accessor :function
attr_accessor :arguments
def initialize(function, *arguments)
@function = function
@arguments = arguments
end
def accept(visitor)
if visitor.visit_scalar_function_column(self)
arguments.each{|x| x.accept visitor}
end
visitor.end_visit_scalar_function_column self
end
def hash
return @hash if @hash
@hash = 1
@hash = @hash * 31 + @function.hash
@arguments.each do |arg|
@hash = @hash * 31 + arg.hash
end
@hash
end
def eql?(other)
other.class == ScalarFunctionColumn && other.function == @function && other.arguments == @arguments
end
alias_method :==, :eql?
def to_s
case function
when Sum, Difference, Product, Quotient
"#{arguments[0].to_s} #{function} #{arguments[1].to_s}"
else
"#{function}(#{arguments.map(&:to_s).join(', ')})"
end
end
end
class AggregateColumn
Avg = Token::Avg
Count = Token::Count
Max = Token::Max
Min = Token::Min
Sum = Token::Sum
attr_accessor :function
attr_accessor :argument
def initialize(function, argument)
@function = function
@argument = argument
end
def accept(visitor)
if visitor.visit_aggregate_column(self)
argument.accept visitor
end
visitor.end_visit_aggregate_column self
end
def hash
return @hash if @hash
@hash = 1
@hash = @hash * 31 + @function.hash
@hash = @hash * 31 + @argument.hash
@hash
end
def eql?(other)
other.class == ScalarFunctionColumn && other.function == @function && other.arguments == @arguments
end
alias_method :==, :eql?
def to_s
"#{function}(#{argument})"
end
end
end