From 79411322ae225289e1c051f4f68ed84c6349e4a0 Mon Sep 17 00:00:00 2001
From: Jon Leighton <j@jonathanleighton.com>
Date: Mon, 8 Aug 2011 23:23:51 +0100
Subject: Make it the responsibility of the connection to hold on to a visitor
 for generating SQL, rather than the TreeManager. (There is a related commit
 coming in Active Record.)

---
 lib/arel/nodes/node.rb      |  3 +--
 lib/arel/select_manager.rb  |  9 +++++----
 lib/arel/tree_manager.rb    | 12 +++++++-----
 lib/arel/visitors/to_sql.rb |  6 ++----
 4 files changed, 15 insertions(+), 15 deletions(-)

(limited to 'lib')

diff --git a/lib/arel/nodes/node.rb b/lib/arel/nodes/node.rb
index 1a5bc27856..4faace3782 100644
--- a/lib/arel/nodes/node.rb
+++ b/lib/arel/nodes/node.rb
@@ -32,8 +32,7 @@ module Arel
       #
       # Maybe we should just use `Table.engine`?  :'(
       def to_sql engine = Table.engine
-        viz = Visitors.for engine
-        viz.accept self
+        engine.connection.visitor.accept self
       end
 
       # Iterate through AST, nodes will be yielded depth-first
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb
index d95a259177..dc80150957 100644
--- a/lib/arel/select_manager.rb
+++ b/lib/arel/select_manager.rb
@@ -149,13 +149,13 @@ module Arel
 
     def wheres
       warn "#{caller[0]}: SelectManager#wheres is deprecated and will be removed in ARel 3.0.0 with no replacement"
-      Compatibility::Wheres.new @engine, @ctx.wheres
+      Compatibility::Wheres.new @engine.connection_pool, @ctx.wheres
     end
 
     def where_sql
       return if @ctx.wheres.empty?
 
-      viz = Visitors::WhereSql.new @engine
+      viz = Visitors::WhereSql.new @engine.connection_pool
       Nodes::SqlLiteral.new viz.accept @ctx
     end
 
@@ -205,12 +205,13 @@ module Arel
     def join_sql
       return nil if @ctx.source.right.empty?
 
-      sql = @visitor.dup.extend(Visitors::JoinSql).accept @ctx
+      sql = visitor.dup.extend(Visitors::JoinSql).accept @ctx
       Nodes::SqlLiteral.new sql
     end
 
     def order_clauses
-      Visitors::OrderClauses.new(@engine).accept(@ast).map { |x|
+      visitor = Visitors::OrderClauses.new(@engine.connection_pool)
+      visitor.accept(@ast).map { |x|
         Nodes::SqlLiteral.new x
       }
     end
diff --git a/lib/arel/tree_manager.rb b/lib/arel/tree_manager.rb
index 5722baca77..99a7164e2e 100644
--- a/lib/arel/tree_manager.rb
+++ b/lib/arel/tree_manager.rb
@@ -4,21 +4,23 @@ module Arel
     include Arel::Relation
     include Arel::FactoryMethods
 
-    attr_accessor :visitor
     attr_reader :ast, :engine
 
     def initialize engine
-      @engine  = engine
-      @visitor = Visitors.visitor_for @engine
-      @ctx     = nil
+      @engine = engine
+      @ctx    = nil
     end
 
     def to_dot
       Visitors::Dot.new.accept @ast
     end
 
+    def visitor
+      engine.connection.visitor
+    end
+
     def to_sql
-      @visitor.accept @ast
+      visitor.accept @ast
     end
 
     def initialize_copy other
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index ec62f4fb89..d424f8602e 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -4,17 +4,15 @@ require 'date'
 module Arel
   module Visitors
     class ToSql < Arel::Visitors::Visitor
-      def initialize engine
-        @engine         = engine
+      def initialize pool
+        @pool           = pool
         @connection     = nil
-        @pool           = nil
         @quoted_tables  = {}
         @quoted_columns = {}
       end
 
       def accept object
         self.last_column = nil
-        @pool = @engine.connection_pool
         @pool.with_connection do |conn|
           @connection = conn
           super
-- 
cgit v1.2.3