blob: 0a955333bc732a3d7974dde2605e31ef254e8d3f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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.
"""
name = 'SQL'
has_content = True
def title(self):
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 ''
def content(self):
context = {'queries': connection.queries}
return render_to_string('debug_toolbar/panels/sql.html', context)
|