Kaydet (Commit) f7408b49 authored tarafından kingbuzzman's avatar kingbuzzman Kaydeden (comit) Mariusz Felisiak

Refs #30148 -- Moved logging queries in CursorDebugWrapper to debug_sql() contextmanager.

üst 6754bffa
...@@ -3,6 +3,7 @@ import decimal ...@@ -3,6 +3,7 @@ import decimal
import functools import functools
import hashlib import hashlib
import logging import logging
from contextlib import contextmanager
from time import time from time import time
from django.conf import settings from django.conf import settings
...@@ -94,40 +95,38 @@ class CursorDebugWrapper(CursorWrapper): ...@@ -94,40 +95,38 @@ class CursorDebugWrapper(CursorWrapper):
# XXX callproc isn't instrumented at this time. # XXX callproc isn't instrumented at this time.
def execute(self, sql, params=None): def execute(self, sql, params=None):
start = time() with self.debug_sql(sql, params, use_last_executed_query=True):
try:
return super().execute(sql, params) return super().execute(sql, params)
finally:
stop = time()
duration = stop - start
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
self.db.queries_log.append({
'sql': sql,
'time': "%.3f" % duration,
})
logger.debug(
'(%.3f) %s; args=%s', duration, sql, params,
extra={'duration': duration, 'sql': sql, 'params': params}
)
def executemany(self, sql, param_list): def executemany(self, sql, param_list):
with self.debug_sql(sql, param_list, many=True):
return super().executemany(sql, param_list)
@contextmanager
def debug_sql(self, sql=None, params=None, use_last_executed_query=False, many=False):
start = time() start = time()
try: try:
return super().executemany(sql, param_list) yield
finally: finally:
stop = time() stop = time()
duration = stop - start duration = stop - start
if use_last_executed_query:
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
try: try:
times = len(param_list) times = len(params) if many else ''
except TypeError: # param_list could be an iterator except TypeError:
# params could be an iterator.
times = '?' times = '?'
self.db.queries_log.append({ self.db.queries_log.append({
'sql': '%s times: %s' % (times, sql), 'sql': '%s times: %s' % (times, sql) if many else sql,
'time': "%.3f" % duration, 'time': '%.3f' % duration,
}) })
logger.debug( logger.debug(
'(%.3f) %s; args=%s', duration, sql, param_list, '(%.3f) %s; args=%s',
extra={'duration': duration, 'sql': sql, 'params': param_list} duration,
sql,
params,
extra={'duration': duration, 'sql': sql, 'params': params},
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment