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.
-
#connection_key ⇒ Object
readonly
Returns the value of attribute connection_key.
-
#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.
-
#get_model_for(table_name) ⇒ Class
Get a dynamic AR model for a table.
-
#initialize(connection_key = nil) ⇒ 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(connection_key = nil) ⇒ Manager
Initialize the database manager
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dbviewer/database/manager.rb', line 10 def initialize(connection_key = nil) @connection_key = connection_key || Dbviewer.configuration.current_connection 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, @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 |
#connection_key ⇒ Object (readonly)
Returns the value of attribute connection_key.
6 7 8 |
# File 'lib/dbviewer/database/manager.rb', line 6 def connection_key @connection_key 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
136 137 138 |
# File 'lib/dbviewer/database/manager.rb', line 136 def clear_all_caches @cache_manager.clear_all end |
#column_count(table_name) ⇒ Integer
Get the number of columns in a table
86 87 88 |
# File 'lib/dbviewer/database/manager.rb', line 86 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
101 102 103 |
# File 'lib/dbviewer/database/manager.rb', line 101 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
26 27 28 |
# File 'lib/dbviewer/database/manager.rb', line 26 def configuration @configuration ||= Dbviewer.configuration end |
#execute_query(sql) ⇒ ActiveRecord::Result
Execute a raw SQL query after validating for safety
109 110 111 |
# File 'lib/dbviewer/database/manager.rb', line 109 def execute_query(sql) @query_executor.execute_query(sql) end |
#execute_sqlite_pragma(pragma) ⇒ ActiveRecord::Result
Execute a SQLite PRAGMA command without adding a LIMIT clause
117 118 119 |
# File 'lib/dbviewer/database/manager.rb', line 117 def execute_sqlite_pragma(pragma) @query_executor.execute_sqlite_pragma(pragma) end |
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys
131 132 133 |
# File 'lib/dbviewer/database/manager.rb', line 131 def fetch_foreign_keys(table_name) @table_metadata_manager.fetch_foreign_keys(table_name) end |
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes
124 125 126 |
# File 'lib/dbviewer/database/manager.rb', line 124 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
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/dbviewer/database/manager.rb', line 142 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
79 80 81 |
# File 'lib/dbviewer/database/manager.rb', line 79 def filtered_record_count(table_name, column_filters = {}) @table_query_operations.filtered_record_count(table_name, column_filters) end |
#get_model_for(table_name) ⇒ Class
Get a dynamic AR model for a table
158 159 160 |
# File 'lib/dbviewer/database/manager.rb', line 158 def get_model_for(table_name) @dynamic_model_factory.get_model_for(table_name) end |
#primary_key(table_name) ⇒ String?
Get the primary key of a table
93 94 95 |
# File 'lib/dbviewer/database/manager.rb', line 93 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)
71 72 73 |
# File 'lib/dbviewer/database/manager.rb', line 71 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
39 40 41 |
# File 'lib/dbviewer/database/manager.rb', line 39 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
53 54 55 |
# File 'lib/dbviewer/database/manager.rb', line 53 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)
46 47 48 |
# File 'lib/dbviewer/database/manager.rb', line 46 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
61 62 63 64 65 66 |
# File 'lib/dbviewer/database/manager.rb', line 61 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
32 33 34 |
# File 'lib/dbviewer/database/manager.rb', line 32 def tables @table_metadata_manager.tables end |