Class: ActiveRecord::MassInsert::Inserter

Inherits:
Statement
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/activerecord/mass_insert/inserter.rb

Overview

Handles the building of the mass insert statement

Instance Attribute Summary

Attributes inherited from Statement

#mapped_columns, #matching_columns, #model, #payload

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Statement

#initialize

Constructor Details

This class inherits a constructor from ActiveRecord::MassInsert::Statement

Class Method Details

.insert(*args) ⇒ Object



13
14
15
# File 'lib/activerecord/mass_insert/inserter.rb', line 13

def self.insert(*args)
  new(*args).insert
end

Instance Method Details

#columnsObject



37
38
39
40
41
# File 'lib/activerecord/mass_insert/inserter.rb', line 37

def columns
  matching_columns.concat(mapped_columns.keys).map do |column|
    arel_table[column]
  end
end

#insertObject



17
18
19
# File 'lib/activerecord/mass_insert/inserter.rb', line 17

def insert
  connection.execute(sql)
end

#projectionObject



43
44
45
# File 'lib/activerecord/mass_insert/inserter.rb', line 43

def projection
  Projection.new(model, payload, *matching_columns, **mapped_columns)
end

#sqlObject



21
22
23
24
25
26
27
# File 'lib/activerecord/mass_insert/inserter.rb', line 21

def sql
  [
    model.send(:sanitize_sql_array, [statement.to_sql, payload]),
    'RETURNING',
    primary_key
  ].join(' ')
end

#statementObject



29
30
31
32
33
34
35
# File 'lib/activerecord/mass_insert/inserter.rb', line 29

def statement
  Arel::Nodes::InsertStatement.new.tap do |new_statement|
    new_statement.relation = arel_table
    new_statement.values = projection.statement
    new_statement.columns = columns
  end
end