Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #473 -- Added a MysqlDebugWrapper to use for MySQL with DEBUG=T…

…rue. It displays more informative error messages for MySQL warnings. Thanks for the patch, mlambert@gmail.com

git-svn-id: http://code.djangoproject.com/svn/django/trunk@806 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a933432a708e77437706051f9e80b05fdf602aee 1 parent b63abf0
Adrian Holovaty authored October 08, 2005

Showing 1 changed file with 27 additions and 1 deletion. Show diff stats Hide diff stats

  1. 28  django/core/db/backends/mysql.py
28  django/core/db/backends/mysql.py
@@ -21,6 +21,32 @@
21 21
     FIELD_TYPE.TIME: typecasts.typecast_time,
22 22
 })
23 23
 
  24
+# This is an extra debug layer over MySQL queries, to display warnings.
  25
+# It's only used when DEBUG=True.
  26
+class MysqlDebugWrapper:
  27
+    def __init__(self, cursor):
  28
+        self.cursor = cursor
  29
+
  30
+    def execute(self, sql, params=()):
  31
+        try:
  32
+            return self.cursor.execute(sql, params)
  33
+        except Database.Warning, w:
  34
+            self.cursor.execute("SHOW WARNINGS")
  35
+            raise Database.Warning, "%s: %s" % (w, self.cursor.fetchall())
  36
+
  37
+    def executemany(self, sql, param_list):
  38
+        try:
  39
+            return self.cursor.executemany(sql, param_list)
  40
+        except Database.Warning:
  41
+            self.cursor.execute("SHOW WARNINGS")
  42
+            raise Database.Warning, "%s: %s" % (w, self.cursor.fetchall())
  43
+
  44
+    def __getattr__(self, attr):
  45
+        if self.__dict__.has_key(attr):
  46
+            return self.__dict__[attr]
  47
+        else:
  48
+            return getattr(self.cursor, attr)
  49
+
24 50
 class DatabaseWrapper:
25 51
     def __init__(self):
26 52
         self.connection = None
@@ -32,7 +58,7 @@ def cursor(self):
32 58
             self.connection = Database.connect(user=DATABASE_USER, db=DATABASE_NAME,
33 59
                 passwd=DATABASE_PASSWORD, host=DATABASE_HOST, conv=django_conversions)
34 60
         if DEBUG:
35  
-            return base.CursorDebugWrapper(self.connection.cursor(), self)
  61
+            return base.CursorDebugWrapper(MysqlDebugWrapper(self.connection.cursor()), self)
36 62
         return self.connection.cursor()
37 63
 
38 64
     def commit(self):

0 notes on commit a933432

Please sign in to comment.
Something went wrong with that request. Please try again.