Skip to content

Instantly share code, notes, and snippets.

@lamby
Created July 21, 2010 13:05
Show Gist options
  • Save lamby/484450 to your computer and use it in GitHub Desktop.
Save lamby/484450 to your computer and use it in GitHub Desktop.
diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py
index f0d3323..1a9310d 100644
--- a/debug_toolbar/panels/sql.py
+++ b/debug_toolbar/panels/sql.py
@@ -7,7 +7,7 @@ import traceback
import django
from django.conf import settings
from django.db import connection
-from django.db.backends import util
+from django.db.backends import util, BaseDatabaseWrapper
from django.views.debug import linebreak_iter
from django.template import Node
from django.template.loader import render_to_string
@@ -137,6 +137,27 @@ class DatabaseStatTracker(util.CursorDebugWrapper):
})
util.CursorDebugWrapper = DatabaseStatTracker
+def create_wrapper_fn(method):
+ def fn(self):
+ if self.connection is None:
+ return
+
+ start = datetime.now()
+ try:
+ return getattr(self.connection, method)()
+ finally:
+ stop = datetime.now()
+
+ self.queries.append({
+ 'sql': method.upper(),
+ 'duration': ms_from_timedelta(stop - start),
+ 'stacktrace': tidy_stacktrace(traceback.extract_stack()),
+ })
+ return fn
+
+BaseDatabaseWrapper._commit = create_wrapper_fn('commit')
+BaseDatabaseWrapper._rollback = create_wrapper_fn('rollback')
+
class SQLDebugPanel(DebugPanel):
"""
Panel that displays information about the SQL queries run while processing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment