diff options
Diffstat (limited to 'debug_toolbar/utils/tracking/db.py')
| -rw-r--r-- | debug_toolbar/utils/tracking/db.py | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index 4d87090..d649984 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -5,13 +5,22 @@ from threading import local from django.conf import settings from django.template import Node -from django.utils import simplejson from django.utils.encoding import force_unicode, smart_str -from django.utils.hashcompat import sha_constructor from debug_toolbar.utils import ms_from_timedelta, tidy_stacktrace, \ get_template_info, get_stack from debug_toolbar.utils.compat.db import connections + +try: + import json +except ImportError: # python < 2.6 + from django.utils import simplejson as json + +try: + from hashlib import sha1 +except ImportError: # python < 2.5 + from django.utils.hashcompat import sha_constructor as sha1 + # TODO:This should be set in the toolbar loader as a default and panels should # get a copy of the toolbar object with access to its config dictionary SQL_WARNING_THRESHOLD = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) \ @@ -82,8 +91,13 @@ class NormalCursorWrapper(object): for key, value in params.iteritems()) return map(self._quote_expr, params) + def _decode(self, param): + try: + return force_unicode(param, strings_only=True) + except UnicodeDecodeError: + return '(encoded string)' + def execute(self, sql, params=()): - __traceback_hide__ = True start = datetime.now() try: return self.cursor.execute(sql, params) @@ -98,10 +112,8 @@ class NormalCursorWrapper(object): stacktrace = [] _params = '' try: - _params = simplejson.dumps( - [force_unicode(x, strings_only=True) for x in params] - ) - except TypeError: + _params = json.dumps(map(self._decode, params)) + except Exception: pass # object not JSON serializable template_info = None @@ -119,7 +131,7 @@ class NormalCursorWrapper(object): del cur_frame alias = getattr(self.db, 'alias', 'default') - conn = connections[alias].connection + conn = self.db.connection # HACK: avoid imports if conn: engine = conn.__class__.__module__.split('.', 1)[0] @@ -129,14 +141,11 @@ class NormalCursorWrapper(object): params = { 'engine': engine, 'alias': alias, - 'sql': self.db.ops.last_executed_query(self.cursor, sql, - self._quote_params(params)), + 'sql': self.db.ops.last_executed_query( + self.cursor, sql, self._quote_params(params)), 'duration': duration, 'raw_sql': sql, 'params': _params, - 'hash': sha_constructor(settings.SECRET_KEY \ - + smart_str(sql) \ - + _params).hexdigest(), 'stacktrace': stacktrace, 'start_time': start, 'stop_time': stop, @@ -146,10 +155,17 @@ class NormalCursorWrapper(object): } if engine == 'psycopg2': + # If an erroneous query was ran on the connection, it might + # be in a state where checking isolation_level raises an + # exception. + try: + iso_level = conn.isolation_level + except conn.InternalError: + iso_level = 'unknown' params.update({ 'trans_id': self.logger.get_transaction_id(alias), 'trans_status': conn.get_transaction_status(), - 'iso_level': conn.isolation_level, + 'iso_level': iso_level, 'encoding': conn.encoding, }) |
