You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
10KB

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