Skip to content

SQL 'Select/Explain' crashes because of redundant params substitution (fix provided) #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue May 15, 2013 · 2 comments

Comments

@ghost
Copy link

ghost commented May 15, 2013

When I click on Select or Explain SQL panel I get following error:

  File "/usr/lib/python2.6/dist-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.6/dist-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
    return view_func(*args, **kwargs)
  File "/www/redsys/core/debug_toolbar/views.py", line 35, in sql_select
    cursor.execute(sql, params)
  File "/usr/lib/pymodules/python2.6/redsys/core/debug_toolbar/utils/tracking/db.py", line 145, in execute
    self.cursor, sql, self._quote_params(params)),
  File "/usr/lib/python2.6/dist-packages/django/db/backends/mysql/base.py", line 243, in last_executed_query
    return cursor._last_executed.decode('utf-8')
  File "/usr/lib/python2.6/dist-packages/django/db/backends/util.py", line 29, in __getattr__
    return getattr(self.cursor, attr)
  File "/usr/lib/python2.6/dist-packages/django/db/backends/mysql/base.py", line 150, in __getattr__
    return getattr(self.cursor, attr)
AttributeError: 'Cursor' object has no attribute '_last_executed'
 {middleware.py:process_exception():42}

After a bit of debugging I found that DebugToolbar is trying to substitute params to Select / Explain query - but this is wrong - parameters are already substituted because query is read from form.cleaned_data.

Fix:

  • See debug_toolbar/views.py.
  • Replace params = form.cleaned_data['params'] with params = () in sql_select(), sql_explain() and probably even in sql_profile() (haven't tested)
@apollo13
Copy link
Contributor

The main issue is that the Form uses sql instead of raw_sql + everything which follows on that. The error I get is:

Traceback (most recent call last):
  File "/home/florian/.virtualenvs/inyoka/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "/home/florian/.virtualenvs/inyoka/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/home/florian/.virtualenvs/inyoka/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/florian/.virtualenvs/inyoka/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/florian/sources/django-debug-toolbar/debug_toolbar/views.py", line 26, in sql_select
    cursor.execute(sql, params)
  File "/home/florian/sources/django-debug-toolbar/debug_toolbar/utils/tracking/db.py", line 98, in execute
    return self.cursor.execute(sql, params)
  File "/home/florian/.virtualenvs/inyoka/lib/python2.7/site-packages/django/db/backends/util.py", line 41, in execute
    return self.cursor.execute(sql, params)
  File "/home/florian/.virtualenvs/inyoka/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 120, in execute
    return self.cursor.execute(query, args)
  File "/home/florian/.virtualenvs/inyoka/lib/python2.7/site-packages/MySQLdb/cursors.py", line 183, in execute
    query = query % db.literal(args)
TypeError: not all arguments converted during string formatting

which is also a result of not interpolating quries properly.

@apollo13
Copy link
Contributor

#417 should fix it.

@jezdez jezdez closed this as completed in 7c8b5b0 Oct 18, 2013
jezdez added a commit that referenced this issue Oct 18, 2013
Fixed #383 -- Feed the proper SQL to cursor.execute (the one with placeholders).
ryneeverett pushed a commit to ryneeverett/django-debug-toolbar that referenced this issue Oct 2, 2016
ryneeverett pushed a commit to ryneeverett/django-debug-toolbar that referenced this issue Oct 2, 2016
Fixed django-commons#383 -- Feed the proper SQL to cursor.execute (the one with placeholders).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant