Class: Benedictus::Runner

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

Constant Summary collapse

"Executed inside rolled-back transaction. " \
"Note: side effects of volatile Postgres functions " \
"(setval, pg_advisory_lock, pg_notify, dblink, …) are NOT reverted by ROLLBACK."
HEURISTIC_OPTION_KEYS =
%i[seq_scan_threshold drift_factor nested_loop_threshold per_row_cost_threshold].freeze

Instance Method Summary collapse

Constructor Details

#initialize(expression:, options: {}, output: $stdout, error: $stderr, skip_rails_load: false) ⇒ Runner

Returns a new instance of Runner.



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

def initialize(expression:, options: {}, output: $stdout, error: $stderr, skip_rails_load: false)
  @expression      = expression
  @options         = normalize_options(options)
  @output          = output
  @error           = error
  @skip_rails_load = skip_rails_load
end

Instance Method Details

#callObject



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

def call
  warn_unused_buffers

  Benedictus::RailsLoader.load! unless @skip_rails_load

  value    = Benedictus::ExpressionEvaluator.call(@expression)
  relation = Benedictus::RelationResolver.call(value)
  sql      = relation.to_sql

  output_string =
    case @options[:format]
    when "tree" then render_tree(relation, sql)
    when "json" then render_json(relation)
    when "raw"  then render_raw(relation)
    else raise ArgumentError, "unknown format: #{@options[:format]}"
    end

  @output.puts(output_string)
end