Class: Dbviewer::DatabaseManager

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabaseManager

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_nameObject (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

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/dbviewer/database_manager.rb', line 12

def connection
  @connection
end

#table_query_operationsObject (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_expiryObject

Get cache expiry from configuration



49
50
51
# File 'lib/dbviewer/database_manager.rb', line 49

def self.cache_expiry
  configuration.cache_expiry
end

.configurationObject

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_pageObject

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_recordsObject

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_cachesObject

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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Integer)

    Number of columns



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

Parameters:

  • table_name (String)

    Name of the table

  • column_name (String)

    Name of the column

Returns:

  • (Boolean)

    true if column exists, false otherwise



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

Parameters:

  • sql (String)

    SQL query to execute

Returns:

  • (ActiveRecord::Result)

    Result set with columns and rows

Raises:

  • (StandardError)

    If the query is invalid or unsafe



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

Parameters:

  • pragma (String)

    PRAGMA command to execute (without the “PRAGMA” keyword)

Returns:

  • (ActiveRecord::Result)

    Result set with the PRAGMA value

Raises:

  • (StandardError)

    If the query is invalid or cannot be executed



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Array<Hash>)

    List of foreign keys with details



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Array<Hash>)

    List of indexes with details



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

Parameters:

  • table_name (String)

    Name of the table

  • column_filters (Hash) (defaults to: {})

    Hash of column_name => filter_value for filtering

Returns:

  • (Integer)

    Number of filtered records



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (String, nil)

    Primary key column name or nil if not found



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

Parameters:

  • table_name (String)

    Name of the table

  • select (String, Array) (defaults to: nil)

    Columns to select

  • order (String, Hash) (defaults to: nil)

    Order by clause

  • limit (Integer) (defaults to: nil)

    Maximum number of records to return

  • offset (Integer) (defaults to: nil)

    Offset from which to start returning records

  • where (String, Hash) (defaults to: nil)

    Where conditions

Returns:

  • (ActiveRecord::Result)

    Result set with columns and rows



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)

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Integer)

    Number of records



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Array<Hash>)

    List of column details with name, type, null, default



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Integer)

    Number of records



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)

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Hash)

    Table metadata



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

Parameters:

  • table_name (String)

    Name of the table

  • query_params (TableQueryParams)

    Query parameters for pagination and sorting

Returns:

  • (ActiveRecord::Result)

    Result set with columns and rows



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

#tablesArray<String>

Returns a sorted list of all tables in the database

Returns:

  • (Array<String>)

    List of table names



55
56
57
# File 'lib/dbviewer/database_manager.rb', line 55

def tables
  @table_metadata_manager.tables
end