From 9fd640376efb9cdc8b213c2397f21ac57c85a90a Mon Sep 17 00:00:00 2001 From: Andrey Deryabin Date: Thu, 26 Apr 2012 21:53:18 +0700 Subject: EXPLAIN only for sqlite3 --- .../connection_adapters/sqlite3_adapter.rb | 25 ++++++++++++++++++++++ .../connection_adapters/sqlite_adapter.rb | 24 --------------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index ee5d10859c..ae5eaa5f1b 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -50,6 +50,31 @@ module ActiveRecord @connection.encoding.to_s end + # Returns true. + def supports_explain? + true + end + + # DATABASE STATEMENTS ====================================== + + def explain(arel, binds = []) + sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}" + ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds)) + end + + class ExplainPrettyPrinter + # Pretty prints the result of a EXPLAIN QUERY PLAN in a way that resembles + # the output of the SQLite shell: + # + # 0|0|0|SEARCH TABLE users USING INTEGER PRIMARY KEY (rowid=?) (~1 rows) + # 0|1|1|SCAN TABLE posts (~100000 rows) + # + def pp(result) # :nodoc: + result.rows.map do |row| + row.join('|') + end.join("\n") + "\n" + end + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 91e1482ffd..e698e7f360 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -116,11 +116,6 @@ module ActiveRecord true end - # Returns true. - def supports_explain? - true - end - def requires_reloading? true end @@ -210,25 +205,6 @@ module ActiveRecord # DATABASE STATEMENTS ====================================== - def explain(arel, binds = []) - sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}" - ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds)) - end - - class ExplainPrettyPrinter - # Pretty prints the result of a EXPLAIN QUERY PLAN in a way that resembles - # the output of the SQLite shell: - # - # 0|0|0|SEARCH TABLE users USING INTEGER PRIMARY KEY (rowid=?) (~1 rows) - # 0|1|1|SCAN TABLE posts (~100000 rows) - # - def pp(result) # :nodoc: - result.rows.map do |row| - row.join('|') - end.join("\n") + "\n" - end - end - def exec_query(sql, name = nil, binds = []) log(sql, name, binds) do -- cgit v1.2.3