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 24 |
# 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, @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 |
#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
137 138 139 |
# File 'lib/dbviewer/database/manager.rb', line 137 def clear_all_caches @cache_manager.clear_all end |
#column_count(table_name) ⇒ Integer
Get the number of columns in a table
87 88 89 |
# File 'lib/dbviewer/database/manager.rb', line 87 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
102 103 104 |
# File 'lib/dbviewer/database/manager.rb', line 102 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
27 28 29 |
# File 'lib/dbviewer/database/manager.rb', line 27 def configuration @configuration ||= Dbviewer.configuration end |
#execute_query(sql) ⇒ ActiveRecord::Result
Execute a raw SQL query after validating for safety
110 111 112 |
# File 'lib/dbviewer/database/manager.rb', line 110 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
118 119 120 |
# File 'lib/dbviewer/database/manager.rb', line 118 def execute_sqlite_pragma(pragma) @table_query_operations.execute_sqlite_pragma(pragma) end |
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys
132 133 134 |
# File 'lib/dbviewer/database/manager.rb', line 132 def fetch_foreign_keys(table_name) @table_metadata_manager.fetch_foreign_keys(table_name) end |
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes
125 126 127 |
# File 'lib/dbviewer/database/manager.rb', line 125 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
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/dbviewer/database/manager.rb', line 143 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
80 81 82 |
# File 'lib/dbviewer/database/manager.rb', line 80 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
159 160 161 |
# File 'lib/dbviewer/database/manager.rb', line 159 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
94 95 96 |
# File 'lib/dbviewer/database/manager.rb', line 94 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)
72 73 74 |
# File 'lib/dbviewer/database/manager.rb', line 72 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
40 41 42 |
# File 'lib/dbviewer/database/manager.rb', line 40 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
54 55 56 |
# File 'lib/dbviewer/database/manager.rb', line 54 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)
47 48 49 |
# File 'lib/dbviewer/database/manager.rb', line 47 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
62 63 64 65 66 67 |
# File 'lib/dbviewer/database/manager.rb', line 62 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
33 34 35 |
# File 'lib/dbviewer/database/manager.rb', line 33 def tables @table_metadata_manager.tables end |