Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

debugsqlshell.py 849B

123456789101112131415161718192021222324
  1. from __future__ import absolute_import, print_function, unicode_literals
  2. from time import time
  3. import sqlparse
  4. # 'debugsqlshell' is the same as the 'shell'.
  5. from django.core.management.commands.shell import Command # noqa
  6. from django.db.backends import utils as db_backends_utils
  7. class PrintQueryWrapper(db_backends_utils.CursorDebugWrapper):
  8. def execute(self, sql, params=()):
  9. start_time = time()
  10. try:
  11. return self.cursor.execute(sql, params)
  12. finally:
  13. raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  14. end_time = time()
  15. duration = (end_time - start_time) * 1000
  16. formatted_sql = sqlparse.format(raw_sql, reindent=True)
  17. print('%s [%.2fms]' % (formatted_sql, duration))
  18. db_backends_utils.CursorDebugWrapper = PrintQueryWrapper