Class: InsertSelect::ActiveRecord::InsertSelectFrom

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, relation, mapping:, on_duplicate:, returning: nil) ⇒ InsertSelectFrom

Returns a new instance of InsertSelectFrom.



53
54
55
56
57
58
59
60
61
# File 'lib/insert_select/active_record.rb', line 53

def initialize(model, relation, mapping:, on_duplicate:, returning: nil)
  @model = model
  @connection = model.connection
  @relation = relation
  @adapter = find_adapter(connection)
  @mapping = mapping
  @returning = returning
  @on_duplicate = on_duplicate
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



51
52
53
# File 'lib/insert_select/active_record.rb', line 51

def adapter
  @adapter
end

#connectionObject (readonly)

Returns the value of attribute connection.



51
52
53
# File 'lib/insert_select/active_record.rb', line 51

def connection
  @connection
end

#mappingObject (readonly)

Returns the value of attribute mapping.



51
52
53
# File 'lib/insert_select/active_record.rb', line 51

def mapping
  @mapping
end

#modelObject (readonly)

Returns the value of attribute model.



51
52
53
# File 'lib/insert_select/active_record.rb', line 51

def model
  @model
end

#on_duplicateObject (readonly)

Returns the value of attribute on_duplicate.



51
52
53
# File 'lib/insert_select/active_record.rb', line 51

def on_duplicate
  @on_duplicate
end

#relationObject (readonly)

Returns the value of attribute relation.



51
52
53
# File 'lib/insert_select/active_record.rb', line 51

def relation
  @relation
end

#returningObject (readonly)

Returns the value of attribute returning.



51
52
53
# File 'lib/insert_select/active_record.rb', line 51

def returning
  @returning
end

Instance Method Details

#builderObject



72
73
74
# File 'lib/insert_select/active_record.rb', line 72

def builder
  @builder ||= Builder.new(self)
end

#ensure_valid_options_for_connection!Object



76
77
78
79
80
# File 'lib/insert_select/active_record.rb', line 76

def ensure_valid_options_for_connection!
  if returning && !connection.supports_insert_returning?
    raise ArgumentError, "#{connection.class} does not support :returning"
  end
end

#executeObject



63
64
65
66
# File 'lib/insert_select/active_record.rb', line 63

def execute
  sql = model.sanitize_sql_array([to_sql, *builder.constant_values])
  connection.exec_insert_all(sql, "Bulk Insert")
end

#to_sqlObject



68
69
70
# File 'lib/insert_select/active_record.rb', line 68

def to_sql
  @to_sql ||= adapter.build_sql(builder)
end