123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::InsertAll::Builder

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/insert_all.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(insert_all) ⇒ Builder

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 230
def initialize(insert_all)
  @insert_all, @model, @connection = insert_all, insert_all.model, insert_all.connection
end

Instance Attribute Details

#connection (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 296
attr_reader :connection, :insert_all

#insert_all (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 296
attr_reader :connection, :insert_all

#keys (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 228
delegate :skip_duplicates?, :update_duplicates?, :keys, :keys_including_timestamps, :record_timestamps?, to: :insert_all

#keys_including_timestamps (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 228
delegate :skip_duplicates?, :update_duplicates?, :keys, :keys_including_timestamps, :record_timestamps?, to: :insert_all

#model (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 226
attr_reader :model

#raw_update_sql (readonly) Also known as: #raw_update_sql?

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 289
def raw_update_sql
  insert_all.update_sql
end

#raw_update_sql? (readonly)

Alias for #raw_update_sql.

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 293
alias raw_update_sql? raw_update_sql

Instance Method Details

#columns_list (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 302
def columns_list
  format_columns(insert_all.keys_including_timestamps)
end

#conflict_target

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 265
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

#extract_types_from_columns_on(table_name, keys:) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 306
def extract_types_from_columns_on(table_name, keys:)
  columns = @model.schema_cache.columns_hash(table_name)

  unknown_column = (keys - columns.keys).first
  raise UnknownAttributeError.new(model.new, unknown_column) if unknown_column

  keys.index_with { |key| model.type_for_attribute(key) }
end

#format_columns(columns) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 315
def format_columns(columns)
  columns.respond_to?(:map) ? quote_columns(columns).join(",") : columns
end

#into

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 234
def into
  "INTO #{model.quoted_table_name} (#{columns_list})"
end

#quote_column(column) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 323
def quote_column(column)
  connection.quote_column_name(column)
end

#quote_columns(columns) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 319
def quote_columns(columns)
  columns.map { |column| quote_column(column) }
end

#record_timestamps?Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 228
delegate :skip_duplicates?, :update_duplicates?, :keys, :keys_including_timestamps, :record_timestamps?, to: :insert_all

#returning

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 249
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

#skip_duplicates?Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 228
delegate :skip_duplicates?, :update_duplicates?, :keys, :keys_including_timestamps, :record_timestamps?, to: :insert_all

#touch_model_timestamps_unless(&block)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 279
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

#touch_timestamp_attribute?(column_name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 298
def touch_timestamp_attribute?(column_name)
  insert_all.updatable_columns.exclude?(column_name)
end

#updatable_columns

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 275
def updatable_columns
  quote_columns(insert_all.updatable_columns)
end

#update_duplicates?Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 228
delegate :skip_duplicates?, :update_duplicates?, :keys, :keys_including_timestamps, :record_timestamps?, to: :insert_all

#values_list

[ GitHub ]

  
# File 'activerecord/lib/active_record/insert_all.rb', line 238
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