module ETL #:nodoc:
module Control #:nodoc:
# Source object which extracts data from a database using ActiveRecord.
class DatabaseSource < Source
# Initialize the source.
#
# Arguments:
# * control: The ETL::Control::Control instance
# * configuration: The configuration Hash
# * definition: The source definition
#
# Required configuration options:
# * :table: The source table name
# * :database: The database name
#
# Other options:
# * :adapter: The adapter to use (defaults to :mysql)
# * :username: The database username (defaults to 'root')
# * :password: The password to the database (defaults to nothing)
# * :host: The host for the database (defaults to 'localhost')
def initialize(control, configuration, definition)
super
connect
end
def to_s
"#{configuration[:host]}/#{configuration[:database]}"
end
# Returns each row from the source
def each
conn = ETL::ActiveRecord::Base.connection
conn.select_all("SELECT * FROM #{configuration[:table]}").each do |row|
yield row
end
end
private
# Connect to the database.
#
# Required options:
# * :database: The database name
#
# Options:
# * :adapter: The adapter to use (defaults to :mysql)
# * :username: The database username (defaults to 'root')
# * :password: The password to the database (defaults to nothing)
# * :host: The host for the database (defaults to 'localhost')
def connect
ETL::ActiveRecord::Base.establish_connection(
:adapter => (configuration[:adapter] || :mysql),
:username => (configuration[:username] || 'root'),
:host => (configuration[:host] || 'localhost'),
:password => configuration[:password],
:database => configuration[:database]
)
end
end
end
end