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.

operations.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import uuid
  2. from django.conf import settings
  3. from django.db.backends.base.operations import BaseDatabaseOperations
  4. from django.utils import timezone
  5. from django.utils.duration import duration_microseconds
  6. class DatabaseOperations(BaseDatabaseOperations):
  7. compiler_module = "django.db.backends.mysql.compiler"
  8. # MySQL stores positive fields as UNSIGNED ints.
  9. integer_field_ranges = {
  10. **BaseDatabaseOperations.integer_field_ranges,
  11. 'PositiveSmallIntegerField': (0, 65535),
  12. 'PositiveIntegerField': (0, 4294967295),
  13. }
  14. cast_data_types = {
  15. 'AutoField': 'signed integer',
  16. 'BigAutoField': 'signed integer',
  17. 'CharField': 'char(%(max_length)s)',
  18. 'DecimalField': 'decimal(%(max_digits)s, %(decimal_places)s)',
  19. 'TextField': 'char',
  20. 'IntegerField': 'signed integer',
  21. 'BigIntegerField': 'signed integer',
  22. 'SmallIntegerField': 'signed integer',
  23. 'PositiveIntegerField': 'unsigned integer',
  24. 'PositiveSmallIntegerField': 'unsigned integer',
  25. }
  26. cast_char_field_without_max_length = 'char'
  27. explain_prefix = 'EXPLAIN'
  28. def date_extract_sql(self, lookup_type, field_name):
  29. # https://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
  30. if lookup_type == 'week_day':
  31. # DAYOFWEEK() returns an integer, 1-7, Sunday=1.
  32. # Note: WEEKDAY() returns 0-6, Monday=0.
  33. return "DAYOFWEEK(%s)" % field_name
  34. elif lookup_type == 'week':
  35. # Override the value of default_week_format for consistency with
  36. # other database backends.
  37. # Mode 3: Monday, 1-53, with 4 or more days this year.
  38. return "WEEK(%s, 3)" % field_name
  39. elif lookup_type == 'iso_year':
  40. # Get the year part from the YEARWEEK function, which returns a
  41. # number as year * 100 + week.
  42. return "TRUNCATE(YEARWEEK(%s, 3), -2) / 100" % field_name
  43. else:
  44. # EXTRACT returns 1-53 based on ISO-8601 for the week number.
  45. return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
  46. def date_trunc_sql(self, lookup_type, field_name):
  47. fields = {
  48. 'year': '%%Y-01-01',
  49. 'month': '%%Y-%%m-01',
  50. } # Use double percents to escape.
  51. if lookup_type in fields:
  52. format_str = fields[lookup_type]
  53. return "CAST(DATE_FORMAT(%s, '%s') AS DATE)" % (field_name, format_str)
  54. elif lookup_type == 'quarter':
  55. return "MAKEDATE(YEAR(%s), 1) + INTERVAL QUARTER(%s) QUARTER - INTERVAL 1 QUARTER" % (
  56. field_name, field_name
  57. )
  58. elif lookup_type == 'week':
  59. return "DATE_SUB(%s, INTERVAL WEEKDAY(%s) DAY)" % (
  60. field_name, field_name
  61. )
  62. else:
  63. return "DATE(%s)" % (field_name)
  64. def _convert_field_to_tz(self, field_name, tzname):
  65. if settings.USE_TZ:
  66. field_name = "CONVERT_TZ(%s, 'UTC', '%s')" % (field_name, tzname)
  67. return field_name
  68. def datetime_cast_date_sql(self, field_name, tzname):
  69. field_name = self._convert_field_to_tz(field_name, tzname)
  70. return "DATE(%s)" % field_name
  71. def datetime_cast_time_sql(self, field_name, tzname):
  72. field_name = self._convert_field_to_tz(field_name, tzname)
  73. return "TIME(%s)" % field_name
  74. def datetime_extract_sql(self, lookup_type, field_name, tzname):
  75. field_name = self._convert_field_to_tz(field_name, tzname)
  76. return self.date_extract_sql(lookup_type, field_name)
  77. def datetime_trunc_sql(self, lookup_type, field_name, tzname):
  78. field_name = self._convert_field_to_tz(field_name, tzname)
  79. fields = ['year', 'month', 'day', 'hour', 'minute', 'second']
  80. format = ('%%Y-', '%%m', '-%%d', ' %%H:', '%%i', ':%%s') # Use double percents to escape.
  81. format_def = ('0000-', '01', '-01', ' 00:', '00', ':00')
  82. if lookup_type == 'quarter':
  83. return (
  84. "CAST(DATE_FORMAT(MAKEDATE(YEAR({field_name}), 1) + "
  85. "INTERVAL QUARTER({field_name}) QUARTER - " +
  86. "INTERVAL 1 QUARTER, '%%Y-%%m-01 00:00:00') AS DATETIME)"
  87. ).format(field_name=field_name)
  88. if lookup_type == 'week':
  89. return (
  90. "CAST(DATE_FORMAT(DATE_SUB({field_name}, "
  91. "INTERVAL WEEKDAY({field_name}) DAY), "
  92. "'%%Y-%%m-%%d 00:00:00') AS DATETIME)"
  93. ).format(field_name=field_name)
  94. try:
  95. i = fields.index(lookup_type) + 1
  96. except ValueError:
  97. sql = field_name
  98. else:
  99. format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]])
  100. sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str)
  101. return sql
  102. def time_trunc_sql(self, lookup_type, field_name):
  103. fields = {
  104. 'hour': '%%H:00:00',
  105. 'minute': '%%H:%%i:00',
  106. 'second': '%%H:%%i:%%s',
  107. } # Use double percents to escape.
  108. if lookup_type in fields:
  109. format_str = fields[lookup_type]
  110. return "CAST(DATE_FORMAT(%s, '%s') AS TIME)" % (field_name, format_str)
  111. else:
  112. return "TIME(%s)" % (field_name)
  113. def date_interval_sql(self, timedelta):
  114. return 'INTERVAL %s MICROSECOND' % duration_microseconds(timedelta)
  115. def format_for_duration_arithmetic(self, sql):
  116. return 'INTERVAL %s MICROSECOND' % sql
  117. def force_no_ordering(self):
  118. """
  119. "ORDER BY NULL" prevents MySQL from implicitly ordering by grouped
  120. columns. If no ordering would otherwise be applied, we don't want any
  121. implicit sorting going on.
  122. """
  123. return [(None, ("NULL", [], False))]
  124. def last_executed_query(self, cursor, sql, params):
  125. # With MySQLdb, cursor objects have an (undocumented) "_executed"
  126. # attribute where the exact query sent to the database is saved.
  127. # See MySQLdb/cursors.py in the source distribution.
  128. query = getattr(cursor, '_executed', None)
  129. if query is not None:
  130. query = query.decode(errors='replace')
  131. return query
  132. def no_limit_value(self):
  133. # 2**64 - 1, as recommended by the MySQL documentation
  134. return 18446744073709551615
  135. def quote_name(self, name):
  136. if name.startswith("`") and name.endswith("`"):
  137. return name # Quoting once is enough.
  138. return "`%s`" % name
  139. def random_function_sql(self):
  140. return 'RAND()'
  141. def sql_flush(self, style, tables, sequences, allow_cascade=False):
  142. # NB: The generated SQL below is specific to MySQL
  143. # 'TRUNCATE x;', 'TRUNCATE y;', 'TRUNCATE z;'... style SQL statements
  144. # to clear all tables of all data
  145. if tables:
  146. sql = ['SET FOREIGN_KEY_CHECKS = 0;']
  147. for table in tables:
  148. sql.append('%s %s;' % (
  149. style.SQL_KEYWORD('TRUNCATE'),
  150. style.SQL_FIELD(self.quote_name(table)),
  151. ))
  152. sql.append('SET FOREIGN_KEY_CHECKS = 1;')
  153. sql.extend(self.sequence_reset_by_name_sql(style, sequences))
  154. return sql
  155. else:
  156. return []
  157. def validate_autopk_value(self, value):
  158. # MySQLism: zero in AUTO_INCREMENT field does not work. Refs #17653.
  159. if value == 0:
  160. raise ValueError('The database backend does not accept 0 as a '
  161. 'value for AutoField.')
  162. return value
  163. def adapt_datetimefield_value(self, value):
  164. if value is None:
  165. return None
  166. # Expression values are adapted by the database.
  167. if hasattr(value, 'resolve_expression'):
  168. return value
  169. # MySQL doesn't support tz-aware datetimes
  170. if timezone.is_aware(value):
  171. if settings.USE_TZ:
  172. value = timezone.make_naive(value, self.connection.timezone)
  173. else:
  174. raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.")
  175. return str(value)
  176. def adapt_timefield_value(self, value):
  177. if value is None:
  178. return None
  179. # Expression values are adapted by the database.
  180. if hasattr(value, 'resolve_expression'):
  181. return value
  182. # MySQL doesn't support tz-aware times
  183. if timezone.is_aware(value):
  184. raise ValueError("MySQL backend does not support timezone-aware times.")
  185. return str(value)
  186. def max_name_length(self):
  187. return 64
  188. def bulk_insert_sql(self, fields, placeholder_rows):
  189. placeholder_rows_sql = (", ".join(row) for row in placeholder_rows)
  190. values_sql = ", ".join("(%s)" % sql for sql in placeholder_rows_sql)
  191. return "VALUES " + values_sql
  192. def combine_expression(self, connector, sub_expressions):
  193. if connector == '^':
  194. return 'POW(%s)' % ','.join(sub_expressions)
  195. # Convert the result to a signed integer since MySQL's binary operators
  196. # return an unsigned integer.
  197. elif connector in ('&', '|', '<<'):
  198. return 'CONVERT(%s, SIGNED)' % connector.join(sub_expressions)
  199. elif connector == '>>':
  200. lhs, rhs = sub_expressions
  201. return 'FLOOR(%(lhs)s / POW(2, %(rhs)s))' % {'lhs': lhs, 'rhs': rhs}
  202. return super().combine_expression(connector, sub_expressions)
  203. def get_db_converters(self, expression):
  204. converters = super().get_db_converters(expression)
  205. internal_type = expression.output_field.get_internal_type()
  206. if internal_type in ['BooleanField', 'NullBooleanField']:
  207. converters.append(self.convert_booleanfield_value)
  208. elif internal_type == 'DateTimeField':
  209. if settings.USE_TZ:
  210. converters.append(self.convert_datetimefield_value)
  211. elif internal_type == 'UUIDField':
  212. converters.append(self.convert_uuidfield_value)
  213. return converters
  214. def convert_booleanfield_value(self, value, expression, connection):
  215. if value in (0, 1):
  216. value = bool(value)
  217. return value
  218. def convert_datetimefield_value(self, value, expression, connection):
  219. if value is not None:
  220. value = timezone.make_aware(value, self.connection.timezone)
  221. return value
  222. def convert_uuidfield_value(self, value, expression, connection):
  223. if value is not None:
  224. value = uuid.UUID(value)
  225. return value
  226. def binary_placeholder_sql(self, value):
  227. return '_binary %s' if value is not None and not hasattr(value, 'as_sql') else '%s'
  228. def subtract_temporals(self, internal_type, lhs, rhs):
  229. lhs_sql, lhs_params = lhs
  230. rhs_sql, rhs_params = rhs
  231. if internal_type == 'TimeField':
  232. if self.connection.mysql_is_mariadb:
  233. # MariaDB includes the microsecond component in TIME_TO_SEC as
  234. # a decimal. MySQL returns an integer without microseconds.
  235. return 'CAST((TIME_TO_SEC(%(lhs)s) - TIME_TO_SEC(%(rhs)s)) * 1000000 AS SIGNED)' % {
  236. 'lhs': lhs_sql, 'rhs': rhs_sql
  237. }, lhs_params + rhs_params
  238. return (
  239. "((TIME_TO_SEC(%(lhs)s) * 1000000 + MICROSECOND(%(lhs)s)) -"
  240. " (TIME_TO_SEC(%(rhs)s) * 1000000 + MICROSECOND(%(rhs)s)))"
  241. ) % {'lhs': lhs_sql, 'rhs': rhs_sql}, lhs_params * 2 + rhs_params * 2
  242. else:
  243. return "TIMESTAMPDIFF(MICROSECOND, %s, %s)" % (rhs_sql, lhs_sql), rhs_params + lhs_params
  244. def explain_query_prefix(self, format=None, **options):
  245. # Alias MySQL's TRADITIONAL to TEXT for consistency with other backends.
  246. if format and format.upper() == 'TEXT':
  247. format = 'TRADITIONAL'
  248. prefix = super().explain_query_prefix(format, **options)
  249. if format:
  250. prefix += ' FORMAT=%s' % format
  251. if self.connection.features.needs_explain_extended and format is None:
  252. # EXTENDED and FORMAT are mutually exclusive options.
  253. prefix += ' EXTENDED'
  254. return prefix
  255. def regex_lookup(self, lookup_type):
  256. # REGEXP BINARY doesn't work correctly in MySQL 8+ and REGEXP_LIKE
  257. # doesn't exist in MySQL 5.6 or in MariaDB.
  258. if self.connection.mysql_version < (8, 0, 0) or self.connection.mysql_is_mariadb:
  259. if lookup_type == 'regex':
  260. return '%s REGEXP BINARY %s'
  261. return '%s REGEXP %s'
  262. match_option = 'c' if lookup_type == 'regex' else 'i'
  263. return "REGEXP_LIKE(%%s, %%s, '%s')" % match_option
  264. def insert_statement(self, ignore_conflicts=False):
  265. return 'INSERT IGNORE INTO' if ignore_conflicts else super().insert_statement(ignore_conflicts)