Sha256: 3469de9e5b8fcccb251af0453cf657e36ff8b01d5a99eedf270e61218b3232dd

Contents?: true

Size: 1.07 KB

Versions: 76

Compression:

Stored size: 1.07 KB

Contents

require 'active_support/lazy_load_hooks'
require 'active_record/explain_registry'

module ActiveRecord
  module Explain
    # Executes the block with the collect flag enabled. Queries are collected
    # asynchronously by the subscriber and returned.
    def collecting_queries_for_explain # :nodoc:
      ExplainRegistry.collect = true
      yield
      ExplainRegistry.queries
    ensure
      ExplainRegistry.reset
    end

    # Makes the adapter execute EXPLAIN for the tuples of queries and bindings.
    # Returns a formatted string ready to be logged.
    def exec_explain(queries) # :nodoc:
      str = queries.map do |sql, bind|
        [].tap do |msg|
          msg << "EXPLAIN for: #{sql}"
          unless bind.empty?
            bind_msg = bind.map {|col, val| [col.name, val]}.inspect
            msg.last << " #{bind_msg}"
          end
          msg << connection.explain(sql, bind)
        end.join("\n")
      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

76 entries across 74 versions & 10 rubygems

Version Path
activerecord-4.2.11.3 lib/active_record/explain.rb
activerecord-4.2.11.2 lib/active_record/explain.rb
activerecord-4.2.11.1 lib/active_record/explain.rb
activerecord-4.2.11 lib/active_record/explain.rb
activerecord-4.2.10 lib/active_record/explain.rb
activerecord-4.2.10.rc1 lib/active_record/explain.rb
activerecord-4.2.9 lib/active_record/explain.rb
activerecord-4.2.9.rc2 lib/active_record/explain.rb
activerecord-4.2.9.rc1 lib/active_record/explain.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/explain.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-4.2.8/lib/active_record/explain.rb
activerecord-5.0.2 lib/active_record/explain.rb
activerecord-5.0.2.rc1 lib/active_record/explain.rb
activerecord-4.2.8 lib/active_record/explain.rb
activerecord-4.2.8.rc1 lib/active_record/explain.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/explain.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/explain.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/explain.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/explain.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/explain.rb