Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/ibm_db_adapter.rb

Instance Method Summary collapse

Instance Method Details

#insert(values) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 224

def insert(values)
  primary_key_value = nil

  if primary_key && values.is_a?(Hash)
    primary_key_value = values[values.keys.find do |k|
      k.name == primary_key
    end]

    if !primary_key_value && connection.prefetch_primary_key?(klass.table_name)
      primary_key_value = connection.next_sequence_value(klass.sequence_name)
      values[klass.arel_table[klass.primary_key]] = primary_key_value
    end
  end

  im = arel.create_insert
  im.into @table

  conn = @klass.connection
  substitutes = values.sort_by { |arel_attr, _| arel_attr.name }
  binds       = substitutes.map do |arel_attr, value|
    [@klass.columns_hash[arel_attr.name], value]
  end

  substitutes, binds = substitute_values values
  if values.empty? # empty insert
    im.values = Arel.sql(connection.empty_insert_statement_value(klass.primary_key, klass.table_name))
  else
    im.insert substitutes
  end

  conn.insert(im, 'SQL', primary_key, primary_key_value, nil, binds)
end