diff options
author | Andrey Deryabin <deriabin@gmail.com> | 2012-04-26 21:53:18 +0700 |
---|---|---|
committer | Andrey Deryabin <deriabin@gmail.com> | 2012-04-26 21:53:18 +0700 |
commit | 9fd640376efb9cdc8b213c2397f21ac57c85a90a (patch) | |
tree | 9e3bcf6e358aa9a09903d89ebccdba7455dcf649 /activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | |
parent | 8cd14c0bc9f9429f03d1181912355d2f48b98157 (diff) | |
download | rails-9fd640376efb9cdc8b213c2397f21ac57c85a90a.tar.gz rails-9fd640376efb9cdc8b213c2397f21ac57c85a90a.tar.bz2 rails-9fd640376efb9cdc8b213c2397f21ac57c85a90a.zip |
EXPLAIN only for sqlite3
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 25 |
1 files changed, 25 insertions, 0 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 |