diff options
-rw-r--r-- | lib/arel/engines/sql/core_extensions/nil_class.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/sql/core_extensions/object.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/sql/predicates.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/sql/primitives.rb | 4 | ||||
-rw-r--r-- | spec/engines/sql/unit/predicates/not_spec.rb | 75 |
5 files changed, 90 insertions, 1 deletions
diff --git a/lib/arel/engines/sql/core_extensions/nil_class.rb b/lib/arel/engines/sql/core_extensions/nil_class.rb index c3dbc8cd76..3f70677ba6 100644 --- a/lib/arel/engines/sql/core_extensions/nil_class.rb +++ b/lib/arel/engines/sql/core_extensions/nil_class.rb @@ -5,6 +5,10 @@ module Arel 'IS' end + def not_predicate_sql + 'IS NOT' + end + NilClass.send(:include, self) end end diff --git a/lib/arel/engines/sql/core_extensions/object.rb b/lib/arel/engines/sql/core_extensions/object.rb index 9f15dff771..b71ef29fd5 100644 --- a/lib/arel/engines/sql/core_extensions/object.rb +++ b/lib/arel/engines/sql/core_extensions/object.rb @@ -9,6 +9,10 @@ module Arel '=' end + def not_predicate_sql + '!=' + end + Object.send(:include, self) end end diff --git a/lib/arel/engines/sql/predicates.rb b/lib/arel/engines/sql/predicates.rb index 3756231a46..e40240eec5 100644 --- a/lib/arel/engines/sql/predicates.rb +++ b/lib/arel/engines/sql/predicates.rb @@ -27,7 +27,9 @@ module Arel end class Not < Binary - def predicate_sql; '!=' end + def predicate_sql + operand2.not_predicate_sql + end end class GreaterThanOrEqualTo < Binary diff --git a/lib/arel/engines/sql/primitives.rb b/lib/arel/engines/sql/primitives.rb index 666579331a..78e1ed7f0b 100644 --- a/lib/arel/engines/sql/primitives.rb +++ b/lib/arel/engines/sql/primitives.rb @@ -34,6 +34,10 @@ module Arel value.equality_predicate_sql end + def not_predicate_sql + value.not_predicate_sql + end + def to_sql(formatter = Sql::WhereCondition.new(relation)) formatter.value value end diff --git a/spec/engines/sql/unit/predicates/not_spec.rb b/spec/engines/sql/unit/predicates/not_spec.rb new file mode 100644 index 0000000000..b124d4c80b --- /dev/null +++ b/spec/engines/sql/unit/predicates/not_spec.rb @@ -0,0 +1,75 @@ +require 'spec_helper' + +module Arel + module Predicates + describe Equality do + before do + @relation1 = Arel::Table.new(:users) + @relation2 = Arel::Table.new(:photos) + @attribute1 = @relation1[:id] + @attribute2 = @relation2[:user_id] + end + + describe '#to_sql' do + describe 'when relating to a non-nil value' do + it "manufactures a not predicate" do + sql = Not.new(@attribute1, @attribute2).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` != `photos`.`user_id`}) + end + + adapter_is :oracle do + sql.should be_like(%Q{"USERS"."ID" != "PHOTOS"."USER_ID"}) + end + + adapter_is_not :mysql, :oracle do + sql.should be_like(%Q{"users"."id" != "photos"."user_id"}) + end + end + end + + describe 'when relation to a nil value' do + before do + @nil = nil + end + + it "manufactures an is null predicate" do + sql = Not.new(@attribute1, @nil).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` IS NOT NULL}) + end + + adapter_is :oracle do + sql.should be_like(%Q{"USERS"."ID" IS NOT NULL}) + end + + adapter_is_not :mysql, :oracle do + sql.should be_like(%Q{"users"."id" IS NOT NULL}) + end + end + end + + describe "when relating to a nil Value" do + it "manufactures an IS NULL predicate" do + value = nil.bind(@relation1) + sql = Not.new(@attribute1, value).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{`users`.`id` IS NOT NULL}) + end + + adapter_is :oracle do + sql.should be_like(%Q{"USERS"."ID" IS NOT NULL}) + end + + adapter_is_not :mysql, :oracle do + sql.should be_like(%Q{"users"."id" IS NOT NULL}) + end + end + end + end + end + end +end |