diff options
| author | Vladislav Poluhin | 2013-04-24 15:54:17 +0800 |
|---|---|---|
| committer | Vladislav Poluhin | 2013-04-24 15:54:17 +0800 |
| commit | 4c1c4f7a8873a80f843ba683240b9d0ff0f49e4b (patch) | |
| tree | 4840de7ec2ec41d1ef7cc362803c2d0ace58d663 /debug_toolbar/utils/tracking/db.py | |
| parent | 3d0467d9a4394c4b994a802e6e861ff2562dbb2b (diff) | |
| parent | 6f79f0b0ada69e1d95d97066ba78e0d18aeab3ba (diff) | |
| download | django-debug-toolbar-4c1c4f7a8873a80f843ba683240b9d0ff0f49e4b.tar.bz2 | |
Merge branch 'master' of https://github.com/django-debug-toolbar/django-debug-toolbar into sql-panel-refactor
Conflicts:
debug_toolbar/static/debug_toolbar/css/toolbar.min.css
debug_toolbar/views.py
Diffstat (limited to 'debug_toolbar/utils/tracking/db.py')
| -rw-r--r-- | debug_toolbar/utils/tracking/db.py | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index 68408c3..8a51c36 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -5,7 +5,6 @@ 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 debug_toolbar.utils import ms_from_timedelta, tidy_stacktrace, \ @@ -13,8 +12,13 @@ from debug_toolbar.utils import ms_from_timedelta, tidy_stacktrace, \ 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: +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 @@ -87,12 +91,25 @@ 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) finally: + # FIXME: Sometimes connections which are not in the connections + # dict are used (for example in test database destroying). + # The code below (at least get_transaction_id(alias) needs to have + # the connection in the connections dict. It would be good to + # not have this requirement at all, but for now lets just skip + # these connections. + if self.db.alias not in connections: + return stop = datetime.now() duration = ms_from_timedelta(stop - start) enable_stacktraces = getattr(settings, @@ -103,10 +120,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 @@ -124,7 +139,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] @@ -148,11 +163,17 @@ class NormalCursorWrapper(object): } if engine == 'psycopg2': - from psycopg2.extensions import TRANSACTION_STATUS_INERROR + # 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 if not conn.get_transaction_status() == TRANSACTION_STATUS_INERROR else "", + 'iso_level': iso_level, 'encoding': conn.encoding, }) |
