diff options
-rw-r--r-- | lib/arel/nodes/update_statement.rb | 3 | ||||
-rw-r--r-- | lib/arel/update_manager.rb | 5 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 3 | ||||
-rw-r--r-- | spec/arel/update_manager_spec.rb | 18 |
4 files changed, 28 insertions, 1 deletions
diff --git a/lib/arel/nodes/update_statement.rb b/lib/arel/nodes/update_statement.rb index 66996f11a3..3df54bb3a9 100644 --- a/lib/arel/nodes/update_statement.rb +++ b/lib/arel/nodes/update_statement.rb @@ -1,11 +1,12 @@ module Arel module Nodes class UpdateStatement - attr_accessor :relation, :wheres + attr_accessor :relation, :wheres, :values def initialize @relation = nil @wheres = [] + @values = nil end end end diff --git a/lib/arel/update_manager.rb b/lib/arel/update_manager.rb index 3af73d2962..cc2f973842 100644 --- a/lib/arel/update_manager.rb +++ b/lib/arel/update_manager.rb @@ -16,5 +16,10 @@ module Arel @head.wheres << expr self end + + def set values + @head.values = values + self + end end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 41d1299948..9a9dd20d52 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -15,6 +15,9 @@ module Arel def visit_Arel_Nodes_UpdateStatement o [ "UPDATE #{visit o.relation}", + ("SET #{o.values.map { |column,value| + "#{quote_column_name(column.name)} = #{quote visit value}" + }.join ', '}" if o.values), ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?) ].compact.join ' ' end diff --git a/spec/arel/update_manager_spec.rb b/spec/arel/update_manager_spec.rb index 9d2346137f..97b497b377 100644 --- a/spec/arel/update_manager_spec.rb +++ b/spec/arel/update_manager_spec.rb @@ -8,6 +8,24 @@ module Arel end end + describe 'set' do + it 'takes a list of lists' do + table = Table.new(:users) + um = Arel::UpdateManager.new Table.engine + um.table table + um.set [[table[:id], 1], [table[:name], 'hello']] + um.to_sql.should be_like %{ + UPDATE "users" SET "id" = 1, "name" = 'hello' + } + end + + it 'chains' do + table = Table.new(:users) + um = Arel::UpdateManager.new Table.engine + um.set([[table[:id], 1], [table[:name], 'hello']]).should == um + end + end + describe 'table' do it 'generates an update statement' do um = Arel::UpdateManager.new Table.engine |