diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-02-01 00:28:14 -0200 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-02-01 00:28:14 -0200 |
commit | cea81220dd8d760cc27814ba82b67472d2f4def0 (patch) | |
tree | 61178a438c7d20b1e6ce280755ee31c52b294319 /activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb | |
parent | c2079f86f84e2ea0594156bcb85d990af644ecdb (diff) | |
parent | 351e39c27a882ccd1269265b757b960b0dcab3c3 (diff) | |
download | rails-cea81220dd8d760cc27814ba82b67472d2f4def0.tar.gz rails-cea81220dd8d760cc27814ba82b67472d2f4def0.tar.bz2 rails-cea81220dd8d760cc27814ba82b67472d2f4def0.zip |
Merge pull request #23389 from kamipo/extract_explain_pretty_printer
Extract `ExplainPrettyPrinter` to appropriate files
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb b/activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb new file mode 100644 index 0000000000..789b88912c --- /dev/null +++ b/activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb @@ -0,0 +1,42 @@ +module ActiveRecord + module ConnectionAdapters + module PostgreSQL + class ExplainPrettyPrinter # :nodoc: + # Pretty prints the result of an EXPLAIN in a way that resembles the output of the + # PostgreSQL shell: + # + # QUERY PLAN + # ------------------------------------------------------------------------------ + # Nested Loop Left Join (cost=0.00..37.24 rows=8 width=0) + # Join Filter: (posts.user_id = users.id) + # -> Index Scan using users_pkey on users (cost=0.00..8.27 rows=1 width=4) + # Index Cond: (id = 1) + # -> Seq Scan on posts (cost=0.00..28.88 rows=8 width=4) + # Filter: (posts.user_id = 1) + # (6 rows) + # + def pp(result) + header = result.columns.first + lines = result.rows.map(&:first) + + # We add 2 because there's one char of padding at both sides, note + # the extra hyphens in the example above. + width = [header, *lines].map(&:length).max + 2 + + pp = [] + + pp << header.center(width).rstrip + pp << '-' * width + + pp += lines.map {|line| " #{line}"} + + nrows = result.rows.length + rows_label = nrows == 1 ? 'row' : 'rows' + pp << "(#{nrows} #{rows_label})" + + pp.join("\n") + "\n" + end + end + end + end +end |