Report abuse


			
  # Pass in the table name and an array containing hashes of property data, the keys should map to the field names in the table
  def self.do_insert(table_name, data)
    # Do our own quoting / insertion as it's *a lot* faster
    values = []
    data.each do |row|
      sql = []
      row.values.each do |value|
        sql << case
          when value.is_a?(Numeric): value
          when value.is_a?(String) || value.is_a?(Time): "'#{value}'"
          when value.is_a?(TrueClass): 1
          when value.is_a?(FalseClass): 0
          when value.nil?: 'NULL'
        end
      end
      values << "(#{sql.join(',')})"
    end
    
    ActiveRecord::Base.connection.execute(
      "INSERT INTO #{table_name} (#{data[0].keys.join(',')}) VALUES #{values.join(',')}"
    )
  end