diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-07-26 11:49:54 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-07-26 11:49:54 -0700 |
commit | 87b2f70fe143ec2abab5f3831ade706b3cf8707e (patch) | |
tree | 33fdc01820a9e5864b6e1cd676ec05ccb24174f8 /lib/arel/algebra | |
parent | 72e76c1757b9fa08545e0340c3c684e836a4bceb (diff) | |
download | rails-87b2f70fe143ec2abab5f3831ade706b3cf8707e.tar.gz rails-87b2f70fe143ec2abab5f3831ade706b3cf8707e.tar.bz2 rails-87b2f70fe143ec2abab5f3831ade706b3cf8707e.zip |
removing unhelful organization
Diffstat (limited to 'lib/arel/algebra')
-rw-r--r-- | lib/arel/algebra/predicates.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/arel/algebra/predicates.rb b/lib/arel/algebra/predicates.rb index 5b65b59c8c..8d3aef9d18 100644 --- a/lib/arel/algebra/predicates.rb +++ b/lib/arel/algebra/predicates.rb @@ -53,6 +53,12 @@ module Arel operation.eval(row) end end + + def to_sql(formatter = nil) + "(" + + predicates.map {|p| p.to_sql(formatter)}.join(" #{predicate_sql} ") + + ")" + end end class Any < Polyadic @@ -61,6 +67,8 @@ module Arel end def compounder; :any? end + + def predicate_sql; "OR" end end class All < Polyadic @@ -69,6 +77,8 @@ module Arel end def compounder; :all? end + + def predicate_sql; "AND" end end class Unary < Predicate @@ -89,6 +99,10 @@ module Arel def eval(row) operand.eval(row).send(operator) end + + def to_sql(formatter = nil) + "#{predicate_sql} (#{operand.to_sql(formatter)})" + end end class Not < Unary @@ -99,6 +113,8 @@ module Arel def eval(row) !operand.eval(row) end + + def predicate_sql; "NOT" end end class Binary < Unary @@ -121,12 +137,23 @@ module Arel def eval(row) operand1.eval(row).send(operator, operand2.eval(row)) end + + def to_sql(formatter = nil) + "#{operand1.to_sql} #{predicate_sql} #{operand1.format(operand2)}" + end + def to_sql(formatter = nil) + "#{operand1.to_sql} #{predicate_sql} #{operand1.format(operand2)}" + end end class CompoundPredicate < Binary def eval(row) eval "operand1.eval(row) #{operator} operand2.eval(row)" end + + def to_sql(formatter = nil) + "(#{operand1.to_sql(formatter)} #{predicate_sql} #{operand2.to_sql(formatter)})" + end end class And < CompoundPredicate @@ -135,13 +162,18 @@ module Arel end def operator; :and end + + def predicate_sql; "AND" end end class Or < CompoundPredicate def complement And.new(operand1.complement, operand2.complement) end + def operator; :or end + + def predicate_sql; "OR" end end class Equality < Binary @@ -156,6 +188,10 @@ module Arel end def operator; :== end + + def predicate_sql + operand2.equality_predicate_sql + end end class Inequality < Equality @@ -166,6 +202,10 @@ module Arel def eval(row) operand1.eval(row) != operand2.eval(row) end + + def predicate_sql + operand2.inequality_predicate_sql + end end class GreaterThanOrEqualTo < Binary @@ -174,6 +214,8 @@ module Arel end def operator; :>= end + + def predicate_sql; '>=' end end class GreaterThan < Binary @@ -182,6 +224,8 @@ module Arel end def operator; :> end + + def predicate_sql; '>' end end class LessThanOrEqualTo < Binary @@ -190,6 +234,8 @@ module Arel end def operator; :<= end + + def predicate_sql; '<=' end end class LessThan < Binary @@ -198,6 +244,8 @@ module Arel end def operator; :< end + + def predicate_sql; '<' end end class Match < Binary @@ -206,6 +254,8 @@ module Arel end def operator; :=~ end + + def predicate_sql; 'LIKE' end end class NotMatch < Binary @@ -216,6 +266,8 @@ module Arel def eval(row) operand1.eval(row) !~ operand2.eval(row) end + + def predicate_sql; 'NOT LIKE' end end class In < Binary @@ -226,6 +278,18 @@ module Arel def eval(row) operand2.eval(row).include?(operand1.eval(row)) end + + 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(formatter) + else + super + end + end + + def predicate_sql; operand2.inclusion_predicate_sql end end class NotIn < Binary @@ -236,6 +300,8 @@ module Arel def eval(row) !(operand2.eval(row).include?(operand1.eval(row))) end + + def predicate_sql; operand2.exclusion_predicate_sql end end end end |