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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
# frozen_string_literal: true
require 'helper'
require 'set'
module Arel
module Visitors
describe 'the to_sql visitor' do
before do
@conn = FakeRecord::Base.new
@visitor = ToSql.new @conn.connection
@table = Table.new(:users)
@attr = @table[:id]
end
def compile node
@visitor.accept(node, Collectors::SQLString.new).value
end
it 'works with BindParams' do
node = Nodes::BindParam.new(1)
sql = compile node
sql.must_be_like '?'
end
it 'does not quote BindParams used as part of a Values' do
bp = Nodes::BindParam.new(1)
values = Nodes::Values.new([bp])
sql = compile values
sql.must_be_like 'VALUES (?)'
end
it 'can define a dispatch method' do
visited = false
viz = Class.new(Arel::Visitors::Reduce) {
define_method(:hello) do |node, c|
visited = true
end
def dispatch
{ Arel::Table => 'hello' }
end
}.new
viz.accept(@table, Collectors::SQLString.new)
assert visited, 'hello method was called'
end
it 'should not quote sql literals' do
node = @table[Arel.star]
sql = compile node
sql.must_be_like '"users".*'
end
it 'should visit named functions' do
function = Nodes::NamedFunction.new('omg', [Arel.star])
assert_equal 'omg(*)', compile(function)
end
it 'should chain predications on named functions' do
function = Nodes::NamedFunction.new('omg', [Arel.star])
sql = compile(function.eq(2))
sql.must_be_like %{ omg(*) = 2 }
end
it 'should handle nil with named functions' do
function = Nodes::NamedFunction.new('omg', [Arel.star])
sql = compile(function.eq(nil))
sql.must_be_like %{ omg(*) IS NULL }
end
it 'should visit built-in functions' do
function = Nodes::Count.new([Arel.star])
assert_equal 'COUNT(*)', compile(function)
function = Nodes::Sum.new([Arel.star])
assert_equal 'SUM(*)', compile(function)
function = Nodes::Max.new([Arel.star])
assert_equal 'MAX(*)', compile(function)
function = Nodes::Min.new([Arel.star])
assert_equal 'MIN(*)', compile(function)
function = Nodes::Avg.new([Arel.star])
assert_equal 'AVG(*)', compile(function)
end
it 'should visit built-in functions operating on distinct values' do
function = Nodes::Count.new([Arel.star])
function.distinct = true
assert_equal 'COUNT(DISTINCT *)', compile(function)
function = Nodes::Sum.new([Arel.star])
function.distinct = true
assert_equal 'SUM(DISTINCT *)', compile(function)
function = Nodes::Max.new([Arel.star])
function.distinct = true
assert_equal 'MAX(DISTINCT *)', compile(function)
function = Nodes::Min.new([Arel.star])
function.distinct = true
assert_equal 'MIN(DISTINCT *)', compile(function)
function = Nodes::Avg.new([Arel.star])
function.distinct = true
assert_equal 'AVG(DISTINCT *)', compile(function)
end
it 'works with lists' do
function = Nodes::NamedFunction.new('omg', [Arel.star, Arel.star])
assert_equal 'omg(*, *)', compile(function)
end
describe 'Nodes::Equality' do
it "should escape strings" do
test = Table.new(:users)[:name].eq 'Aaron Patterson'
compile(test).must_be_like %{
"users"."name" = 'Aaron Patterson'
}
end
it 'should handle false' do
table = Table.new(:users)
val = Nodes.build_quoted(false, table[:active])
sql = compile Nodes::Equality.new(val, val)
sql.must_be_like %{ 'f' = 'f' }
end
it 'should handle nil' do
sql = compile Nodes::Equality.new(@table[:name], nil)
sql.must_be_like %{ "users"."name" IS NULL }
end
end
describe 'Nodes::Grouping' do
it 'wraps nested groupings in brackets only once' do
sql = compile Nodes::Grouping.new(Nodes::Grouping.new(Nodes.build_quoted('foo')))
sql.must_equal "('foo')"
end
end
describe 'Nodes::NotEqual' do
it 'should handle false' do
val = Nodes.build_quoted(false, @table[:active])
sql = compile Nodes::NotEqual.new(@table[:active], val)
sql.must_be_like %{ "users"."active" != 'f' }
end
it 'should handle nil' do
val = Nodes.build_quoted(nil, @table[:active])
sql = compile Nodes::NotEqual.new(@table[:name], val)
sql.must_be_like %{ "users"."name" IS NOT NULL }
end
end
it "should visit string subclass" do
[
Class.new(String).new(":'("),
Class.new(Class.new(String)).new(":'("),
].each do |obj|
val = Nodes.build_quoted(obj, @table[:active])
sql = compile Nodes::NotEqual.new(@table[:name], val)
sql.must_be_like %{ "users"."name" != ':\\'(' }
end
end
it "should visit_Class" do
compile(Nodes.build_quoted(DateTime)).must_equal "'DateTime'"
end
it "should escape LIMIT" do
sc = Arel::Nodes::SelectStatement.new
sc.limit = Arel::Nodes::Limit.new(Nodes.build_quoted("omg"))
assert_match(/LIMIT 'omg'/, compile(sc))
end
it "should contain a single space before ORDER BY" do
table = Table.new(:users)
test = table.order(table[:name])
sql = compile test
assert_match(/"users" ORDER BY/, sql)
end
it "should quote LIMIT without column type coercion" do
table = Table.new(:users)
sc = table.where(table[:name].eq(0)).take(1).ast
assert_match(/WHERE "users"."name" = 0 LIMIT 1/, compile(sc))
end
it "should visit_DateTime" do
dt = DateTime.now
table = Table.new(:users)
test = table[:created_at].eq dt
sql = compile test
sql.must_be_like %{"users"."created_at" = '#{dt.strftime("%Y-%m-%d %H:%M:%S")}'}
end
it "should visit_Float" do
test = Table.new(:products)[:price].eq 2.14
sql = compile test
sql.must_be_like %{"products"."price" = 2.14}
end
it "should visit_Not" do
sql = compile Nodes::Not.new(Arel.sql("foo"))
sql.must_be_like "NOT (foo)"
end
it "should apply Not to the whole expression" do
node = Nodes::And.new [@attr.eq(10), @attr.eq(11)]
sql = compile Nodes::Not.new(node)
sql.must_be_like %{NOT ("users"."id" = 10 AND "users"."id" = 11)}
end
it "should visit_As" do
as = Nodes::As.new(Arel.sql("foo"), Arel.sql("bar"))
sql = compile as
sql.must_be_like "foo AS bar"
end
it "should visit_Bignum" do
compile 8787878092
end
it "should visit_Hash" do
compile(Nodes.build_quoted({:a => 1}))
end
it "should visit_Set" do
compile Nodes.build_quoted(Set.new([1, 2]))
end
it "should visit_BigDecimal" do
compile Nodes.build_quoted(BigDecimal.new('2.14'))
end
it "should visit_Date" do
dt = Date.today
table = Table.new(:users)
test = table[:created_at].eq dt
sql = compile test
sql.must_be_like %{"users"."created_at" = '#{dt.strftime("%Y-%m-%d")}'}
end
it "should visit_NilClass" do
compile(Nodes.build_quoted(nil)).must_be_like "NULL"
end
it "unsupported input should raise UnsupportedVisitError" do
error = assert_raises(UnsupportedVisitError) { compile(nil) }
assert_match(/\AUnsupported/, error.message)
end
it "should visit_Arel_SelectManager, which is a subquery" do
mgr = Table.new(:foo).project(:bar)
compile(mgr).must_be_like '(SELECT bar FROM "foo")'
end
it "should visit_Arel_Nodes_And" do
node = Nodes::And.new [@attr.eq(10), @attr.eq(11)]
compile(node).must_be_like %{
"users"."id" = 10 AND "users"."id" = 11
}
end
it "should visit_Arel_Nodes_Or" do
node = Nodes::Or.new @attr.eq(10), @attr.eq(11)
compile(node).must_be_like %{
"users"."id" = 10 OR "users"."id" = 11
}
end
it "should visit_Arel_Nodes_Assignment" do
column = @table["id"]
node = Nodes::Assignment.new(
Nodes::UnqualifiedColumn.new(column),
Nodes::UnqualifiedColumn.new(column)
)
compile(node).must_be_like %{
"id" = "id"
}
end
it "should visit visit_Arel_Attributes_Time" do
attr = Attributes::Time.new(@attr.relation, @attr.name)
compile attr
end
it "should visit_TrueClass" do
test = Table.new(:users)[:bool].eq(true)
compile(test).must_be_like %{ "users"."bool" = 't' }
end
describe "Nodes::Matches" do
it "should know how to visit" do
node = @table[:name].matches('foo%')
compile(node).must_be_like %{
"users"."name" LIKE 'foo%'
}
end
it "can handle ESCAPE" do
node = @table[:name].matches('foo!%', '!')
compile(node).must_be_like %{
"users"."name" LIKE 'foo!%' ESCAPE '!'
}
end
it 'can handle subqueries' do
subquery = @table.project(:id).where(@table[:name].matches('foo%'))
node = @attr.in subquery
compile(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" LIKE 'foo%')
}
end
end
describe "Nodes::DoesNotMatch" do
it "should know how to visit" do
node = @table[:name].does_not_match('foo%')
compile(node).must_be_like %{
"users"."name" NOT LIKE 'foo%'
}
end
it "can handle ESCAPE" do
node = @table[:name].does_not_match('foo!%', '!')
compile(node).must_be_like %{
"users"."name" NOT LIKE 'foo!%' ESCAPE '!'
}
end
it 'can handle subqueries' do
subquery = @table.project(:id).where(@table[:name].does_not_match('foo%'))
node = @attr.in subquery
compile(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" NOT LIKE 'foo%')
}
end
end
describe "Nodes::Ordering" do
it "should know how to visit" do
node = @attr.desc
compile(node).must_be_like %{
"users"."id" DESC
}
end
end
describe "Nodes::In" do
it "should know how to visit" do
node = @attr.in [1, 2, 3]
compile(node).must_be_like %{
"users"."id" IN (1, 2, 3)
}
end
it "should return 1=0 when empty right which is always false" do
node = @attr.in []
compile(node).must_equal '1=0'
end
it 'can handle two dot ranges' do
node = @attr.between 1..3
compile(node).must_be_like %{
"users"."id" BETWEEN 1 AND 3
}
end
it 'can handle three dot ranges' do
node = @attr.between 1...3
compile(node).must_be_like %{
"users"."id" >= 1 AND "users"."id" < 3
}
end
it 'can handle ranges bounded by infinity' do
node = @attr.between 1..Float::INFINITY
compile(node).must_be_like %{
"users"."id" >= 1
}
node = @attr.between(-Float::INFINITY..3)
compile(node).must_be_like %{
"users"."id" <= 3
}
node = @attr.between(-Float::INFINITY...3)
compile(node).must_be_like %{
"users"."id" < 3
}
node = @attr.between(-Float::INFINITY..Float::INFINITY)
compile(node).must_be_like %{1=1}
end
it 'can handle subqueries' do
table = Table.new(:users)
subquery = table.project(:id).where(table[:name].eq('Aaron'))
node = @attr.in subquery
compile(node).must_be_like %{
"users"."id" IN (SELECT id FROM "users" WHERE "users"."name" = 'Aaron')
}
end
end
describe "Nodes::InfixOperation" do
it "should handle Multiplication" do
node = Arel::Attributes::Decimal.new(Table.new(:products), :price) * Arel::Attributes::Decimal.new(Table.new(:currency_rates), :rate)
compile(node).must_equal %("products"."price" * "currency_rates"."rate")
end
it "should handle Division" do
node = Arel::Attributes::Decimal.new(Table.new(:products), :price) / 5
compile(node).must_equal %("products"."price" / 5)
end
it "should handle Addition" do
node = Arel::Attributes::Decimal.new(Table.new(:products), :price) + 6
compile(node).must_equal %(("products"."price" + 6))
end
it "should handle Subtraction" do
node = Arel::Attributes::Decimal.new(Table.new(:products), :price) - 7
compile(node).must_equal %(("products"."price" - 7))
end
it "should handle Concatination" do
table = Table.new(:users)
node = table[:name].concat(table[:name])
compile(node).must_equal %("users"."name" || "users"."name")
end
it "should handle BitwiseAnd" do
node = Arel::Attributes::Integer.new(Table.new(:products), :bitmap) & 16
compile(node).must_equal %(("products"."bitmap" & 16))
end
it "should handle BitwiseOr" do
node = Arel::Attributes::Integer.new(Table.new(:products), :bitmap) | 16
compile(node).must_equal %(("products"."bitmap" | 16))
end
it "should handle BitwiseXor" do
node = Arel::Attributes::Integer.new(Table.new(:products), :bitmap) ^ 16
compile(node).must_equal %(("products"."bitmap" ^ 16))
end
it "should handle BitwiseShiftLeft" do
node = Arel::Attributes::Integer.new(Table.new(:products), :bitmap) << 4
compile(node).must_equal %(("products"."bitmap" << 4))
end
it "should handle BitwiseShiftRight" do
node = Arel::Attributes::Integer.new(Table.new(:products), :bitmap) >> 4
compile(node).must_equal %(("products"."bitmap" >> 4))
end
it "should handle arbitrary operators" do
node = Arel::Nodes::InfixOperation.new(
'&&',
Arel::Attributes::String.new(Table.new(:products), :name),
Arel::Attributes::String.new(Table.new(:products), :name)
)
compile(node).must_equal %("products"."name" && "products"."name")
end
end
describe "Nodes::UnaryOperation" do
it "should handle BitwiseNot" do
node = ~ Arel::Attributes::Integer.new(Table.new(:products), :bitmap)
compile(node).must_equal %( ~ "products"."bitmap")
end
it "should handle arbitrary operators" do
node = Arel::Nodes::UnaryOperation.new('!', Arel::Attributes::String.new(Table.new(:products), :active))
compile(node).must_equal %( ! "products"."active")
end
end
describe "Nodes::NotIn" do
it "should know how to visit" do
node = @attr.not_in [1, 2, 3]
compile(node).must_be_like %{
"users"."id" NOT IN (1, 2, 3)
}
end
it "should return 1=1 when empty right which is always true" do
node = @attr.not_in []
compile(node).must_equal '1=1'
end
it 'can handle two dot ranges' do
node = @attr.not_between 1..3
compile(node).must_equal(
%{("users"."id" < 1 OR "users"."id" > 3)}
)
end
it 'can handle three dot ranges' do
node = @attr.not_between 1...3
compile(node).must_equal(
%{("users"."id" < 1 OR "users"."id" >= 3)}
)
end
it 'can handle ranges bounded by infinity' do
node = @attr.not_between 1..Float::INFINITY
compile(node).must_be_like %{
"users"."id" < 1
}
node = @attr.not_between(-Float::INFINITY..3)
compile(node).must_be_like %{
"users"."id" > 3
}
node = @attr.not_between(-Float::INFINITY...3)
compile(node).must_be_like %{
"users"."id" >= 3
}
node = @attr.not_between(-Float::INFINITY..Float::INFINITY)
compile(node).must_be_like %{1=0}
end
it 'can handle subqueries' do
table = Table.new(:users)
subquery = table.project(:id).where(table[:name].eq('Aaron'))
node = @attr.not_in subquery
compile(node).must_be_like %{
"users"."id" NOT IN (SELECT id FROM "users" WHERE "users"."name" = 'Aaron')
}
end
end
describe 'Constants' do
it "should handle true" do
test = Table.new(:users).create_true
compile(test).must_be_like %{
TRUE
}
end
it "should handle false" do
test = Table.new(:users).create_false
compile(test).must_be_like %{
FALSE
}
end
end
describe 'TableAlias' do
it "should use the underlying table for checking columns" do
test = Table.new(:users).alias('zomgusers')[:id].eq '3'
compile(test).must_be_like %{
"zomgusers"."id" = '3'
}
end
end
describe 'distinct on' do
it 'raises not implemented error' do
core = Arel::Nodes::SelectCore.new
core.set_quantifier = Arel::Nodes::DistinctOn.new(Arel.sql('aaron'))
assert_raises(NotImplementedError) do
compile(core)
end
end
end
describe 'Nodes::Regexp' do
it 'raises not implemented error' do
node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%'))
assert_raises(NotImplementedError) do
compile(node)
end
end
end
describe 'Nodes::NotRegexp' do
it 'raises not implemented error' do
node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%'))
assert_raises(NotImplementedError) do
compile(node)
end
end
end
describe 'Nodes::Case' do
it 'supports simple case expressions' do
node = Arel::Nodes::Case.new(@table[:name])
.when('foo').then(1)
.else(0)
compile(node).must_be_like %{
CASE "users"."name" WHEN 'foo' THEN 1 ELSE 0 END
}
end
it 'supports extended case expressions' do
node = Arel::Nodes::Case.new
.when(@table[:name].in(%w(foo bar))).then(1)
.else(0)
compile(node).must_be_like %{
CASE WHEN "users"."name" IN ('foo', 'bar') THEN 1 ELSE 0 END
}
end
it 'works without default branch' do
node = Arel::Nodes::Case.new(@table[:name])
.when('foo').then(1)
compile(node).must_be_like %{
CASE "users"."name" WHEN 'foo' THEN 1 END
}
end
it 'allows chaining multiple conditions' do
node = Arel::Nodes::Case.new(@table[:name])
.when('foo').then(1)
.when('bar').then(2)
.else(0)
compile(node).must_be_like %{
CASE "users"."name" WHEN 'foo' THEN 1 WHEN 'bar' THEN 2 ELSE 0 END
}
end
it 'supports #when with two arguments and no #then' do
node = Arel::Nodes::Case.new @table[:name]
{ foo: 1, bar: 0 }.reduce(node) { |_node, pair| _node.when(*pair) }
compile(node).must_be_like %{
CASE "users"."name" WHEN 'foo' THEN 1 WHEN 'bar' THEN 0 END
}
end
it 'can be chained as a predicate' do
node = @table[:name].when('foo').then('bar').else('baz')
compile(node).must_be_like %{
CASE "users"."name" WHEN 'foo' THEN 'bar' ELSE 'baz' END
}
end
end
end
end
end
|