Development of an internal social media platform with personalised dashboards for students
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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import datetime
  2. import decimal
  3. import uuid
  4. from django.conf import settings
  5. from django.core.exceptions import FieldError
  6. from django.db import utils
  7. from django.db.backends.base.operations import BaseDatabaseOperations
  8. from django.db.models import aggregates, fields
  9. from django.db.models.expressions import Col
  10. from django.utils import timezone
  11. from django.utils.dateparse import parse_date, parse_datetime, parse_time
  12. from django.utils.duration import duration_microseconds
  13. class DatabaseOperations(BaseDatabaseOperations):
  14. cast_char_field_without_max_length = 'text'
  15. cast_data_types = {
  16. 'DateField': 'TEXT',
  17. 'DateTimeField': 'TEXT',
  18. }
  19. explain_prefix = 'EXPLAIN QUERY PLAN'
  20. def bulk_batch_size(self, fields, objs):
  21. """
  22. SQLite has a compile-time default (SQLITE_LIMIT_VARIABLE_NUMBER) of
  23. 999 variables per query.
  24. If there's only a single field to insert, the limit is 500
  25. (SQLITE_MAX_COMPOUND_SELECT).
  26. """
  27. if len(fields) == 1:
  28. return 500
  29. elif len(fields) > 1:
  30. return self.connection.features.max_query_params // len(fields)
  31. else:
  32. return len(objs)
  33. def check_expression_support(self, expression):
  34. bad_fields = (fields.DateField, fields.DateTimeField, fields.TimeField)
  35. bad_aggregates = (aggregates.Sum, aggregates.Avg, aggregates.Variance, aggregates.StdDev)
  36. if isinstance(expression, bad_aggregates):
  37. for expr in expression.get_source_expressions():
  38. try:
  39. output_field = expr.output_field
  40. except FieldError:
  41. # Not every subexpression has an output_field which is fine
  42. # to ignore.
  43. pass
  44. else:
  45. if isinstance(output_field, bad_fields):
  46. raise utils.NotSupportedError(
  47. 'You cannot use Sum, Avg, StdDev, and Variance '
  48. 'aggregations on date/time fields in sqlite3 '
  49. 'since date/time is saved as text.'
  50. )
  51. def date_extract_sql(self, lookup_type, field_name):
  52. """
  53. Support EXTRACT with a user-defined function django_date_extract()
  54. that's registered in connect(). Use single quotes because this is a
  55. string and could otherwise cause a collision with a field name.
  56. """
  57. return "django_date_extract('%s', %s)" % (lookup_type.lower(), field_name)
  58. def date_interval_sql(self, timedelta):
  59. return str(duration_microseconds(timedelta))
  60. def format_for_duration_arithmetic(self, sql):
  61. """Do nothing since formatting is handled in the custom function."""
  62. return sql
  63. def date_trunc_sql(self, lookup_type, field_name):
  64. return "django_date_trunc('%s', %s)" % (lookup_type.lower(), field_name)
  65. def time_trunc_sql(self, lookup_type, field_name):
  66. return "django_time_trunc('%s', %s)" % (lookup_type.lower(), field_name)
  67. def _convert_tzname_to_sql(self, tzname):
  68. return "'%s'" % tzname if settings.USE_TZ else 'NULL'
  69. def datetime_cast_date_sql(self, field_name, tzname):
  70. return "django_datetime_cast_date(%s, %s)" % (
  71. field_name, self._convert_tzname_to_sql(tzname),
  72. )
  73. def datetime_cast_time_sql(self, field_name, tzname):
  74. return "django_datetime_cast_time(%s, %s)" % (
  75. field_name, self._convert_tzname_to_sql(tzname),
  76. )
  77. def datetime_extract_sql(self, lookup_type, field_name, tzname):
  78. return "django_datetime_extract('%s', %s, %s)" % (
  79. lookup_type.lower(), field_name, self._convert_tzname_to_sql(tzname),
  80. )
  81. def datetime_trunc_sql(self, lookup_type, field_name, tzname):
  82. return "django_datetime_trunc('%s', %s, %s)" % (
  83. lookup_type.lower(), field_name, self._convert_tzname_to_sql(tzname),
  84. )
  85. def time_extract_sql(self, lookup_type, field_name):
  86. return "django_time_extract('%s', %s)" % (lookup_type.lower(), field_name)
  87. def pk_default_value(self):
  88. return "NULL"
  89. def _quote_params_for_last_executed_query(self, params):
  90. """
  91. Only for last_executed_query! Don't use this to execute SQL queries!
  92. """
  93. # This function is limited both by SQLITE_LIMIT_VARIABLE_NUMBER (the
  94. # number of parameters, default = 999) and SQLITE_MAX_COLUMN (the
  95. # number of return values, default = 2000). Since Python's sqlite3
  96. # module doesn't expose the get_limit() C API, assume the default
  97. # limits are in effect and split the work in batches if needed.
  98. BATCH_SIZE = 999
  99. if len(params) > BATCH_SIZE:
  100. results = ()
  101. for index in range(0, len(params), BATCH_SIZE):
  102. chunk = params[index:index + BATCH_SIZE]
  103. results += self._quote_params_for_last_executed_query(chunk)
  104. return results
  105. sql = 'SELECT ' + ', '.join(['QUOTE(?)'] * len(params))
  106. # Bypass Django's wrappers and use the underlying sqlite3 connection
  107. # to avoid logging this query - it would trigger infinite recursion.
  108. cursor = self.connection.connection.cursor()
  109. # Native sqlite3 cursors cannot be used as context managers.
  110. try:
  111. return cursor.execute(sql, params).fetchone()
  112. finally:
  113. cursor.close()
  114. def last_executed_query(self, cursor, sql, params):
  115. # Python substitutes parameters in Modules/_sqlite/cursor.c with:
  116. # pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
  117. # Unfortunately there is no way to reach self->statement from Python,
  118. # so we quote and substitute parameters manually.
  119. if params:
  120. if isinstance(params, (list, tuple)):
  121. params = self._quote_params_for_last_executed_query(params)
  122. else:
  123. values = tuple(params.values())
  124. values = self._quote_params_for_last_executed_query(values)
  125. params = dict(zip(params, values))
  126. return sql % params
  127. # For consistency with SQLiteCursorWrapper.execute(), just return sql
  128. # when there are no parameters. See #13648 and #17158.
  129. else:
  130. return sql
  131. def quote_name(self, name):
  132. if name.startswith('"') and name.endswith('"'):
  133. return name # Quoting once is enough.
  134. return '"%s"' % name
  135. def no_limit_value(self):
  136. return -1
  137. def sql_flush(self, style, tables, sequences, allow_cascade=False):
  138. sql = ['%s %s %s;' % (
  139. style.SQL_KEYWORD('DELETE'),
  140. style.SQL_KEYWORD('FROM'),
  141. style.SQL_FIELD(self.quote_name(table))
  142. ) for table in tables]
  143. # Note: No requirement for reset of auto-incremented indices (cf. other
  144. # sql_flush() implementations). Just return SQL at this point
  145. return sql
  146. def execute_sql_flush(self, using, sql_list):
  147. # To prevent possible violation of foreign key constraints, deactivate
  148. # constraints outside of the transaction created in super().
  149. with self.connection.constraint_checks_disabled():
  150. super().execute_sql_flush(using, sql_list)
  151. def adapt_datetimefield_value(self, value):
  152. if value is None:
  153. return None
  154. # Expression values are adapted by the database.
  155. if hasattr(value, 'resolve_expression'):
  156. return value
  157. # SQLite doesn't support tz-aware datetimes
  158. if timezone.is_aware(value):
  159. if settings.USE_TZ:
  160. value = timezone.make_naive(value, self.connection.timezone)
  161. else:
  162. raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.")
  163. return str(value)
  164. def adapt_timefield_value(self, value):
  165. if value is None:
  166. return None
  167. # Expression values are adapted by the database.
  168. if hasattr(value, 'resolve_expression'):
  169. return value
  170. # SQLite doesn't support tz-aware datetimes
  171. if timezone.is_aware(value):
  172. raise ValueError("SQLite backend does not support timezone-aware times.")
  173. return str(value)
  174. def get_db_converters(self, expression):
  175. converters = super().get_db_converters(expression)
  176. internal_type = expression.output_field.get_internal_type()
  177. if internal_type == 'DateTimeField':
  178. converters.append(self.convert_datetimefield_value)
  179. elif internal_type == 'DateField':
  180. converters.append(self.convert_datefield_value)
  181. elif internal_type == 'TimeField':
  182. converters.append(self.convert_timefield_value)
  183. elif internal_type == 'DecimalField':
  184. converters.append(self.get_decimalfield_converter(expression))
  185. elif internal_type == 'UUIDField':
  186. converters.append(self.convert_uuidfield_value)
  187. elif internal_type in ('NullBooleanField', 'BooleanField'):
  188. converters.append(self.convert_booleanfield_value)
  189. return converters
  190. def convert_datetimefield_value(self, value, expression, connection):
  191. if value is not None:
  192. if not isinstance(value, datetime.datetime):
  193. value = parse_datetime(value)
  194. if settings.USE_TZ and not timezone.is_aware(value):
  195. value = timezone.make_aware(value, self.connection.timezone)
  196. return value
  197. def convert_datefield_value(self, value, expression, connection):
  198. if value is not None:
  199. if not isinstance(value, datetime.date):
  200. value = parse_date(value)
  201. return value
  202. def convert_timefield_value(self, value, expression, connection):
  203. if value is not None:
  204. if not isinstance(value, datetime.time):
  205. value = parse_time(value)
  206. return value
  207. def get_decimalfield_converter(self, expression):
  208. # SQLite stores only 15 significant digits. Digits coming from
  209. # float inaccuracy must be removed.
  210. create_decimal = decimal.Context(prec=15).create_decimal_from_float
  211. if isinstance(expression, Col):
  212. quantize_value = decimal.Decimal(1).scaleb(-expression.output_field.decimal_places)
  213. def converter(value, expression, connection):
  214. if value is not None:
  215. return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
  216. else:
  217. def converter(value, expression, connection):
  218. if value is not None:
  219. return create_decimal(value)
  220. return converter
  221. def convert_uuidfield_value(self, value, expression, connection):
  222. if value is not None:
  223. value = uuid.UUID(value)
  224. return value
  225. def convert_booleanfield_value(self, value, expression, connection):
  226. return bool(value) if value in (1, 0) else value
  227. def bulk_insert_sql(self, fields, placeholder_rows):
  228. return " UNION ALL ".join(
  229. "SELECT %s" % ", ".join(row)
  230. for row in placeholder_rows
  231. )
  232. def combine_expression(self, connector, sub_expressions):
  233. # SQLite doesn't have a power function, so we fake it with a
  234. # user-defined function django_power that's registered in connect().
  235. if connector == '^':
  236. return 'django_power(%s)' % ','.join(sub_expressions)
  237. return super().combine_expression(connector, sub_expressions)
  238. def combine_duration_expression(self, connector, sub_expressions):
  239. if connector not in ['+', '-']:
  240. raise utils.DatabaseError('Invalid connector for timedelta: %s.' % connector)
  241. fn_params = ["'%s'" % connector] + sub_expressions
  242. if len(fn_params) > 3:
  243. raise ValueError('Too many params for timedelta operations.')
  244. return "django_format_dtdelta(%s)" % ', '.join(fn_params)
  245. def integer_field_range(self, internal_type):
  246. # SQLite doesn't enforce any integer constraints
  247. return (None, None)
  248. def subtract_temporals(self, internal_type, lhs, rhs):
  249. lhs_sql, lhs_params = lhs
  250. rhs_sql, rhs_params = rhs
  251. if internal_type == 'TimeField':
  252. return "django_time_diff(%s, %s)" % (lhs_sql, rhs_sql), lhs_params + rhs_params
  253. return "django_timestamp_diff(%s, %s)" % (lhs_sql, rhs_sql), lhs_params + rhs_params