Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Use 1 = 0 as the clause for a :value => [] case
Browse files Browse the repository at this point in the history
This case can be sent to the datastore as part of an invalid
branch for an OrOperation. This makes sure that IN () is not sent
to the database, since that's invalid syntax for SQL. The 1 = 0
is equivalent to IN () in this case.
  • Loading branch information
dbussink committed Oct 27, 2009
1 parent b543106 commit 0e5e389
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/dm-core/adapters/data_objects_adapter.rb
Expand Up @@ -617,6 +617,8 @@ def comparison_statement(comparison, qualify)
end
elsif @negated && comparison.slug == :in && !value.any?
return [ '1 = 1' ] # match everything
elsif comparison.slug == :in && !value.any?
return [ '1 = 0' ] # match nothing
end

operator = comparison_operator(comparison)
Expand Down
7 changes: 7 additions & 0 deletions lib/dm-core/spec/adapter_shared_spec.rb
Expand Up @@ -181,6 +181,13 @@ class ::Heffalump
Heffalump.all(:color.not => []).should == [ @red, @two, @five ]
end

it 'should be able to search for objects in an empty list and another OR condition (match none on the empty list)' do
Heffalump.all(:conditions => DataMapper::Query::Conditions::Operation.new(
:or,
DataMapper::Query::Conditions::Comparison.new(:in, Heffalump.properties[:color], []),
DataMapper::Query::Conditions::Comparison.new(:in, Heffalump.properties[:num_spots], [5]))).should == [ @five ]
end

it 'should be able to search for objects not included in an array of values' do
Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should be_include(@two)
end
Expand Down

0 comments on commit 0e5e389

Please sign in to comment.