1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib/arel/insert_manager.rb

50 lines
952 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-02-24 01:45:50 -05:00
2018-02-24 02:41:47 -05:00
module Arel # :nodoc: all
class InsertManager < Arel::TreeManager
def initialize
super
2010-11-05 17:09:09 -04:00
@ast = Nodes::InsertStatement.new
end
2018-02-24 01:45:50 -05:00
def into(table)
2010-11-05 17:09:09 -04:00
@ast.relation = table
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)
@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?
if String === fields
2014-02-05 15:40:52 -05:00
@ast.values = Nodes::SqlLiteral.new(fields)
else
2010-11-05 17:09:09 -04:00
@ast.relation ||= fields.first.first.relation
2010-08-13 21:28:38 -04:00
values = []
fields.each do |column, value|
2010-11-05 17:09:09 -04:00
@ast.columns << column
values << value
end
@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
def create_values(values)
Nodes::ValuesList.new([values])
end
2017-05-21 08:12:08 -04:00
def create_values_list(rows)
Nodes::ValuesList.new(rows)
end
end
end