Class: PgReports::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_reports/executor.rb

Overview

Executes SQL queries and returns results.

The connection is resolved lazily on every #execute call so that thread-local context set by PgReports.with_target / with_database is honored even when an Executor instance has been memoized at the module level.

Instance Method Summary collapse

Constructor Details

#initialize(connection: nil) ⇒ Executor

Returns a new instance of Executor.



10
11
12
# File 'lib/pg_reports/executor.rb', line 10

def initialize(connection: nil)
  @connection_override = connection
end

Instance Method Details

#connectionObject

Resolved on every call: explicit override > thread-local > registry default.



33
34
35
# File 'lib/pg_reports/executor.rb', line 33

def connection
  @connection_override || PgReports.config.connection
end

#execute(sql, **params) ⇒ Object

Execute raw SQL and return results as array of hashes.

Every query is tagged with the "PgReports" AR statement name so the Query Monitor can skip our own queries by name (see QueryMonitor#should_skip?), reliably and independent of backtrace depth — the internal live_metrics / status polling would otherwise leak into the monitor's history.



26
27
28
29
30
# File 'lib/pg_reports/executor.rb', line 26

def execute(sql, **params)
  processed_sql = interpolate_params(sql, params)
  result = connection.exec_query(processed_sql, "PgReports")
  result.to_a
end

#execute_from_file(category, name, **params) ⇒ Object

Execute SQL from a file and return results as array of hashes



15
16
17
18
# File 'lib/pg_reports/executor.rb', line 15

def execute_from_file(category, name, **params)
  sql = SqlLoader.load(category, name)
  execute(sql, **params)
end