提交 e1155aa9 编写于 作者: A Aaron Patterson

update is working on the select manager

上级 703326cd
......@@ -26,6 +26,15 @@ def take limit
self
end
# FIXME: this method should go away
def update values
um = UpdateManager.new @engine
um.table values.first.first.relation
um.set values
um.wheres = @ctx.wheres
@engine.connection.execute um.to_sql
end
# FIXME: this method should go away
def insert values
im = InsertManager.new @engine
......
......@@ -12,6 +12,10 @@ def table table
self
end
def wheres= exprs
@head.wheres = exprs
end
def where expr
@head.wheres << expr
self
......
......@@ -16,7 +16,7 @@ def visit_Arel_Nodes_UpdateStatement o
[
"UPDATE #{visit o.relation}",
("SET #{o.values.map { |column,value|
"#{quote_column_name(column.name)} = #{quote visit value}"
"#{quote_column_name(column.name)} = #{value ? quote(visit(value)) : 'NULL'}"
}.join ', '}" unless o.values.empty?),
("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?)
].compact.join ' '
......
require 'spec_helper'
module Arel
class EngineProxy
attr_reader :executed
def initialize engine
@engine = engine
@executed = []
end
def connection
self
end
def quote_table_name thing; @engine.connection.quote_table_name thing end
def quote_column_name thing; @engine.connection.quote_column_name thing end
def quote thing, column; @engine.connection.quote thing, column end
def execute sql
@executed << sql
end
end
describe 'select manager' do
describe 'update' do
it 'copies where clauses' do
engine = EngineProxy.new Table.engine
table = Table.new :users
manager = Arel::SelectManager.new engine
manager.where table[:id].eq 10
manager.from table
manager.update(table[:id] => 1)
engine.executed.last.should be_like %{
UPDATE "users" SET "id" = 1 WHERE "users"."id" = 10
}
end
it 'executes an update statement' do
engine = EngineProxy.new Table.engine
table = Table.new :users
manager = Arel::SelectManager.new engine
manager.from table
manager.update(table[:id] => 1)
engine.executed.last.should be_like %{
UPDATE "users" SET "id" = 1
}
end
end
describe 'project' do
it 'takes strings' do
table = Table.new :users
......
......@@ -9,6 +9,14 @@ module Arel
end
describe 'set' do
it "updates with null" do
table = Table.new(:users)
um = Arel::UpdateManager.new Table.engine
um.table table
um.set [[table[:name], nil]]
um.to_sql.should be_like %{ UPDATE "users" SET "name" = NULL }
end
it 'takes a list of lists' do
table = Table.new(:users)
um = Arel::UpdateManager.new Table.engine
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册