Class: OlapReport::Cube::Adapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/olap_report/cube/adapters/abstract_adapter.rb

Direct Known Subclasses

Mysql2Adapter, PostgreSQLAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ AbstractAdapter

Returns a new instance of AbstractAdapter.

Raises:

  • (ArgumentError)


5
6
7
8
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 5

def initialize(model)
  raise ArgumentError unless model
  @connection = model.connection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 10

def method_missing(meth, *args, &block)
  if @connection.respond_to?(meth)
    @connection.public_send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 3

def connection
  @connection
end

Instance Method Details

#create_aggregated_table(table) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 19

def create_aggregated_table(table)
  connection.create_table table.table_name, id: false, force: true do |t|
    table.columns.each do |column_obj|
      options = {}
      [:default, :limit, :scale, :precision, :null, :primary, :coder].each do |opt|
        options[opt] = column_obj.public_send(opt)
      end

      t.public_send column_obj.type, column_obj.name, options
    end
  end

  index_columns = table.levels.map(&:name).sort
  index_name = (%w|idx| + index_columns).join('_')
  connection.add_index table.table_name, index_columns, unique: true, name: index_name
end

#measure_update_sql(measure, measures = []) ⇒ Object

Build update statements for measures’ values

sum   = sum + new sum
count = count + new count
max   = MAX(max, new max)
min   = MIN(min, new min)
avg
  if no count or sum measures in current aggregation, recreate aggregated table from scratch
  if count measure exists
    avg = (avg * count + new avg * new count) / (count + new count)
  if sum measure exists
    avg = (sum + new sum) / (sum / avg + new sum / new avg)

Parameters:



49
50
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 49

def measure_update_sql(measure, measures=[])
end

#update_aggregated_table(query, update_values_sql) ⇒ Object



52
53
54
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 52

def update_aggregated_table(query, update_values_sql)
  connection.execute query
end