diff options
-rw-r--r-- | lib/arel/crud.rb | 7 | ||||
-rw-r--r-- | lib/arel/nodes.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/unqualified_column.rb | 13 | ||||
-rw-r--r-- | lib/arel/update_manager.rb | 11 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 12 | ||||
-rw-r--r-- | spec/arel/select_manager_spec.rb | 10 | ||||
-rw-r--r-- | spec/arel/update_manager_spec.rb | 8 |
7 files changed, 56 insertions, 6 deletions
diff --git a/lib/arel/crud.rb b/lib/arel/crud.rb index 5b432cb2b6..7f78acef84 100644 --- a/lib/arel/crud.rb +++ b/lib/arel/crud.rb @@ -5,7 +5,12 @@ module Arel # FIXME: this method should go away def update values um = UpdateManager.new @engine - um.table values.first.first.relation + + if String === values + um.table @ctx.froms.last + else + um.table values.first.first.relation + end um.set values um.wheres = @ctx.wheres @engine.connection.execute um.to_sql diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 12759a462f..bf144ecaf8 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -8,3 +8,4 @@ require 'arel/nodes/select_core' require 'arel/nodes/select_statement' require 'arel/nodes/insert_statement' require 'arel/nodes/update_statement' +require 'arel/nodes/unqualified_column' diff --git a/lib/arel/nodes/unqualified_column.rb b/lib/arel/nodes/unqualified_column.rb new file mode 100644 index 0000000000..b86b553074 --- /dev/null +++ b/lib/arel/nodes/unqualified_column.rb @@ -0,0 +1,13 @@ +module Arel + module Nodes + class UnqualifiedColumn + def initialize attribute + @attribute = attribute + end + + def name + @attribute.name + end + end + end +end diff --git a/lib/arel/update_manager.rb b/lib/arel/update_manager.rb index f712103f98..cd4342529b 100644 --- a/lib/arel/update_manager.rb +++ b/lib/arel/update_manager.rb @@ -22,7 +22,16 @@ module Arel end def set values - @head.values = values + if String === values + @head.values = [values] + else + @head.values = values.map { |column,value| + Nodes::Equality.new( + Nodes::UnqualifiedColumn.new(column), + value + ) + } + end self end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 9dc7696ad8..fd467495ae 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -15,9 +15,7 @@ module Arel def visit_Arel_Nodes_UpdateStatement o [ "UPDATE #{visit o.relation}", - ("SET #{o.values.map { |column,value| - "#{quote_column_name(column.name)} = #{value ? quote(visit(value)) : 'NULL'}" - }.join ', '}" unless o.values.empty?), + ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?), ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?) ].compact.join ' ' end @@ -65,7 +63,13 @@ module Arel end def visit_Arel_Nodes_Equality o - "#{visit o.left} = #{quote visit o.right}" + right = o.right + right = right ? quote(visit(right)) : 'NULL' + "#{visit o.left} = #{right}" + end + + def visit_Arel_Nodes_UnqualifiedColumn o + "#{quote_column_name o.name}" end def visit_Arel_Attributes_Attribute o diff --git a/spec/arel/select_manager_spec.rb b/spec/arel/select_manager_spec.rb index 038475bf3d..a728366e0e 100644 --- a/spec/arel/select_manager_spec.rb +++ b/spec/arel/select_manager_spec.rb @@ -24,6 +24,16 @@ module Arel describe 'select manager' do describe 'update' do + it 'takes a string' do + engine = EngineProxy.new Table.engine + table = Table.new :users + manager = Arel::SelectManager.new engine + manager.from table + manager.update('foo = bar') + + engine.executed.last.should be_like %{ UPDATE "users" SET foo = bar } + end + it 'copies where clauses' do engine = EngineProxy.new Table.engine table = Table.new :users diff --git a/spec/arel/update_manager_spec.rb b/spec/arel/update_manager_spec.rb index 9c3789193e..8c4c4f93b8 100644 --- a/spec/arel/update_manager_spec.rb +++ b/spec/arel/update_manager_spec.rb @@ -17,6 +17,14 @@ module Arel um.to_sql.should be_like %{ UPDATE "users" SET "name" = NULL } end + it 'takes a string' do + table = Table.new(:users) + um = Arel::UpdateManager.new Table.engine + um.table table + um.set "foo = bar" + um.to_sql.should be_like %{ UPDATE "users" SET foo = bar } + end + it 'takes a list of lists' do table = Table.new(:users) um = Arel::UpdateManager.new Table.engine |