diff options
Diffstat (limited to 'lib/arel/algebra')
-rw-r--r-- | lib/arel/algebra/attributes/attribute.rb | 12 | ||||
-rw-r--r-- | lib/arel/algebra/expression.rb | 33 | ||||
-rw-r--r-- | lib/arel/algebra/ordering.rb | 13 | ||||
-rw-r--r-- | lib/arel/algebra/value.rb | 24 |
4 files changed, 76 insertions, 6 deletions
diff --git a/lib/arel/algebra/attributes/attribute.rb b/lib/arel/algebra/attributes/attribute.rb index 9206b228c8..8a7993f284 100644 --- a/lib/arel/algebra/attributes/attribute.rb +++ b/lib/arel/algebra/attributes/attribute.rb @@ -270,5 +270,17 @@ module Arel end end include Types + + def column + original_relation.column_for(self) + end + + def format(object) + object.to_sql(Sql::Attribute.new(self)) + end + + def to_sql(formatter = Sql::WhereCondition.new(relation)) + formatter.attribute self + end end end diff --git a/lib/arel/algebra/expression.rb b/lib/arel/algebra/expression.rb index 183de3c005..5a93ca9735 100644 --- a/lib/arel/algebra/expression.rb +++ b/lib/arel/algebra/expression.rb @@ -16,6 +16,10 @@ module Arel true end + def to_sql(formatter = Sql::SelectClause.new(relation)) + formatter.expression self + end + module Transformations def as(aliaz) self.class.new(attribute, aliaz, self) @@ -32,11 +36,28 @@ module Arel include Transformations end - class Count < Expression; end - class Distinct < Expression; end - class Sum < Expression; end - class Maximum < Expression; end - class Minimum < Expression; end - class Average < Expression; end + class Count < Expression + def function_sql; 'COUNT' end + end + + class Distinct < Expression + def function_sql; 'DISTINCT' end + end + + class Sum < Expression + def function_sql; 'SUM' end + end + + class Maximum < Expression + def function_sql; 'MAX' end + end + + class Minimum < Expression + def function_sql; 'MIN' end + end + + class Average < Expression + def function_sql; 'AVG' end + end end diff --git a/lib/arel/algebra/ordering.rb b/lib/arel/algebra/ordering.rb index 984327aaf7..8ed3e1e6e6 100644 --- a/lib/arel/algebra/ordering.rb +++ b/lib/arel/algebra/ordering.rb @@ -13,10 +13,23 @@ module Arel def == other super || (self.class === other && attribute == other.attribute) end + + def eval(row1, row2) + (attribute.eval(row1) <=> attribute.eval(row2)) * direction + end + + def to_sql(formatter = Sql::OrderClause.new(relation)) + formatter.ordering self + end end class Ascending < Ordering + def direction; 1 end + def direction_sql; 'ASC' end end + class Descending < Ordering + def direction_sql; 'DESC' end + def direction; -1 end end end diff --git a/lib/arel/algebra/value.rb b/lib/arel/algebra/value.rb index d9fc2e061c..90787fa5ea 100644 --- a/lib/arel/algebra/value.rb +++ b/lib/arel/algebra/value.rb @@ -21,5 +21,29 @@ module Arel def to_ordering self end + + 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 to_sql(formatter = Sql::WhereCondition.new(relation)) + formatter.value value + end + + def format(object) + object.to_sql(Sql::Value.new(relation)) + end end end |