Class: PgReports::Executor

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

Overview

Executes SQL queries and returns results

Instance Method Summary collapse

Constructor Details

#initialize(connection: nil) ⇒ Executor

Returns a new instance of Executor.



6
7
8
# File 'lib/pg_reports/executor.rb', line 6

def initialize(connection: nil)
  @connection = connection || PgReports.config.connection
end

Instance Method Details

#execute(sql, **params) ⇒ Object

Execute raw SQL and return results as array of hashes



17
18
19
20
21
# File 'lib/pg_reports/executor.rb', line 17

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



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

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