Class: SQLResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/etl/transform/foreign_key_lookup_transform.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, field, connection = nil) ⇒ SQLResolver

Initialize the SQL resolver. Use the given table and field name to search for the appropriate foreign key. The field should be the name of a natural key that is used to locate the surrogate key for the record.

The connection argument is optional. If specified it can be either a symbol referencing a connection defined in the ETL database.yml file or an actual ActiveRecord connection instance. If the connection is not specified then the ActiveRecord::Base.connection will be used.



77
78
79
80
81
82
# File 'lib/etl/transform/foreign_key_lookup_transform.rb', line 77

def initialize(table, field, connection=nil)
  @table = table
  @field = field
  @connection = (connection.respond_to?(:quote) ? connection : ETL::Engine.connection(connection)) if connection
  @connection ||= ActiveRecord::Base.connection
end

Instance Method Details

#resolve(value) ⇒ Object



83
84
85
# File 'lib/etl/transform/foreign_key_lookup_transform.rb', line 83

def resolve(value)
  @connection.select_value("SELECT id FROM #{@table} WHERE #{@field} = #{@connection.quote(value)}")
end