Class: Dbviewer::Database::Manager

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

Instance Method Summary collapse

Constructor Details

#initialize(connection_key = nil) ⇒ Manager

Initialize the database manager

Parameters:

  • connection_key (Symbol) (defaults to: nil)

    The key identifying the connection in configuration



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

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/dbviewer/database/manager.rb', line 6

def connection
  @connection
end

#connection_keyObject (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_operationsObject (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_cachesObject

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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Integer)

    Number of columns



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

Parameters:

  • table_name (String)

    Name of the table

  • column_name (String)

    Name of the column

Returns:

  • (Boolean)

    true if column exists, false otherwise



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

#configurationObject

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

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



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

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



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Array<Hash>)

    List of foreign keys with details



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Array<Hash>)

    List of indexes with details



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_sizeInteger?

Calculate the total size of the database schema

Returns:

  • (Integer, nil)

    Database size in bytes or nil if unsupported



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

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



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Class)

    ActiveRecord model class



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (String, nil)

    Primary key column name or nil if not found



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)

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Integer)

    Number of records



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Array<Hash>)

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



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

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Integer)

    Number of records



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)

Parameters:

  • table_name (String)

    Name of the table

Returns:

  • (Hash)

    Table metadata



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

Parameters:

Returns:

  • (ActiveRecord::Result)

    Result set with columns and rows



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

#tablesArray<String>

Returns a sorted list of all tables in the database

Returns:

  • (Array<String>)

    List of table names



32
33
34
# File 'lib/dbviewer/database/manager.rb', line 32

def tables
  @table_metadata_manager.tables
end