Module: ActiveRecord::Import::AbstractAdapter::ClassMethods

Included in:
ConnectionAdapters::AbstractAdapter
Defined in:
lib/activerecord-import-rails4/adapters/abstract_adapter.rb

Instance Method Summary collapse

Instance Method Details

#get_insert_value_sets(values, sql_size, max_bytes) ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/activerecord-import-rails4/adapters/abstract_adapter.rb', line 6

def get_insert_value_sets( values, sql_size, max_bytes ) # :nodoc:
  value_sets = []
  arr, current_arr_values_size, current_size = [], 0, 0
  values.each_with_index do |val,i|
    comma_bytes = arr.size
    sql_size_thus_far = sql_size + current_size + val.bytesize + comma_bytes
    if NO_MAX_PACKET == max_bytes or sql_size_thus_far <= max_bytes
      current_size += val.bytesize
      arr << val
    else
      value_sets << arr
      arr = [ val ]
      current_size = val.bytesize
    end

    # if we're on the last iteration push whatever we have in arr to value_sets
    value_sets << arr if i == (values.size-1)
  end
  [ *value_sets ]
end