the connection pool caches table_exists? calls

This commit is contained in:
Aaron Patterson
2011-02-07 09:25:57 -08:00
parent 285fdbae2b
commit 0de661d6c7
2 changed files with 16 additions and 8 deletions

View File

@@ -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

View File

@@ -851,6 +851,8 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_loading_with_conditions_on_join_model_preloads
Author.columns
authors = assert_queries(2) do
Author.find(:all, :include => :author_address, :joins => :comments, :conditions => "posts.title like 'Welcome%'")
end