Class: ActiveWarehouse::Aggregate::RolapAggregate

Inherits:
Aggregate
  • Object
show all
Includes:
RolapCommon
Defined in:
lib/active_warehouse/aggregate/rolap_aggregate.rb

Overview

Basic implementation of a ROLAP engine that stores all possible combinations of fact and dimensional values for a specific cube.

Instance Attribute Summary

Attributes inherited from Aggregate

#cube_class

Instance Method Summary collapse

Methods included from RolapCommon

#aggregate_fields, #aggregated_fact_column_sql, #dimensions_to_columns, #flat_table_name, #rollup_table_name, #tables_and_joins

Methods inherited from Aggregate

#initialize

Constructor Details

This class inherits a constructor from ActiveWarehouse::Aggregate::Aggregate

Instance Method Details

#populate(options = {}) ⇒ Object

Build and populate the data store



11
12
13
# File 'lib/active_warehouse/aggregate/rolap_aggregate.rb', line 11

def populate(options={})
  populate_rollup_cube
end

#query(*args) ⇒ Object

Query the aggregate, returning a QueryResult object



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_warehouse/aggregate/rolap_aggregate.rb', line 16

def query(*args)
  options = parse_query_args(*args)
  
  column_dimension_name = options[:column_dimension_name]
  column_hierarchy_name = options[:column_hierarchy_name]
  row_dimension_name = options[:row_dimension_name]
  row_hierarchy_name = options[:row_hierarchy_name]
  conditions = options[:conditions]
  cstage = options[:cstage]
  rstage = options[:rstage]
  filters = options[:filters]
  
  column_dimension = fact_class.dimension_class(column_dimension_name)
  column_hierarchy = column_dimension.hierarchy(column_hierarchy_name)
  row_dimension = fact_class.dimension_class(row_dimension_name)
  row_hierarchy = row_dimension.hierarchy(row_hierarchy_name)
  current_column_name = column_hierarchy[cstage]
  current_row_name = row_hierarchy[rstage]
  full_column_name = "#{column_dimension_name}_#{current_column_name}"
  full_row_name = "#{row_dimension_name}_#{current_row_name}"

  # build the SQL query
  sql = ''
  sql += 'SELECT '
  sql += "#{full_column_name} AS #{current_column_name},"
  sql += "#{full_row_name} AS #{current_row_name},"
  sql += aggregate_fields.collect{|c| "#{c.label_for_table} as '#{c.label}'"}.join(",")
  sql += " FROM #{rollup_table_name} "

  # build the where clause
  where_clause = []
  0.upto(column_hierarchy.length - 1) do |stage|
    column_name = column_hierarchy[stage]
    name = "#{column_dimension_name}_#{column_name}"
    filter_value = filters.delete(column_name)
    if filter_value
      where_clause << "#{name} = '#{filter_value}'" # TODO: protect from
                                                    # SQL injection
    else
      where_clause << "#{name} is null" if stage > cstage
    end
  end
  0.upto(row_hierarchy.length - 1) do |stage|
    row_name = row_hierarchy[stage]
    name = "#{row_dimension_name}_#{row_name}"
    filter_value = filters.delete(row_name)
    if filter_value
      where_clause << "#{name} = '#{filter_value}'" # TODO: protect from
                                                    # SQL injection
    else
      where_clause << "#{name} is null" if stage > rstage
    end
  end
  where_clause << "#{full_column_name} is not null"
  where_clause << "#{full_row_name} is not null"
  filters.each do |key, value|
    dimension_name, column = key.split('.')
    where_clause << "#{dimension_name}_#{column} = '#{value}'" # TODO: protect from SQL injection
  end
  sql += %Q( WHERE #{where_clause.join(" AND ")} ) if where_clause.length > 0
  
   if conditions
      sql += "\n WHERE\n" unless sql =~ /WHERE/i
      sql += conditions
    end
  
  # execute the query and return the results as a CubeQueryResult object
  result = ActiveWarehouse::CubeQueryResult.new(
    aggregate_fields
  )
  rows = connection.select_all(sql)
#        fact_column_names = fact_class.aggregate_fields.collect{|f| f.to_s}
  rows.each do |row|
    result.add_data(row.delete(current_row_name.to_s),
                    row.delete(current_column_name.to_s),
                    row) # the rest of the members of row are the fact columns
  end
  result
end