Class: ActiveRecord::InsertAll::Builder

Inherits:
Object
  • Object
show all
Defined in:
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.



139
140
141
# File 'lib/active_record/insert_all.rb', line 139

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.



135
136
137
# File 'lib/active_record/insert_all.rb', line 135

def model
  @model
end

Instance Method Details

#conflict_targetObject



161
162
163
164
165
166
167
168
169
# File 'lib/active_record/insert_all.rb', line 161

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



143
144
145
# File 'lib/active_record/insert_all.rb', line 143

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

#returningObject



157
158
159
# File 'lib/active_record/insert_all.rb', line 157

def returning
  format_columns(insert_all.returning) if insert_all.returning
end

#touch_model_timestamps_unless(&block) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/active_record/insert_all.rb', line 175

def touch_model_timestamps_unless(&block)
  model.send(:timestamp_attributes_for_update_in_model).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 CURRENT_TIMESTAMP END),"
    end
  end.compact.join
end

#updatable_columnsObject



171
172
173
# File 'lib/active_record/insert_all.rb', line 171

def updatable_columns
  quote_columns(insert_all.updatable_columns)
end

#values_listObject



147
148
149
150
151
152
153
154
155
# File 'lib/active_record/insert_all.rb', line 147

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

  values_list = insert_all.map_key_with_value do |key, value|
    connection.with_yaml_fallback(types[key].serialize(value))
  end

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