From 4cba5d5e1340139822d3fd3e25af0871749c6345 Mon Sep 17 00:00:00 2001 From: Rob Hudson Date: Mon, 8 Sep 2008 10:26:03 -0700 Subject: Added a database stats tracking class to pass through params. This is setup for a later addition of being able to add EXPLAIN support to individual queries for SQL debugging. Contributed by David Cramer. --- debug_toolbar/panels/sql.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'debug_toolbar') diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index d699e1a..2ca50ed 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -1,7 +1,29 @@ +from time import time from debug_toolbar.panels import DebugPanel from django.db import connection +from django.db.backends import util from django.template.loader import render_to_string +class DatabaseStatTracker(util.CursorDebugWrapper): + """ + Replacement for CursorDebugWrapper which stores additional information + in `connection.queries`. + """ + def execute(self, sql, params=()): + start = time() + try: + return self.cursor.execute(sql, params) + finally: + stop = time() + # We keep `sql` to maintain backwards compatibility + self.db.queries.append({ + 'sql': self.db.ops.last_executed_query(self.cursor, sql, params), + 'time': stop - start, + 'raw_sql': sql, + 'params': params, + }) +util.CursorDebugWrapper = DatabaseStatTracker + class SQLDebugPanel(DebugPanel): """ Panel that displays information about the SQL queries run while processing the request. @@ -9,7 +31,8 @@ class SQLDebugPanel(DebugPanel): name = 'SQL' def title(self): - return '%d SQL Queries' % (len(connection.queries)) + total_time = sum(map(lambda q: float(q['time']) * 1000, connection.queries)) + return '%d SQL Queries (%.2fms)' % (len(connection.queries), total_time) def url(self): return '' -- cgit v1.2.3