Skip to content

Commit

Permalink
- Modified SQLResolver to allow for multiple key lookups on dimension
Browse files Browse the repository at this point in the history
table.
  • Loading branch information
cdimartino authored and aeden committed Feb 26, 2010
1 parent 4579546 commit 0d9a484
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/etl/transform/foreign_key_lookup_transform.rb
Expand Up @@ -95,10 +95,9 @@ def initialize(table, field, connection=nil)
end
def resolve(value)
if @use_cache
cache[value]
cache[cache_key(value)]
else
q = "SELECT id FROM #{table_name} WHERE #{@field} = #{@connection.quote(value)}"
ETL::Engine.logger.debug("Executing query: #{q}")
q = "SELECT id FROM #{table_name} WHERE #{wheres(value)}"
@connection.select_value(q)
end
end
Expand All @@ -110,11 +109,30 @@ def cache
end
def load_cache
@use_cache = true
q = "SELECT id, #{@field} FROM #{table_name}"
q = "SELECT id, #{field.join(', ')} FROM #{table_name}"
@connection.select_all(q).each do |record|
cache[record[@field]] = record['id']
cache[cache_key(record.values_at(*field))] = record['id']
end
end

private
def field
unless @field.kind_of?(Array)
@field = [ @field ]
end
@field
end

def cache_key(value)
value.hash
end

def wheres(value)
value = [ value ] unless value.kind_of?(Array)
field.zip(value).collect { |a|
"#{a[0]} = #{@connection.quote(a[1])}"
}.join(' AND ')
end
end

class FlatFileResolver
Expand Down Expand Up @@ -148,4 +166,4 @@ def resolve(value)
end
nil
end
end
end

0 comments on commit 0d9a484

Please sign in to comment.