diff options
-rw-r--r-- | lib/arel/algebra/predicates.rb | 66 | ||||
-rw-r--r-- | lib/arel/engines/sql.rb | 1 | ||||
-rw-r--r-- | lib/arel/engines/sql/predicates.rb | 103 |
3 files changed, 66 insertions, 104 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 diff --git a/lib/arel/engines/sql.rb b/lib/arel/engines/sql.rb index a7721eb909..686078826c 100644 --- a/lib/arel/engines/sql.rb +++ b/lib/arel/engines/sql.rb @@ -2,7 +2,6 @@ require 'arel/engines/sql/attributes' require 'arel/engines/sql/engine' require 'arel/engines/sql/relations' require 'arel/engines/sql/primitives' -require 'arel/engines/sql/predicates' require 'arel/engines/sql/formatters' require 'arel/engines/sql/core_extensions' require 'arel/engines/sql/christener' diff --git a/lib/arel/engines/sql/predicates.rb b/lib/arel/engines/sql/predicates.rb deleted file mode 100644 index 74a36d77c9..0000000000 --- a/lib/arel/engines/sql/predicates.rb +++ /dev/null @@ -1,103 +0,0 @@ -module Arel - module Predicates - class Binary < Unary - def to_sql(formatter = nil) - "#{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) - "(#{operand1.to_sql(formatter)} #{predicate_sql} #{operand2.to_sql(formatter)})" - end - end - - class Or < CompoundPredicate - def predicate_sql; "OR" end - end - - class And < CompoundPredicate - def predicate_sql; "AND" end - end - - class Polyadic < Predicate - def to_sql(formatter = nil) - "(" + - predicates.map {|p| p.to_sql(formatter)}.join(" #{predicate_sql} ") + - ")" - end - end - - class Any < Polyadic - def predicate_sql; "OR" end - end - - class All < Polyadic - def predicate_sql; "AND" end - end - - class Equality < Binary - def predicate_sql - operand2.equality_predicate_sql - end - end - - class Inequality < Equality - def predicate_sql - operand2.inequality_predicate_sql - end - end - - class GreaterThanOrEqualTo < Binary - def predicate_sql; '>=' end - end - - class GreaterThan < Binary - def predicate_sql; '>' end - end - - class LessThanOrEqualTo < Binary - def predicate_sql; '<=' end - end - - class LessThan < Binary - def predicate_sql; '<' end - end - - class Match < Binary - def predicate_sql; 'LIKE' end - end - - class NotMatch < Binary - def predicate_sql; 'NOT LIKE' end - end - - class In < Binary - 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 - def predicate_sql; operand2.exclusion_predicate_sql end - end - end -end |