Skip to content

Instantly share code, notes, and snippets.

@romiras
Created February 21, 2013 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romiras/5003700 to your computer and use it in GitHub Desktop.
Save romiras/5003700 to your computer and use it in GitHub Desktop.
A compact class which collects and inserts bulk of data into MySql. Used for inserting large data.
# A class which collects and inserts bulk of data
# Implemented for MySQL 5
# Count of records inserted at once is configured by MAX
class BulkImporter
MAX = 100
# posts_writer = BulkImporter.new( ActiveRecord::Base.connection, Post )
def initialize(connection, klass)
@connection = connection
@table = klass.table_name
@column_names = klass.column_names
@buffer = []
end
# posts_writer.collect( { :title => 'My post', :descr => 'Description' } )
def collect(values)
if @buffer.size == MAX
push_values
end
@buffer << values
end
# posts_writer.finalize - should be called after data inserts' loop
def finalize
push_values unless @buffer.empty?
end
private
def push_values
quoted_column_names = @column_names.collect {|col| "`#{ col }`" }
rows = @buffer.collect {|row|
quoted_attributes = @column_names.collect{|col| "#{ ActiveRecord::Base.sanitize( row[col] ) }" }
"(#{ quoted_attributes.join(',') })" # ('value1','value2')
}.join(',') # ('value1','value2'),('value1','value2'), ...
sql = "INSERT INTO `#{@table}` (#{ quoted_column_names.join(',') }) VALUES #{rows}"
@buffer.clear
@connection.execute(sql)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment