diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/visitors/depth_first.rb | 17 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 33 |
2 files changed, 36 insertions, 14 deletions
diff --git a/lib/arel/visitors/depth_first.rb b/lib/arel/visitors/depth_first.rb index 4d2ecfa5e1..458934f854 100644 --- a/lib/arel/visitors/depth_first.rb +++ b/lib/arel/visitors/depth_first.rb @@ -5,11 +5,22 @@ module Arel @block = block || Proc.new end + def accept object + visit object + end + private - def visit o - super - @block.call o + def visit object + send dispatch[object.class], object + rescue NoMethodError => e + raise e if respond_to?(dispatch[object.class], true) + superklass = object.class.ancestors.find { |klass| + respond_to?(dispatch[klass], true) + } + raise(TypeError, "Cannot visit #{object.class}") unless superklass + dispatch[object.class] = dispatch[superklass] + retry end def unary o diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index a137e2a708..b60edeb720 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -280,12 +280,14 @@ module Arel "WITH #{o.children.map { |x| visit x }.join(', ')}" end - def visit_Arel_Nodes_WithRecursive o - "WITH RECURSIVE #{o.children.map { |x| visit x }.join(', ')}" + def visit_Arel_Nodes_WithRecursive o, collector + collector << "WITH RECURSIVE " + inject_join o.children, collector, ', ' end - def visit_Arel_Nodes_Union o - "( #{visit o.left} UNION #{visit o.right} )" + def visit_Arel_Nodes_Union o, collector + collector << "( " + infix_value(o, collector, " UNION ") << " )" end def visit_Arel_Nodes_UnionAll o @@ -296,7 +298,7 @@ module Arel "( #{visit o.left} INTERSECT #{visit o.right} )" end - def visit_Arel_Nodes_Except o + def visit_Arel_Nodes_Except o, collector "( #{visit o.left} EXCEPT #{visit o.right} )" end @@ -393,8 +395,8 @@ module Arel collector end - def visit_Arel_Nodes_Lock o - visit o.expr + def visit_Arel_Nodes_Lock o, collector + visit o.expr, collector end def visit_Arel_Nodes_Grouping o, collector @@ -522,16 +524,19 @@ module Arel raise NotImplementedError, '!~ not implemented for this db' end - def visit_Arel_Nodes_StringJoin o - visit o.left + def visit_Arel_Nodes_StringJoin o, collector + visit o.left, collector end def visit_Arel_Nodes_FullOuterJoin o "FULL OUTER JOIN #{visit o.left} #{visit o.right}" end - def visit_Arel_Nodes_OuterJoin o - "LEFT OUTER JOIN #{visit o.left} #{visit o.right}" + def visit_Arel_Nodes_OuterJoin o, collector + collector << "LEFT OUTER JOIN " + collector = visit o.left, collector + collector << " " + visit o.right, collector end def visit_Arel_Nodes_RightOuterJoin o @@ -735,6 +740,12 @@ module Arel } end + def infix_value o, collector, value + collector = visit o.left, collector + collector << " UNION " + visit o.right, collector + end + def aggregate name, o, collector collector << "#{name}(" if o.distinct |