aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/primitives/expression.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arel/primitives/expression.rb')
-rw-r--r--lib/arel/primitives/expression.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/arel/primitives/expression.rb b/lib/arel/primitives/expression.rb
new file mode 100644
index 0000000000..bf674cc9e1
--- /dev/null
+++ b/lib/arel/primitives/expression.rb
@@ -0,0 +1,44 @@
+module Arel
+ class Expression < Attribute
+ include Sql::Quoting
+
+ attr_reader :attribute, :function_sql
+ 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
+ end
+
+ module Transformations
+ def as(aliaz)
+ Expression.new(attribute, function_sql, aliaz, self)
+ end
+
+ def bind(new_relation)
+ new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self)
+ end
+
+ def to_attribute
+ Attribute.new(relation, @alias, :ancestor => self)
+ end
+ end
+ include Transformations
+
+ def to_sql(formatter = nil)
+ "#{function_sql}(#{attribute.to_sql})" + (@alias ? " AS #{quote_column_name(@alias)}" : '')
+ end
+
+ def aggregation?
+ true
+ end
+
+ def ==(other)
+ self.class == other.class and
+ attribute == other.attribute and
+ function_sql == other.function_sql and
+ ancestor == other.ancestor and
+ @alias == other.alias
+ end
+ end
+end \ No newline at end of file