diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-08-14 18:29:32 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-08-14 18:29:32 -0700 |
commit | dddf2d1f095230fde5e8704632df1ecca54e5ca6 (patch) | |
tree | 29386e06582ed029cf94218a87b7f71d88866254 | |
parent | 74a27a0d83ecd7ed4975851ea92ee517a1581faf (diff) | |
download | rails-dddf2d1f095230fde5e8704632df1ecca54e5ca6.tar.gz rails-dddf2d1f095230fde5e8704632df1ecca54e5ca6.tar.bz2 rails-dddf2d1f095230fde5e8704632df1ecca54e5ca6.zip |
basic updates are working
-rw-r--r-- | lib/arel.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/update_statement.rb | 12 | ||||
-rw-r--r-- | lib/arel/update_manager.rb | 20 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 7 | ||||
-rw-r--r-- | spec/arel/update_manager_spec.rb | 43 |
6 files changed, 84 insertions, 0 deletions
diff --git a/lib/arel.rb b/lib/arel.rb index e63e205779..5ffc46c934 100644 --- a/lib/arel.rb +++ b/lib/arel.rb @@ -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 diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index eed9641bbc..9dea981a06 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -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' diff --git a/lib/arel/nodes/update_statement.rb b/lib/arel/nodes/update_statement.rb new file mode 100644 index 0000000000..66996f11a3 --- /dev/null +++ b/lib/arel/nodes/update_statement.rb @@ -0,0 +1,12 @@ +module Arel + module Nodes + class UpdateStatement + attr_accessor :relation, :wheres + + def initialize + @relation = nil + @wheres = [] + end + end + end +end diff --git a/lib/arel/update_manager.rb b/lib/arel/update_manager.rb new file mode 100644 index 0000000000..3af73d2962 --- /dev/null +++ b/lib/arel/update_manager.rb @@ -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 diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index d47c621f6a..41d1299948 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -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}", diff --git a/spec/arel/update_manager_spec.rb b/spec/arel/update_manager_spec.rb new file mode 100644 index 0000000000..9d2346137f --- /dev/null +++ b/spec/arel/update_manager_spec.rb @@ -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 |