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.
-
#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
71 72 73 74 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 71 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
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 92 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
79 80 81 82 83 84 85 86 87 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 79 def fetch_indexes(table_name) @connection.indexes(table_name).map do |index| { name: index.name, columns: index.columns, unique: index.unique } end end |
#primary_key(table_name) ⇒ String?
Get the primary key of a table
60 61 62 63 64 65 |
# File 'lib/dbviewer/table_metadata_manager.rb', line 60 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 |
# 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) } @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 |