Code

Ticket #5236: 5236.diff

File 5236.diff, 3.2 KB (added by PhiR, 7 years ago)

patch slightly cleaned up, but mostly documented in the FAQ

Line 
1Index: django/db/backends/util.py
2===================================================================
3--- django/db/backends/util.py  (revision 6338)
4+++ django/db/backends/util.py  (working copy)
5@@ -1,5 +1,6 @@
6 import datetime
7 import md5
8+import traceback
9 from time import time
10 from django.utils.encoding import smart_unicode, force_unicode
11 
12@@ -18,23 +19,25 @@
13         try:
14             return self.cursor.execute(sql, params)
15         finally:
16-            stop = time()
17-            self.db.queries.append({
18-                'sql': smart_unicode(sql) % convert_args(params),
19-                'time': "%.3f" % (stop - start),
20-            })
21+            self.trace_entry(smart_unicode(sql) % convert_args(params), start)
22 
23     def executemany(self, sql, param_list):
24         start = time()
25         try:
26             return self.cursor.executemany(sql, param_list)
27         finally:
28-            stop = time()
29-            self.db.queries.append({
30-                'sql': 'MANY: ' + sql + ' ' + smart_unicode(tuple(param_list)),
31-                'time': "%.3f" % (stop - start),
32-            })
33+            self.trace_entry('MANY: ' + sql + ' ' + smart_unicode(tuple(param_list)), start)
34 
35+    def trace_entry (self, sql, start):
36+        stop = time()
37+        entry = {
38+            'sql': sql,
39+            'time': "%.3f" % (stop - start),
40+            # remove ourselves from the stack
41+            'stack': traceback.format_stack()[:-2],
42+        }
43+        self.db.queries.append(entry)
44+
45     def __getattr__(self, attr):
46         if attr in self.__dict__:
47             return self.__dict__[attr]
48Index: docs/faq.txt
49===================================================================
50--- docs/faq.txt        (revision 6338)
51+++ docs/faq.txt        (working copy)
52@@ -447,15 +447,28 @@
53 this::
54 
55     >>> from django.db import connection
56-    >>> connection.queries
57-    [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
58-    'time': '0.002'}]
59+    >>> len(connection.queries)
60+    12
61+    >>> connection.queries[0]['sql']
62+    u'SELECT "polls_poll"."id","polls_poll"."question","polls_poll"."pub_date" FROM
63+    "polls_poll"'
64+    >>> connection.queries[0]['time']
65+    '0.000'
66+    >>> type(connection.queries[0]['stack'])
67+    <type 'list'>
68+    >>> connection.queries[0]['stack'][-1]
69+    '  File "H:\\cygwin\\home\\Philippe\\django\\trunk\\django\\db\\models\\query.py",
70+    line 189, in iterator\ncursor.execute("SELECT " + (self._distinct and "DISTINCT "
71+    or "") + ",".join(select) + sql, params)\n'
72+    >>> connection.queries[0]['stack'][0]
73+    '  File "./manage.py", line 11, in <module>\n    execute_manager(settings)\n'
74 
75 ``connection.queries`` is only available if ``DEBUG`` is ``True``. It's a list
76 of dictionaries in order of query execution. Each dictionary has the following::
77 
78     ``sql`` -- The raw SQL statement
79-    ``time`` -- How long the statement took to execute, in seconds.
80+    ``time`` -- How long the statement took to execute, in seconds
81+    ``stack`` -- the stack, as a list of strings.
82 
83 ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES,
84 SELECTs, etc. Each time your app hits the database, the query will be recorded.