5 These classes are dictated by the DB API v2.0:
7 http://www.python.org/topics/database/DatabaseAPI-2.0.html
10 # from __future__ import absolute_import
11 # Unfortunately, you cannot put the above in a conditional statement.
12 # It would make things much cleaner for Python-2.5, but breaks older.
15 from exceptions import Exception, StandardError, Warning
18 e = sys.modules['exceptions']
19 StandardError = e.StandardError
22 from MySQLdb.constants import ER
24 class MySQLError(StandardError):
26 """Exception related to operation with MySQL."""
29 class Warning(Warning, MySQLError):
31 """Exception raised for important warnings like data truncations
32 while inserting, etc."""
34 class Error(MySQLError):
36 """Exception that is the base class of all other error exceptions
40 class InterfaceError(Error):
42 """Exception raised for errors that are related to the database
43 interface rather than the database itself."""
46 class DatabaseError(Error):
48 """Exception raised for errors that are related to the
52 class DataError(DatabaseError):
54 """Exception raised for errors that are due to problems with the
55 processed data like division by zero, numeric value out of range,
59 class OperationalError(DatabaseError):
61 """Exception raised for errors that are related to the database's
62 operation and not necessarily under the control of the programmer,
63 e.g. an unexpected disconnect occurs, the data source name is not
64 found, a transaction could not be processed, a memory allocation
65 error occurred during processing, etc."""
68 class IntegrityError(DatabaseError):
70 """Exception raised when the relational integrity of the database
71 is affected, e.g. a foreign key check fails, duplicate key,
75 class InternalError(DatabaseError):
77 """Exception raised when the database encounters an internal
78 error, e.g. the cursor is not valid anymore, the transaction is
82 class ProgrammingError(DatabaseError):
84 """Exception raised for programming errors, e.g. table not found
85 or already exists, syntax error in the SQL statement, wrong number
86 of parameters specified, etc."""
89 class NotSupportedError(DatabaseError):
91 """Exception raised in case a method or database API was used
92 which is not supported by the database, e.g. requesting a
93 .rollback() on a connection that does not support transaction or
94 has transactions turned off."""
99 def _map_error(exc, *errors):
101 error_map[error] = exc
103 _map_error(ProgrammingError, ER.DB_CREATE_EXISTS, ER.SYNTAX_ERROR,
104 ER.PARSE_ERROR, ER.NO_SUCH_TABLE, ER.WRONG_DB_NAME,
105 ER.WRONG_TABLE_NAME, ER.FIELD_SPECIFIED_TWICE,
106 ER.INVALID_GROUP_FUNC_USE, ER.UNSUPPORTED_EXTENSION,
107 ER.TABLE_MUST_HAVE_COLUMNS, ER.CANT_DO_THIS_DURING_AN_TRANSACTION)
108 _map_error(DataError, ER.WARN_DATA_TRUNCATED, ER.WARN_NULL_TO_NOTNULL,
109 ER.WARN_DATA_OUT_OF_RANGE, ER.NO_DEFAULT, ER.PRIMARY_CANT_HAVE_NULL,
110 ER.DATA_TOO_LONG, ER.DATETIME_FUNCTION_OVERFLOW)
111 _map_error(IntegrityError, ER.DUP_ENTRY, ER.NO_REFERENCED_ROW,
112 ER.NO_REFERENCED_ROW_2, ER.ROW_IS_REFERENCED, ER.ROW_IS_REFERENCED_2,
113 ER.CANNOT_ADD_FOREIGN)
114 _map_error(NotSupportedError, ER.WARNING_NOT_COMPLETE_ROLLBACK,
115 ER.NOT_SUPPORTED_YET, ER.FEATURE_DISABLED, ER.UNKNOWN_STORAGE_ENGINE)
117 del StandardError, _map_error, ER