diff options
| -rw-r--r-- | debug_toolbar/templates/debug_toolbar/panels/sql_profile.html | 51 | ||||
| -rw-r--r-- | debug_toolbar/views.py | 23 |
2 files changed, 44 insertions, 30 deletions
diff --git a/debug_toolbar/templates/debug_toolbar/panels/sql_profile.html b/debug_toolbar/templates/debug_toolbar/panels/sql_profile.html index 7df65da..0dcb0b9 100644 --- a/debug_toolbar/templates/debug_toolbar/panels/sql_profile.html +++ b/debug_toolbar/templates/debug_toolbar/panels/sql_profile.html @@ -4,28 +4,35 @@ <h3>SQL Profiled</h3> </div> <div class="djDebugPanelContent"> - <dl> - <dt>{% trans "Executed SQL" %}</dt> - <dd><pre>{{ sql|safe }}</pre></dd> - <dt>{% trans "Time" %}</dt> - <dd>{{ time }} ms</dd> - </dl> - <table> - <thead> - <tr> - {% for h in headers %} - <th>{{ h|upper }}</th> - {% endfor %} - </tr> - </thead> - <tbody> - {% for row in result %} - <tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}"> - {% for column in row %} - <td>{{ column|escape }}</td> + {% if result %} + <dl> + <dt>{% trans "Executed SQL" %}</dt> + <dd><pre>{{ sql|safe }}</pre></dd> + <dt>{% trans "Time" %}</dt> + <dd>{{ time }} ms</dd> + </dl> + <table> + <thead> + <tr> + {% for h in headers %} + <th>{{ h|upper }}</th> {% endfor %} </tr> - {% endfor %} - </tbody> - </table> + </thead> + <tbody> + {% for row in result %} + <tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}"> + {% for column in row %} + <td>{{ column|escape }}</td> + {% endfor %} + </tr> + {% endfor %} + </tbody> + </table> + {% else %} + <dl> + <dt>{% trans 'Error' %}</dt> + <dd>{{ result_error }}</dd> + </dl> + {% endif %} </div> diff --git a/debug_toolbar/views.py b/debug_toolbar/views.py index 9123a00..255a49c 100644 --- a/debug_toolbar/views.py +++ b/debug_toolbar/views.py @@ -109,21 +109,28 @@ def sql_profile(request): if sql.lower().strip().startswith('select'): params = simplejson.loads(params) cursor = connection.cursor() - cursor.execute("SET PROFILING=1") # Enable profiling - cursor.execute(sql, params) # Execute SELECT - cursor.execute("SET PROFILING=0") # Disable profiling - # The Query ID should always be 1 here but I'll subselect to get the last one just in case... - cursor.execute("SELECT * FROM information_schema.profiling WHERE query_id=(SELECT query_id FROM information_schema.profiling ORDER BY query_id DESC LIMIT 1)") - headers = [d[0] for d in cursor.description] - result = cursor.fetchall() + result = None + headers = None + result_error = None + try: + cursor.execute("SET PROFILING=1") # Enable profiling + cursor.execute(sql, params) # Execute SELECT + cursor.execute("SET PROFILING=0") # Disable profiling + # The Query ID should always be 1 here but I'll subselect to get the last one just in case... + cursor.execute("SELECT * FROM information_schema.profiling WHERE query_id=(SELECT query_id FROM information_schema.profiling ORDER BY query_id DESC LIMIT 1)") + headers = [d[0] for d in cursor.description] + result = cursor.fetchall() + except: + result_error = "Profiling is either not available or not supported by your database." cursor.close() context = { 'result': result, + 'result_error': result_error, 'sql': reformat_sql(cursor.db.ops.last_executed_query(cursor, sql, params)), 'time': request.GET.get('time', 0.0), 'headers': headers, } - return render_to_response('debug_toolbar/panels/sql_explain.html', context) + return render_to_response('debug_toolbar/panels/sql_profile.html', context) raise InvalidSQLError("Only 'select' queries are allowed.") def template_source(request): |
