Class: PgReports::Executor
- Inherits:
-
Object
- Object
- PgReports::Executor
- 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
-
#connection ⇒ Object
Resolved on every call: explicit override > thread-local > registry default.
-
#execute(sql, **params) ⇒ Object
Execute raw SQL and return results as array of hashes.
-
#execute_from_file(category, name, **params) ⇒ Object
Execute SQL from a file and return results as array of hashes.
-
#initialize(connection: nil) ⇒ Executor
constructor
A new instance of Executor.
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
#connection ⇒ Object
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 |