Class: ETL::Processor::CheckExistProcessor
- Inherits:
-
RowProcessor
- Object
- Processor
- RowProcessor
- ETL::Processor::CheckExistProcessor
- Defined in:
- lib/etl/processor/check_exist_processor.rb
Overview
A row-level processor that checks if the row already exists in the target table
Instance Attribute Summary collapse
-
#columns ⇒ Object
An array of columns representing the natural key.
-
#should_check ⇒ Object
Is set to true if the processor should execute the check.
-
#skip ⇒ Object
A symbol or array of symbols representing keys that should be skipped.
-
#table ⇒ Object
The name of the table to check against.
Instance Method Summary collapse
-
#initialize(control, configuration) ⇒ CheckExistProcessor
constructor
Initialize the processor Configuration options: *
:skip: A symbol or array of column names that should not be checked *:table: The table name *:columns: An array of columns which represent the natural key. -
#process(row) ⇒ Object
Process the row.
-
#should_check? ⇒ Boolean
Return true if the row should be checked.
-
#skip?(key) ⇒ Boolean
Return true if the given key should be skipped.
Constructor Details
#initialize(control, configuration) ⇒ CheckExistProcessor
Initialize the processor Configuration options:
-
:skip: A symbol or array of column names that should not be checked -
:table: The table name -
:columns: An array of columns which represent the natural key
26 27 28 29 30 31 32 33 34 |
# File 'lib/etl/processor/check_exist_processor.rb', line 26 def initialize(control, configuration) super @skip = configuration[:skip] || [] @table = configuration[:table] @columns = configuration[:columns] q = "SELECT COUNT(*) FROM #{table}" @should_check = ETL::ActiveRecord::Base.connection.select_value(q).to_i > 0 end |
Instance Attribute Details
#columns ⇒ Object
An array of columns representing the natural key
13 14 15 |
# File 'lib/etl/processor/check_exist_processor.rb', line 13 def columns @columns end |
#should_check ⇒ Object
Is set to true if the processor should execute the check. If there are no rows in the target table then this should return false.
17 18 19 |
# File 'lib/etl/processor/check_exist_processor.rb', line 17 def should_check @should_check end |
#skip ⇒ Object
A symbol or array of symbols representing keys that should be skipped
7 8 9 |
# File 'lib/etl/processor/check_exist_processor.rb', line 7 def skip @skip end |
#table ⇒ Object
The name of the table to check against
10 11 12 |
# File 'lib/etl/processor/check_exist_processor.rb', line 10 def table @table end |
Instance Method Details
#process(row) ⇒ Object
Process the row
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/etl/processor/check_exist_processor.rb', line 52 def process(row) return row unless should_check? connection = ETL::ActiveRecord::Base.connection q = "SELECT * FROM #{table} WHERE " conditions = [] row.each do |k,v| if columns.nil? || columns.include?(k.to_sym) conditions << "#{k} = #{connection.quote(v)}" unless skip?(k.to_sym) end end q << conditions.join(" AND ") #puts "query: #{q}" result = connection.select_one(q) return row if result.nil? end |
#should_check? ⇒ Boolean
Return true if the row should be checked
47 48 49 |
# File 'lib/etl/processor/check_exist_processor.rb', line 47 def should_check? @should_check ? true : false end |
#skip?(key) ⇒ Boolean
Return true if the given key should be skipped
37 38 39 40 41 42 43 44 |
# File 'lib/etl/processor/check_exist_processor.rb', line 37 def skip?(key) case skip when Array skip.include?(key) else skip.to_sym == key.to_sym end end |