Sha256: 7ee3d94be31b15c7019342c0d279668e11dc5f84c1d017293b1cbbc23d0f10f3

Contents?: true

Size: 995 Bytes

Versions: 18

Compression:

Stored size: 995 Bytes

Contents

class ActiveRecord::Base

  def self.bulk_insert!(record_list)
    return if record_list.empty?
    
    adapter_type = connection.adapter_name.downcase.to_sym
    case adapter_type
    when :mysql
      raise NotImplementedError, "Not implemented type '#{adapter_type}'"
    when :sqlite
      self.create record_list
    when :postgresql
      key_list, value_list = convert_record_list(record_list)        
      sql = "INSERT INTO #{self.table_name} (#{key_list.join(", ")}) VALUES #{value_list.map {|rec| "(#{rec.join(", ")})" }.join(" ,")}"
      self.connection.insert_sql(sql)
    else
      raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
    end
    
  end

  def self.convert_record_list(record_list)
    key_list = record_list.map(&:keys).flatten.uniq.sort

    value_list = record_list.map do |rec|
      list = []
      key_list.each {|key| list <<  ActiveRecord::Base.connection.quote(rec[key]) }
      list
    end

    return [key_list, value_list]
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
notifiable-rails-0.12.0 lib/notifiable/active_record.rb
notifiable-rails-0.11.0 lib/notifiable/active_record.rb
notifiable-rails-0.10.1 lib/notifiable/active_record.rb
notifiable-rails-0.10.0 lib/notifiable/active_record.rb
notifiable-rails-0.9.0 lib/notifiable/active_record.rb
notifiable-rails-0.8.0 lib/notifiable/active_record.rb
notifiable-rails-0.7.2 lib/notifiable/active_record.rb
notifiable-rails-0.7.1 lib/notifiable/active_record.rb
notifiable-rails-0.7.0 lib/notifiable/active_record.rb
notifiable-rails-0.6.1 lib/notifiable/active_record.rb
notifiable-rails-0.6.0 lib/notifiable/active_record.rb
notifiable-rails-0.5.1 lib/notifiable/active_record.rb
notifiable-rails-0.5.0 lib/notifiable/active_record.rb
notifiable-rails-0.4.0 lib/notifiable/active_record.rb
notifiable-rails-0.3.0 lib/notifiable/active_record.rb
notifiable-rails-0.2.0 lib/notifiable/active_record.rb
notifiable-rails-0.1.1 lib/notifiable/active_record.rb
notifiable-rails-0.1.0 lib/notifiable/active_record.rb