Skip to content

Commit cbff1bc

Browse files
committed
Fix in [] to be false, in [] to be true
This is in response to discussion on 62207fa
1 parent 62207fa commit cbff1bc

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

lib/arel/visitors/to_sql.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,19 @@ def visit_Arel_Table o
370370
end
371371

372372
def visit_Arel_Nodes_In o
373-
"#{visit o.left} IN (#{visit o.right})"
373+
if Array === o.right && o.right.empty?
374+
'1=0'
375+
else
376+
"#{visit o.left} IN (#{visit o.right})"
377+
end
374378
end
375379

376380
def visit_Arel_Nodes_NotIn o
377-
"#{visit o.left} NOT IN (#{visit o.right})"
381+
if Array === o.right && o.right.empty?
382+
'1=1'
383+
else
384+
"#{visit o.left} NOT IN (#{visit o.right})"
385+
end
378386
end
379387

380388
def visit_Arel_Nodes_And o

test/visitors/test_to_sql.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,9 @@ def dispatch
167167
}
168168
end
169169

170-
it "should return IN () when empty right which is invalid SQL" do
170+
it "should return 1=0 when empty right which is always false" do
171171
node = @attr.in []
172-
@visitor.accept(node).must_be_like %{
173-
"users"."id" IN ()
174-
}
172+
@visitor.accept(node).must_equal '1=0'
175173
end
176174

177175
it 'can handle two dot ranges' do
@@ -255,11 +253,9 @@ def quote value, column = nil
255253
}
256254
end
257255

258-
it "should return NOT IN () when empty right which is invalid SQL" do
256+
it "should return 1=1 when empty right which is always true" do
259257
node = @attr.not_in []
260-
@visitor.accept(node).must_be_like %{
261-
"users"."id" NOT IN ()
262-
}
258+
@visitor.accept(node).must_equal '1=1'
263259
end
264260

265261
it 'can handle two dot ranges' do

0 commit comments

Comments
 (0)