Sha256: f72ac38c6caec9ae901a6d8c752dc11bf85614d10fc0ec5e7eebddc86a0ec095

Contents?: true

Size: 1.43 KB

Versions: 187

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

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

187 entries across 182 versions & 15 rubygems

Version Path
activerecord-6.0.5.1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.2.8.1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.2.8 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.0.5 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.1.6 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-7.0.3 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-7.0.2.4 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.1.5.1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.0.4.8 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.2.7.1 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.2.7 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.1.5 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-7.0.2.3 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.1.4.7 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.0.4.7 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.2.6.3 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-7.0.2.2 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.1.4.6 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-6.0.4.6 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
activerecord-5.2.6.2 lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb