blob: 2d4bfa63ace4e83cc56f81d2581496ca7407ec78 (
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
|
from __future__ import absolute_import, print_function, unicode_literals
from time import time
# 'debugsqlshell' is the same as the 'shell'.
from django.core.management.commands.shell import Command # noqa
from django.db.backends import util
import sqlparse
class PrintQueryWrapper(util.CursorDebugWrapper):
def execute(self, sql, params=()):
start_time = time()
try:
return self.cursor.execute(sql, params)
finally:
raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
end_time = time()
duration = (end_time - start_time) * 1000
formatted_sql = sqlparse.format(raw_sql, reindent=True)
print('%s [%.2fms]' % (formatted_sql, duration))
util.CursorDebugWrapper = PrintQueryWrapper
|