Skip to content

Commit 0f48f79

Browse files
authored
Merge pull request #2008 from kamipo/import_visit_arel_nodes_in
Import `visit_Arel_Nodes_{In,NotIn}` from Rails code base
2 parents 9640407 + a834a42 commit 0f48f79

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

lib/arel/visitors/oracle.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,59 @@ def visit_Arel_Nodes_Except(o, collector)
8686
collector << " )"
8787
end
8888

89+
def visit_Arel_Nodes_In(o, collector)
90+
attr, values = o.left, o.right
91+
92+
if Array === values
93+
unless values.empty?
94+
values.delete_if { |value| unboundable?(value) }
95+
end
96+
97+
return collector << "1=0" if values.empty?
98+
end
99+
100+
in_clause_length = @connection.in_clause_length
101+
102+
if !Array === values || values.length <= in_clause_length
103+
visit(attr, collector) << " IN ("
104+
visit(values, collector) << ")"
105+
else
106+
collector << "("
107+
values.each_slice(in_clause_length).each_with_index do |valuez, i|
108+
collector << " OR " unless i == 0
109+
visit(attr, collector) << " IN ("
110+
visit(valuez, collector) << ")"
111+
end
112+
collector << ")"
113+
end
114+
end
115+
116+
def visit_Arel_Nodes_NotIn(o, collector)
117+
attr, values = o.left, o.right
118+
119+
if Array === values
120+
unless values.empty?
121+
values.delete_if { |value| unboundable?(value) }
122+
end
123+
124+
return collector << "1=1" if values.empty?
125+
end
126+
127+
in_clause_length = @connection.in_clause_length
128+
129+
if !Array === values || values.length <= in_clause_length
130+
visit(attr, collector) << " NOT IN ("
131+
visit(values, collector) << ")"
132+
else
133+
values.each_slice(in_clause_length).each_with_index do |valuez, i|
134+
collector << " AND " unless i == 0
135+
visit(attr, collector) << " NOT IN ("
136+
visit(valuez, collector) << ")"
137+
end
138+
collector
139+
end
140+
end
141+
89142
def visit_Arel_Nodes_UpdateStatement(o, collector)
90143
# Oracle does not allow ORDER BY/LIMIT in UPDATEs.
91144
if o.orders.any? && o.limit.nil?

lib/arel/visitors/oracle12.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,59 @@ def visit_Arel_Nodes_Except(o, collector)
3939
collector << " )"
4040
end
4141

42+
def visit_Arel_Nodes_In(o, collector)
43+
attr, values = o.left, o.right
44+
45+
if Array === values
46+
unless values.empty?
47+
values.delete_if { |value| unboundable?(value) }
48+
end
49+
50+
return collector << "1=0" if values.empty?
51+
end
52+
53+
in_clause_length = @connection.in_clause_length
54+
55+
if !Array === values || values.length <= in_clause_length
56+
visit(attr, collector) << " IN ("
57+
visit(values, collector) << ")"
58+
else
59+
collector << "("
60+
values.each_slice(in_clause_length).each_with_index do |valuez, i|
61+
collector << " OR " unless i == 0
62+
visit(attr, collector) << " IN ("
63+
visit(valuez, collector) << ")"
64+
end
65+
collector << ")"
66+
end
67+
end
68+
69+
def visit_Arel_Nodes_NotIn(o, collector)
70+
attr, values = o.left, o.right
71+
72+
if Array === values
73+
unless values.empty?
74+
values.delete_if { |value| unboundable?(value) }
75+
end
76+
77+
return collector << "1=1" if values.empty?
78+
end
79+
80+
in_clause_length = @connection.in_clause_length
81+
82+
if !Array === values || values.length <= in_clause_length
83+
visit(attr, collector) << " NOT IN ("
84+
visit(values, collector) << ")"
85+
else
86+
values.each_slice(in_clause_length).each_with_index do |valuez, i|
87+
collector << " AND " unless i == 0
88+
visit(attr, collector) << " NOT IN ("
89+
visit(valuez, collector) << ")"
90+
end
91+
collector
92+
end
93+
end
94+
4295
def visit_Arel_Nodes_UpdateStatement(o, collector)
4396
# Oracle does not allow ORDER BY/LIMIT in UPDATEs.
4497
if o.orders.any? && o.limit.nil?

0 commit comments

Comments
 (0)