Permalink
Browse files

Merge pull request #5998 from aderyabin/fix7

EXPLAIN only for sqlite3
  • Loading branch information...
2 parents 8cd14c0 + 9fd6403 commit 9a3d5792a1fd2fca1554af4efcb98c2553930ac4 @fxn fxn committed Apr 26, 2012
@@ -50,6 +50,31 @@ def encoding
@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
@@ -116,11 +116,6 @@ def supports_primary_key? #:nodoc:
true
end
- # Returns true.
- def supports_explain?
- true
- end
-
def requires_reloading?
true
end
@@ -210,25 +205,6 @@ def type_cast(value, column) # :nodoc:
# 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

0 comments on commit 9a3d579

Please sign in to comment.