Sha256: a646d01b62e0738c4c3eaa1fb8afa5f225a03f02a9704836ddb8900050fdaf7b

Contents?: true

Size: 1005 Bytes

Versions: 3

Compression:

Stored size: 1005 Bytes

Contents

module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module DatabaseStatements
        def analyze(arel, binds = [])
          sql = "EXPLAIN ANALYZE #{to_sql(arel, binds)}"
          PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN ANALYZE", binds))
        end
      end
    end
  end
end

module ActiveRecord
  class Relation
    def analyze
      exec_analyze(collecting_queries_for_explain { exec_queries })
    end
  end
end

module ActiveRecord
  module Explain
    def exec_analyze(queries) # :nodoc:
      str = queries.map do |sql, binds|
        msg = "EXPLAIN ANALYZE for: #{sql}".dup
        unless binds.empty?
          msg << " "
          msg << binds.map { |attr| render_bind(attr) }.inspect
        end
        msg << "\n"
        msg << connection.analyze(sql, binds)
      end.join("\n")

      # Overriding inspect to be more human readable, especially in the console.
      def str.inspect
        self
      end

      str
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-analyze-0.3.0 lib/activerecord-analyze/main.rb
activerecord-analyze-0.2.0 lib/activerecord-analyze/main.rb
activerecord-analyze-0.1.0 lib/activerecord-analyze/main.rb