Sha256: bd931488e72cee00146003c8af0e71fc2b5fa2c11f95530dc5c580091afb1ec7

Contents?: true

Size: 1.4 KB

Versions: 33

Compression:

Stored size: 1.4 KB

Contents

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

Version data entries

33 entries across 33 versions & 4 rubygems

Version Path
activerecord-5.0.7.2 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.7.1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.7 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.6 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.6.rc1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.5 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.5.rc2 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.5.rc1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.4 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.4.rc1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.3 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.2 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.0.2.rc1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb