1. Vernon Cole
  2. django-mssql-ado-merge

Commits

Vernon Cole  committed b9ac4d5

new cursor wrapper object

  • Participants
  • Parent commits 26afc2e
  • Branches default

Comments (0)

Files changed (1)

File sqlserver_ado/base.py

View file
  • Ignore whitespace
 """Microsoft SQL Server database backend for Django."""
+import sys
+
 import django
 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient
 from django.db.backends.signals import connection_created
 from django.core.exceptions import ImproperlyConfigured, ValidationError
+from django.db import utils
+from django.utils import six
 
 import adodbapi.apibase as api
 ##import adodbapi.ado_consts as adc
 DatabaseError = api.DatabaseError
 IntegrityError = api.IntegrityError
 
+class CursorWrapper(object):
+    """
+    A thin wrapper around adodbapi's normal cursor class so that we can catch
+    particular exception instances and reraise them with the right types.
+    """
+
+    def __init__(self, cursor):
+        self.cursor = cursor
+
+    def execute(self, query, args=None):
+        try:
+            self.cursor.execute(query, args)
+        except api.IntegrityError as e:
+            six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
+        except api.DatabaseError as e:
+            six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
+
+    def executemany(self, query, args):
+        try:
+            self.cursor.executemany(query, args)
+        except api.IntegrityError as e:
+            six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
+        except api.DatabaseError as e:
+            six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
+
+    def __getattr__(self, attr):
+        if attr in self.__dict__:
+            return self.__dict__[attr]
+        else:
+            return getattr(self.cursor, attr)
+
+    def __iter__(self):
+        return iter(self.cursor)
+
 class DatabaseFeatures(BaseDatabaseFeatures):
     uses_custom_query_class = True
     has_bulk_insert = False
     def _cursor(self):
         if self.connection is None:
             self.__connect()
-        return self.connection.cursor()
+        cursor = self.connection.cursor()
+        return CursorWrapper(cursor)
 
     def disable_constraint_checking(self):
         """