2017-02-13 13:58:58 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-24 01:45:50 -05:00
|
|
|
|
2018-02-24 02:41:47 -05:00
|
|
|
module Arel # :nodoc: all
|
2010-08-13 18:30:22 -04:00
|
|
|
class InsertManager < Arel::TreeManager
|
2014-11-29 19:22:17 -05:00
|
|
|
def initialize
|
2010-08-13 18:30:22 -04:00
|
|
|
super
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast = Nodes::InsertStatement.new
|
2010-08-13 18:30:22 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def into(table)
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast.relation = table
|
2010-08-13 18:30:22 -04:00
|
|
|
self
|
|
|
|
end
|
2010-08-13 18:43:32 -04:00
|
|
|
|
2010-11-05 17:09:09 -04:00
|
|
|
def columns; @ast.columns end
|
2018-02-24 01:45:50 -05:00
|
|
|
def values=(val); @ast.values = val; end
|
2010-08-13 21:28:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def select(select)
|
2014-04-10 10:08:37 -04:00
|
|
|
@ast.select = select
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def insert(fields)
|
2010-08-13 21:28:38 -04:00
|
|
|
return if fields.empty?
|
|
|
|
|
2010-09-09 18:20:25 -04:00
|
|
|
if String === fields
|
2014-02-05 15:40:52 -05:00
|
|
|
@ast.values = Nodes::SqlLiteral.new(fields)
|
2010-09-09 18:20:25 -04:00
|
|
|
else
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast.relation ||= fields.first.first.relation
|
2010-08-13 21:28:38 -04:00
|
|
|
|
2010-09-09 18:20:25 -04:00
|
|
|
values = []
|
|
|
|
|
|
|
|
fields.each do |column, value|
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast.columns << column
|
2010-09-09 18:20:25 -04:00
|
|
|
values << value
|
|
|
|
end
|
2019-03-17 14:09:00 -04:00
|
|
|
@ast.values = create_values(values)
|
2010-08-13 21:28:38 -04:00
|
|
|
end
|
2017-02-28 16:10:40 -05:00
|
|
|
self
|
2010-08-13 21:28:38 -04:00
|
|
|
end
|
2011-03-22 00:31:45 -04:00
|
|
|
|
2019-03-17 14:09:00 -04:00
|
|
|
def create_values(values)
|
|
|
|
Nodes::ValuesList.new([values])
|
2011-03-22 00:31:45 -04:00
|
|
|
end
|
2017-05-21 08:12:08 -04:00
|
|
|
|
|
|
|
def create_values_list(rows)
|
|
|
|
Nodes::ValuesList.new(rows)
|
|
|
|
end
|
2010-08-13 18:30:22 -04:00
|
|
|
end
|
|
|
|
end
|