From 03724fb1789198cc394f6e8b69cf9404e03eddd7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 23 Sep 2010 15:45:00 -0700 Subject: caching visitor on a per engine basis --- lib/arel/tree_manager.rb | 17 ++--------------- lib/arel/visitors.rb | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/arel/tree_manager.rb b/lib/arel/tree_manager.rb index fb5037cd9f..8a13345c91 100644 --- a/lib/arel/tree_manager.rb +++ b/lib/arel/tree_manager.rb @@ -3,30 +3,17 @@ module Arel # FIXME: Remove this. include Arel::Relation - VISITORS = { - 'postgresql' => Arel::Visitors::PostgreSQL, - 'mysql' => Arel::Visitors::MySQL, - 'mysql2' => Arel::Visitors::MySQL, - } - - attr_writer :visitor + attr_accessor :visitor def initialize engine @engine = engine - @visitor = nil + @visitor = Visitors.visitor_for @engine end def to_dot Visitors::Dot.new.accept @head end - def visitor - return @visitor if @visitor - pool = @engine.connection_pool - adapter = pool.spec.config[:adapter] - @visitor = (VISITORS[adapter] || Visitors::ToSql).new(@engine) - end - def to_sql visitor.accept @head end diff --git a/lib/arel/visitors.rb b/lib/arel/visitors.rb index 22077d5943..879635575b 100644 --- a/lib/arel/visitors.rb +++ b/lib/arel/visitors.rb @@ -4,3 +4,23 @@ require 'arel/visitors/mysql' require 'arel/visitors/join_sql' require 'arel/visitors/order_clauses' require 'arel/visitors/dot' + +module Arel + module Visitors + VISITORS = { + 'postgresql' => Arel::Visitors::PostgreSQL, + 'mysql' => Arel::Visitors::MySQL, + 'mysql2' => Arel::Visitors::MySQL, + } + + ENGINE_VISITORS = Hash.new do |hash, engine| + pool = engine.connection_pool + adapter = pool.spec.config[:adapter] + hash[engine] = (VISITORS[adapter] || Visitors::ToSql).new(engine) + end + + def self.visitor_for engine + ENGINE_VISITORS[engine] + end + end +end -- cgit v1.2.3