diff options
author | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 14:46:08 -0400 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 14:46:08 -0400 |
commit | 892337509b2bd269920dc567bc48c6a28c7222d2 (patch) | |
tree | 442843e51bce0a9bda8b1a83d30252a39d37e2d3 /lib | |
parent | b0a45d52fdb7d8ce564f4dc2013bc790f98f1da3 (diff) | |
download | rails-892337509b2bd269920dc567bc48c6a28c7222d2.tar.gz rails-892337509b2bd269920dc567bc48c6a28c7222d2.tar.bz2 rails-892337509b2bd269920dc567bc48c6a28c7222d2.zip |
removed function_sql in favor of polymorphism
Conflicts:
lib/arel/algebra/primitives/attribute.rb
lib/arel/algebra/primitives/expression.rb
spec/arel/unit/primitives/expression_spec.rb
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/algebra/primitives/attribute.rb | 10 | ||||
-rw-r--r-- | lib/arel/algebra/primitives/expression.rb | 18 | ||||
-rw-r--r-- | lib/arel/engines/sql/primitives.rb | 36 |
3 files changed, 48 insertions, 16 deletions
diff --git a/lib/arel/algebra/primitives/attribute.rb b/lib/arel/algebra/primitives/attribute.rb index 5e216770e4..943c5b030e 100644 --- a/lib/arel/algebra/primitives/attribute.rb +++ b/lib/arel/algebra/primitives/attribute.rb @@ -109,23 +109,23 @@ module Arel module Expressions def count(distinct = false) - distinct ? Expression.new(self, "DISTINCT").count : Expression.new(self, "COUNT") + distinct ? Distinct.new(self).count : Count.new(self) end def sum - Expression.new(self, "SUM") + Sum.new(self) end def maximum - Expression.new(self, "MAX") + Maximum.new(self) end def minimum - Expression.new(self, "MIN") + Minimum.new(self) end def average - Expression.new(self, "AVG") + Average.new(self) end end include Expressions diff --git a/lib/arel/algebra/primitives/expression.rb b/lib/arel/algebra/primitives/expression.rb index b67a5e1f8e..989397720c 100644 --- a/lib/arel/algebra/primitives/expression.rb +++ b/lib/arel/algebra/primitives/expression.rb @@ -1,12 +1,12 @@ module Arel class Expression < Attribute - attributes :attribute, :function_sql, :alias, :ancestor + attributes :attribute, :alias, :ancestor deriving :== delegate :relation, :to => :attribute alias_method :name, :alias - def initialize(attribute, function_sql, aliaz = nil, ancestor = nil) - @attribute, @function_sql, @alias, @ancestor = attribute, function_sql, aliaz, ancestor + def initialize(attribute, aliaz = nil, ancestor = nil) + @attribute, @alias, @ancestor = attribute, aliaz, ancestor end def aggregation? @@ -15,11 +15,11 @@ module Arel module Transformations def as(aliaz) - Expression.new(attribute, function_sql, aliaz, self) + self.class.new(attribute, aliaz, self) end def bind(new_relation) - new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self) + new_relation == relation ? self : self.class.new(attribute.bind(new_relation), @alias, self) end def to_attribute @@ -28,4 +28,12 @@ module Arel end 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 end + diff --git a/lib/arel/engines/sql/primitives.rb b/lib/arel/engines/sql/primitives.rb index 5544d63710..c4968558a2 100644 --- a/lib/arel/engines/sql/primitives.rb +++ b/lib/arel/engines/sql/primitives.rb @@ -13,12 +13,6 @@ module Arel end end - class Expression < Attribute - def to_sql(formatter = Sql::SelectClause.new(relation)) - formatter.expression self - end - end - class Value def to_sql(formatter = Sql::WhereCondition.new(relation)) formatter.value value @@ -28,4 +22,34 @@ module Arel object.to_sql(Sql::Value.new(relation)) end end + + class Expression < Attribute + def to_sql(formatter = Sql::SelectClause.new(relation)) + formatter.expression self + end + 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
\ No newline at end of file |