Class: ActiveRecord::InsertAll
- Inherits:
-
Object
- Object
- ActiveRecord::InsertAll
- Defined in:
- lib/active_record/insert_all.rb
Overview
:nodoc:
Defined Under Namespace
Classes: Builder
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#inserts ⇒ Object
readonly
Returns the value of attribute inserts.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#on_duplicate ⇒ Object
readonly
Returns the value of attribute on_duplicate.
-
#returning ⇒ Object
readonly
Returns the value of attribute returning.
-
#unique_by ⇒ Object
readonly
Returns the value of attribute unique_by.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(model, inserts, on_duplicate:, returning: nil, unique_by: nil) ⇒ InsertAll
constructor
A new instance of InsertAll.
- #map_key_with_value ⇒ Object
- #primary_keys ⇒ Object
- #skip_duplicates? ⇒ Boolean
- #updatable_columns ⇒ Object
- #update_duplicates? ⇒ Boolean
Constructor Details
#initialize(model, inserts, on_duplicate:, returning: nil, unique_by: nil) ⇒ InsertAll
Returns a new instance of InsertAll.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/active_record/insert_all.rb', line 8 def initialize(model, inserts, on_duplicate:, returning: nil, unique_by: nil) raise ArgumentError, "Empty list of attributes passed" if inserts.blank? @model, @connection, @inserts, @keys = model, model.connection, inserts, inserts.first.keys.map(&:to_s).to_set @on_duplicate, @returning, @unique_by = on_duplicate, returning, unique_by @returning = (connection.supports_insert_returning? ? primary_keys : false) if @returning.nil? @returning = false if @returning == [] @unique_by = find_unique_index_for(unique_by) if unique_by @on_duplicate = :skip if @on_duplicate == :update && updatable_columns.empty? end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
5 6 7 |
# File 'lib/active_record/insert_all.rb', line 5 def connection @connection end |
#inserts ⇒ Object (readonly)
Returns the value of attribute inserts.
5 6 7 |
# File 'lib/active_record/insert_all.rb', line 5 def inserts @inserts end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
5 6 7 |
# File 'lib/active_record/insert_all.rb', line 5 def keys @keys end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/active_record/insert_all.rb', line 5 def model @model end |
#on_duplicate ⇒ Object (readonly)
Returns the value of attribute on_duplicate.
6 7 8 |
# File 'lib/active_record/insert_all.rb', line 6 def on_duplicate @on_duplicate end |
#returning ⇒ Object (readonly)
Returns the value of attribute returning.
6 7 8 |
# File 'lib/active_record/insert_all.rb', line 6 def returning @returning end |
#unique_by ⇒ Object (readonly)
Returns the value of attribute unique_by.
6 7 8 |
# File 'lib/active_record/insert_all.rb', line 6 def unique_by @unique_by end |
Instance Method Details
#execute ⇒ Object
23 24 25 26 27 28 |
# File 'lib/active_record/insert_all.rb', line 23 def execute = +"#{model} " << "Bulk " if inserts.many? << (on_duplicate == :update ? "Upsert" : "Insert") connection.exec_insert_all to_sql, end |
#map_key_with_value ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/active_record/insert_all.rb', line 47 def map_key_with_value inserts.map do |attributes| attributes = attributes.stringify_keys verify_attributes(attributes) keys.map do |key| yield key, attributes[key] end end end |
#primary_keys ⇒ Object
34 35 36 |
# File 'lib/active_record/insert_all.rb', line 34 def primary_keys Array(model.primary_key) end |
#skip_duplicates? ⇒ Boolean
39 40 41 |
# File 'lib/active_record/insert_all.rb', line 39 def skip_duplicates? on_duplicate == :skip end |
#updatable_columns ⇒ Object
30 31 32 |
# File 'lib/active_record/insert_all.rb', line 30 def updatable_columns keys - readonly_columns - unique_by_columns end |
#update_duplicates? ⇒ Boolean
43 44 45 |
# File 'lib/active_record/insert_all.rb', line 43 def update_duplicates? on_duplicate == :update end |