From 9653da3c7963834eb9ed93bf24837147fa9b0461 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 14 Dec 2010 09:43:19 -0800 Subject: adding a join source node --- lib/arel/visitors/depth_first.rb | 10 ++-------- lib/arel/visitors/dot.rb | 4 ++-- lib/arel/visitors/join_sql.rb | 17 +---------------- lib/arel/visitors/to_sql.rb | 16 +++++++++++++--- 4 files changed, 18 insertions(+), 29 deletions(-) (limited to 'lib/arel/visitors') diff --git a/lib/arel/visitors/depth_first.rb b/lib/arel/visitors/depth_first.rb index 49133653c5..9979c8313e 100644 --- a/lib/arel/visitors/depth_first.rb +++ b/lib/arel/visitors/depth_first.rb @@ -39,14 +39,6 @@ module Arel visit o.distinct end - def join o - visit o.left - visit o.right - visit o.constraint - end - alias :visit_Arel_Nodes_InnerJoin :join - alias :visit_Arel_Nodes_OuterJoin :join - def nary o o.children.each { |child| visit child } end @@ -65,6 +57,7 @@ module Arel alias :visit_Arel_Nodes_GreaterThan :binary alias :visit_Arel_Nodes_GreaterThanOrEqual :binary alias :visit_Arel_Nodes_In :binary + alias :visit_Arel_Nodes_InnerJoin :binary alias :visit_Arel_Nodes_LessThan :binary alias :visit_Arel_Nodes_LessThanOrEqual :binary alias :visit_Arel_Nodes_Matches :binary @@ -72,6 +65,7 @@ module Arel alias :visit_Arel_Nodes_NotIn :binary alias :visit_Arel_Nodes_Or :binary alias :visit_Arel_Nodes_Ordering :binary + alias :visit_Arel_Nodes_OuterJoin :binary alias :visit_Arel_Nodes_StringJoin :binary alias :visit_Arel_Nodes_TableAlias :binary alias :visit_Arel_Nodes_Values :binary diff --git a/lib/arel/visitors/dot.rb b/lib/arel/visitors/dot.rb index 4f80f31421..c525a64a89 100644 --- a/lib/arel/visitors/dot.rb +++ b/lib/arel/visitors/dot.rb @@ -36,7 +36,6 @@ module Arel def visit_Arel_Nodes_TableAlias o visit_edge o, "name" visit_edge o, "relation" - visit_edge o, "columns" end def visit_Arel_Nodes_Sum o @@ -90,7 +89,7 @@ module Arel end def visit_Arel_Nodes_SelectCore o - visit_edge o, "froms" + visit_edge o, "source" visit_edge o, "projections" visit_edge o, "wheres" end @@ -146,6 +145,7 @@ module Arel alias :visit_Arel_Nodes_NotIn :visit_Arel_Nodes_Equality alias :visit_Arel_Nodes_DoesNotMatch :visit_Arel_Nodes_Equality alias :visit_Arel_Nodes_Matches :visit_Arel_Nodes_Equality + alias :visit_Arel_Nodes_JoinSource :visit_Arel_Nodes_Equality def visit_String o @node_stack.last.fields << o diff --git a/lib/arel/visitors/join_sql.rb b/lib/arel/visitors/join_sql.rb index 427f2bb726..8c31934cd0 100644 --- a/lib/arel/visitors/join_sql.rb +++ b/lib/arel/visitors/join_sql.rb @@ -12,8 +12,7 @@ module Arel private def visit_Arel_Nodes_SelectCore o - return '' unless Nodes::Join === o.from - visit o.from + o.source.right.map { |j| visit j }.join ' ' end def visit_Arel_Nodes_StringJoin o @@ -22,20 +21,6 @@ module Arel visit(o.right) ].compact.join ' ' end - - def visit_Arel_Nodes_OuterJoin o - [ - (visit o.left if Nodes::Join === o.left), - "LEFT OUTER JOIN #{visit o.right} #{visit o.constraint if o.constraint}" - ].compact.join ' ' - end - - def visit_Arel_Nodes_InnerJoin o - [ - (visit o.left if Nodes::Join === o.left), - "INNER JOIN #{visit o.right} #{visit o.constraint if o.constraint}" - ].compact.join ' ' - end end end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 5767fbae8d..b83bf2f6e7 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -127,7 +127,7 @@ module Arel def visit_Arel_Nodes_SelectCore o [ "SELECT #{o.projections.map { |x| visit x }.join ', '}", - ("FROM #{visit o.from}" if o.from), + visit(o.source), ("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), @@ -217,16 +217,26 @@ module Arel "#{visit o.left} NOT LIKE #{visit o.right}" 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 ' ' + end + def visit_Arel_Nodes_StringJoin o "#{visit o.left} #{visit o.right}" end def visit_Arel_Nodes_OuterJoin o - "#{visit o.left} LEFT OUTER JOIN #{visit o.right} #{visit o.constraint}" + "LEFT OUTER JOIN #{visit o.left} #{visit o.right}" end def visit_Arel_Nodes_InnerJoin o - "#{visit o.left} INNER JOIN #{visit o.right} #{visit o.constraint if o.constraint}" + "INNER JOIN #{visit o.left} #{visit o.right if o.right}" end def visit_Arel_Nodes_On o -- cgit v1.2.3