Class: ETL::Generator::SurrogateKeyGenerator

Inherits:
Generator show all
Defined in:
lib/etl/generator/surrogate_key_generator.rb

Overview

Surrogate key generator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Generator

class_for_name

Constructor Details

#initialize(options = {}) ⇒ SurrogateKeyGenerator

Initialize the generator



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/etl/generator/surrogate_key_generator.rb', line 13

def initialize(options={})
  @table = options[:table]
  @target = options[:target]
  @column = options[:column] || 'id'
  @query = options[:query]
  
  if table
    @surrogate_key = ETL::Engine.connection(target).select_value("SELECT max(#{column}) FROM #{table_name}")
  elsif query
    @surrogate_key = ETL::Engine.connection(target).select_value(query)
  end
  @surrogate_key = 0 if @surrogate_key.blank?
  @surrogate_key = @surrogate_key.to_i
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



9
10
11
# File 'lib/etl/generator/surrogate_key_generator.rb', line 9

def column
  @column
end

#queryObject (readonly)

Returns the value of attribute query.



10
11
12
# File 'lib/etl/generator/surrogate_key_generator.rb', line 10

def query
  @query
end

#tableObject (readonly)

Returns the value of attribute table.



7
8
9
# File 'lib/etl/generator/surrogate_key_generator.rb', line 7

def table
  @table
end

#targetObject (readonly)

Returns the value of attribute target.



8
9
10
# File 'lib/etl/generator/surrogate_key_generator.rb', line 8

def target
  @target
end

Instance Method Details

#nextObject

Get the next surrogate key



29
30
31
32
# File 'lib/etl/generator/surrogate_key_generator.rb', line 29

def next
  @surrogate_key ||= 0
  @surrogate_key += 1
end

#table_nameObject



34
35
36
# File 'lib/etl/generator/surrogate_key_generator.rb', line 34

def table_name
  ETL::Engine.table(table, ETL::Engine.connection(target))
end