Class: Sequel::ColdColDatabase::SchemaRegistry

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(db) ⇒ SchemaRegistry

Initialize a new schema registry for the given database.

Parameters:

  • db (Sequel::Database)

    the database instance this registry belongs to



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.

Parameters:

  • name (String)

    the literal table name from the database

  • columns (Array)

    array of [column_name, column_info] pairs



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.

Parameters:

  • name (String)

    the literal view name from the database

  • columns (Array)

    array of [column_name, column_info] pairs



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.

Parameters:

  • name (String, Symbol)

    the table name

  • columns (Array)

    array of [column_name, column_info] pairs



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:

  1. Created views (highest priority - most recent)

  2. Created tables (medium priority - session specific)

  3. Loaded schemas (lowest priority - external definitions)

Parameters:

  • name (String, Symbol)

    the table/view name to look up

Returns:

  • (Array<Symbol>, nil)

    array of column names as symbols, or nil if not found



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.

Parameters:

  • new_schemas (Hash)

    hash of table_name => column_definitions



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.

Parameters:

  • schemas_hash (Hash)

    the new schemas hash to use



139
140
141
# File 'lib/sequel/extensions/cold_col.rb', line 139

def set_schemas(schemas_hash)
  Sequel.synchronize { @schemas = schemas_hash }
end