Permalink
Browse files

Support update statements containing joins

  • Loading branch information...
1 parent ac4fe77 commit 8fe010a5370f5249de962e1ad345d29a00aec70d @jonleighton jonleighton committed Aug 8, 2011
Showing with 18 additions and 4 deletions.
  1. +4 −0 lib/arel/nodes/join_source.rb
  2. +1 −4 lib/arel/visitors/to_sql.rb
  3. +13 −0 test/test_update_manager.rb
@@ -9,6 +9,10 @@ class JoinSource < Arel::Nodes::Binary
def initialize single_source, joinop = []
super
end
+
+ def empty?
+ !left && right.empty?
+ end
end
end
end
@@ -142,7 +142,7 @@ def visit_Arel_Nodes_SelectCore o
(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 @@ def visit_Arel_Nodes_DoesNotMatch o
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 ' '
@@ -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

0 comments on commit 8fe010a

Please sign in to comment.