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:
parent
74a27a0d83
commit
dddf2d1f09
6 changed files with 84 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
|
12
lib/arel/nodes/update_statement.rb
Normal file
12
lib/arel/nodes/update_statement.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
class UpdateStatement
|
||||
attr_accessor :relation, :wheres
|
||||
|
||||
def initialize
|
||||
@relation = nil
|
||||
@wheres = []
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
20
lib/arel/update_manager.rb
Normal file
20
lib/arel/update_manager.rb
Normal 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
|
|
@ -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}",
|
||||
|
|
43
spec/arel/update_manager_spec.rb
Normal file
43
spec/arel/update_manager_spec.rb
Normal 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
|
Loading…
Reference in a new issue