Sha256: 95bac93ac6844f80923ec2f1af68a92280aed8670394f713a381843bf251edc3

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module ETL
  module Control
    class DatabaseDestination < Destination
      attr_reader :order
      def initialize(control, configuration, mapping)
        super
        @order = mapping[:order] || order_from_source
        raise ControlError, "Order required in mapping" unless @order
        connect
      end
      
      def flush
        conn = ActiveRecord::Base.connection
        conn.transaction do
          buffer.each do |row|
            names = []
            values = []
            order.each do |name|
              names << name
              values << "'#{row[name]}'"
            end
            q = "INSERT INTO #{configuration[:table]} (#{names.join(',')}) VALUES (#{values.join(',')})"
            ETL::Engine.logger.debug("Query: #{q}")
            conn.execute(q, "Insert row #{current_row}")
            @current_row += 1
          end
          buffer.clear
        end
      end
      def close
        flush
        ActiveRecord::Base.connection.disconnect!
      end
      
      private
      def connect
        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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activewarehouse-etl-0.1.0 lib/etl/control/destination/database_destination.rb