pymysql.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # mysql/pymysql.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """
  8. .. dialect:: mysql+pymysql
  9. :name: PyMySQL
  10. :dbapi: pymysql
  11. :connectstring: mysql+pymysql://<username>:<password>@<host>/<dbname>\
  12. [?<options>]
  13. :url: http://www.pymysql.org/
  14. Unicode
  15. -------
  16. Please see :ref:`mysql_unicode` for current recommendations on unicode
  17. handling.
  18. MySQL-Python Compatibility
  19. --------------------------
  20. The pymysql DBAPI is a pure Python port of the MySQL-python (MySQLdb) driver,
  21. and targets 100% compatibility. Most behavioral notes for MySQL-python apply
  22. to the pymysql driver as well.
  23. """
  24. from .mysqldb import MySQLDialect_mysqldb
  25. from ...util import langhelpers, py3k
  26. class MySQLDialect_pymysql(MySQLDialect_mysqldb):
  27. driver = 'pymysql'
  28. description_encoding = None
  29. # generally, these two values should be both True
  30. # or both False. PyMySQL unicode tests pass all the way back
  31. # to 0.4 either way. See [ticket:3337]
  32. supports_unicode_statements = True
  33. supports_unicode_binds = True
  34. def __init__(self, server_side_cursors=False, **kwargs):
  35. super(MySQLDialect_pymysql, self).__init__(**kwargs)
  36. self.server_side_cursors = server_side_cursors
  37. @langhelpers.memoized_property
  38. def supports_server_side_cursors(self):
  39. try:
  40. cursors = __import__('pymysql.cursors').cursors
  41. self._sscursor = cursors.SSCursor
  42. return True
  43. except (ImportError, AttributeError):
  44. return False
  45. @classmethod
  46. def dbapi(cls):
  47. return __import__('pymysql')
  48. if py3k:
  49. def _extract_error_code(self, exception):
  50. if isinstance(exception.args[0], Exception):
  51. exception = exception.args[0]
  52. return exception.args[0]
  53. dialect = MySQLDialect_pymysql