mysqlconnector.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # mysql/mysqlconnector.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+mysqlconnector
  9. :name: MySQL Connector/Python
  10. :dbapi: myconnpy
  11. :connectstring: mysql+mysqlconnector://<user>:<password>@\
  12. <host>[:<port>]/<dbname>
  13. :url: http://dev.mysql.com/downloads/connector/python/
  14. Unicode
  15. -------
  16. Please see :ref:`mysql_unicode` for current recommendations on unicode
  17. handling.
  18. """
  19. from .base import (MySQLDialect, MySQLExecutionContext,
  20. MySQLCompiler, MySQLIdentifierPreparer,
  21. BIT)
  22. from ... import util
  23. import re
  24. class MySQLExecutionContext_mysqlconnector(MySQLExecutionContext):
  25. def get_lastrowid(self):
  26. return self.cursor.lastrowid
  27. class MySQLCompiler_mysqlconnector(MySQLCompiler):
  28. def visit_mod_binary(self, binary, operator, **kw):
  29. if self.dialect._mysqlconnector_double_percents:
  30. return self.process(binary.left, **kw) + " %% " + \
  31. self.process(binary.right, **kw)
  32. else:
  33. return self.process(binary.left, **kw) + " % " + \
  34. self.process(binary.right, **kw)
  35. def post_process_text(self, text):
  36. if self.dialect._mysqlconnector_double_percents:
  37. return text.replace('%', '%%')
  38. else:
  39. return text
  40. def escape_literal_column(self, text):
  41. if self.dialect._mysqlconnector_double_percents:
  42. return text.replace('%', '%%')
  43. else:
  44. return text
  45. class MySQLIdentifierPreparer_mysqlconnector(MySQLIdentifierPreparer):
  46. def _escape_identifier(self, value):
  47. value = value.replace(self.escape_quote, self.escape_to_quote)
  48. if self.dialect._mysqlconnector_double_percents:
  49. return value.replace("%", "%%")
  50. else:
  51. return value
  52. class _myconnpyBIT(BIT):
  53. def result_processor(self, dialect, coltype):
  54. """MySQL-connector already converts mysql bits, so."""
  55. return None
  56. class MySQLDialect_mysqlconnector(MySQLDialect):
  57. driver = 'mysqlconnector'
  58. supports_unicode_binds = True
  59. supports_sane_rowcount = True
  60. supports_sane_multi_rowcount = True
  61. supports_native_decimal = True
  62. default_paramstyle = 'format'
  63. execution_ctx_cls = MySQLExecutionContext_mysqlconnector
  64. statement_compiler = MySQLCompiler_mysqlconnector
  65. preparer = MySQLIdentifierPreparer_mysqlconnector
  66. colspecs = util.update_copy(
  67. MySQLDialect.colspecs,
  68. {
  69. BIT: _myconnpyBIT,
  70. }
  71. )
  72. @util.memoized_property
  73. def supports_unicode_statements(self):
  74. return util.py3k or self._mysqlconnector_version_info > (2, 0)
  75. @classmethod
  76. def dbapi(cls):
  77. from mysql import connector
  78. return connector
  79. def create_connect_args(self, url):
  80. opts = url.translate_connect_args(username='user')
  81. opts.update(url.query)
  82. util.coerce_kw_type(opts, 'allow_local_infile', bool)
  83. util.coerce_kw_type(opts, 'autocommit', bool)
  84. util.coerce_kw_type(opts, 'buffered', bool)
  85. util.coerce_kw_type(opts, 'compress', bool)
  86. util.coerce_kw_type(opts, 'connection_timeout', int)
  87. util.coerce_kw_type(opts, 'connect_timeout', int)
  88. util.coerce_kw_type(opts, 'consume_results', bool)
  89. util.coerce_kw_type(opts, 'force_ipv6', bool)
  90. util.coerce_kw_type(opts, 'get_warnings', bool)
  91. util.coerce_kw_type(opts, 'pool_reset_session', bool)
  92. util.coerce_kw_type(opts, 'pool_size', int)
  93. util.coerce_kw_type(opts, 'raise_on_warnings', bool)
  94. util.coerce_kw_type(opts, 'raw', bool)
  95. util.coerce_kw_type(opts, 'ssl_verify_cert', bool)
  96. util.coerce_kw_type(opts, 'use_pure', bool)
  97. util.coerce_kw_type(opts, 'use_unicode', bool)
  98. # unfortunately, MySQL/connector python refuses to release a
  99. # cursor without reading fully, so non-buffered isn't an option
  100. opts.setdefault('buffered', True)
  101. # FOUND_ROWS must be set in ClientFlag to enable
  102. # supports_sane_rowcount.
  103. if self.dbapi is not None:
  104. try:
  105. from mysql.connector.constants import ClientFlag
  106. client_flags = opts.get(
  107. 'client_flags', ClientFlag.get_default())
  108. client_flags |= ClientFlag.FOUND_ROWS
  109. opts['client_flags'] = client_flags
  110. except Exception:
  111. pass
  112. return [[], opts]
  113. @util.memoized_property
  114. def _mysqlconnector_version_info(self):
  115. if self.dbapi and hasattr(self.dbapi, '__version__'):
  116. m = re.match(r'(\d+)\.(\d+)(?:\.(\d+))?',
  117. self.dbapi.__version__)
  118. if m:
  119. return tuple(
  120. int(x)
  121. for x in m.group(1, 2, 3)
  122. if x is not None)
  123. @util.memoized_property
  124. def _mysqlconnector_double_percents(self):
  125. return not util.py3k and self._mysqlconnector_version_info < (2, 0)
  126. def _get_server_version_info(self, connection):
  127. dbapi_con = connection.connection
  128. version = dbapi_con.get_server_version()
  129. return tuple(version)
  130. def _detect_charset(self, connection):
  131. return connection.connection.charset
  132. def _extract_error_code(self, exception):
  133. return exception.errno
  134. def is_disconnect(self, e, connection, cursor):
  135. errnos = (2006, 2013, 2014, 2045, 2055, 2048)
  136. exceptions = (self.dbapi.OperationalError, self.dbapi.InterfaceError)
  137. if isinstance(e, exceptions):
  138. return e.errno in errnos or \
  139. "MySQL Connection not available." in str(e)
  140. else:
  141. return False
  142. def _compat_fetchall(self, rp, charset=None):
  143. return rp.fetchall()
  144. def _compat_fetchone(self, rp, charset=None):
  145. return rp.fetchone()
  146. _isolation_lookup = set(['SERIALIZABLE', 'READ UNCOMMITTED',
  147. 'READ COMMITTED', 'REPEATABLE READ',
  148. 'AUTOCOMMIT'])
  149. def _set_isolation_level(self, connection, level):
  150. if level == 'AUTOCOMMIT':
  151. connection.autocommit = True
  152. else:
  153. connection.autocommit = False
  154. super(MySQLDialect_mysqlconnector, self)._set_isolation_level(
  155. connection, level)
  156. dialect = MySQLDialect_mysqlconnector