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

insert manager implements "insert"

This commit is contained in:
Aaron Patterson 2010-08-13 18:28:38 -07:00
parent 3eeed18e0c
commit 9fc70e22f9
5 changed files with 61 additions and 6 deletions

View file

@ -12,5 +12,16 @@ module Arel
def columns; @head.columns end
def values; @head.values end
def insert fields
return if fields.empty?
@head.relation ||= fields.first.first.relation
fields.each do |column, value|
@head.columns << column
@head.values << value
end
end
end
end

View file

@ -26,9 +26,11 @@ module Arel
self
end
def insert stuff
viz = Visitors::Dot.new
puts viz.accept stuff
# FIXME: this method should go away
def insert values
im = InsertManager.new @engine
im.insert values
raise
end
end
end

View file

@ -28,6 +28,17 @@ module Arel
end
private
def visit_Arel_Nodes_SelectCore o
visit_edge o, "froms"
visit_edge o, "projections"
visit_edge o, "wheres"
end
def visit_Arel_Nodes_SelectStatement o
visit_edge o, "cores"
visit_edge o, "limit"
end
def visit_Arel_Table o
visit_edge o, "name"
end
@ -47,9 +58,14 @@ module Arel
alias :visit_NilClass :visit_String
def visit_Hash o
o.each_with_index do |(k,v),i|
edge("key_#{i}") { visit k }
edge("value_#{i}") { visit v }
o.each_with_index do |pair, i|
edge("pair_#{i}") { visit pair }
end
end
def visit_Array o
o.each_with_index do |x,i|
edge(i) { visit x }
end
end

View file

@ -8,6 +8,32 @@ module Arel
end
end
describe 'insert' do
it 'takes a list of lists' do
table = Table.new(:users)
manager = Arel::InsertManager.new Table.engine
manager.into table
manager.insert [[table[:id], 1], [table[:name], 'aaron']]
manager.to_sql.should be_like %{
INSERT INTO "users" ("users"."id", "users"."name") VALUES (1, 'aaron')
}
end
it 'defaults the table' do
table = Table.new(:users)
manager = Arel::InsertManager.new Table.engine
manager.insert [[table[:id], 1], [table[:name], 'aaron']]
manager.to_sql.should be_like %{
INSERT INTO "users" ("users"."id", "users"."name") VALUES (1, 'aaron')
}
end
it 'takes an empty list' do
manager = Arel::InsertManager.new Table.engine
manager.insert []
end
end
describe 'into' do
it 'takes an engine' do
manager = Arel::InsertManager.new Table.engine