Module: ActiveRecord::Import::AbstractAdapter::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#after_import_synchronize(instances) ⇒ Object

Synchronizes the passed in ActiveRecord instances with the records in the database by calling reload on each instance.



93
94
95
# File 'lib/activerecord-import-rails4/adapters/abstract_adapter.rb', line 93

def after_import_synchronize( instances )
  instances.each { |e| e.reload }
end

#insert_many(sql, values, *args) ⇒ Object

sql can be a single string or an array. If it is an array all elements that are in position >= 1 will be appended to the final SQL.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/activerecord-import-rails4/adapters/abstract_adapter.rb', line 35

def insert_many( sql, values, *args ) # :nodoc:
  # the number of inserts default
  number_of_inserts = 0

  base_sql,post_sql = if sql.is_a?( String )
    [ sql, '' ]
  elsif sql.is_a?( Array )
    [ sql.shift, sql.join( ' ' ) ]
  end

  sql_size = QUERY_OVERHEAD + base_sql.size + post_sql.size

  # the number of bytes the requested insert statement values will take up
  values_in_bytes = values.sum {|value| value.bytesize }

  # the number of bytes (commas) it will take to comma separate our values
  comma_separated_bytes = values.size-1

  # the total number of bytes required if this statement is one statement
  total_bytes = sql_size + values_in_bytes + comma_separated_bytes

  max = max_allowed_packet

  # if we can insert it all as one statement
  if NO_MAX_PACKET == max or total_bytes < max
    number_of_inserts += 1
    sql2insert = base_sql + values.join( ',' ) + post_sql
    insert( sql2insert, *args )
  else
    value_sets = self.class.get_insert_value_sets( values, sql_size, max )
    value_sets.each do |values|
      number_of_inserts += 1
      sql2insert = base_sql + values.join( ',' ) + post_sql
      insert( sql2insert, *args )
    end
  end

  number_of_inserts
end

#max_allowed_packetObject

Returns the maximum number of bytes that the server will allow in a single packet



115
116
117
# File 'lib/activerecord-import-rails4/adapters/abstract_adapter.rb', line 115

def max_allowed_packet
  NO_MAX_PACKET
end

#next_value_for_sequence(sequence_name) ⇒ Object



29
30
31
# File 'lib/activerecord-import-rails4/adapters/abstract_adapter.rb', line 29

def next_value_for_sequence(sequence_name)
  %{#{sequence_name}.nextval}
end

#post_sql_statements(table_name, options) ⇒ Object

Returns an array of post SQL statements given the passed in options.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/activerecord-import-rails4/adapters/abstract_adapter.rb', line 98

def post_sql_statements( table_name, options ) # :nodoc:
  post_sql_statements = []
  if options[:on_duplicate_key_update]
    post_sql_statements << sql_for_on_duplicate_key_update( table_name, options[:on_duplicate_key_update] )
  end

  #custom user post_sql
  post_sql_statements << options[:post_sql] if options[:post_sql]

  #with rollup
  post_sql_statements << rollup_sql if options[:rollup]

  post_sql_statements
end

#pre_sql_statements(options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/activerecord-import-rails4/adapters/abstract_adapter.rb', line 75

def pre_sql_statements(options)
  sql = []
  sql << options[:pre_sql] if options[:pre_sql]
  sql << options[:command] if options[:command]
  sql << "IGNORE" if options[:ignore]

  #add keywords like IGNORE or DELAYED
  if options[:keywords].is_a?(Array)
    sql.concat(options[:keywords])
  elsif options[:keywords]
    sql << options[:keywords].to_s
  end

  sql
end