Class: Sequel::ColdColDatabase::SchemaRegistry
- Inherits:
-
Object
- Object
- Sequel::ColdColDatabase::SchemaRegistry
- Defined in:
- lib/sequel/extensions/cold_col.rb
Overview
Internal schema registry for managing column information across different sources. This class centralizes the storage and retrieval of table/view column metadata from multiple sources including created tables, views, and manually loaded schemas.
Instance Method Summary collapse
-
#add_created_table(name, columns) ⇒ Object
Record column information for a table created during the session.
-
#add_created_view(name, columns) ⇒ Object
Record column information for a view created during the session.
-
#add_schema(name, columns) ⇒ Object
Add schema information for a table that was manually specified.
-
#find_columns(name) ⇒ Array<Symbol>?
Find column information for a given table/view name.
-
#initialize(db) ⇒ SchemaRegistry
constructor
Initialize a new schema registry for the given database.
-
#merge_schemas(new_schemas) ⇒ Object
Merge new schema definitions into the existing schemas registry.
-
#set_schemas(schemas_hash) ⇒ Object
Directly set the schemas registry (primarily for test setup).
Constructor Details
#initialize(db) ⇒ SchemaRegistry
Initialize a new schema registry for the given database.
56 57 58 59 60 61 |
# File 'lib/sequel/extensions/cold_col.rb', line 56 def initialize(db) @db = db @created_tables = {} # Tables created during the session @created_views = {} # Views created during the session @schemas = {} # Manually loaded/added schemas end |
Instance Method Details
#add_created_table(name, columns) ⇒ Object
Record column information for a table created during the session. This method is called automatically when CREATE TABLE statements are executed.
77 78 79 |
# File 'lib/sequel/extensions/cold_col.rb', line 77 def add_created_table(name, columns) Sequel.synchronize { @created_tables[name] = columns } end |
#add_created_view(name, columns) ⇒ Object
Record column information for a view created during the session. This method is called automatically when CREATE VIEW statements are executed.
86 87 88 |
# File 'lib/sequel/extensions/cold_col.rb', line 86 def add_created_view(name, columns) Sequel.synchronize { @created_views[name] = columns } end |
#add_schema(name, columns) ⇒ Object
Add schema information for a table that was manually specified. This method stores schemas added via add_table_schema or load_schema.
68 69 70 |
# File 'lib/sequel/extensions/cold_col.rb', line 68 def add_schema(name, columns) Sequel.synchronize { @schemas[name.to_s] = columns } end |
#find_columns(name) ⇒ Array<Symbol>?
Find column information for a given table/view name. This method searches through all available registries in priority order:
-
Created views (highest priority - most recent)
-
Created tables (medium priority - session specific)
-
Loaded schemas (lowest priority - external definitions)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sequel/extensions/cold_col.rb', line 98 def find_columns(name) table_name = name.to_s literal_name = @db.literal(name) # Search through registries in priority order [@created_views, @created_tables, @schemas].each do |registry| next unless registry # Try literal representation first (most common for created tables/views) if (columns = Sequel.synchronize { registry[literal_name] }) return columns.map { |c, _| c } end # Try string representation (for manually added schemas) if (columns = Sequel.synchronize { registry[table_name] }) return columns.map { |c, _| c } end # Try finding by Sequel::LiteralString key (for test setup compatibility) registry.each_key do |key| if key.respond_to?(:to_s) && key.to_s == literal_name && (columns = Sequel.synchronize { registry[key] }) return columns.map { |c, _| c } end end end nil end |
#merge_schemas(new_schemas) ⇒ Object
Merge new schema definitions into the existing schemas registry. Used when loading schemas from YAML files.
131 132 133 |
# File 'lib/sequel/extensions/cold_col.rb', line 131 def merge_schemas(new_schemas) Sequel.synchronize { @schemas.merge!(new_schemas) } end |
#set_schemas(schemas_hash) ⇒ Object
Directly set the schemas registry (primarily for test setup). This method replaces the entire schemas hash.
139 140 141 |
# File 'lib/sequel/extensions/cold_col.rb', line 139 def set_schemas(schemas_hash) Sequel.synchronize { @schemas = schemas_hash } end |