aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/relations/aggregation.rb
blob: 7e9cdfe6125cec34761a00c854fea2c0caf9a0b0 (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
module Arel
  class Aggregation < Compound
    include Recursion::BaseCase

    def initialize(relation)
      @relation = relation
    end
    
    def selects
      []
    end
  
    def table_sql(formatter = Sql::TableReference.new(relation))
      formatter.select relation.select_sql, self
    end
  
    def attributes
      @attributes ||= relation.attributes.collect(&:to_attribute).collect { |a| a.bind(self) }
    end
    
    def name
      relation.name + '_aggregation'
    end
    
    def ==(other)
      Aggregation   === other and
      self.relation ==  other.relation
    end
  end
  
  class Relation
    def externalize
      @externalized ||= aggregation?? Aggregation.new(self) : self
    end
    
    def aggregation?
      false
    end
  end
end