Class: ActiveRecord::ConnectionAdapters::SchemaCache
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::SchemaCache
- Defined in:
- lib/active_record/connection_adapters/schema_cache.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#add(table_name) ⇒ Object
Add internal cache for table with
table_name
. -
#clear! ⇒ Object
Clears out internal caches.
-
#clear_table_cache!(table_name) ⇒ Object
Clear out internal caches for table with
table_name
. -
#columns(table_name) ⇒ Object
Get the columns for a table.
-
#columns_hash(table_name) ⇒ Object
Get the columns for a table as a hash, key is the column name value is the column object.
-
#initialize(conn) ⇒ SchemaCache
constructor
A new instance of SchemaCache.
- #marshal_dump ⇒ Object
- #marshal_load(array) ⇒ Object
- #primary_keys(table_name) ⇒ Object
- #size ⇒ Object
-
#table_exists?(name) ⇒ Boolean
A cached lookup for table existence.
- #tables(name) ⇒ Object
Constructor Details
#initialize(conn) ⇒ SchemaCache
Returns a new instance of SchemaCache.
7 8 9 10 11 12 13 14 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 7 def initialize(conn) @connection = conn @columns = {} @columns_hash = {} @primary_keys = {} @tables = {} end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
5 6 7 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 5 def connection @connection end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
4 5 6 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 4 def version @version end |
Instance Method Details
#add(table_name) ⇒ Object
Add internal cache for table with table_name
.
29 30 31 32 33 34 35 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 29 def add(table_name) if table_exists?(table_name) primary_keys(table_name) columns(table_name) columns_hash(table_name) end end |
#clear! ⇒ Object
Clears out internal caches
55 56 57 58 59 60 61 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 55 def clear! @columns.clear @columns_hash.clear @primary_keys.clear @tables.clear @version = nil end |
#clear_table_cache!(table_name) ⇒ Object
Clear out internal caches for table with table_name
.
70 71 72 73 74 75 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 70 def clear_table_cache!(table_name) @columns.delete table_name @columns_hash.delete table_name @primary_keys.delete table_name @tables.delete table_name end |
#columns(table_name) ⇒ Object
Get the columns for a table
42 43 44 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 42 def columns(table_name) @columns[table_name] ||= connection.columns(table_name) end |
#columns_hash(table_name) ⇒ Object
Get the columns for a table as a hash, key is the column name value is the column object.
48 49 50 51 52 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 48 def columns_hash(table_name) @columns_hash[table_name] ||= Hash[columns(table_name).map { |col| [col.name, col] }] end |
#marshal_dump ⇒ Object
77 78 79 80 81 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 77 def marshal_dump # if we get current version during initialization, it happens stack over flow. @version = ActiveRecord::Migrator.current_version [@version, @columns, @columns_hash, @primary_keys, @tables] end |
#marshal_load(array) ⇒ Object
83 84 85 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 83 def marshal_load(array) @version, @columns, @columns_hash, @primary_keys, @tables = array end |
#primary_keys(table_name) ⇒ Object
16 17 18 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 16 def primary_keys(table_name) @primary_keys[table_name] ||= table_exists?(table_name) ? connection.primary_key(table_name) : nil end |
#size ⇒ Object
63 64 65 66 67 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 63 def size [@columns, @columns_hash, @primary_keys, @tables].map { |x| x.size }.inject :+ end |
#table_exists?(name) ⇒ Boolean
A cached lookup for table existence.
21 22 23 24 25 26 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 21 def table_exists?(name) prepare_tables if @tables.empty? return @tables[name] if @tables.key? name @tables[name] = connection.table_exists?(name) end |
#tables(name) ⇒ Object
37 38 39 |
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 37 def tables(name) @tables[name] end |