Class: ETL::Processor::SurrogateKeyProcessor

Inherits:
RowProcessor show all
Defined in:
lib/etl/processor/surrogate_key_processor.rb

Overview

A row level processor that provides surrogate keys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(control, configuration) ⇒ SurrogateKeyProcessor

Initialize the surrogate key generator

Configuration options

  • :query: If specified it contains a query to be used to locate the last surrogate key. If this is specified then :target must also be specified.

  • :target: The target connection

  • :destination: The destination column name (defaults to :id)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/etl/processor/surrogate_key_processor.rb', line 18

def initialize(control, configuration)
  super
  @table = configuration[:table]
  @column = configuration[:column] || 'id'
  @target = configuration[:target]
  if configuration[:query]
    raise ControlError, "Query option is no longer value, use :column and :table instead"
  end
  if table
    @surrogate_key = ETL::Engine.connection(target).select_value("SELECT max(#{column}) FROM #{table_name}")
  end
  #puts "initial surrogate key: #{@surrogate_key}"
  @surrogate_key = 0 if @surrogate_key.blank?
  @surrogate_key = @surrogate_key.to_i
  #puts "surrogate key: #{@surrogate_key}"
  @destination = configuration[:destination] || :id
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



7
8
9
# File 'lib/etl/processor/surrogate_key_processor.rb', line 7

def column
  @column
end

#destinationObject

Returns the value of attribute destination.



5
6
7
# File 'lib/etl/processor/surrogate_key_processor.rb', line 5

def destination
  @destination
end

#tableObject

Returns the value of attribute table.



6
7
8
# File 'lib/etl/processor/surrogate_key_processor.rb', line 6

def table
  @table
end

#targetObject

Returns the value of attribute target.



8
9
10
# File 'lib/etl/processor/surrogate_key_processor.rb', line 8

def target
  @target
end

Instance Method Details

#process(row) ⇒ Object

Add a surrogate key to the row



37
38
39
40
41
42
43
44
45
# File 'lib/etl/processor/surrogate_key_processor.rb', line 37

def process(row)
  if row
    #puts "processing row #{row.inspect}"
    @surrogate_key += 1
    #puts "adding surrogate key to row: #{@surrogate_key}"
    row[destination] = @surrogate_key
    row
  end
end