SourceForge: mysql-python/MySQLdb-2.0: MySQLdb/exceptions.py@e705129ff06f
MySQLdb/exceptions.py
author Andy Dustman <adustman@users.sourceforge.net>
Tue Aug 31 22:28:13 2010 -0400 (20 months ago)
branchMySQLdb
changeset 83 e705129ff06f
permissions -rw-r--r--
Merge some Kyle stuff
     1 """
     2 MySQLdb.exceptions
     3 ==================
     4 
     5 These classes are dictated by the DB API v2.0:
     6 
     7     http://www.python.org/topics/database/DatabaseAPI-2.0.html
     8 """
     9 
    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.
    13 
    14 try:
    15     from exceptions import Exception, StandardError, Warning
    16 except ImportError:
    17     import sys
    18     e = sys.modules['exceptions']
    19     StandardError = e.StandardError
    20     Warning = e.Warning
    21     
    22 from MySQLdb.constants import ER
    23 
    24 class MySQLError(StandardError):
    25     
    26     """Exception related to operation with MySQL."""
    27 
    28 
    29 class Warning(Warning, MySQLError):
    30 
    31     """Exception raised for important warnings like data truncations
    32     while inserting, etc."""
    33 
    34 class Error(MySQLError):
    35 
    36     """Exception that is the base class of all other error exceptions
    37     (not Warning)."""
    38 
    39 
    40 class InterfaceError(Error):
    41 
    42     """Exception raised for errors that are related to the database
    43     interface rather than the database itself."""
    44 
    45 
    46 class DatabaseError(Error):
    47 
    48     """Exception raised for errors that are related to the
    49     database."""
    50 
    51 
    52 class DataError(DatabaseError):
    53 
    54     """Exception raised for errors that are due to problems with the
    55     processed data like division by zero, numeric value out of range,
    56     etc."""
    57 
    58 
    59 class OperationalError(DatabaseError):
    60 
    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."""
    66 
    67 
    68 class IntegrityError(DatabaseError):
    69 
    70     """Exception raised when the relational integrity of the database
    71     is affected, e.g. a foreign key check fails, duplicate key,
    72     etc."""
    73 
    74 
    75 class InternalError(DatabaseError):
    76 
    77     """Exception raised when the database encounters an internal
    78     error, e.g. the cursor is not valid anymore, the transaction is
    79     out of sync, etc."""
    80 
    81 
    82 class ProgrammingError(DatabaseError):
    83 
    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."""
    87 
    88 
    89 class NotSupportedError(DatabaseError):
    90 
    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."""
    95 
    96 
    97 error_map = {}
    98 
    99 def _map_error(exc, *errors):
   100     for error in errors:
   101         error_map[error] = exc
   102 
   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)
   116 
   117 del StandardError, _map_error, ER