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
|
require 'helper'
require 'arel/collectors/bind'
module Arel
module Collectors
class TestBindCollector < Arel::Test
def setup
@conn = FakeRecord::Base.new
@visitor = Visitors::ToSql.new @conn.connection
super
end
def collect node
@visitor.accept(node, Collectors::Bind.new)
end
def compile node
collect(node).value
end
def ast_with_binds bv
table = Table.new(:users)
manager = Arel::SelectManager.new table
manager.where(table[:age].eq(bv))
manager.where(table[:name].eq(bv))
manager.ast
end
def test_leaves_binds
node = Nodes::BindParam.new
list = compile node
assert_equal node, list.first
assert_equal node.class, list.first.class
end
def test_adds_strings
bv = Nodes::BindParam.new
list = compile ast_with_binds bv
assert_operator list.length, :>, 0
assert_equal bv, list.grep(Nodes::BindParam).first
assert_equal bv.class, list.grep(Nodes::BindParam).first.class
end
def test_substitute_binds
bv = Nodes::BindParam.new
collector = collect ast_with_binds bv
values = collector.value
offsets = values.map.with_index { |v,i|
[v,i]
}.find_all { |(v,_)| Nodes::BindParam === v }.map(&:last)
list = collector.substitute_binds ["hello", "world"]
assert_equal "hello", list[offsets[0]]
assert_equal "world", list[offsets[1]]
assert_equal 'SELECT FROM "users" WHERE "users"."age" = hello AND "users"."name" = world', list.join
end
def test_compile
bv = Nodes::BindParam.new
collector = collect ast_with_binds bv
sql = collector.compile ["hello", "world"]
assert_equal 'SELECT FROM "users" WHERE "users"."age" = hello AND "users"."name" = world', sql
end
end
end
end
|