diff options
author | Ernie Miller <ernie@metautonomo.us> | 2010-04-09 19:52:43 -0400 |
---|---|---|
committer | Ernie Miller <ernie@metautonomo.us> | 2010-05-07 13:07:43 -0400 |
commit | bd473344000bd538d353e4bc6d20ca8fff2e4704 (patch) | |
tree | c2586b9dc543aa828dcbefd2769e7a1d7485280a /lib/arel/engines | |
parent | 0433b054eebd5a53ff6c5f35383a6c0aed0015b2 (diff) | |
download | rails-bd473344000bd538d353e4bc6d20ca8fff2e4704.tar.gz rails-bd473344000bd538d353e4bc6d20ca8fff2e4704.tar.bz2 rails-bd473344000bd538d353e4bc6d20ca8fff2e4704.zip |
Support predicate complements and alternate not syntax (overload BasicObject#!)
Diffstat (limited to 'lib/arel/engines')
-rw-r--r-- | lib/arel/engines/memory/predicates.rb | 39 | ||||
-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 | 14 | ||||
-rw-r--r-- | lib/arel/engines/sql/primitives.rb | 8 |
5 files changed, 42 insertions, 27 deletions
diff --git a/lib/arel/engines/memory/predicates.rb b/lib/arel/engines/memory/predicates.rb index c0ee862626..0e88810e7d 100644 --- a/lib/arel/engines/memory/predicates.rb +++ b/lib/arel/engines/memory/predicates.rb @@ -13,41 +13,40 @@ module Arel end class Not < Unary - def operator; '!' end - end - - class CompoundPredicate < Binary def eval(row) - eval "operand1.eval(row) #{operator} operand2.eval(row)" + !operand.eval(row) end end - - class Or < CompoundPredicate - def operator; :or end - end - - class And < CompoundPredicate - def operator; :and end - end - class GroupedPredicate < Polyadic + class Polyadic < Predicate def eval(row) - group = additional_operands.inject([]) do |results, operand| - results << operator.new(operand1, operand) - end - group.send(compounder) do |operation| + predicates.send(compounder) do |operation| operation.eval(row) end end end - class Any < GroupedPredicate + class Any < Polyadic def compounder; :any? end end - class All < GroupedPredicate + class All < Polyadic def compounder; :all? end end + + class CompoundPredicate < Binary + def eval(row) + eval "operand1.eval(row) #{operator} operand2.eval(row)" + end + end + + class Or < CompoundPredicate + def operator; :or end + end + + class And < CompoundPredicate + def operator; :and end + end class Equality < Binary def operator; :== end diff --git a/lib/arel/engines/sql/core_extensions/nil_class.rb b/lib/arel/engines/sql/core_extensions/nil_class.rb index 3f70677ba6..d4bb0e4c33 100644 --- a/lib/arel/engines/sql/core_extensions/nil_class.rb +++ b/lib/arel/engines/sql/core_extensions/nil_class.rb @@ -4,6 +4,10 @@ module Arel def equality_predicate_sql 'IS' end + + def inequality_predicate_sql + 'IS NOT' + end def not_predicate_sql 'IS NOT' diff --git a/lib/arel/engines/sql/core_extensions/object.rb b/lib/arel/engines/sql/core_extensions/object.rb index b71ef29fd5..5415c84706 100644 --- a/lib/arel/engines/sql/core_extensions/object.rb +++ b/lib/arel/engines/sql/core_extensions/object.rb @@ -8,6 +8,10 @@ module Arel def equality_predicate_sql '=' end + + def inequality_predicate_sql + '!=' + end def not_predicate_sql '!=' diff --git a/lib/arel/engines/sql/predicates.rb b/lib/arel/engines/sql/predicates.rb index b459168620..df8700a500 100644 --- a/lib/arel/engines/sql/predicates.rb +++ b/lib/arel/engines/sql/predicates.rb @@ -30,21 +30,19 @@ module Arel def predicate_sql; "AND" end end - class GroupedPredicate < Polyadic + class Polyadic < Predicate def to_sql(formatter = nil) "(" + - additional_operands.inject([]) { |predicates, operand| - predicates << operator.new(operand1, operand).to_sql(formatter) - }.join(" #{predicate_sql} ") + + predicates.map {|p| p.to_sql(formatter)}.join(" #{predicate_sql} ") + ")" end end - class Any < GroupedPredicate + class Any < Polyadic def predicate_sql; "OR" end end - class All < GroupedPredicate + class All < Polyadic def predicate_sql; "AND" end end @@ -55,7 +53,9 @@ module Arel end class Inequality < Equality - def predicate_sql; '!=' end + def predicate_sql + operand2.inequality_predicate_sql + end end class GreaterThanOrEqualTo < Binary diff --git a/lib/arel/engines/sql/primitives.rb b/lib/arel/engines/sql/primitives.rb index 78e1ed7f0b..41769fa510 100644 --- a/lib/arel/engines/sql/primitives.rb +++ b/lib/arel/engines/sql/primitives.rb @@ -29,10 +29,18 @@ module Arel def inclusion_predicate_sql value.inclusion_predicate_sql end + + def exclusion_predicate_sql + value.exclusion_predicate_sql + end def equality_predicate_sql value.equality_predicate_sql end + + def inequality_predicate_sql + value.inequality_predicate_sql + end def not_predicate_sql value.not_predicate_sql |