aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/algebra/expression.rb
blob: 875498c28282e1a1ebb7cabdb765063efca6a6d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Arel
  class Expression < Attribute
    attributes :attribute, :alias, :ancestor
    deriving :==
    delegate :relation, :to => :attribute
    alias_method :name, :alias

    def initialize(attribute, aliaz = nil, ancestor = nil)
      @attribute, @alias, @ancestor = attribute, aliaz, ancestor
    end

    def aggregation?
      true
    end

    def inspect
      "<#{self.class.name} #{attribute.inspect}>"
    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; end
  class Distinct < Expression; end
  class Sum      < Expression; end
  class Maximum  < Expression; end
  class Minimum  < Expression; end
  class Average  < Expression; end
end