aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation.rb
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2011-09-20 10:50:08 -0700
committerXavier Noria <fxn@hashref.com>2011-11-05 18:30:19 -0700
commite7b7b4412380e7ce2d8e6ae402cb7fe02d7666b8 (patch)
tree00d94c919f9a3e4e4551a5a47e5a6d048b45335b /activerecord/lib/active_record/relation.rb
parent89d7372dac7357134af6877ded159b16a8d3bc9b (diff)
downloadrails-e7b7b4412380e7ce2d8e6ae402cb7fe02d7666b8.tar.gz
rails-e7b7b4412380e7ce2d8e6ae402cb7fe02d7666b8.tar.bz2
rails-e7b7b4412380e7ce2d8e6ae402cb7fe02d7666b8.zip
implements AR::Relation#explain
This is a first implementation, EXPLAIN is highly dependent on the database and I have made some compromises. On one hand, the method allows you to run the most common EXPLAIN and that's it. If you want EXPLAIN ANALYZE in PostgreSQL you need to do it by hand. On the other hand, I've tried to construct a string as close as possible to the ones built by the respective shells. The rationale is that IMO the user should feel at home with the output and recognize it at first sight. Per database. I don't know whether this implementation is going to work well. Let's see whether people like it.
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r--activerecord/lib/active_record/relation.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 3baf9b3f49..f0891440a6 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -143,6 +143,22 @@ module ActiveRecord
super
end
+ def explain
+ queries = []
+ callback = lambda do |*args|
+ payload = args.last
+ queries << payload[:sql] unless payload[:exception] || %w(SCHEMA EXPLAIN).include?(payload[:name])
+ end
+
+ ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
+ to_a
+ end
+
+ queries.map do |sql|
+ @klass.connection.explain(sql)
+ end.join
+ end
+
def to_a
return @records if loaded?