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.

base.py 15 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. """
  2. MySQL database backend for Django.
  3. Requires mysqlclient: https://pypi.python.org/pypi/mysqlclient/
  4. MySQLdb is supported for Python 2 only: http://sourceforge.net/projects/mysql-python
  5. """
  6. from __future__ import unicode_literals
  7. import datetime
  8. import re
  9. import sys
  10. import warnings
  11. from django.conf import settings
  12. from django.db import utils
  13. from django.db.backends import utils as backend_utils
  14. from django.db.backends.base.base import BaseDatabaseWrapper
  15. from django.utils import six, timezone
  16. from django.utils.deprecation import RemovedInDjango20Warning
  17. from django.utils.encoding import force_str
  18. from django.utils.functional import cached_property
  19. from django.utils.safestring import SafeBytes, SafeText
  20. try:
  21. import MySQLdb as Database
  22. except ImportError as e:
  23. from django.core.exceptions import ImproperlyConfigured
  24. raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
  25. from MySQLdb.constants import CLIENT, FIELD_TYPE # isort:skip
  26. from MySQLdb.converters import Thing2Literal, conversions # isort:skip
  27. # Some of these import MySQLdb, so import them after checking if it's installed.
  28. from .client import DatabaseClient # isort:skip
  29. from .creation import DatabaseCreation # isort:skip
  30. from .features import DatabaseFeatures # isort:skip
  31. from .introspection import DatabaseIntrospection # isort:skip
  32. from .operations import DatabaseOperations # isort:skip
  33. from .schema import DatabaseSchemaEditor # isort:skip
  34. from .validation import DatabaseValidation # isort:skip
  35. # We want version (1, 2, 1, 'final', 2) or later. We can't just use
  36. # lexicographic ordering in this check because then (1, 2, 1, 'gamma')
  37. # inadvertently passes the version test.
  38. version = Database.version_info
  39. if (version < (1, 2, 1) or (version[:3] == (1, 2, 1) and
  40. (len(version) < 5 or version[3] != 'final' or version[4] < 2))):
  41. from django.core.exceptions import ImproperlyConfigured
  42. raise ImproperlyConfigured("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__)
  43. DatabaseError = Database.DatabaseError
  44. IntegrityError = Database.IntegrityError
  45. def adapt_datetime_warn_on_aware_datetime(value, conv):
  46. # Remove this function and rely on the default adapter in Django 2.0.
  47. if settings.USE_TZ and timezone.is_aware(value):
  48. warnings.warn(
  49. "The MySQL database adapter received an aware datetime (%s), "
  50. "probably from cursor.execute(). Update your code to pass a "
  51. "naive datetime in the database connection's time zone (UTC by "
  52. "default).", RemovedInDjango20Warning)
  53. # This doesn't account for the database connection's timezone,
  54. # which isn't known. (That's why this adapter is deprecated.)
  55. value = value.astimezone(timezone.utc).replace(tzinfo=None)
  56. return Thing2Literal(value.strftime("%Y-%m-%d %H:%M:%S.%f"), conv)
  57. # MySQLdb-1.2.1 returns TIME columns as timedelta -- they are more like
  58. # timedelta in terms of actual behavior as they are signed and include days --
  59. # and Django expects time, so we still need to override that. We also need to
  60. # add special handling for SafeText and SafeBytes as MySQLdb's type
  61. # checking is too tight to catch those (see Django ticket #6052).
  62. django_conversions = conversions.copy()
  63. django_conversions.update({
  64. FIELD_TYPE.TIME: backend_utils.typecast_time,
  65. FIELD_TYPE.DECIMAL: backend_utils.typecast_decimal,
  66. FIELD_TYPE.NEWDECIMAL: backend_utils.typecast_decimal,
  67. datetime.datetime: adapt_datetime_warn_on_aware_datetime,
  68. })
  69. # This should match the numerical portion of the version numbers (we can treat
  70. # versions like 5.0.24 and 5.0.24a as the same). Based on the list of version
  71. # at http://dev.mysql.com/doc/refman/4.1/en/news.html and
  72. # http://dev.mysql.com/doc/refman/5.0/en/news.html .
  73. server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})')
  74. # MySQLdb-1.2.1 and newer automatically makes use of SHOW WARNINGS on
  75. # MySQL-4.1 and newer, so the MysqlDebugWrapper is unnecessary. Since the
  76. # point is to raise Warnings as exceptions, this can be done with the Python
  77. # warning module, and this is setup when the connection is created, and the
  78. # standard backend_utils.CursorDebugWrapper can be used. Also, using sql_mode
  79. # TRADITIONAL will automatically cause most warnings to be treated as errors.
  80. class CursorWrapper(object):
  81. """
  82. A thin wrapper around MySQLdb's normal cursor class so that we can catch
  83. particular exception instances and reraise them with the right types.
  84. Implemented as a wrapper, rather than a subclass, so that we aren't stuck
  85. to the particular underlying representation returned by Connection.cursor().
  86. """
  87. codes_for_integrityerror = (1048,)
  88. def __init__(self, cursor):
  89. self.cursor = cursor
  90. def execute(self, query, args=None):
  91. try:
  92. # args is None means no string interpolation
  93. return self.cursor.execute(query, args)
  94. except Database.OperationalError as e:
  95. # Map some error codes to IntegrityError, since they seem to be
  96. # misclassified and Django would prefer the more logical place.
  97. if e.args[0] in self.codes_for_integrityerror:
  98. six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  99. raise
  100. def executemany(self, query, args):
  101. try:
  102. return self.cursor.executemany(query, args)
  103. except Database.OperationalError as e:
  104. # Map some error codes to IntegrityError, since they seem to be
  105. # misclassified and Django would prefer the more logical place.
  106. if e.args[0] in self.codes_for_integrityerror:
  107. six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  108. raise
  109. def __getattr__(self, attr):
  110. if attr in self.__dict__:
  111. return self.__dict__[attr]
  112. else:
  113. return getattr(self.cursor, attr)
  114. def __iter__(self):
  115. return iter(self.cursor)
  116. def __enter__(self):
  117. return self
  118. def __exit__(self, type, value, traceback):
  119. # Ticket #17671 - Close instead of passing thru to avoid backend
  120. # specific behavior.
  121. self.close()
  122. class DatabaseWrapper(BaseDatabaseWrapper):
  123. vendor = 'mysql'
  124. # This dictionary maps Field objects to their associated MySQL column
  125. # types, as strings. Column-type strings can contain format strings; they'll
  126. # be interpolated against the values of Field.__dict__ before being output.
  127. # If a column type is set to None, it won't be included in the output.
  128. _data_types = {
  129. 'AutoField': 'integer AUTO_INCREMENT',
  130. 'BinaryField': 'longblob',
  131. 'BooleanField': 'bool',
  132. 'CharField': 'varchar(%(max_length)s)',
  133. 'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
  134. 'DateField': 'date',
  135. 'DateTimeField': 'datetime',
  136. 'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
  137. 'DurationField': 'bigint',
  138. 'FileField': 'varchar(%(max_length)s)',
  139. 'FilePathField': 'varchar(%(max_length)s)',
  140. 'FloatField': 'double precision',
  141. 'IntegerField': 'integer',
  142. 'BigIntegerField': 'bigint',
  143. 'IPAddressField': 'char(15)',
  144. 'GenericIPAddressField': 'char(39)',
  145. 'NullBooleanField': 'bool',
  146. 'OneToOneField': 'integer',
  147. 'PositiveIntegerField': 'integer UNSIGNED',
  148. 'PositiveSmallIntegerField': 'smallint UNSIGNED',
  149. 'SlugField': 'varchar(%(max_length)s)',
  150. 'SmallIntegerField': 'smallint',
  151. 'TextField': 'longtext',
  152. 'TimeField': 'time',
  153. 'UUIDField': 'char(32)',
  154. }
  155. @cached_property
  156. def data_types(self):
  157. if self.features.supports_microsecond_precision:
  158. return dict(self._data_types, DateTimeField='datetime(6)', TimeField='time(6)')
  159. else:
  160. return self._data_types
  161. operators = {
  162. 'exact': '= %s',
  163. 'iexact': 'LIKE %s',
  164. 'contains': 'LIKE BINARY %s',
  165. 'icontains': 'LIKE %s',
  166. 'regex': 'REGEXP BINARY %s',
  167. 'iregex': 'REGEXP %s',
  168. 'gt': '> %s',
  169. 'gte': '>= %s',
  170. 'lt': '< %s',
  171. 'lte': '<= %s',
  172. 'startswith': 'LIKE BINARY %s',
  173. 'endswith': 'LIKE BINARY %s',
  174. 'istartswith': 'LIKE %s',
  175. 'iendswith': 'LIKE %s',
  176. }
  177. # The patterns below are used to generate SQL pattern lookup clauses when
  178. # the right-hand side of the lookup isn't a raw string (it might be an expression
  179. # or the result of a bilateral transformation).
  180. # In those cases, special characters for LIKE operators (e.g. \, *, _) should be
  181. # escaped on database side.
  182. #
  183. # Note: we use str.format() here for readability as '%' is used as a wildcard for
  184. # the LIKE operator.
  185. pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\\', '\\\\'), '%%', '\%%'), '_', '\_')"
  186. pattern_ops = {
  187. 'contains': "LIKE BINARY CONCAT('%%', {}, '%%')",
  188. 'icontains': "LIKE CONCAT('%%', {}, '%%')",
  189. 'startswith': "LIKE BINARY CONCAT({}, '%%')",
  190. 'istartswith': "LIKE CONCAT({}, '%%')",
  191. 'endswith': "LIKE BINARY CONCAT('%%', {})",
  192. 'iendswith': "LIKE CONCAT('%%', {})",
  193. }
  194. Database = Database
  195. SchemaEditorClass = DatabaseSchemaEditor
  196. def __init__(self, *args, **kwargs):
  197. super(DatabaseWrapper, self).__init__(*args, **kwargs)
  198. self.features = DatabaseFeatures(self)
  199. self.ops = DatabaseOperations(self)
  200. self.client = DatabaseClient(self)
  201. self.creation = DatabaseCreation(self)
  202. self.introspection = DatabaseIntrospection(self)
  203. self.validation = DatabaseValidation(self)
  204. def get_connection_params(self):
  205. kwargs = {
  206. 'conv': django_conversions,
  207. 'charset': 'utf8',
  208. }
  209. if six.PY2:
  210. kwargs['use_unicode'] = True
  211. settings_dict = self.settings_dict
  212. if settings_dict['USER']:
  213. kwargs['user'] = settings_dict['USER']
  214. if settings_dict['NAME']:
  215. kwargs['db'] = settings_dict['NAME']
  216. if settings_dict['PASSWORD']:
  217. kwargs['passwd'] = force_str(settings_dict['PASSWORD'])
  218. if settings_dict['HOST'].startswith('/'):
  219. kwargs['unix_socket'] = settings_dict['HOST']
  220. elif settings_dict['HOST']:
  221. kwargs['host'] = settings_dict['HOST']
  222. if settings_dict['PORT']:
  223. kwargs['port'] = int(settings_dict['PORT'])
  224. # We need the number of potentially affected rows after an
  225. # "UPDATE", not the number of changed rows.
  226. kwargs['client_flag'] = CLIENT.FOUND_ROWS
  227. kwargs.update(settings_dict['OPTIONS'])
  228. return kwargs
  229. def get_new_connection(self, conn_params):
  230. conn = Database.connect(**conn_params)
  231. conn.encoders[SafeText] = conn.encoders[six.text_type]
  232. conn.encoders[SafeBytes] = conn.encoders[bytes]
  233. return conn
  234. def init_connection_state(self):
  235. with self.cursor() as cursor:
  236. # SQL_AUTO_IS_NULL in MySQL controls whether an AUTO_INCREMENT column
  237. # on a recently-inserted row will return when the field is tested for
  238. # NULL. Disabling this value brings this aspect of MySQL in line with
  239. # SQL standards.
  240. cursor.execute('SET SQL_AUTO_IS_NULL = 0')
  241. def create_cursor(self):
  242. cursor = self.connection.cursor()
  243. return CursorWrapper(cursor)
  244. def _rollback(self):
  245. try:
  246. BaseDatabaseWrapper._rollback(self)
  247. except Database.NotSupportedError:
  248. pass
  249. def _set_autocommit(self, autocommit):
  250. with self.wrap_database_errors:
  251. self.connection.autocommit(autocommit)
  252. def disable_constraint_checking(self):
  253. """
  254. Disables foreign key checks, primarily for use in adding rows with forward references. Always returns True,
  255. to indicate constraint checks need to be re-enabled.
  256. """
  257. self.cursor().execute('SET foreign_key_checks=0')
  258. return True
  259. def enable_constraint_checking(self):
  260. """
  261. Re-enable foreign key checks after they have been disabled.
  262. """
  263. # Override needs_rollback in case constraint_checks_disabled is
  264. # nested inside transaction.atomic.
  265. self.needs_rollback, needs_rollback = False, self.needs_rollback
  266. try:
  267. self.cursor().execute('SET foreign_key_checks=1')
  268. finally:
  269. self.needs_rollback = needs_rollback
  270. def check_constraints(self, table_names=None):
  271. """
  272. Checks each table name in `table_names` for rows with invalid foreign
  273. key references. This method is intended to be used in conjunction with
  274. `disable_constraint_checking()` and `enable_constraint_checking()`, to
  275. determine if rows with invalid references were entered while constraint
  276. checks were off.
  277. Raises an IntegrityError on the first invalid foreign key reference
  278. encountered (if any) and provides detailed information about the
  279. invalid reference in the error message.
  280. Backends can override this method if they can more directly apply
  281. constraint checking (e.g. via "SET CONSTRAINTS ALL IMMEDIATE")
  282. """
  283. cursor = self.cursor()
  284. if table_names is None:
  285. table_names = self.introspection.table_names(cursor)
  286. for table_name in table_names:
  287. primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
  288. if not primary_key_column_name:
  289. continue
  290. key_columns = self.introspection.get_key_columns(cursor, table_name)
  291. for column_name, referenced_table_name, referenced_column_name in key_columns:
  292. cursor.execute("""
  293. SELECT REFERRING.`%s`, REFERRING.`%s` FROM `%s` as REFERRING
  294. LEFT JOIN `%s` as REFERRED
  295. ON (REFERRING.`%s` = REFERRED.`%s`)
  296. WHERE REFERRING.`%s` IS NOT NULL AND REFERRED.`%s` IS NULL"""
  297. % (primary_key_column_name, column_name, table_name, referenced_table_name,
  298. column_name, referenced_column_name, column_name, referenced_column_name))
  299. for bad_row in cursor.fetchall():
  300. raise utils.IntegrityError("The row in table '%s' with primary key '%s' has an invalid "
  301. "foreign key: %s.%s contains a value '%s' that does not have a corresponding value in %s.%s."
  302. % (table_name, bad_row[0],
  303. table_name, column_name, bad_row[1],
  304. referenced_table_name, referenced_column_name))
  305. def is_usable(self):
  306. try:
  307. self.connection.ping()
  308. except Database.Error:
  309. return False
  310. else:
  311. return True
  312. @cached_property
  313. def mysql_version(self):
  314. with self.temporary_connection():
  315. server_info = self.connection.get_server_info()
  316. match = server_version_re.match(server_info)
  317. if not match:
  318. raise Exception('Unable to determine MySQL version from version string %r' % server_info)
  319. return tuple(int(x) for x in match.groups())