Class: ActiveRecord::InsertAll::Builder

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/insert_all.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(insert_all) ⇒ Builder

Returns a new instance of Builder.


212
213
214
# File 'activerecord/lib/active_record/insert_all.rb', line 212

def initialize(insert_all)
  @insert_all, @model, @connection = insert_all, insert_all.model, insert_all.connection
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.


208
209
210
# File 'activerecord/lib/active_record/insert_all.rb', line 208

def model
  @model
end

Instance Method Details

#conflict_targetObject


247
248
249
250
251
252
253
254
255
# File 'activerecord/lib/active_record/insert_all.rb', line 247

def conflict_target
  if index = insert_all.unique_by
    sql = +"(#{format_columns(index.columns)})"
    sql << " WHERE #{index.where}" if index.where
    sql
  elsif update_duplicates?
    "(#{format_columns(insert_all.primary_keys)})"
  end
end

#intoObject


216
217
218
# File 'activerecord/lib/active_record/insert_all.rb', line 216

def into
  "INTO #{model.quoted_table_name} (#{columns_list})"
end

#raw_update_sqlObject Also known as: raw_update_sql?


271
272
273
# File 'activerecord/lib/active_record/insert_all.rb', line 271

def raw_update_sql
  insert_all.update_sql
end

#returningObject


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'activerecord/lib/active_record/insert_all.rb', line 231

def returning
  return unless insert_all.returning

  if insert_all.returning.is_a?(String)
    insert_all.returning
  else
    Array(insert_all.returning).map do |attribute|
      if model.attribute_alias?(attribute)
        "#{quote_column(model.attribute_alias(attribute))} AS #{quote_column(attribute)}"
      else
        quote_column(attribute)
      end
    end.join(",")
  end
end

#touch_model_timestamps_unless(&block) ⇒ Object


261
262
263
264
265
266
267
268
269
# File 'activerecord/lib/active_record/insert_all.rb', line 261

def touch_model_timestamps_unless(&block)
  return "" unless update_duplicates? && record_timestamps?

  model.timestamp_attributes_for_update_in_model.filter_map do |column_name|
    if touch_timestamp_attribute?(column_name)
      "#{column_name}=(CASE WHEN (#{updatable_columns.map(&block).join(" AND ")}) THEN #{model.quoted_table_name}.#{column_name} ELSE #{connection.high_precision_current_timestamp} END),"
    end
  end.join
end

#updatable_columnsObject


257
258
259
# File 'activerecord/lib/active_record/insert_all.rb', line 257

def updatable_columns
  quote_columns(insert_all.updatable_columns)
end

#values_listObject


220
221
222
223
224
225
226
227
228
229
# File 'activerecord/lib/active_record/insert_all.rb', line 220

def values_list
  types = extract_types_from_columns_on(model.table_name, keys: keys_including_timestamps)

  values_list = insert_all.map_key_with_value do |key, value|
    next value if Arel::Nodes::SqlLiteral === value
    connection.with_yaml_fallback(types[key].serialize(value))
  end

  connection.visitor.compile(Arel::Nodes::ValuesList.new(values_list))
end