-
Notifications
You must be signed in to change notification settings - Fork 3
/
ruby.rb
351 lines (268 loc) · 9.11 KB
/
ruby.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
require 'bigdecimal'
require 'date'
module Arel
module Visitors
class Ruby < Arel::Visitors::Visitor
def initialize
@connection = Object.new.extend(ActiveRecord::ConnectionAdapters::Quoting)
@dummy_column = Struct.new(:type).new
end
private
# def visit_Arel_Nodes_DeleteStatement o
# end
# def build_subselect key, o
# end
# def visit_Arel_Nodes_UpdateStatement o
# end
# def visit_Arel_Nodes_InsertStatement o
# end
# def visit_Arel_Nodes_Exists o
# end
# def visit_Arel_Nodes_True o
# end
# def visit_Arel_Nodes_False o
# end
# def visit_Arel_Nodes_Values o
# end
def visit_Arel_Nodes_SelectStatement o
ProcWithSourceCollection.new([
o.cores.map { |x| visit_Arel_Nodes_SelectCore x },
(o.orders.map { |x| visit_Arel_Nodes_OrderCore x } unless o.orders.empty?),
(visit(o.offset) if o.offset),
(visit(o.limit) if o.limit),
o.cores.map { |x| x.groups.map { |x| visit x} },
])
end
def visit_Arel_Nodes_SelectCore o
[
# ("#{o.projections.map { |x| visit x }.join ', '}" unless o.projections.empty?),
# (visit(o.source) if o.source && !o.source.empty?),
(o.wheres.map { |x| visit x } unless o.wheres.empty?)
# (visit(o.having) if o.having),
]
end
def visit_Arel_Nodes_OrderCore order
#FIXME order_by('a, b') shouldn't actaully be sort_by(&:a).sort_by(&:b)
order.split(',').map(&:strip).map do |o|
attr, direction = o.split(/\s+/)
v = visit attr
ProcWithSource.new("sort_by(&:#{v})#{'.reverse' if direction == 'desc'}") do |collection|
col = collection.sort_by {|c| c.send v}
col.reverse! if direction == 'desc'
col
end
end
end
# def visit_Arel_Nodes_Bin o
# end
# def visit_Arel_Nodes_Distinct o
# end
# def visit_Arel_Nodes_DistinctOn o
# end
# def visit_Arel_Nodes_With o
# end
# def visit_Arel_Nodes_WithRecursive o
# end
# def visit_Arel_Nodes_Union o
# end
# def visit_Arel_Nodes_UnionAll o
# end
# def visit_Arel_Nodes_Intersect o
# end
# def visit_Arel_Nodes_Except o
# end
# def visit_Arel_Nodes_NamedWindow o
# end
# def visit_Arel_Nodes_Window o
# end
# def visit_Arel_Nodes_Rows o
# end
# def visit_Arel_Nodes_Range o
# end
# def visit_Arel_Nodes_Preceding o
# end
# def visit_Arel_Nodes_Following o
# end
# def visit_Arel_Nodes_CurrentRow o
# end
# def visit_Arel_Nodes_Over o
# end
# def visit_Arel_Nodes_Having o
# end
def visit_Arel_Nodes_Offset o
v = visit o.expr
ProcWithSource.new("from(#{v})") {|collection| collection.from(v) }
end
def visit_Arel_Nodes_Limit o
v = visit o.expr
ProcWithSource.new("take(#{v})") {|collection| collection.take(v) }
end
def visit_Arel_Nodes_Grouping o
# doubtful implementation
v = visit o.expr
ProcWithSource.new("select {|g| g.#{v.to_source}}") { |collection| collection.select {|obj| v.call(obj) } }
end
# def visit_Arel_Nodes_Ascending o
# end
# def visit_Arel_Nodes_Descending o
# end
def visit_Arel_Nodes_Group o
v = visit o.expr
ProcWithSource.new("group_by {|g| g.#{v} }") {|collection| collection.group_by {|g| g.send v } }
end
# def visit_Arel_Nodes_NamedFunction o
# end
# def visit_Arel_Nodes_Extract o
# end
# def visit_Arel_Nodes_Count o
# end
# def visit_Arel_Nodes_Sum o
# end
# def visit_Arel_Nodes_Max o
# end
# def visit_Arel_Nodes_Min o
# end
# def visit_Arel_Nodes_Avg o
# end
# def visit_Arel_Nodes_TableAlias o
# end
def visit_Arel_Nodes_Between o
l, r = visit(o.left), Range.new(o.right.children.first, o.right.children.last)
ProcWithSource.new("#{l}.in? #{r.inspect}") { |o| o.send(l).in?(r) }
end
# def visit_Arel_Nodes_GreaterThanOrEqual o
# end
# def visit_Arel_Nodes_GreaterThan o
# end
# def visit_Arel_Nodes_LessThanOrEqual o
# end
# def visit_Arel_Nodes_LessThan o
# end
# def visit_Arel_Nodes_Matches o
# end
# def visit_Arel_Nodes_DoesNotMatch o
# end
def visit_Arel_Nodes_JoinSource o
# do nothing
end
# def visit_Arel_Nodes_StringJoin o
# end
# def visit_Arel_Nodes_OuterJoin o
# end
# def visit_Arel_Nodes_InnerJoin o
# end
# def visit_Arel_Nodes_On o
# end
def visit_Arel_Nodes_Not o
case o.expr
when Arel::Nodes::Between
c = o.expr
l, r = visit(c.left), Range.new(c.right.children.first, c.right.children.last)
# FIXME: 'not_in?' does not actually exist
ProcWithSource.new("#{l}.not_in? #{r.inspect}") { |o| !o.send(l).in?(r) }
else
raise NotImplementedError, 'general Arel::Nodes::Not not implemented'
end
end
# def visit_Arel_Table o
# end
def visit_Arel_Nodes_In o
l, r = visit(o.left), visit(o.right)
ProcWithSource.new("#{l}.in? #{r.inspect}") { |o| o.send(l).in?(r) }
end
def visit_Arel_Nodes_NotIn o
l, r = visit(o.left), visit(o.right)
# FIXME: 'not_in?' does not actually exist
ProcWithSource.new("#{l}.not_in? #{r.inspect}") { |o| !o.send(l).in?(r) }
end
def visit_Arel_Nodes_And o
o.children.map { |x| ProcWithSource.new("select {|o| o.#{visit(x).to_source}}") { |collection| collection.select {|obj| visit(x).call(obj) } } }
end
# def visit_Arel_Nodes_Or o
# end
# def visit_Arel_Nodes_Assignment o
# end
def visit_Arel_Nodes_Equality o
l, r = visit(o.left), visit(o.right)
ProcWithSource.new("#{l} == #{r.inspect}") { |o| o.send(l) == r }
end
def visit_Arel_Nodes_NotEqual o
l, r = visit(o.left), visit(o.right)
ProcWithSource.new("#{l} != #{r.inspect}") { |o| o.send(l) != r }
end
# def visit_Arel_Nodes_As o
# end
# def visit_Arel_Nodes_UnqualifiedColumn o
# end
def visit_Arel_Attributes_Attribute o
o.name
end
alias :visit_Arel_Attributes_Integer :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Float :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Decimal :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_String :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Time :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Boolean :visit_Arel_Attributes_Attribute
def literal o; o end
alias :visit_Arel_Nodes_BindParam :literal
alias :visit_Arel_Nodes_SqlLiteral :literal
alias :visit_Bignum :literal
alias :visit_Fixnum :literal
def quoted o
# don't actually quote...
o
end
alias :visit_ActiveSupport_Multibyte_Chars :quoted
alias :visit_ActiveSupport_StringInquirer :quoted
alias :visit_BigDecimal :quoted
alias :visit_Class :quoted
alias :visit_Date :quoted
alias :visit_DateTime :quoted
alias :visit_FalseClass :quoted
alias :visit_Float :quoted
alias :visit_Hash :quoted
alias :visit_NilClass :quoted
alias :visit_String :quoted
alias :visit_Symbol :quoted
alias :visit_Time :quoted
alias :visit_TrueClass :quoted
# def visit_Arel_Nodes_InfixOperation o
# "#{visit o.left} #{o.operator} #{visit o.right}"
# end
# alias :visit_Arel_Nodes_Addition :visit_Arel_Nodes_InfixOperation
# alias :visit_Arel_Nodes_Subtraction :visit_Arel_Nodes_InfixOperation
# alias :visit_Arel_Nodes_Multiplication :visit_Arel_Nodes_InfixOperation
# alias :visit_Arel_Nodes_Division :visit_Arel_Nodes_InfixOperation
def visit_Array o
o.map { |x| visit x }
end
def quote value
@connection.quote value, @dummy_column
end
end
class ProcWithSource
def initialize(source, &block)
@source, @block = source, block
end
def call(*args)
@block.call(*args)
end
def to_source
@source
end
end
class ProcWithSourceCollection
def initialize(procs)
@procs = procs.flatten.compact
end
def call(collection)
@procs.inject(collection) do |result, lmd|
lmd.call result
end
end
def to_source
@procs.map(&:to_source).join('.')
end
end
end
end