Class: Dbviewer::DatabaseManager
- Inherits:
-
Object
- Object
- Dbviewer::DatabaseManager
- Defined in:
- lib/dbviewer/database_manager.rb
Overview
DatabaseManager 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
(also: #query_operations)
readonly
Returns the value of attribute table_query_operations.
Class Method Summary collapse
-
.cache_expiry ⇒ Object
Get cache expiry from configuration.
-
.configuration ⇒ Object
Get configuration from class method or Dbviewer.
-
.default_per_page ⇒ Object
Get default per page from configuration.
-
.max_records ⇒ Object
Get max records from configuration.
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.
-
#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.
-
#filtered_record_count(table_name, column_filters = {}) ⇒ Integer
Get the number of records in a table with filters applied.
-
#initialize ⇒ DatabaseManager
constructor
Initialize the database manager.
-
#primary_key(table_name) ⇒ String?
Get the primary key of a table.
-
#query_table(table_name, select: nil, order: nil, limit: nil, offset: nil, where: nil) ⇒ ActiveRecord::Result
Query a table with more granular control using ActiveRecord.
-
#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 ⇒ DatabaseManager
Initialize the database manager
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dbviewer/database_manager.rb', line 18 def initialize ensure_connection @cache_manager = CacheManager.new(self.class.configuration) @table_metadata_manager = TableMetadataManager.new(@connection, @cache_manager) @dynamic_model_factory = DynamicModelFactory.new(@connection, @cache_manager) @query_executor = QueryExecutor.new(@connection, self.class.configuration) @table_query_operations = TableQueryOperations.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.
12 13 14 |
# File 'lib/dbviewer/database_manager.rb', line 12 def adapter_name @adapter_name end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
12 13 14 |
# File 'lib/dbviewer/database_manager.rb', line 12 def connection @connection end |
#table_query_operations ⇒ Object (readonly) Also known as: query_operations
Returns the value of attribute table_query_operations.
12 13 14 |
# File 'lib/dbviewer/database_manager.rb', line 12 def table_query_operations @table_query_operations end |
Class Method Details
.cache_expiry ⇒ Object
Get cache expiry from configuration
49 50 51 |
# File 'lib/dbviewer/database_manager.rb', line 49 def self.cache_expiry configuration.cache_expiry end |
.configuration ⇒ Object
Get configuration from class method or Dbviewer
34 35 36 |
# File 'lib/dbviewer/database_manager.rb', line 34 def self.configuration Dbviewer.configuration end |
.default_per_page ⇒ Object
Get default per page from configuration
39 40 41 |
# File 'lib/dbviewer/database_manager.rb', line 39 def self.default_per_page configuration.default_per_page end |
.max_records ⇒ Object
Get max records from configuration
44 45 46 |
# File 'lib/dbviewer/database_manager.rb', line 44 def self.max_records configuration.max_records end |
Instance Method Details
#clear_all_caches ⇒ Object
Clear all caches - useful when schema changes are detected
179 180 181 |
# File 'lib/dbviewer/database_manager.rb', line 179 def clear_all_caches @cache_manager.clear_all end |
#column_count(table_name) ⇒ Integer
Get the number of columns in a table
109 110 111 |
# File 'lib/dbviewer/database_manager.rb', line 109 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
124 125 126 |
# File 'lib/dbviewer/database_manager.rb', line 124 def column_exists?(table_name, column_name) @table_metadata_manager.column_exists?(table_name, column_name) end |
#execute_query(sql) ⇒ ActiveRecord::Result
Execute a raw SQL query after validating for safety
132 133 134 |
# File 'lib/dbviewer/database_manager.rb', line 132 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
140 141 142 |
# File 'lib/dbviewer/database_manager.rb', line 140 def execute_sqlite_pragma(pragma) @table_query_operations.execute_sqlite_pragma(pragma) end |
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys
174 175 176 |
# File 'lib/dbviewer/database_manager.rb', line 174 def fetch_foreign_keys(table_name) @table_metadata_manager.fetch_foreign_keys(table_name) end |
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes
167 168 169 |
# File 'lib/dbviewer/database_manager.rb', line 167 def fetch_indexes(table_name) @table_metadata_manager.fetch_indexes(table_name) end |
#filtered_record_count(table_name, column_filters = {}) ⇒ Integer
Get the number of records in a table with filters applied
102 103 104 |
# File 'lib/dbviewer/database_manager.rb', line 102 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
116 117 118 |
# File 'lib/dbviewer/database_manager.rb', line 116 def primary_key(table_name) @table_metadata_manager.primary_key(table_name) end |
#query_table(table_name, select: nil, order: nil, limit: nil, offset: nil, where: nil) ⇒ ActiveRecord::Result
Query a table with more granular control using ActiveRecord
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/dbviewer/database_manager.rb', line 152 def query_table(table_name, select: nil, order: nil, limit: nil, offset: nil, where: nil) @table_query_operations.query_table( table_name, select: select, order: order, limit: limit, offset: offset, where: where, max_records: self.class.max_records ) end |
#record_count(table_name) ⇒ Integer
Get the number of records in a table (alias for table_count)
94 95 96 |
# File 'lib/dbviewer/database_manager.rb', line 94 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
62 63 64 |
# File 'lib/dbviewer/database_manager.rb', line 62 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
76 77 78 |
# File 'lib/dbviewer/database_manager.rb', line 76 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)
69 70 71 |
# File 'lib/dbviewer/database_manager.rb', line 69 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
84 85 86 87 88 89 |
# File 'lib/dbviewer/database_manager.rb', line 84 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
55 56 57 |
# File 'lib/dbviewer/database_manager.rb', line 55 def tables @table_metadata_manager.tables end |