Ticket #12702: t12702-r12351.1.diff
File t12702-r12351.1.diff, 12.7 KB (added by , 15 years ago) |
---|
-
django/db/__init__.py
diff -r 7145c0852013 django/db/__init__.py
a b 1 1 from django.conf import settings 2 2 from django.core import signals 3 3 from django.core.exceptions import ImproperlyConfigured 4 from django.db.utils import ConnectionHandler, ConnectionRouter, load_backend, DEFAULT_DB_ALIAS 4 from django.db.utils import ConnectionHandler, ConnectionRouter, load_backend, DEFAULT_DB_ALIAS, \ 5 DatabaseError, IntegrityError 5 6 from django.utils.functional import curry 6 7 7 8 __all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError', … … 73 74 # connections['default'] instead. 74 75 connection = connections[DEFAULT_DB_ALIAS] 75 76 backend = load_backend(connection.settings_dict['ENGINE']) 76 DatabaseError = backend.DatabaseError77 IntegrityError = backend.IntegrityError78 77 79 78 # Register an event that closes the database connection 80 79 # when a Django request is finished. -
django/db/backends/mysql/base.py
diff -r 7145c0852013 django/db/backends/mysql/base.py
a b 5 5 """ 6 6 7 7 import re 8 import sys 8 9 9 10 try: 10 11 import MySQLdb as Database … … 24 25 from MySQLdb.converters import conversions 25 26 from MySQLdb.constants import FIELD_TYPE, FLAG, CLIENT 26 27 28 from django.db import utils 27 29 from django.db.backends import * 28 30 from django.db.backends.signals import connection_created 29 31 from django.db.backends.mysql.client import DatabaseClient … … 82 84 def execute(self, query, args=None): 83 85 try: 84 86 return self.cursor.execute(query, args) 87 except Database.IntegrityError, e: 88 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 85 89 except Database.OperationalError, e: 86 90 # Map some error codes to IntegrityError, since they seem to be 87 91 # misclassified and Django would prefer the more logical place. 88 92 if e[0] in self.codes_for_integrityerror: 89 raise Database.IntegrityError(tuple(e))93 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 90 94 raise 95 except Database.DatabaseError, e: 96 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 91 97 92 98 def executemany(self, query, args): 93 99 try: 94 100 return self.cursor.executemany(query, args) 101 except Database.IntegrityError, e: 102 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 95 103 except Database.OperationalError, e: 96 104 # Map some error codes to IntegrityError, since they seem to be 97 105 # misclassified and Django would prefer the more logical place. 98 106 if e[0] in self.codes_for_integrityerror: 99 raise Database.IntegrityError(tuple(e))107 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 100 108 raise 101 109 except Database.DatabaseError, e: 110 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 102 111 def __getattr__(self, attr): 103 112 if attr in self.__dict__: 104 113 return self.__dict__[attr] -
django/db/backends/oracle/base.py
diff -r 7145c0852013 django/db/backends/oracle/base.py
a b 4 4 Requires cx_Oracle: http://cx-oracle.sourceforge.net/ 5 5 """ 6 6 7 8 import datetime 7 9 import os 8 import datetime10 import sys 9 11 import time 10 12 try: 11 13 from decimal import Decimal … … 24 26 from django.core.exceptions import ImproperlyConfigured 25 27 raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e) 26 28 29 from django.db import utils 27 30 from django.db.backends import * 28 31 from django.db.backends.signals import connection_created 29 32 from django.db.backends.oracle.client import DatabaseClient … … 480 483 self._guess_input_sizes([params]) 481 484 try: 482 485 return self.cursor.execute(query, self._param_generator(params)) 483 except DatabaseError, e: 486 except Database.IntegrityError, e: 487 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 488 except Database.DatabaseError, e: 484 489 # cx_Oracle <= 4.4.0 wrongly raises a DatabaseError for ORA-01400. 485 490 if e.args[0].code == 1400 and not isinstance(e, IntegrityError): 486 e = IntegrityError(e.args[0])487 raise e491 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 492 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 488 493 489 494 def executemany(self, query, params=None): 490 495 try: … … 504 509 try: 505 510 return self.cursor.executemany(query, 506 511 [self._param_generator(p) for p in formatted]) 507 except DatabaseError, e: 512 except Database.IntegrityError, e: 513 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 514 except Database.DatabaseError, e: 508 515 # cx_Oracle <= 4.4.0 wrongly raises a DatabaseError for ORA-01400. 509 516 if e.args[0].code == 1400 and not isinstance(e, IntegrityError): 510 e = IntegrityError(e.args[0])511 raise e517 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 518 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 512 519 513 520 def fetchone(self): 514 521 row = self.cursor.fetchone() -
django/db/backends/postgresql/base.py
diff -r 7145c0852013 django/db/backends/postgresql/base.py
a b 4 4 Requires psycopg 1: http://initd.org/projects/psycopg1 5 5 """ 6 6 7 import sys 8 9 from django.db import utils 7 10 from django.db.backends import * 8 11 from django.db.backends.signals import connection_created 9 12 from django.db.backends.postgresql.client import DatabaseClient … … 50 53 return tuple([smart_str(p, self.charset, True) for p in params]) 51 54 52 55 def execute(self, sql, params=()): 53 return self.cursor.execute(smart_str(sql, self.charset), self.format_params(params)) 56 try: 57 return self.cursor.execute(query, args) 58 except Database.IntegrityError, e: 59 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 60 except Database.DatabaseError, e: 61 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 54 62 55 63 def executemany(self, sql, param_list): 56 new_param_list = [self.format_params(params) for params in param_list] 57 return self.cursor.executemany(sql, new_param_list) 64 try: 65 new_param_list = [self.format_params(params) for params in param_list] 66 return self.cursor.executemany(sql, new_param_list) 67 except Database.IntegrityError, e: 68 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 69 except Database.DatabaseError, e: 70 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 58 71 59 72 def __getattr__(self, attr): 60 73 if attr in self.__dict__: -
django/db/backends/postgresql_psycopg2/base.py
diff -r 7145c0852013 django/db/backends/postgresql_psycopg2/base.py
a b 4 4 Requires psycopg 2: http://initd.org/projects/psycopg2 5 5 """ 6 6 7 import sys 8 9 from django.db import utils 7 10 from django.db.backends import * 8 11 from django.db.backends.signals import connection_created 9 12 from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations … … 27 30 psycopg2.extensions.register_adapter(SafeString, psycopg2.extensions.QuotedString) 28 31 psycopg2.extensions.register_adapter(SafeUnicode, psycopg2.extensions.QuotedString) 29 32 33 class CursorWrapper(object): 34 """ 35 A thin wrapper around psycopg2's normal cursor class so that we can catch 36 particular exception instances and reraise them with the right types. 37 """ 38 39 def __init__(self, cursor): 40 self.cursor = cursor 41 42 def execute(self, query, args=None): 43 try: 44 return self.cursor.execute(query, args) 45 except Database.IntegrityError, e: 46 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 47 except Database.DatabaseError, e: 48 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 49 50 def executemany(self, query, args): 51 try: 52 return self.cursor.executemany(query, args) 53 except Database.IntegrityError, e: 54 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 55 except Database.DatabaseError, e: 56 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 57 58 def __getattr__(self, attr): 59 if attr in self.__dict__: 60 return self.__dict__[attr] 61 else: 62 return getattr(self.cursor, attr) 63 64 def __iter__(self): 65 return iter(self.cursor) 66 30 67 class DatabaseFeatures(BaseDatabaseFeatures): 31 68 needs_datetime_string_cast = False 32 69 can_return_id_from_insert = False … … 118 155 # versions that support it, but, right now, that's hard to 119 156 # do without breaking other things (#10509). 120 157 self.features.can_return_id_from_insert = True 121 return cursor158 return CursorWrapper(cursor) 122 159 123 160 def _enter_transaction_management(self, managed): 124 161 """ -
django/db/backends/sqlite3/base.py
diff -r 7145c0852013 django/db/backends/sqlite3/base.py
a b 7 7 standard library. 8 8 """ 9 9 10 import sys 11 12 from django.db import utils 10 13 from django.db.backends import * 11 14 from django.db.backends.signals import connection_created 12 15 from django.db.backends.sqlite3.client import DatabaseClient … … 185 188 you'll need to use "%%s". 186 189 """ 187 190 def execute(self, query, params=()): 188 query = self.convert_query(query, len(params)) 189 return Database.Cursor.execute(self, query, params) 191 try: 192 query = self.convert_query(query, len(params)) 193 return Database.Cursor.execute(self, query, params) 194 except Database.IntegrityError, e: 195 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 196 except Database.DatabaseError, e: 197 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 190 198 191 199 def executemany(self, query, param_list): 192 200 try: 193 query = self.convert_query(query, len(param_list[0]))194 return Database.Cursor.executemany(self, query, param_list)201 query = self.convert_query(query, len(param_list[0])) 202 return Database.Cursor.executemany(self, query, param_list) 195 203 except (IndexError,TypeError): 196 # No parameter list provided 197 return None 204 # No parameter list provided 205 return None 206 except Database.IntegrityError, e: 207 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 208 except Database.DatabaseError, e: 209 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] 198 210 199 211 def convert_query(self, query, num_params): 200 212 return query % tuple("?" * num_params) -
django/db/utils.py
diff -r 7145c0852013 django/db/utils.py
a b 7 7 8 8 DEFAULT_DB_ALIAS = 'default' 9 9 10 # Define some exceptions that mirror the PEP249 interface. 11 # We will rethrow any backend-specific errors using these 12 # common wrappers 13 class DatabaseError(Exception): 14 pass 15 16 class IntegrityError(DatabaseError): 17 pass 18 19 10 20 def load_backend(backend_name): 11 21 try: 12 22 module = import_module('.base', 'django.db.backends.%s' % backend_name) … … 40 50 else: 41 51 raise # If there's some other error, this must be an error in Django itself. 42 52 53 43 54 class ConnectionDoesNotExist(Exception): 44 55 pass 45 56 57 46 58 class ConnectionHandler(object): 47 59 def __init__(self, databases): 48 60 self.databases = databases … … 87 99 def all(self): 88 100 return [self[alias] for alias in self] 89 101 102 90 103 class ConnectionRouter(object): 91 104 def __init__(self, routers): 92 105 self.routers = []