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.
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.
-
#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, page = 1, order_by = nil, direction = "ASC", per_page = nil) ⇒ 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
14 15 16 17 18 19 20 21 |
# File 'lib/dbviewer/database_manager.rb', line 14 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) reset_cache_if_needed end |
Instance Attribute Details
#adapter_name ⇒ Object (readonly)
Returns the value of attribute adapter_name.
11 12 13 |
# File 'lib/dbviewer/database_manager.rb', line 11 def adapter_name @adapter_name end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
11 12 13 |
# File 'lib/dbviewer/database_manager.rb', line 11 def connection @connection end |
Class Method Details
.cache_expiry ⇒ Object
Get cache expiry from configuration
39 40 41 |
# File 'lib/dbviewer/database_manager.rb', line 39 def self.cache_expiry configuration.cache_expiry end |
.configuration ⇒ Object
Get configuration from class method or Dbviewer
24 25 26 |
# File 'lib/dbviewer/database_manager.rb', line 24 def self.configuration Dbviewer.configuration end |
.default_per_page ⇒ Object
Get default per page from configuration
29 30 31 |
# File 'lib/dbviewer/database_manager.rb', line 29 def self.default_per_page configuration.default_per_page end |
.max_records ⇒ Object
Get max records from configuration
34 35 36 |
# File 'lib/dbviewer/database_manager.rb', line 34 def self.max_records configuration.max_records end |
Instance Method Details
#clear_all_caches ⇒ Object
Clear all caches - useful when schema changes are detected
199 200 201 |
# File 'lib/dbviewer/database_manager.rb', line 199 def clear_all_caches @cache_manager.clear_all end |
#column_count(table_name) ⇒ Integer
Get the number of columns in a table
116 117 118 |
# File 'lib/dbviewer/database_manager.rb', line 116 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
131 132 133 |
# File 'lib/dbviewer/database_manager.rb', line 131 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
139 140 141 |
# File 'lib/dbviewer/database_manager.rb', line 139 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
147 148 149 |
# File 'lib/dbviewer/database_manager.rb', line 147 def execute_sqlite_pragma(pragma) @query_executor.execute_sqlite_pragma(pragma) end |
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys
194 195 196 |
# File 'lib/dbviewer/database_manager.rb', line 194 def fetch_foreign_keys(table_name) @table_metadata_manager.fetch_foreign_keys(table_name) end |
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes
187 188 189 |
# File 'lib/dbviewer/database_manager.rb', line 187 def fetch_indexes(table_name) @table_metadata_manager.fetch_indexes(table_name) end |
#primary_key(table_name) ⇒ String?
Get the primary key of a table
123 124 125 |
# File 'lib/dbviewer/database_manager.rb', line 123 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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/dbviewer/database_manager.rb', line 159 def query_table(table_name, select: nil, order: nil, limit: nil, offset: nil, where: nil) model = get_model_for(table_name) query = model.all query = query.select(select) if select.present? query = query.where(where) if where.present? query = query.order(order) if order.present? # Get max records from configuration max_records = self.class.max_records query = query.limit([ limit || max_records, max_records ].min) # Apply safety limit query = query.offset(offset) if offset.present? # Get column names for the result set column_names = if select.is_a?(Array) select elsif select.is_a?(String) && !select.include?("*") select.split(",").map(&:strip) else table_columns(table_name).map { |c| c[:name] } end @query_executor.to_result_set(query, column_names) end |
#record_count(table_name) ⇒ Integer
Get the number of records in a table (alias for table_count)
109 110 111 |
# File 'lib/dbviewer/database_manager.rb', line 109 def record_count(table_name) table_count(table_name) end |
#table_columns(table_name) ⇒ Array<Hash>
Returns column information for a specific table
52 53 54 |
# File 'lib/dbviewer/database_manager.rb', line 52 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
66 67 68 69 |
# File 'lib/dbviewer/database_manager.rb', line 66 def table_count(table_name) model = get_model_for(table_name) model.count end |
#table_metadata(table_name) ⇒ Hash
Get detailed metadata about a table (primary keys, indexes, foreign keys)
59 60 61 |
# File 'lib/dbviewer/database_manager.rb', line 59 def (table_name) @table_metadata_manager.(table_name) end |
#table_records(table_name, page = 1, order_by = nil, direction = "ASC", per_page = nil) ⇒ ActiveRecord::Result
Get records from a table with pagination and sorting
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/dbviewer/database_manager.rb', line 78 def table_records(table_name, page = 1, order_by = nil, direction = "ASC", per_page = nil) page = [ 1, page.to_i ].max default_per_page = self.class.default_per_page max_records = self.class.max_records per_page = (per_page || default_per_page).to_i # Ensure we don't fetch too many records for performance/memory reasons per_page = [ per_page, max_records ].min model = get_model_for(table_name) query = model.all # Apply sorting if provided if order_by.present? && column_exists?(table_name, order_by) direction = %w[ASC DESC].include?(direction.to_s.upcase) ? direction.to_s.upcase : "ASC" query = query.order("#{connection.quote_column_name(order_by)} #{direction}") end # Apply pagination records = query.limit(per_page).offset((page - 1) * per_page) # Get column names for consistent ordering column_names = table_columns(table_name).map { |c| c[:name] } # Format results @query_executor.to_result_set(records, column_names) end |
#tables ⇒ Array<String>
Returns a sorted list of all tables in the database
45 46 47 |
# File 'lib/dbviewer/database_manager.rb', line 45 def tables @table_metadata_manager.tables end |