From 4a59813e75d7cb3b88bf2b223b21d4e23d9ecb03 Mon Sep 17 00:00:00 2001 From: Rob Hudson Date: Fri, 28 Aug 2009 09:13:37 -0700 Subject: Fixed sql profiling so it doesn't return a 500 if the SQL `SET PROFILING=1` results in an error. Also fixed view to render the correct template. --- .../debug_toolbar/panels/sql_profile.html | 51 ++++++++++++---------- debug_toolbar/views.py | 23 ++++++---- 2 files changed, 44 insertions(+), 30 deletions(-) (limited to 'debug_toolbar') 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 @@

SQL Profiled

-
-
{% trans "Executed SQL" %}
-
{{ sql|safe }}
-
{% trans "Time" %}
-
{{ time }} ms
-
- - - - {% for h in headers %} - - {% endfor %} - - - - {% for row in result %} - - {% for column in row %} - + {% if result %} +
+
{% trans "Executed SQL" %}
+
{{ sql|safe }}
+
{% trans "Time" %}
+
{{ time }} ms
+
+
{{ h|upper }}
{{ column|escape }}
+ + + {% for h in headers %} + {% endfor %} - {% endfor %} - -
{{ h|upper }}
+ + + {% for row in result %} + + {% for column in row %} + {{ column|escape }} + {% endfor %} + + {% endfor %} + + + {% else %} +
+
{% trans 'Error' %}
+
{{ result_error }}
+
+ {% endif %}
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): -- cgit v1.2.3