Module: Dbviewer

Defined in:
lib/dbviewer.rb,
lib/dbviewer/engine.rb,
lib/dbviewer/version.rb,
lib/dbviewer/query/logger.rb,
lib/dbviewer/query/parser.rb,
lib/dbviewer/storage/base.rb,
lib/dbviewer/configuration.rb,
lib/dbviewer/sql_validator.rb,
lib/dbviewer/query/analyzer.rb,
lib/dbviewer/query/executor.rb,
lib/dbviewer/database/manager.rb,
lib/dbviewer/query/collection.rb,
app/jobs/dbviewer/application_job.rb,
lib/dbviewer/storage/file_storage.rb,
lib/dbviewer/database/cache_manager.rb,
lib/dbviewer/datatable/query_params.rb,
app/models/dbviewer/application_record.rb,
lib/dbviewer/database/metadata_manager.rb,
lib/dbviewer/storage/in_memory_storage.rb,
app/helpers/dbviewer/application_helper.rb,
app/mailers/dbviewer/application_mailer.rb,
lib/dbviewer/datatable/query_operations.rb,
app/controllers/dbviewer/home_controller.rb,
app/controllers/dbviewer/logs_controller.rb,
lib/generators/dbviewer/install_generator.rb,
app/controllers/dbviewer/tables_controller.rb,
lib/dbviewer/database/dynamic_model_factory.rb,
app/controllers/dbviewer/api/base_controller.rb,
app/controllers/dbviewer/api/tables_controller.rb,
app/controllers/dbviewer/api/queries_controller.rb,
app/controllers/dbviewer/application_controller.rb,
app/controllers/dbviewer/connections_controller.rb,
app/controllers/concerns/dbviewer/error_handling.rb,
app/controllers/dbviewer/api/database_controller.rb,
app/controllers/concerns/dbviewer/pagination_concern.rb,
app/controllers/concerns/dbviewer/database_operations.rb,
app/controllers/dbviewer/entity_relationship_diagrams_controller.rb,
app/controllers/dbviewer/api/entity_relationship_diagrams_controller.rb

Defined Under Namespace

Modules: Api, ApplicationHelper, Database, DatabaseOperations, Datatable, ErrorHandling, Generators, PaginationConcern, Query, Storage Classes: ApplicationController, ApplicationJob, ApplicationMailer, ApplicationRecord, Configuration, ConnectionsController, Engine, EntityRelationshipDiagramsController, HomeController, LogsController, SqlValidator, TablesController

Constant Summary collapse

VERSION =
"0.6.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Get configuration settings



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

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Configure the engine with a block

Examples:

Dbviewer.configure do |config|
  config.per_page_options = [10, 25, 50]
  config.default_per_page = 25
end

Yields:



43
44
45
# File 'lib/dbviewer.rb', line 43

def configure
  yield(configuration) if block_given?
end

.initObject

Initialize engine with default values or user-provided configuration



100
101
102
103
104
105
106
107
108
109
# File 'lib/dbviewer.rb', line 100

def init
  # Define class methods to access configuration
  Dbviewer::SqlValidator.singleton_class.class_eval do
    define_method(:configuration) { Dbviewer.configuration }
    define_method(:max_query_length) { Dbviewer.configuration.max_query_length }
  end

  # Log initialization
  Rails.logger.info("[DBViewer] Initialized with configuration: #{configuration.inspect}")
end

.reset_configurationObject

Reset configuration to defaults



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

def reset_configuration
  @configuration = Configuration.new
end

.setupObject

This class method will be called by the engine when it’s appropriate



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dbviewer.rb', line 53

def setup
  # Configure the query logger with current configuration settings
  Dbviewer::Query::Logger.configure(
    enable_query_logging: configuration.enable_query_logging,
    query_logging_mode: configuration.query_logging_mode
  )

  # Check all configured connections
  connection_errors = []
  configuration.database_connections.each do |key, config|
    begin
      connection_class = nil

      if config[:connection]
        connection_class = config[:connection]
      elsif config[:connection_class].is_a?(String)
        # Try to load the class if it's defined as a string
        begin
          connection_class = config[:connection_class].constantize
        rescue NameError => e
          Rails.logger.warn "DBViewer could not load connection class #{config[:connection_class]}: #{e.message}"
          next
        end
      end

      if connection_class
        connection = connection_class.connection
        adapter_name = connection.adapter_name
        Rails.logger.info "DBViewer successfully connected to #{config[:name] || key.to_s} database (#{adapter_name})"

        # Store the resolved connection class back in the config
        config[:connection] = connection_class
      else
        raise "No valid connection configuration found for #{key}"
      end
    rescue => e
      connection_errors << { key: key, error: e.message }
      Rails.logger.error "DBViewer could not connect to #{config[:name] || key.to_s} database: #{e.message}"
    end
  end

  if connection_errors.length == configuration.database_connections.length
    raise "DBViewer could not connect to any configured database"
  end
end