Module: ActiveRecordExplainAnalyze::PostgreSQLAdapter

Defined in:
lib/activerecord-explain-analyze/postgresql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#explain_with_options(arel, binds = [], analyze, format) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/activerecord-explain-analyze/postgresql_adapter.rb', line 3

def explain_with_options(arel, binds = [], analyze, format)
  options = []
  options.concat(["ANALYZE", "COSTS", "VERBOSE", "BUFFERS"]) if analyze
  options << "FORMAT #{format}" unless format == "TEXT"
  options_sql = options.size > 0 ? "(#{options.join(', ')})" : ""

  sql = "EXPLAIN #{options_sql} #{to_sql(arel, binds)}"
  result = exec_query(sql, "EXPLAIN", binds)

  if format == "TEXT" && explain_pretty_printer
    explain_pretty_printer.new.pp(result)
  else
    result.rows.map(&:first).join("\n")
  end
end