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

basic updates are working

This commit is contained in:
Aaron Patterson 2010-08-14 18:29:32 -07:00
parent 74a27a0d83
commit dddf2d1f09
6 changed files with 84 additions and 0 deletions

View file

@ -10,6 +10,7 @@ require 'arel/relation'
require 'arel/tree_manager'
require 'arel/insert_manager'
require 'arel/select_manager'
require 'arel/update_manager'
require 'arel/nodes'
#### these are deprecated

View file

@ -3,3 +3,4 @@ require 'arel/nodes/sql_literal'
require 'arel/nodes/select_core'
require 'arel/nodes/select_statement'
require 'arel/nodes/insert_statement'
require 'arel/nodes/update_statement'

View file

@ -0,0 +1,12 @@
module Arel
module Nodes
class UpdateStatement
attr_accessor :relation, :wheres
def initialize
@relation = nil
@wheres = []
end
end
end
end

View file

@ -0,0 +1,20 @@
module Arel
class UpdateManager < Arel::TreeManager
def initialize engine
super
@head = Nodes::UpdateStatement.new
end
###
# UPDATE +table+
def table table
@head.relation = table
self
end
def where expr
@head.wheres << expr
self
end
end
end

View file

@ -12,6 +12,13 @@ module Arel
end
private
def visit_Arel_Nodes_UpdateStatement o
[
"UPDATE #{visit o.relation}",
("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?)
].compact.join ' '
end
def visit_Arel_Nodes_InsertStatement o
[
"INSERT INTO #{visit o.relation}",

View file

@ -0,0 +1,43 @@
require 'spec_helper'
module Arel
describe 'update manager' do
describe 'new' do
it 'takes an engine' do
Arel::UpdateManager.new Table.engine
end
end
describe 'table' do
it 'generates an update statement' do
um = Arel::UpdateManager.new Table.engine
um.table Table.new(:users)
um.to_sql.should be_like %{ UPDATE "users" }
end
it 'chains' do
um = Arel::UpdateManager.new Table.engine
um.table(Table.new(:users)).should == um
end
end
describe 'where' do
it 'generates a where clause' do
table = Table.new :users
um = Arel::UpdateManager.new Table.engine
um.table table
um.where table[:id].eq(1)
um.to_sql.should be_like %{
UPDATE "users" WHERE "users"."id" = 1
}
end
it 'chains' do
table = Table.new :users
um = Arel::UpdateManager.new Table.engine
um.table table
um.where(table[:id].eq(1)).should == um
end
end
end
end