Method: ActiveRecord::Import::PostgreSQLAdapter#insert_many

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

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

:nodoc:



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
45
46
47
48
49
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 9

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

  base_sql, post_sql = case sql
                       when String
                         [sql, '']
                       when Array
                         [sql.shift, sql.join( ' ' )]
  end

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

  selections = returning_selections(options)
  if selections.blank? || (options[:no_returning] && !options[:recursive])
    insert( sql2insert, *args )
  else
    returned_values = if selections.size > 1
      # Select composite columns
      db_result = select_all( sql2insert, *args )
      { values: db_result.rows, columns: db_result.columns }
    else
      { values: select_values( sql2insert, *args ) }
    end
    clear_query_cache if query_cache_enabled
  end

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

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