#25705: Parameters are not adapted or quoted in Query.__str__
-------------------------------------+-------------------------------------
     Reporter:  Dmitry Dygalo        |                    Owner:  Alex
         Type:                       |                   Status:  assigned
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  1                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Alex):

 Added a draft PR https://github.com/django/django/pull/18468 copying the
 description for posterity.

 No tests done yet. I'll like to know first if the approach I did is valid.

 Since the DB connections are per-thread as far as I know, this approach
 should not have any race condition issues. And since it's done before the
 prefetch, it should pick the Queryset query. Also, since queries are only
 logged if DEBUG is enabled, this approach requires that too.


 My discarded approach was to to get it somehow in the return of
 `self._result_cache = list(self._iterable_class(self))`, or stored in the
 QuerySet.
 However, it's much more complicated, because as far as I see the flow is
 something like
 QuerySet when
 
[https://github.com/django/django/blob/d5bebc1c26d4c0ec9eaa057aefc5b38649c0ba3b/django/db/models/query.py#L1909
 evaluating] calls the
 
[https://github.com/django/django/blob/d5bebc1c26d4c0ec9eaa057aefc5b38649c0ba3b/django/db/models/query.py#L82
 IterableClass] which then
 
[https://github.com/django/django/blob/d5bebc1c26d4c0ec9eaa057aefc5b38649c0ba3b/django/db/models/query.py#L91
 executes the query] which then gets the
 
[https://github.com/django/django/blob/d5bebc1c26d4c0ec9eaa057aefc5b38649c0ba3b/django/db/models/sql/compiler.py#L1585
 connection cursor], which is wrapped in the previous mentioned one that
 can log the query.

 So the a way would be to make all 4 `SqlCompiler.execute_sql` return the
 result **and** the SQL query. Then you have to adapt all 6
 `Iterable.__iter__` to also return the query. Then you can store it in the
 Queryset. Since these methods are part of the public API, this would be a
 breaking change in both methods. Also I'm not 100% sure how to get the
 query in the wrapper since the `Cursor.execute` is wrapped in a context
 manager which is the one logging the query.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/25705#comment:22>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107019145ea2bd7-2af48402-2e5f-4db7-8787-d0a484ec4be5-000000%40eu-central-1.amazonses.com.

Reply via email to