PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/gluon/contrib/pymysql/err.py

http://github.com/web2py/web2py
Python | 107 lines | 79 code | 15 blank | 13 comment | 3 complexity | da364fc7b8be1a822d9e018aa62da301 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, BSD-2-Clause, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. import struct
  2. from .constants import ER
  3. class MySQLError(Exception):
  4. """Exception related to operation with MySQL."""
  5. class Warning(Warning, MySQLError):
  6. """Exception raised for important warnings like data truncations
  7. while inserting, etc."""
  8. class Error(MySQLError):
  9. """Exception that is the base class of all other error exceptions
  10. (not Warning)."""
  11. class InterfaceError(Error):
  12. """Exception raised for errors that are related to the database
  13. interface rather than the database itself."""
  14. class DatabaseError(Error):
  15. """Exception raised for errors that are related to the
  16. database."""
  17. class DataError(DatabaseError):
  18. """Exception raised for errors that are due to problems with the
  19. processed data like division by zero, numeric value out of range,
  20. etc."""
  21. class OperationalError(DatabaseError):
  22. """Exception raised for errors that are related to the database's
  23. operation and not necessarily under the control of the programmer,
  24. e.g. an unexpected disconnect occurs, the data source name is not
  25. found, a transaction could not be processed, a memory allocation
  26. error occurred during processing, etc."""
  27. class IntegrityError(DatabaseError):
  28. """Exception raised when the relational integrity of the database
  29. is affected, e.g. a foreign key check fails, duplicate key,
  30. etc."""
  31. class InternalError(DatabaseError):
  32. """Exception raised when the database encounters an internal
  33. error, e.g. the cursor is not valid anymore, the transaction is
  34. out of sync, etc."""
  35. class ProgrammingError(DatabaseError):
  36. """Exception raised for programming errors, e.g. table not found
  37. or already exists, syntax error in the SQL statement, wrong number
  38. of parameters specified, etc."""
  39. class NotSupportedError(DatabaseError):
  40. """Exception raised in case a method or database API was used
  41. which is not supported by the database, e.g. requesting a
  42. .rollback() on a connection that does not support transaction or
  43. has transactions turned off."""
  44. error_map = {}
  45. def _map_error(exc, *errors):
  46. for error in errors:
  47. error_map[error] = exc
  48. _map_error(ProgrammingError, ER.DB_CREATE_EXISTS, ER.SYNTAX_ERROR,
  49. ER.PARSE_ERROR, ER.NO_SUCH_TABLE, ER.WRONG_DB_NAME,
  50. ER.WRONG_TABLE_NAME, ER.FIELD_SPECIFIED_TWICE,
  51. ER.INVALID_GROUP_FUNC_USE, ER.UNSUPPORTED_EXTENSION,
  52. ER.TABLE_MUST_HAVE_COLUMNS, ER.CANT_DO_THIS_DURING_AN_TRANSACTION)
  53. _map_error(DataError, ER.WARN_DATA_TRUNCATED, ER.WARN_NULL_TO_NOTNULL,
  54. ER.WARN_DATA_OUT_OF_RANGE, ER.NO_DEFAULT, ER.PRIMARY_CANT_HAVE_NULL,
  55. ER.DATA_TOO_LONG, ER.DATETIME_FUNCTION_OVERFLOW)
  56. _map_error(IntegrityError, ER.DUP_ENTRY, ER.NO_REFERENCED_ROW,
  57. ER.NO_REFERENCED_ROW_2, ER.ROW_IS_REFERENCED, ER.ROW_IS_REFERENCED_2,
  58. ER.CANNOT_ADD_FOREIGN, ER.BAD_NULL_ERROR)
  59. _map_error(NotSupportedError, ER.WARNING_NOT_COMPLETE_ROLLBACK,
  60. ER.NOT_SUPPORTED_YET, ER.FEATURE_DISABLED, ER.UNKNOWN_STORAGE_ENGINE)
  61. _map_error(OperationalError, ER.DBACCESS_DENIED_ERROR, ER.ACCESS_DENIED_ERROR,
  62. ER.CON_COUNT_ERROR, ER.TABLEACCESS_DENIED_ERROR,
  63. ER.COLUMNACCESS_DENIED_ERROR)
  64. del _map_error, ER
  65. def raise_mysql_exception(data):
  66. errno = struct.unpack('<h', data[1:3])[0]
  67. is_41 = data[3:4] == b"#"
  68. if is_41:
  69. # client protocol 4.1
  70. errval = data[9:].decode('utf-8', 'replace')
  71. else:
  72. errval = data[3:].decode('utf-8', 'replace')
  73. errorclass = error_map.get(errno, InternalError)
  74. raise errorclass(errno, errval)