提交 74a27a0d 编写于 作者: A Aaron Patterson

inserts have the correct syntax

上级 9fc70e22
......@@ -30,7 +30,7 @@ def take limit
def insert values
im = InsertManager.new @engine
im.insert values
raise
@engine.connection.execute im.to_sql
end
end
end
......@@ -15,8 +15,15 @@ def accept object
def visit_Arel_Nodes_InsertStatement o
[
"INSERT INTO #{visit o.relation}",
("(#{o.columns.map { |x| visit x }.join ', '})" unless o.columns.empty?),
("VALUES (#{o.values.map { |x| quote visit x }.join ', '})" unless o.values.empty?),
("(#{o.columns.map { |x|
quote_column_name x.name
}.join ', '})" unless o.columns.empty?),
("VALUES (#{o.values.map { |value|
value ? quote(visit(value)) : 'NULL'
}.join ', '})" unless o.values.empty?),
].compact.join ' '
end
......@@ -43,12 +50,15 @@ def visit_Arel_Nodes_Equality o
"#{visit o.left} = #{visit o.right}"
end
def visit_Arel_Attributes_Integer o
def visit_Arel_Attributes_Attribute o
"#{quote_table_name o.relation.name}.#{quote_column_name o.name}"
end
alias :visit_Arel_Attributes_String :visit_Arel_Attributes_Integer
alias :visit_Arel_Attributes_Integer :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_String :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Time :visit_Arel_Attributes_Attribute
def visit_Fixnum o; o end
alias :visit_Time :visit_Fixnum
alias :visit_String :visit_Fixnum
alias :visit_Arel_Nodes_SqlLiteral :visit_Fixnum
alias :visit_Arel_SqlLiteral :visit_Fixnum # This is deprecated
......
......@@ -9,13 +9,33 @@ module Arel
end
describe 'insert' do
it "inserts null" do
table = Table.new(:users)
manager = Arel::InsertManager.new Table.engine
manager.insert [[table[:id], nil]]
manager.to_sql.should be_like %{
INSERT INTO "users" ("id") VALUES (NULL)
}
end
it "inserts time" do
table = Table.new(:users)
manager = Arel::InsertManager.new Table.engine
time = Time.now
manager.insert [[table[:id], time]]
manager.to_sql.should be_like %{
INSERT INTO "users" ("id") VALUES (#{Table.engine.connection.quote time})
}
end
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')
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
}
end
......@@ -24,7 +44,7 @@ module Arel
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')
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
}
end
......@@ -57,7 +77,7 @@ module Arel
manager.into table
manager.columns << table[:id]
manager.to_sql.should be_like %{
INSERT INTO "users" ("users"."id")
INSERT INTO "users" ("id")
}
end
end
......@@ -86,7 +106,7 @@ module Arel
manager.columns << table[:id]
manager.columns << table[:name]
manager.to_sql.should be_like %{
INSERT INTO "users" ("users"."id", "users"."name") VALUES (1, 'aaron')
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
}
end
end
......
require 'spec_helper'
module Arel
module Visitors
describe 'the to_sql visitor' do
before do
@visitor = ToSql.new Table.engine
@attr = Table.new(:users)[:id]
end
it "should visit visit_Arel_Attributes_Time" do
attr = Attributes::Time.new(@attr.relation, @attr.name, @attr.column)
@visitor.accept attr
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册