aboutsummaryrefslogblamecommitdiffstats
path: root/lib/arel/algebra/expression.rb
blob: 5a93ca9735e7b6a7e0cbe182808687eb77a0e8bf (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
           
                              

                          
 
                                                          





                                                                              
       
 


                    
 



                                                           

                          
                                              
         
 
                            
                                                                                                    
         
 
                                



                                                          
     
 






















                                    
   
 
module Arel
  class Expression < Attribute
    attr_reader :attribute
    alias :name :alias

    def initialize(attribute, aliaz = nil, ancestor = nil)
      super(attribute.relation, aliaz, :alias => aliaz, :ancestor => ancestor)
      @attribute = attribute
    end

    def == other
      super && Expression === other && attribute == other.attribute
    end

    def aggregation?
      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)
      end

      def bind(new_relation)
        new_relation == relation ? self : self.class.new(attribute.bind(new_relation), @alias, self)
      end

      def to_attribute(relation)
        Attribute.new(relation, @alias, :ancestor => self)
      end
    end
    include Transformations
  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