aboutsummaryrefslogtreecommitdiffstats
path: root/debug_toolbar/management/commands/debugsqlshell.py
blob: 45a23c2850e803c505669dfbb6117a7208a99cce (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
from __future__ import print_function

from datetime import datetime

from django.db.backends import util

import sqlparse

from debug_toolbar.utils import ms_from_timedelta


class PrintQueryWrapper(util.CursorDebugWrapper):
    def execute(self, sql, params=()):
        starttime = datetime.now()
        try:
            return self.cursor.execute(sql, params)
        finally:
            raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
            execution_time = ms_from_timedelta(datetime.now() - starttime)
            formatted_sql = sqlparse.format(raw_sql, reindent=True)
            print('%s [%.2fms]' % (formatted_sql, execution_time))


util.CursorDebugWrapper = PrintQueryWrapper