Class: Dbviewer::Database::Manager
- Inherits:
-
Object
- Object
- Dbviewer::Database::Manager
- Defined in:
- lib/dbviewer/database/manager.rb
Overview
Manager handles all database interactions for the DBViewer engine It provides methods to access database structure and data
Instance Attribute Summary collapse
-
#adapter_name ⇒ Object
readonly
Returns the value of attribute adapter_name.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#table_query_operations ⇒ Object
readonly
Returns the value of attribute table_query_operations.
Instance Method Summary collapse
-
#clear_all_caches ⇒ Object
Clear all caches - useful when schema changes are detected.
-
#column_count(table_name) ⇒ Integer
Get the number of columns in a table.
-
#column_exists?(table_name, column_name) ⇒ Boolean
Check if a column exists in a table.
-
#configuration ⇒ Object
Get configuration from class method or Dbviewer.
-
#execute_query(sql) ⇒ ActiveRecord::Result
Execute a raw SQL query after validating for safety.
-
#execute_sqlite_pragma(pragma) ⇒ ActiveRecord::Result
Execute a SQLite PRAGMA command without adding a LIMIT clause.
-
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys.
-
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes.
-
#fetch_schema_size ⇒ Integer?
Calculate the total size of the database schema.
-
#filtered_record_count(table_name, column_filters = {}) ⇒ Integer
Get the number of records in a table with filters applied.
-
#initialize ⇒ Manager
constructor
Initialize the database manager.
-
#primary_key(table_name) ⇒ String?
Get the primary key of a table.
-
#record_count(table_name) ⇒ Integer
Get the number of records in a table (alias for table_count).
-
#table_columns(table_name) ⇒ Array<Hash>
Returns column information for a specific table.
-
#table_count(table_name) ⇒ Integer
Get the total count of records in a table.
-
#table_metadata(table_name) ⇒ Hash
Get detailed metadata about a table (primary keys, indexes, foreign keys).
-
#table_records(table_name, query_params) ⇒ ActiveRecord::Result
Get records from a table with pagination and sorting.
-
#tables ⇒ Array<String>
Returns a sorted list of all tables in the database.
Constructor Details
#initialize ⇒ Manager
Initialize the database manager
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dbviewer/database/manager.rb', line 9 def initialize ensure_connection @cache_manager = ::Dbviewer::Database::CacheManager.new(configuration.cache_expiry) @table_metadata_manager = ::Dbviewer::Database::MetadataManager.new(@connection, @cache_manager) @dynamic_model_factory = ::Dbviewer::Database::DynamicModelFactory.new(@connection, @cache_manager) @query_executor = ::Dbviewer::Query::Executor.new(@connection, configuration) @table_query_operations = ::Dbviewer::Datatable::QueryOperations.new( @connection, @dynamic_model_factory, @query_executor, @table_metadata_manager ) reset_cache_if_needed end |
Instance Attribute Details
#adapter_name ⇒ Object (readonly)
Returns the value of attribute adapter_name.
6 7 8 |
# File 'lib/dbviewer/database/manager.rb', line 6 def adapter_name @adapter_name end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/dbviewer/database/manager.rb', line 6 def connection @connection end |
#table_query_operations ⇒ Object (readonly)
Returns the value of attribute table_query_operations.
6 7 8 |
# File 'lib/dbviewer/database/manager.rb', line 6 def table_query_operations @table_query_operations end |
Instance Method Details
#clear_all_caches ⇒ Object
Clear all caches - useful when schema changes are detected
135 136 137 |
# File 'lib/dbviewer/database/manager.rb', line 135 def clear_all_caches @cache_manager.clear_all end |
#column_count(table_name) ⇒ Integer
Get the number of columns in a table
85 86 87 |
# File 'lib/dbviewer/database/manager.rb', line 85 def column_count(table_name) table_columns(table_name).size end |
#column_exists?(table_name, column_name) ⇒ Boolean
Check if a column exists in a table
100 101 102 |
# File 'lib/dbviewer/database/manager.rb', line 100 def column_exists?(table_name, column_name) @table_metadata_manager.column_exists?(table_name, column_name) end |
#configuration ⇒ Object
Get configuration from class method or Dbviewer
25 26 27 |
# File 'lib/dbviewer/database/manager.rb', line 25 def configuration @configuration ||= Dbviewer.configuration end |
#execute_query(sql) ⇒ ActiveRecord::Result
Execute a raw SQL query after validating for safety
108 109 110 |
# File 'lib/dbviewer/database/manager.rb', line 108 def execute_query(sql) @table_query_operations.execute_query(sql) end |
#execute_sqlite_pragma(pragma) ⇒ ActiveRecord::Result
Execute a SQLite PRAGMA command without adding a LIMIT clause
116 117 118 |
# File 'lib/dbviewer/database/manager.rb', line 116 def execute_sqlite_pragma(pragma) @table_query_operations.execute_sqlite_pragma(pragma) end |
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys
130 131 132 |
# File 'lib/dbviewer/database/manager.rb', line 130 def fetch_foreign_keys(table_name) @table_metadata_manager.fetch_foreign_keys(table_name) end |
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes
123 124 125 |
# File 'lib/dbviewer/database/manager.rb', line 123 def fetch_indexes(table_name) @table_metadata_manager.fetch_indexes(table_name) end |
#fetch_schema_size ⇒ Integer?
Calculate the total size of the database schema
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/dbviewer/database/manager.rb', line 141 def fetch_schema_size case adapter_name when /mysql/ fetch_mysql_size when /postgres/ fetch_postgres_size when /sqlite/ fetch_sqlite_size else nil # Unsupported database type for size calculation end end |
#filtered_record_count(table_name, column_filters = {}) ⇒ Integer
Get the number of records in a table with filters applied
78 79 80 |
# File 'lib/dbviewer/database/manager.rb', line 78 def filtered_record_count(table_name, column_filters = {}) @table_query_operations.filtered_record_count(table_name, column_filters) end |
#primary_key(table_name) ⇒ String?
Get the primary key of a table
92 93 94 |
# File 'lib/dbviewer/database/manager.rb', line 92 def primary_key(table_name) @table_metadata_manager.primary_key(table_name) end |
#record_count(table_name) ⇒ Integer
Get the number of records in a table (alias for table_count)
70 71 72 |
# File 'lib/dbviewer/database/manager.rb', line 70 def record_count(table_name) @table_query_operations.record_count(table_name) end |
#table_columns(table_name) ⇒ Array<Hash>
Returns column information for a specific table
38 39 40 |
# File 'lib/dbviewer/database/manager.rb', line 38 def table_columns(table_name) @table_metadata_manager.table_columns(table_name) end |
#table_count(table_name) ⇒ Integer
Get the total count of records in a table
52 53 54 |
# File 'lib/dbviewer/database/manager.rb', line 52 def table_count(table_name) @table_query_operations.table_count(table_name) end |
#table_metadata(table_name) ⇒ Hash
Get detailed metadata about a table (primary keys, indexes, foreign keys)
45 46 47 |
# File 'lib/dbviewer/database/manager.rb', line 45 def (table_name) @table_metadata_manager.(table_name) end |
#table_records(table_name, query_params) ⇒ ActiveRecord::Result
Get records from a table with pagination and sorting
60 61 62 63 64 65 |
# File 'lib/dbviewer/database/manager.rb', line 60 def table_records(table_name, query_params) @table_query_operations.table_records( table_name, query_params ) end |
#tables ⇒ Array<String>
Returns a sorted list of all tables in the database
31 32 33 |
# File 'lib/dbviewer/database/manager.rb', line 31 def tables @table_metadata_manager.tables end |