aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/explain.rb
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2013-04-16 22:46:55 +0200
committerXavier Noria <fxn@hashref.com>2013-04-16 22:46:55 +0200
commitef7a48df7584b038c6e3e0f1dbca5749310ef9f6 (patch)
tree41103e1a939c2f9f2d9abea0c23e0e1310dd2b60 /activerecord/lib/active_record/explain.rb
parent0513f6ca9c43f475ddad97037ea8f42705147bd8 (diff)
downloadrails-ef7a48df7584b038c6e3e0f1dbca5749310ef9f6.tar.gz
rails-ef7a48df7584b038c6e3e0f1dbca5749310ef9f6.tar.bz2
rails-ef7a48df7584b038c6e3e0f1dbca5749310ef9f6.zip
let EXPLAIN use a thread locals registry [John J. Wang & Xavier Noria]
Closes #10198.
Diffstat (limited to 'activerecord/lib/active_record/explain.rb')
-rw-r--r--activerecord/lib/active_record/explain.rb15
1 files changed, 8 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/explain.rb b/activerecord/lib/active_record/explain.rb
index 15736575a2..e65dab07ba 100644
--- a/activerecord/lib/active_record/explain.rb
+++ b/activerecord/lib/active_record/explain.rb
@@ -1,22 +1,22 @@
require 'active_support/lazy_load_hooks'
+require 'active_record/explain_registry'
module ActiveRecord
module Explain
- # Relation#explain needs to be able to collect the queries.
+ # Executes the block with the collect flag enabled. Queries are collected
+ # asynchronously by the subscriber and returned.
def collecting_queries_for_explain # :nodoc:
- current = Thread.current
- original, current[:available_queries_for_explain] = current[:available_queries_for_explain], []
+ ExplainRegistry.collect = true
yield
- return current[:available_queries_for_explain]
+ ExplainRegistry.queries
ensure
- # Note that the return value above does not depend on this assignment.
- current[:available_queries_for_explain] = original
+ 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 && queries.map do |sql, bind|
+ str = queries.map do |sql, bind|
[].tap do |msg|
msg << "EXPLAIN for: #{sql}"
unless bind.empty?
@@ -31,6 +31,7 @@ module ActiveRecord
def str.inspect
self
end
+
str
end
end