From 0afcfa27c9f386ca7c190cd1f66db1cdd9971f3b Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Tue, 30 Mar 2010 09:52:22 -0400 Subject: Rename Attribute Not -> Inequality and add a Not predicate (complement) --- lib/arel/algebra/attributes/attribute.rb | 2 +- lib/arel/algebra/predicates.rb | 11 ++++++++++- lib/arel/engines/memory/predicates.rb | 2 +- lib/arel/engines/sql/predicates.rb | 20 ++++++++++++++------ 4 files changed, 26 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/arel/algebra/attributes/attribute.rb b/lib/arel/algebra/attributes/attribute.rb index 5689d69b02..0266b38db3 100644 --- a/lib/arel/algebra/attributes/attribute.rb +++ b/lib/arel/algebra/attributes/attribute.rb @@ -84,7 +84,7 @@ module Arel module Predications methods = { :eq => "Equality", - :not => "Not", + :noteq => "Inequality", :lt => "LessThan", :lteq => "LessThanOrEqualTo", :gt => "GreaterThan", diff --git a/lib/arel/algebra/predicates.rb b/lib/arel/algebra/predicates.rb index ea1f771abb..43606139b0 100644 --- a/lib/arel/algebra/predicates.rb +++ b/lib/arel/algebra/predicates.rb @@ -8,6 +8,10 @@ module Arel def and(other_predicate) And.new(self, other_predicate) end + + def not + Not.new(self) + end end class Grouped < Predicate @@ -35,6 +39,11 @@ module Arel a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h } end end + + class Unary < Predicate + attributes :operand + deriving :initialize, :== + end class Binary < Predicate attributes :operand1, :operand2 @@ -59,7 +68,7 @@ module Arel end end - class Not < Equality; end + class Inequality < Equality; end class GreaterThanOrEqualTo < Binary; end class GreaterThan < Binary; end class LessThanOrEqualTo < Binary; end diff --git a/lib/arel/engines/memory/predicates.rb b/lib/arel/engines/memory/predicates.rb index b8642136d8..d0963e2f74 100644 --- a/lib/arel/engines/memory/predicates.rb +++ b/lib/arel/engines/memory/predicates.rb @@ -10,7 +10,7 @@ module Arel def operator; :== end end - class Not < Equality + class Inequality < Equality def eval(row) operand1.eval(row) != operand2.eval(row) end diff --git a/lib/arel/engines/sql/predicates.rb b/lib/arel/engines/sql/predicates.rb index 7fac09e750..29bc74c605 100644 --- a/lib/arel/engines/sql/predicates.rb +++ b/lib/arel/engines/sql/predicates.rb @@ -5,6 +5,16 @@ module Arel "#{operand1.to_sql} #{predicate_sql} #{operand1.format(operand2)}" end end + + class Unary < Predicate + def to_sql(formatter = nil) + "#{predicate_sql} (#{operand.to_sql(formatter)})" + end + end + + class Not < Unary + def predicate_sql; "NOT" end + end class CompoundPredicate < Binary def to_sql(formatter = nil) @@ -44,10 +54,8 @@ module Arel end end - class Not < Binary - def predicate_sql - operand2.not_predicate_sql - end + class Inequality < Equality + def predicate_sql; '!=' end end class GreaterThanOrEqualTo < Binary @@ -75,11 +83,11 @@ module Arel end class In < Binary - def to_sql + def to_sql(formatter = nil) if operand2.is_a?(Range) && operand2.exclude_end? GreaterThanOrEqualTo.new(operand1, operand2.begin).and( LessThan.new(operand1, operand2.end) - ).to_sql + ).to_sql(formatter) else super end -- cgit v1.2.3