Class: Dbviewer::TableMetadataManager
- Inherits:
-
Object
- Object
- Dbviewer::TableMetadataManager
- Defined in:
- lib/dbviewer/table_metadata_manager.rb
Overview
TableMetadataManager handles retrieving and managing table structure information
Instance Method Summary collapse
-
#column_exists?(table_name, column_name) ⇒ Boolean
Check if a column exists in a table.
-
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys.
-
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes.
-
#fetch_reverse_foreign_keys(table_name) ⇒ Array<Hash>
Get reverse foreign keys (tables that reference this table).
-
#initialize(connection, cache_manager) ⇒ TableMetadataManager
constructor
Initialize with a connection and cache manager.
-
#primary_key(table_name) ⇒ String?
Get the primary key of a table.
-
#table_columns(table_name) ⇒ Array<Hash>
Get column information for a table.
-
#table_metadata(table_name) ⇒ Hash
Get detailed metadata about a table.
-
#tables ⇒ Array<String>
Get all tables in the database.
Constructor Details
#initialize(connection, cache_manager) ⇒ TableMetadataManager
Initialize with a connection and cache manager
7 8 9 10 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 7 def initialize(connection, cache_manager) @connection = connection @cache_manager = cache_manager end |
Instance Method Details
#column_exists?(table_name, column_name) ⇒ Boolean
Check if a column exists in a table
72 73 74 75 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 72 def column_exists?(table_name, column_name) columns = table_columns(table_name) columns.any? { |col| col[:name].to_s == column_name.to_s } end |
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 93 def fetch_foreign_keys(table_name) @connection.foreign_keys(table_name).map do |fk| { name: fk.name, from_table: fk.from_table, to_table: fk.to_table, column: fk.column, primary_key: fk.primary_key } end end |
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes
80 81 82 83 84 85 86 87 88 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 80 def fetch_indexes(table_name) @connection.indexes(table_name).map do |index| { name: index.name, columns: index.columns, unique: index.unique } end end |
#fetch_reverse_foreign_keys(table_name) ⇒ Array<Hash>
Get reverse foreign keys (tables that reference this table)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 108 def fetch_reverse_foreign_keys(table_name) reverse_fks = [] # Get all tables and check their foreign keys tables.each do |other_table| next if other_table == table_name begin @connection.foreign_keys(other_table).each do |fk| if fk.to_table == table_name reverse_fks << { name: fk.name, from_table: fk.from_table, to_table: fk.to_table, column: fk.column, primary_key: fk.primary_key, relationship_type: "has_many" } end end rescue => e Rails.logger.error("[DBViewer] Error retrieving foreign keys for table #{other_table}: #{e.}") end end reverse_fks end |
#primary_key(table_name) ⇒ String?
Get the primary key of a table
61 62 63 64 65 66 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 61 def primary_key(table_name) @connection.primary_key(table_name) rescue => e Rails.logger.error("[DBViewer] Error retrieving primary key for table #{table_name}: #{e.}") nil end |
#table_columns(table_name) ⇒ Array<Hash>
Get column information for a table
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 21 def table_columns(table_name) cached_columns = @cache_manager.get_columns(table_name) return cached_columns if cached_columns columns = @connection.columns(table_name).map do |column| { name: column.name, type: column.type, null: column.null, default: column.default, primary: column.name == primary_key(table_name) } end # Cache the result @cache_manager.store_columns(table_name, columns) columns end |
#table_metadata(table_name) ⇒ Hash
Get detailed metadata about a table
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 43 def (table_name) = @cache_manager.(table_name) return if = { primary_key: primary_key(table_name), indexes: fetch_indexes(table_name), foreign_keys: fetch_foreign_keys(table_name), reverse_foreign_keys: fetch_reverse_foreign_keys(table_name) } @cache_manager.(table_name, ) end |
#tables ⇒ Array<String>
Get all tables in the database
14 15 16 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 14 def tables @connection.tables.sort end |