diff options
-rw-r--r-- | lib/arel/nodes/join_source.rb | 4 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 5 | ||||
-rw-r--r-- | test/test_update_manager.rb | 13 |
3 files changed, 18 insertions, 4 deletions
diff --git a/lib/arel/nodes/join_source.rb b/lib/arel/nodes/join_source.rb index c57ad0e930..da828cf9ee 100644 --- a/lib/arel/nodes/join_source.rb +++ b/lib/arel/nodes/join_source.rb @@ -9,6 +9,10 @@ module Arel def initialize single_source, joinop = [] super end + + def empty? + !left && right.empty? + end end end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 8aec4cb147..ec62f4fb89 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -142,7 +142,7 @@ key on UpdateManager using UpdateManager#key= (visit(o.top) if o.top), (visit(o.set_quantifier) if o.set_quantifier), ("#{o.projections.map { |x| visit x }.join ', '}" unless o.projections.empty?), - (visit(o.source) if o.source), + ("FROM #{visit(o.source)}" if o.source && !o.source.empty?), ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?), ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?), (visit(o.having) if o.having), @@ -288,10 +288,7 @@ key on UpdateManager using UpdateManager#key= end def visit_Arel_Nodes_JoinSource o - return unless o.left || !o.right.empty? - [ - "FROM", (visit(o.left) if o.left), o.right.map { |j| visit j }.join(' ') ].compact.join ' ' diff --git a/test/test_update_manager.rb b/test/test_update_manager.rb index 62a2eccaf5..3099fb367e 100644 --- a/test/test_update_manager.rb +++ b/test/test_update_manager.rb @@ -62,6 +62,19 @@ module Arel um = Arel::UpdateManager.new Table.engine um.table(Table.new(:users)).must_equal um end + + it 'generates an update statement with joins' do + um = Arel::UpdateManager.new Table.engine + + table = Table.new(:users) + join_source = Arel::Nodes::JoinSource.new( + table, + [table.create_join(Table.new(:posts))] + ) + + um.table join_source + um.to_sql.must_be_like %{ UPDATE "users" INNER JOIN "posts" } + end end describe 'where' do |