diff options
-rw-r--r-- | lib/arel/crud.rb | 2 | ||||
-rw-r--r-- | lib/arel/nodes/update_statement.rb | 6 | ||||
-rw-r--r-- | lib/arel/update_manager.rb | 4 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 17 | ||||
-rw-r--r-- | test/test_select_manager.rb | 2 |
5 files changed, 25 insertions, 6 deletions
diff --git a/lib/arel/crud.rb b/lib/arel/crud.rb index ec58734456..e6b1cd9675 100644 --- a/lib/arel/crud.rb +++ b/lib/arel/crud.rb @@ -6,7 +6,7 @@ module Arel um = UpdateManager.new @engine if Nodes::SqlLiteral === values - relation = @ctx.froms + relation = @ctx.from else relation = values.first.first.relation end diff --git a/lib/arel/nodes/update_statement.rb b/lib/arel/nodes/update_statement.rb index 288e9f4676..c08f1b2c5e 100644 --- a/lib/arel/nodes/update_statement.rb +++ b/lib/arel/nodes/update_statement.rb @@ -2,13 +2,15 @@ module Arel module Nodes class UpdateStatement < Arel::Nodes::Node attr_accessor :relation, :wheres, :values, :orders, :limit + attr_accessor :key def initialize @relation = nil @wheres = [] @values = [] - @orders = [] - @limit = nil + @orders = [] + @limit = nil + @key = nil end def initialize_copy other diff --git a/lib/arel/update_manager.rb b/lib/arel/update_manager.rb index 821dce7d81..cf24dbac92 100644 --- a/lib/arel/update_manager.rb +++ b/lib/arel/update_manager.rb @@ -11,6 +11,10 @@ module Arel self end + def key= key + @ast.key = key + end + def order *expr @ast.orders = expr self diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 7669fd67ab..f25c0ee69f 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -42,20 +42,31 @@ module Arel if o.orders.empty? && o.limit.nil? wheres = o.wheres else + key = o.key + unless key + warn(<<-eowarn) if $VERBOSE +(#{caller.first}) Using UpdateManager without setting UpdateManager#key is +deprecated and support will be removed in ARel 3.0.0. Please set the primary +key on UpdateManager using UpdateManager#key= +eowarn + key = o.relation.primary_key + end + + wheres = o.wheres stmt = Nodes::SelectStatement.new core = stmt.cores.first core.froms = o.relation - core.projections = [o.relation.primary_key] + core.projections = [key] stmt.limit = o.limit stmt.orders = o.orders - wheres = [Nodes::In.new(o.relation.primary_key, [stmt])] + wheres = [Nodes::In.new(key, [stmt])] end [ "UPDATE #{visit o.relation}", ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?), - ("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?) + ("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?), ].compact.join ' ' end diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb index 3bfb3a529e..7c8da972e1 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -480,6 +480,7 @@ module Arel manager.from table manager.take 1 stmt = manager.compile_update(SqlLiteral.new('foo = bar')) + stmt.key = table['id'] stmt.to_sql.must_be_like %{ UPDATE "users" SET foo = bar @@ -494,6 +495,7 @@ module Arel manager.from table manager.order :foo stmt = manager.compile_update(SqlLiteral.new('foo = bar')) + stmt.key = table['id'] stmt.to_sql.must_be_like %{ UPDATE "users" SET foo = bar |