From 0de661d6c74172a9fedcced6a4e99d007df953ef Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 7 Feb 2011 09:25:57 -0800 Subject: the connection pool caches table_exists? calls --- .../abstract/connection_pool.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 4475019e0e..b1754e61df 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -59,7 +59,7 @@ module ActiveRecord class ConnectionPool attr_accessor :automatic_reconnect attr_reader :spec, :connections - attr_reader :columns, :columns_hash, :primary_keys + attr_reader :columns, :columns_hash, :primary_keys, :tables # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification # object which describes database connection information (e.g. adapter, @@ -84,12 +84,7 @@ module ActiveRecord @connections = [] @checked_out = [] @automatic_reconnect = true - - @tables = Hash.new do |h, table_name| - with_connection do |conn| - h[table_name] = conn.table_exists?(table_name) - end - end + @tables = {} @columns = Hash.new do |h, table_name| h[table_name] = with_connection do |conn| @@ -113,11 +108,22 @@ module ActiveRecord @primary_keys = Hash.new do |h, table_name| h[table_name] = with_connection do |conn| - @tables[table_name] ? conn.primary_key(table_name) : 'id' + table_exists?(table_name) ? conn.primary_key(table_name) : 'id' end end end + # A cached lookup for table existence + def table_exists?(name) + return true if @tables.key? name + + with_connection do |conn| + conn.tables.each { |table| @tables[table] = true } + end + + @tables.key? name + end + # Clears out internal caches: # # * columns -- cgit v1.2.3