mysqldb.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # dialects/mysql/mysqldb.py
  2. # Copyright (C) 2005-2024 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: https://www.opensource.org/licenses/mit-license.php
  7. """
  8. .. dialect:: mysql+mysqldb
  9. :name: mysqlclient (maintained fork of MySQL-Python)
  10. :dbapi: mysqldb
  11. :connectstring: mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
  12. :url: https://pypi.org/project/mysqlclient/
  13. Driver Status
  14. -------------
  15. The mysqlclient DBAPI is a maintained fork of the
  16. `MySQL-Python <https://sourceforge.net/projects/mysql-python>`_ DBAPI
  17. that is no longer maintained. `mysqlclient`_ supports Python 2 and Python 3
  18. and is very stable.
  19. .. _mysqlclient: https://github.com/PyMySQL/mysqlclient-python
  20. .. _mysqldb_unicode:
  21. Unicode
  22. -------
  23. Please see :ref:`mysql_unicode` for current recommendations on unicode
  24. handling.
  25. .. _mysqldb_ssl:
  26. SSL Connections
  27. ----------------
  28. The mysqlclient and PyMySQL DBAPIs accept an additional dictionary under the
  29. key "ssl", which may be specified using the
  30. :paramref:`_sa.create_engine.connect_args` dictionary::
  31. engine = create_engine(
  32. "mysql+mysqldb://scott:tiger@192.168.0.134/test",
  33. connect_args={
  34. "ssl": {
  35. "ca": "/home/gord/client-ssl/ca.pem",
  36. "cert": "/home/gord/client-ssl/client-cert.pem",
  37. "key": "/home/gord/client-ssl/client-key.pem"
  38. }
  39. }
  40. )
  41. For convenience, the following keys may also be specified inline within the URL
  42. where they will be interpreted into the "ssl" dictionary automatically:
  43. "ssl_ca", "ssl_cert", "ssl_key", "ssl_capath", "ssl_cipher",
  44. "ssl_check_hostname". An example is as follows::
  45. connection_uri = (
  46. "mysql+mysqldb://scott:tiger@192.168.0.134/test"
  47. "?ssl_ca=/home/gord/client-ssl/ca.pem"
  48. "&ssl_cert=/home/gord/client-ssl/client-cert.pem"
  49. "&ssl_key=/home/gord/client-ssl/client-key.pem"
  50. )
  51. .. seealso::
  52. :ref:`pymysql_ssl` in the PyMySQL dialect
  53. Using MySQLdb with Google Cloud SQL
  54. -----------------------------------
  55. Google Cloud SQL now recommends use of the MySQLdb dialect. Connect
  56. using a URL like the following::
  57. mysql+mysqldb://root@/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>
  58. Server Side Cursors
  59. -------------------
  60. The mysqldb dialect supports server-side cursors. See :ref:`mysql_ss_cursors`.
  61. """
  62. import re
  63. from .base import MySQLCompiler
  64. from .base import MySQLDialect
  65. from .base import MySQLExecutionContext
  66. from .base import MySQLIdentifierPreparer
  67. from .base import TEXT
  68. from ... import sql
  69. from ... import util
  70. class MySQLExecutionContext_mysqldb(MySQLExecutionContext):
  71. @property
  72. def rowcount(self):
  73. if hasattr(self, "_rowcount"):
  74. return self._rowcount
  75. else:
  76. return self.cursor.rowcount
  77. class MySQLCompiler_mysqldb(MySQLCompiler):
  78. pass
  79. class MySQLDialect_mysqldb(MySQLDialect):
  80. driver = "mysqldb"
  81. supports_statement_cache = True
  82. supports_unicode_statements = True
  83. supports_sane_rowcount = True
  84. supports_sane_multi_rowcount = True
  85. supports_native_decimal = True
  86. default_paramstyle = "format"
  87. execution_ctx_cls = MySQLExecutionContext_mysqldb
  88. statement_compiler = MySQLCompiler_mysqldb
  89. preparer = MySQLIdentifierPreparer
  90. def __init__(self, **kwargs):
  91. super(MySQLDialect_mysqldb, self).__init__(**kwargs)
  92. self._mysql_dbapi_version = (
  93. self._parse_dbapi_version(self.dbapi.__version__)
  94. if self.dbapi is not None and hasattr(self.dbapi, "__version__")
  95. else (0, 0, 0)
  96. )
  97. def _parse_dbapi_version(self, version):
  98. m = re.match(r"(\d+)\.(\d+)(?:\.(\d+))?", version)
  99. if m:
  100. return tuple(int(x) for x in m.group(1, 2, 3) if x is not None)
  101. else:
  102. return (0, 0, 0)
  103. @util.langhelpers.memoized_property
  104. def supports_server_side_cursors(self):
  105. try:
  106. cursors = __import__("MySQLdb.cursors").cursors
  107. self._sscursor = cursors.SSCursor
  108. return True
  109. except (ImportError, AttributeError):
  110. return False
  111. @classmethod
  112. def dbapi(cls):
  113. return __import__("MySQLdb")
  114. def on_connect(self):
  115. super_ = super(MySQLDialect_mysqldb, self).on_connect()
  116. def on_connect(conn):
  117. if super_ is not None:
  118. super_(conn)
  119. charset_name = conn.character_set_name()
  120. if charset_name is not None:
  121. cursor = conn.cursor()
  122. cursor.execute("SET NAMES %s" % charset_name)
  123. cursor.close()
  124. return on_connect
  125. def _ping_impl(self, dbapi_connection):
  126. return dbapi_connection.ping()
  127. def do_ping(self, dbapi_connection):
  128. try:
  129. self._ping_impl(dbapi_connection)
  130. except self.dbapi.Error as err:
  131. if self.is_disconnect(err, dbapi_connection, None):
  132. return False
  133. else:
  134. raise
  135. else:
  136. return True
  137. def do_executemany(self, cursor, statement, parameters, context=None):
  138. rowcount = cursor.executemany(statement, parameters)
  139. if context is not None:
  140. context._rowcount = rowcount
  141. def _check_unicode_returns(self, connection):
  142. # work around issue fixed in
  143. # https://github.com/farcepest/MySQLdb1/commit/cd44524fef63bd3fcb71947392326e9742d520e8
  144. # specific issue w/ the utf8mb4_bin collation and unicode returns
  145. collation = connection.exec_driver_sql(
  146. "show collation where %s = 'utf8mb4' and %s = 'utf8mb4_bin'"
  147. % (
  148. self.identifier_preparer.quote("Charset"),
  149. self.identifier_preparer.quote("Collation"),
  150. )
  151. ).scalar()
  152. has_utf8mb4_bin = self.server_version_info > (5,) and collation
  153. if has_utf8mb4_bin:
  154. additional_tests = [
  155. sql.collate(
  156. sql.cast(
  157. sql.literal_column("'test collated returns'"),
  158. TEXT(charset="utf8mb4"),
  159. ),
  160. "utf8mb4_bin",
  161. )
  162. ]
  163. else:
  164. additional_tests = []
  165. return super(MySQLDialect_mysqldb, self)._check_unicode_returns(
  166. connection, additional_tests
  167. )
  168. def create_connect_args(self, url, _translate_args=None):
  169. if _translate_args is None:
  170. _translate_args = dict(
  171. database="db", username="user", password="passwd"
  172. )
  173. opts = url.translate_connect_args(**_translate_args)
  174. opts.update(url.query)
  175. util.coerce_kw_type(opts, "compress", bool)
  176. util.coerce_kw_type(opts, "connect_timeout", int)
  177. util.coerce_kw_type(opts, "read_timeout", int)
  178. util.coerce_kw_type(opts, "write_timeout", int)
  179. util.coerce_kw_type(opts, "client_flag", int)
  180. util.coerce_kw_type(opts, "local_infile", int)
  181. # Note: using either of the below will cause all strings to be
  182. # returned as Unicode, both in raw SQL operations and with column
  183. # types like String and MSString.
  184. util.coerce_kw_type(opts, "use_unicode", bool)
  185. util.coerce_kw_type(opts, "charset", str)
  186. # Rich values 'cursorclass' and 'conv' are not supported via
  187. # query string.
  188. ssl = {}
  189. keys = [
  190. ("ssl_ca", str),
  191. ("ssl_key", str),
  192. ("ssl_cert", str),
  193. ("ssl_capath", str),
  194. ("ssl_cipher", str),
  195. ("ssl_check_hostname", bool),
  196. ]
  197. for key, kw_type in keys:
  198. if key in opts:
  199. ssl[key[4:]] = opts[key]
  200. util.coerce_kw_type(ssl, key[4:], kw_type)
  201. del opts[key]
  202. if ssl:
  203. opts["ssl"] = ssl
  204. # FOUND_ROWS must be set in CLIENT_FLAGS to enable
  205. # supports_sane_rowcount.
  206. client_flag = opts.get("client_flag", 0)
  207. client_flag_found_rows = self._found_rows_client_flag()
  208. if client_flag_found_rows is not None:
  209. client_flag |= client_flag_found_rows
  210. opts["client_flag"] = client_flag
  211. return [[], opts]
  212. def _found_rows_client_flag(self):
  213. if self.dbapi is not None:
  214. try:
  215. CLIENT_FLAGS = __import__(
  216. self.dbapi.__name__ + ".constants.CLIENT"
  217. ).constants.CLIENT
  218. except (AttributeError, ImportError):
  219. return None
  220. else:
  221. return CLIENT_FLAGS.FOUND_ROWS
  222. else:
  223. return None
  224. def _extract_error_code(self, exception):
  225. return exception.args[0]
  226. def _detect_charset(self, connection):
  227. """Sniff out the character set in use for connection results."""
  228. try:
  229. # note: the SQL here would be
  230. # "SHOW VARIABLES LIKE 'character_set%%'"
  231. cset_name = connection.connection.character_set_name
  232. except AttributeError:
  233. util.warn(
  234. "No 'character_set_name' can be detected with "
  235. "this MySQL-Python version; "
  236. "please upgrade to a recent version of MySQL-Python. "
  237. "Assuming latin1."
  238. )
  239. return "latin1"
  240. else:
  241. return cset_name()
  242. _isolation_lookup = set(
  243. [
  244. "SERIALIZABLE",
  245. "READ UNCOMMITTED",
  246. "READ COMMITTED",
  247. "REPEATABLE READ",
  248. "AUTOCOMMIT",
  249. ]
  250. )
  251. def _set_isolation_level(self, connection, level):
  252. if level == "AUTOCOMMIT":
  253. connection.autocommit(True)
  254. else:
  255. connection.autocommit(False)
  256. super(MySQLDialect_mysqldb, self)._set_isolation_level(
  257. connection, level
  258. )
  259. dialect = MySQLDialect_mysqldb