Skip to content

Commit

Permalink
Merge pull request rails#5998 from aderyabin/fix7
Browse files Browse the repository at this point in the history
EXPLAIN only for sqlite3
  • Loading branch information
fxn committed Apr 26, 2012
2 parents 8cd14c0 + 9fd6403 commit 9a3d579
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ def supports_primary_key? #:nodoc:
true
end

# Returns true.
def supports_explain?
true
end

def requires_reloading?
true
end
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9a3d579

Please sign in to comment.