Class: Benedictus::PlanRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/benedictus/plan_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation:, analyze: false, buffers: false, verbose: false, format: "JSON") ⇒ PlanRunner

Returns a new instance of PlanRunner.



9
10
11
12
13
14
15
# File 'lib/benedictus/plan_runner.rb', line 9

def initialize(relation:, analyze: false, buffers: false, verbose: false, format: "JSON")
  @relation = relation
  @analyze  = analyze
  @buffers  = buffers
  @verbose  = verbose
  @format   = format
end

Class Method Details

.call(**kwargs) ⇒ Object



5
6
7
# File 'lib/benedictus/plan_runner.rb', line 5

def self.call(**kwargs)
  new(**kwargs).call
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/benedictus/plan_runner.rb', line 17

def call
  reject_raw_sql_adapter_under_analyze!

  sql = @relation.to_sql
  Benedictus::SafetyGuard.assert_select!(sql) if @analyze

  explain_sql = "EXPLAIN (#{options_string}) #{sql}"

  if @analyze
    Benedictus::SafetyGuard.with_rollback(@relation.klass) { execute(explain_sql) }
  else
    execute(explain_sql)
  end
rescue StandardError => e
  raise if e.is_a?(Benedictus::Error)

  raise Benedictus::DatabaseError, "EXPLAIN failed: #{e.class}: #{e.message}"
end