diff options
author | Ernie Miller <ernie@erniemiller.org> | 2012-06-14 11:45:51 -0400 |
---|---|---|
committer | Ernie Miller <ernie@erniemiller.org> | 2012-06-14 11:45:51 -0400 |
commit | cbff1bcf385aba876933b2b4569826e9bc46183c (patch) | |
tree | 06c9fb457daaa8a73be891978d7d410894a79eab | |
parent | 62207faee92c820da9fbc9cc6fe785461c2d826b (diff) | |
download | rails-cbff1bcf385aba876933b2b4569826e9bc46183c.tar.gz rails-cbff1bcf385aba876933b2b4569826e9bc46183c.tar.bz2 rails-cbff1bcf385aba876933b2b4569826e9bc46183c.zip |
Fix in [] to be false, in [] to be true
This is in response to discussion on 62207fa
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 12 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 12 |
2 files changed, 14 insertions, 10 deletions
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index cb8df8329a..a350daa3da 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -370,11 +370,19 @@ key on UpdateManager using UpdateManager#key= end def visit_Arel_Nodes_In o - "#{visit o.left} IN (#{visit o.right})" + if Array === o.right && o.right.empty? + '1=0' + else + "#{visit o.left} IN (#{visit o.right})" + end end def visit_Arel_Nodes_NotIn o - "#{visit o.left} NOT IN (#{visit o.right})" + if Array === o.right && o.right.empty? + '1=1' + else + "#{visit o.left} NOT IN (#{visit o.right})" + end end def visit_Arel_Nodes_And o diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index 58eec1f63b..1caedacd45 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -167,11 +167,9 @@ module Arel } end - it "should return IN () when empty right which is invalid SQL" do + it "should return 1=0 when empty right which is always false" do node = @attr.in [] - @visitor.accept(node).must_be_like %{ - "users"."id" IN () - } + @visitor.accept(node).must_equal '1=0' end it 'can handle two dot ranges' do @@ -255,11 +253,9 @@ module Arel } end - it "should return NOT IN () when empty right which is invalid SQL" do + it "should return 1=1 when empty right which is always true" do node = @attr.not_in [] - @visitor.accept(node).must_be_like %{ - "users"."id" NOT IN () - } + @visitor.accept(node).must_equal '1=1' end it 'can handle two dot ranges' do |