Module: ROM::Plugins::Relation::SQL::Postgres::Explain
- Defined in:
- lib/rom/plugins/relation/sql/postgres/explain.rb
Overview
PG-specific extensions which adds ‘Relation#explain` method
Instance Method Summary collapse
-
#explain(format: :text, **options) ⇒ Hash, String
Show the execution plan One of four different output formats are supported: plain text, XML, JSON, YAML JSON format will be parsed and unwrapped automatically, plan in other formats will be returned as a plain string.
Instance Method Details
#explain(format: :text, **options) ⇒ Hash, String
Show the execution plan One of four different output formats are supported: plain text, XML, JSON, YAML JSON format will be parsed and unwrapped automatically, plan in other formats will be returned as a plain string. Other options will be transparently added to the statement.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rom/plugins/relation/sql/postgres/explain.rb', line 28 def explain(format: :text, **) = .map { |opt, value| "#{opt.to_s.upcase} #{!!value}" } format_option = "FORMAT #{format.to_s.upcase}" explain_value = [format_option, *].join(', ') query = "EXPLAIN (#{explain_value}) #{dataset.sql}" rows = dataset.with_sql(query).map(:'QUERY PLAN') case format when :json rows[0][0]['Plan'] else rows.join("\n") end end |