Method: ActiveRecord::Import::PostgreSQLAdapter#insert_many

Defined in:
lib/activerecord-import/adapters/postgresql_adapter.rb

#insert_many(sql, values, options = {}, *args) ⇒ Object

:nodoc:


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 7

def insert_many( sql, values, options = {}, *args ) # :nodoc:
  number_of_inserts = 1
  returned_values = []
  ids = []
  results = []

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

  sql2insert = base_sql + values.join( ',' ) + post_sql

  columns = returning_columns(options)
  if columns.blank? || (options[:no_returning] && !options[:recursive])
    insert( sql2insert, *args )
  else
    returned_values = if columns.size > 1
      # Select composite columns
      select_rows( sql2insert, *args )
    else
      select_values( sql2insert, *args )
    end
    clear_query_cache if query_cache_enabled
  end

  if options[:returning].blank?
    ids = returned_values
  elsif options[:primary_key].blank?
    results = returned_values
  else
    # split primary key and returning columns
    ids, results = split_ids_and_results(returned_values, columns, options)
  end

  ActiveRecord::Import::Result.new([], number_of_inserts, ids, results)
end