Class: OlapReport::Cube::Aggregation::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/olap_report/cube/aggregation/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, levels) ⇒ Table

Returns a new instance of Table.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/olap_report/cube/aggregation/table.rb', line 7

def initialize(model, levels)
  raise ArgumentError unless model && levels

  @model = model

  @levels = levels.each_with_object([]) do |(dim,lvl),acc|
    acc << dimension(dim)[lvl]
  end
end

Instance Attribute Details

#levelsObject (readonly)

Returns the value of attribute levels.



3
4
5
# File 'lib/olap_report/cube/aggregation/table.rb', line 3

def levels
  @levels
end

Instance Method Details

#==(obj) ⇒ Object



39
40
41
# File 'lib/olap_report/cube/aggregation/table.rb', line 39

def ==(obj)
  obj.is_a?(self.class) && self.levels == obj.levels
end

#aggregate_table!Object

Creates tables with aggregated data and fills them with actual values



18
19
20
21
# File 'lib/olap_report/cube/aggregation/table.rb', line 18

def aggregate_table!
  adapter.create_aggregated_table self
  connection.execute fill_sql
end

#build_update_statements_for_measuresObject



55
56
57
58
59
60
# File 'lib/olap_report/cube/aggregation/table.rb', line 55

def build_update_statements_for_measures
  measures.each_with_object([]) do |m,acc|
    stmt = adapter.measure_update_sql(m, measures)
    stmt && acc << stmt
  end
end

#columnsObject



35
36
37
# File 'lib/olap_report/cube/aggregation/table.rb', line 35

def columns
  level_column_definitions + measure_column_definitions
end

#slice(msrs) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/olap_report/cube/aggregation/table.rb', line 43

def slice(msrs)
  relation = levels.inject(model) do |rel,l|
    rel.select quote_column_name(l.name)
  end

  relation = msrs.inject(relation) do |rel,m|
    rel.select quote_column_name(m)
  end

  relation.from table_name
end

#table_nameObject



31
32
33
# File 'lib/olap_report/cube/aggregation/table.rb', line 31

def table_name
  @table_name ||= ([@model.table_name, 'by'] << levels.map(&:name).sort.join('_and_')).join('_')
end

#update!(start_id = nil) ⇒ Object

Updates already existing aggregations with values from new records



24
25
26
27
28
29
# File 'lib/olap_report/cube/aggregation/table.rb', line 24

def update!(start_id=nil)
  query = fill_sql do |rel|
    rel.where("#{quote_table_column(@model.primary_key)} > ?", start_id)
  end
  adapter.update_aggregated_table query, build_update_statements_for_measures
end