2010-08-13 18:30:22 -04:00
|
|
|
module Arel
|
|
|
|
class InsertManager < Arel::TreeManager
|
|
|
|
def initialize engine
|
|
|
|
super
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast = Nodes::InsertStatement.new
|
2010-08-13 18:30:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
def values= val; @ast.values = val; end
|
2010-08-13 21:28:38 -04:00
|
|
|
|
|
|
|
def insert fields
|
|
|
|
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
|
2011-03-22 00:31:45 -04:00
|
|
|
@ast.values = create_values values, @ast.columns
|
2010-08-13 21:28:38 -04:00
|
|
|
end
|
|
|
|
end
|
2011-03-22 00:31:45 -04:00
|
|
|
|
|
|
|
def create_values values, columns
|
|
|
|
Nodes::Values.new values, columns
|
|
|
|
end
|
2010-08-13 18:30:22 -04:00
|
|
|
end
|
|
|
|
end
|