File: debugsqlshell.py

package info (click to toggle)
python-django-debug-toolbar 1%3A5.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,984 kB
  • sloc: python: 6,880; javascript: 631; makefile: 62; sh: 16
file content (33 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (2)
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
from time import perf_counter

import sqlparse
from django.core.management.commands.shell import Command
from django.db import connection

if connection.vendor == "postgresql":
    from django.db.backends.postgresql import base as base_module
else:
    from django.db.backends import utils as base_module

# 'debugsqlshell' is the same as the 'shell'.


# Command is required to exist to be loaded via
# django.core.managementload_command_class
__all__ = ["Command", "PrintQueryWrapper"]


class PrintQueryWrapper(base_module.CursorDebugWrapper):
    def execute(self, sql, params=()):
        start_time = perf_counter()
        try:
            return self.cursor.execute(sql, params)
        finally:
            raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
            end_time = perf_counter()
            duration = (end_time - start_time) * 1000
            formatted_sql = sqlparse.format(raw_sql, reindent=True)
            print(f"{formatted_sql} [{duration:.2f}ms]")


base_module.CursorDebugWrapper = PrintQueryWrapper