Skip to content

Commit ee46d62

Browse files
tim-schillingliving180
authored andcommitted
Inherit from django.db.backends.utils.CursorWrapper
This switches the Debug Toolbar cursor wrappers to inherit from the Django class django.db.backends.utils.CursorWrapper. This reduces some of the code we need.
1 parent 32ab363 commit ee46d62

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

debug_toolbar/panels/sql/tracking.py

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from time import time
55

66
import django.test.testcases
7+
from django.db.backends.utils import CursorWrapper
78
from django.utils.encoding import force_str
89

910
from debug_toolbar import settings as dt_settings
@@ -64,7 +65,7 @@ def chunked_cursor(*args, **kwargs):
6465
# solves https://github.com/jazzband/django-debug-toolbar/issues/1239
6566
logger = connection._djdt_logger
6667
cursor = connection._djdt_chunked_cursor(*args, **kwargs)
67-
if logger is not None and not isinstance(cursor, BaseCursorWrapper):
68+
if logger is not None and not isinstance(cursor, DjDTCursorWrapper):
6869
if allow_sql.get():
6970
wrapper = NormalCursorWrapper
7071
else:
@@ -76,35 +77,28 @@ def chunked_cursor(*args, **kwargs):
7677
connection.chunked_cursor = chunked_cursor
7778

7879

79-
class BaseCursorWrapper:
80-
pass
80+
class DjDTCursorWrapper(CursorWrapper):
81+
def __init__(self, cursor, db, logger):
82+
super().__init__(cursor, db)
83+
# logger must implement a ``record`` method
84+
self.logger = logger
8185

8286

83-
class ExceptionCursorWrapper(BaseCursorWrapper):
87+
class ExceptionCursorWrapper(DjDTCursorWrapper):
8488
"""
8589
Wraps a cursor and raises an exception on any operation.
8690
Used in Templates panel.
8791
"""
8892

89-
def __init__(self, cursor, db, logger):
90-
pass
91-
9293
def __getattr__(self, attr):
9394
raise SQLQueryTriggered()
9495

9596

96-
class NormalCursorWrapper(BaseCursorWrapper):
97+
class NormalCursorWrapper(DjDTCursorWrapper):
9798
"""
9899
Wraps a cursor and logs queries.
99100
"""
100101

101-
def __init__(self, cursor, db, logger):
102-
self.cursor = cursor
103-
# Instance of a BaseDatabaseWrapper subclass
104-
self.db = db
105-
# logger must implement a ``record`` method
106-
self.logger = logger
107-
108102
def _quote_expr(self, element):
109103
if isinstance(element, str):
110104
return "'%s'" % element.replace("'", "''")
@@ -246,22 +240,10 @@ def _record(self, method, sql, params):
246240
self.logger.record(**params)
247241

248242
def callproc(self, procname, params=None):
249-
return self._record(self.cursor.callproc, procname, params)
243+
return self._record(super().callproc, procname, params)
250244

251245
def execute(self, sql, params=None):
252-
return self._record(self.cursor.execute, sql, params)
246+
return self._record(super().execute, sql, params)
253247

254248
def executemany(self, sql, param_list):
255-
return self._record(self.cursor.executemany, sql, param_list)
256-
257-
def __getattr__(self, attr):
258-
return getattr(self.cursor, attr)
259-
260-
def __iter__(self):
261-
return iter(self.cursor)
262-
263-
def __enter__(self):
264-
return self
265-
266-
def __exit__(self, type, value, traceback):
267-
self.close()
249+
return self._record(super().executemany, sql, param_list)

0 commit comments

Comments
 (0)