Class: QueryGuard::Explain::AdapterInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/query_guard/explain/adapter_interface.rb

Overview

Base interface for database-specific EXPLAIN plan adapters. Implementations should handle:

  • Safe query execution without side effects
  • Plan data extraction in JSON format
  • Graceful error handling

Example:

adapter = PostgreSQLAdapter.new(connection)
plan = adapter.get_plan("SELECT * FROM users")
# => { "Plan" => { "Node Type" => "Seq Scan", ... }, ... }

Direct Known Subclasses

PostgreSQLAdapter

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ AdapterInterface

Initialize adapter with database connection

Parameters:

  • connection (Object) —

    Database-specific connection object



19
20
21
# File 'lib/query_guard/explain/adapter_interface.rb', line 19

def initialize(connection)
  @connection = connection
end

Instance Method Details

#can_explain?(sql) ⇒ Boolean

Check if adapter can execute EXPLAIN for this query type

Parameters:

  • sql (String) —

    SQL query

Returns:

  • (Boolean) —

    True if EXPLAIN is supported

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/query_guard/explain/adapter_interface.rb', line 50

def can_explain?(sql)
  raise NotImplementedError, "Subclasses must implement can_explain?"
end

#engine_name ⇒ Symbol

Get engine name for this adapter

Returns:

  • (Symbol) —

    Engine identifier (:postgresql, :mysql, etc.)

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/query_guard/explain/adapter_interface.rb', line 57

def engine_name
  raise NotImplementedError, "Subclasses must implement engine_name"
end

#get_plan(sql, options = {}) ⇒ Hash

Get EXPLAIN plan for a query (JSON format when possible)

Example return structure (PostgreSQL):

{
"Plan" => {
  "Node Type" => "Seq Scan",
  "Relation Name" => "users",
  "Plans" => [...],
  "Actual Rows" => 100,
  "Actual Total Time" => 2.543
},
"Planning Time" => 0.234,
"Execution Time" => 2.543
}

Parameters:

  • sql (String) —

    SQL query to analyze

  • options (Hash) (defaults to: {}) —

    Adapter-specific options

Returns:

  • (Hash) —

    Parsed plan JSON or nil if unavailable

Raises:



42
43
44
# File 'lib/query_guard/explain/adapter_interface.rb', line 42

def get_plan(sql, options = {})
  raise NotImplementedError, "Subclasses must implement get_plan"
end