5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/multi_insert/query_builder.rb', line 5
def self.insert(table, columns, values, opts = {})
ar = ActiveRecord::Base.connection
options = {time: true}
options.merge!(opts)
now = Time.now.to_s(:db) if options[:time]
table = ar.quote_table_name(table.to_s)
columns = columns + [:created_at, :updated_at] if options[:time]
columns = columns.map!{|c| ar.quote_column_name(c.to_s)}
columns = self.join_params(columns)
if options[:time]
values = values.map{|v| v + [now, now]}
end
values = values.map{|v| self.join_params(v.map{|vv| ar.quote(vv.to_s)})}.join(',')
"INSERT INTO #{table} #{columns} VALUES #{values}"
end
|