Sha256: ffc8e74a6ea80219a4d5bf9069abfabcd6f9b43967194bd75efaf0e3d97f9e85

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module ETL
  module Processor
    # Processor which is used to bulk import data into a target database
    class BulkImportProcessor < ETL::Processor::Processor
      attr_reader :file, :target, :truncate, :columns
      def initialize(control, configuration)
        super
        @file = File.join(File.dirname(control.file), configuration[:file])
        @target = configuration[:target]
        @truncate = configuration[:truncate] ||= false
        @columns = configuration[:columns]
        connect
      end
      def process
        # columns = control.destinations.first.order.join(',') # TODO: support multiple destinations?
        conn = ActiveRecord::Base.connection
        conn.transaction do
          # TODO: Support all database types
          # Since LOCAL is used this must be allowed by both the client and server
          conn.truncate(target[:table]) if truncate
          options = {}
          options[:columns] = columns
          conn.bulk_load(file, target[:table], options)
        end
      end
      
      private
      # Connect to the database
      def connect
        ActiveRecord::Base.establish_connection(
            :adapter  => (target[:adapter] || :mysql),
            :username => (target[:username] || 'root'),
            :host     => (target[:host] || 'localhost'),
            :password => target[:password],
            :database => target[:database] 
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activewarehouse-etl-0.4.0 lib/etl/processor/bulk_import_processor.rb