Ticket #15901: db-backend-exceptions.patch
File db-backend-exceptions.patch, 6.8 KB (added by , 13 years ago) |
---|
-
django/db/utils.py
17 17 pass 18 18 19 19 20 def patch_exceptions(db_module): 21 """Patch a DB-API module's exceptions so that they derrive from 22 the backend independent Django exceptions.""" 23 for django_exception in [DatabaseError, IntegrityError]: 24 db_exception = getattr(db_module, django_exception.__name__, None) 25 if not (db_exception is None or 26 issubclass(db_exception, django_exception)): 27 db_exception.__bases__ += (django_exception,) 28 29 20 30 def load_backend(backend_name): 21 31 # Look for a fully qualified database backend name 22 32 try: -
django/db/backends/sqlite3/base.py
30 30 31 31 DatabaseError = Database.DatabaseError 32 32 IntegrityError = Database.IntegrityError 33 utils.patch_exceptions(Database) 33 34 34 35 Database.register_converter("bool", lambda s: str(s) == '1') 35 36 Database.register_converter("time", util.typecast_time) … … 267 268 """ 268 269 def execute(self, query, params=()): 269 270 query = self.convert_query(query) 270 try: 271 return Database.Cursor.execute(self, query, params) 272 except Database.IntegrityError, e: 273 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 274 except Database.DatabaseError, e: 275 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 271 return Database.Cursor.execute(self, query, params) 276 272 277 273 def executemany(self, query, param_list): 278 274 query = self.convert_query(query) 279 try: 280 return Database.Cursor.executemany(self, query, param_list) 281 except Database.IntegrityError, e: 282 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 283 except Database.DatabaseError, e: 284 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 275 return Database.Cursor.executemany(self, query, param_list) 285 276 286 277 def convert_query(self, query): 287 278 return FORMAT_QMARK_REGEX.sub('?', query).replace('%%','%') -
django/db/backends/mysql/base.py
42 42 43 43 DatabaseError = Database.DatabaseError 44 44 IntegrityError = Database.IntegrityError 45 utils.patch_exceptions(Database) 45 46 46 47 # MySQLdb-1.2.1 returns TIME columns as timedelta -- they are more like 47 48 # timedelta in terms of actual behavior as they are signed and include days -- … … 84 85 def execute(self, query, args=None): 85 86 try: 86 87 return self.cursor.execute(query, args) 87 except Database.IntegrityError, e:88 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]89 88 except Database.OperationalError, e: 90 89 # Map some error codes to IntegrityError, since they seem to be 91 90 # misclassified and Django would prefer the more logical place. 92 91 if e[0] in self.codes_for_integrityerror: 93 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]92 raise Database.IntegrityError, Database.IntegrityError(*tuple(e)), sys.exc_info()[2] 94 93 raise 95 except Database.DatabaseError, e:96 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]97 94 98 95 def executemany(self, query, args): 99 96 try: 100 97 return self.cursor.executemany(query, args) 101 except Database.IntegrityError, e:102 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]103 98 except Database.OperationalError, e: 104 99 # Map some error codes to IntegrityError, since they seem to be 105 100 # misclassified and Django would prefer the more logical place. 106 101 if e[0] in self.codes_for_integrityerror: 107 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]102 raise Database.IntegrityError, Database.IntegrityError(*tuple(e)), sys.exc_info()[2] 108 103 raise 109 except Database.DatabaseError, e:110 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]111 104 112 105 def __getattr__(self, attr): 113 106 if attr in self.__dict__: -
django/db/backends/postgresql_psycopg2/base.py
29 29 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) 30 30 psycopg2.extensions.register_adapter(SafeString, psycopg2.extensions.QuotedString) 31 31 psycopg2.extensions.register_adapter(SafeUnicode, psycopg2.extensions.QuotedString) 32 utils.patch_exceptions(Database) 32 33 33 34 logger = getLogger('django.db.backends') 34 35 35 class CursorWrapper(object):36 """37 A thin wrapper around psycopg2's normal cursor class so that we can catch38 particular exception instances and reraise them with the right types.39 """40 41 def __init__(self, cursor):42 self.cursor = cursor43 44 def execute(self, query, args=None):45 try:46 return self.cursor.execute(query, args)47 except Database.IntegrityError, e:48 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]49 except Database.DatabaseError, e:50 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]51 52 def executemany(self, query, args):53 try:54 return self.cursor.executemany(query, args)55 except Database.IntegrityError, e:56 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]57 except Database.DatabaseError, e:58 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]59 60 def __getattr__(self, attr):61 if attr in self.__dict__:62 return self.__dict__[attr]63 else:64 return getattr(self.cursor, attr)65 66 def __iter__(self):67 return iter(self.cursor)68 69 36 class DatabaseFeatures(BaseDatabaseFeatures): 70 37 needs_datetime_string_cast = False 71 38 can_return_id_from_insert = True … … 176 143 if set_tz: 177 144 cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']]) 178 145 self._get_pg_version() 179 return CursorWrapper(cursor)146 return cursor 180 147 181 148 def _enter_transaction_management(self, managed): 182 149 """