Class: TempTable::Insert

Inherits:
Object
  • Object
show all
Defined in:
lib/temp_table/insert.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, data) ⇒ Insert

Returns a new instance of Insert.



7
8
9
10
11
# File 'lib/temp_table/insert.rb', line 7

def initialize(table_name, data)
  super()
  @table_name = table_name
  @data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/temp_table/insert.rb', line 5

def data
  @data
end

#table_nameObject

Returns the value of attribute table_name.



5
6
7
# File 'lib/temp_table/insert.rb', line 5

def table_name
  @table_name
end

Instance Method Details

#performObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/temp_table/insert.rb', line 13

def perform
  data.each do |row|
    column_names = row.keys.join(", ")
    values = row.values.map do |value|
      if value.is_a?(Array)
        ActiveRecord::Base.connection.quote(value.to_json)
      else
        ActiveRecord::Base.connection.quote(value)
      end
    end
    ActiveRecord::Base.connection.execute <<-SQL.squish
      INSERT INTO #{@table_name} (#{column_names})
      VALUES (#{values.join(', ')});
    SQL
  end
end