Asymmetrical cursor creation in django.db.backeds.BaseDatabaseWrapper
|Reported by:||emil.filipov@…||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.5|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
We currently have the following cursor creation code in BaseDatabaseWrapper:
def cursor(self): self.validate_thread_sharing() if (self.use_debug_cursor or (self.use_debug_cursor is None and settings.DEBUG)): cursor = self.make_debug_cursor(self._cursor()) else: cursor = util.CursorWrapper(self._cursor(), self) return cursor
The issue I have with this piece of code is that the cursor is created in a very different way, depending on whether we have a debug mode or not. The symmetrical way of doing it would be to call something like self.make_regular_cursor(self._cursor()) for the cursor creating in the regular case.
Modules like Django Debug Toolbar wrap the .cursor() invocation on the base class, it's all working nicely for the standard backends, that do not override cursor(). The problem, however, becomes apparent when you write your own backend, that overrides the cursor() method. Then you have to call the parent method, to ensure compatibility with DDT, but that parent method will either return the generic Cursor class (for a regular cursor), or your own, custom Cursor class (for a debug cursor, where you have overridden make_debug_cursor()) - a situation requiring a decision based on the type of the returned argument.
Fairly trivial - just add another member function and wrap util.CursorWrapper(self._cursor(), self) inside it. I can provide one if needed.
Change History (1)
comment:1 Changed 7 months ago by akaariai
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Summary changed from Asymmetrical cursor creation in djnago.db.backeds.BaseDatabaseWrapper to Asymmetrical cursor creation in django.db.backeds.BaseDatabaseWrapper
- Triage Stage changed from Unreviewed to Accepted