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.



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

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

#execute(sql, **params) ⇒ Object

Execute raw SQL and return results as array of hashes



21
22
23
24
25
# File 'lib/pg_reports/executor.rb', line 21

def execute(sql, **params)
  processed_sql = interpolate_params(sql, params)
  result = connection.exec_query(processed_sql)
  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