cymysql.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # mysql/cymysql.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+cymysql
  9. :name: CyMySQL
  10. :dbapi: cymysql
  11. :connectstring: mysql+cymysql://<username>:<password>@<host>/<dbname>\
  12. [?<options>]
  13. :url: https://github.com/nakagami/CyMySQL
  14. """
  15. import re
  16. from .mysqldb import MySQLDialect_mysqldb
  17. from .base import (BIT, MySQLDialect)
  18. from ... import util
  19. class _cymysqlBIT(BIT):
  20. def result_processor(self, dialect, coltype):
  21. """Convert a MySQL's 64 bit, variable length binary string to a long.
  22. """
  23. def process(value):
  24. if value is not None:
  25. v = 0
  26. for i in util.iterbytes(value):
  27. v = v << 8 | i
  28. return v
  29. return value
  30. return process
  31. class MySQLDialect_cymysql(MySQLDialect_mysqldb):
  32. driver = 'cymysql'
  33. description_encoding = None
  34. supports_sane_rowcount = True
  35. supports_sane_multi_rowcount = False
  36. supports_unicode_statements = True
  37. colspecs = util.update_copy(
  38. MySQLDialect.colspecs,
  39. {
  40. BIT: _cymysqlBIT,
  41. }
  42. )
  43. @classmethod
  44. def dbapi(cls):
  45. return __import__('cymysql')
  46. def _get_server_version_info(self, connection):
  47. dbapi_con = connection.connection
  48. version = []
  49. r = re.compile(r'[.\-]')
  50. for n in r.split(dbapi_con.server_version):
  51. try:
  52. version.append(int(n))
  53. except ValueError:
  54. version.append(n)
  55. return tuple(version)
  56. def _detect_charset(self, connection):
  57. return connection.connection.charset
  58. def _extract_error_code(self, exception):
  59. return exception.errno
  60. def is_disconnect(self, e, connection, cursor):
  61. if isinstance(e, self.dbapi.OperationalError):
  62. return self._extract_error_code(e) in \
  63. (2006, 2013, 2014, 2045, 2055)
  64. elif isinstance(e, self.dbapi.InterfaceError):
  65. # if underlying connection is closed,
  66. # this is the error you get
  67. return True
  68. else:
  69. return False
  70. dialect = MySQLDialect_cymysql