features.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import operator
  2. import sqlite3
  3. from django.db import transaction
  4. from django.db.backends.base.features import BaseDatabaseFeatures
  5. from django.db.utils import OperationalError
  6. from django.utils.functional import cached_property
  7. from django.utils.version import PY314
  8. from .base import Database
  9. class DatabaseFeatures(BaseDatabaseFeatures):
  10. minimum_database_version = (3, 37)
  11. test_db_allows_multiple_connections = False
  12. supports_unspecified_pk = True
  13. supports_timezones = False
  14. supports_transactions = True
  15. atomic_transactions = False
  16. can_rollback_ddl = True
  17. can_create_inline_fk = False
  18. requires_literal_defaults = True
  19. can_clone_databases = True
  20. supports_temporal_subtraction = True
  21. ignores_table_name_case = True
  22. supports_cast_with_precision = False
  23. time_cast_precision = 3
  24. can_release_savepoints = True
  25. has_case_insensitive_like = True
  26. supports_parentheses_in_compound = False
  27. can_defer_constraint_checks = True
  28. supports_over_clause = True
  29. supports_frame_range_fixed_distance = True
  30. supports_frame_exclusion = True
  31. supports_aggregate_filter_clause = True
  32. supports_aggregate_order_by_clause = Database.sqlite_version_info >= (3, 44, 0)
  33. supports_aggregate_distinct_multiple_argument = False
  34. supports_any_value = True
  35. order_by_nulls_first = True
  36. supports_json_field_contains = False
  37. supports_update_conflicts = True
  38. supports_update_conflicts_with_target = True
  39. supports_stored_generated_columns = True
  40. supports_virtual_generated_columns = True
  41. test_collations = {
  42. "ci": "nocase",
  43. "cs": "binary",
  44. "non_default": "nocase",
  45. "virtual": "nocase",
  46. }
  47. django_test_expected_failures = {
  48. # The django_format_dtdelta() function doesn't properly handle mixed
  49. # Date/DateTime fields and timedeltas.
  50. "expressions.tests.FTimeDeltaTests.test_mixed_comparisons1",
  51. }
  52. insert_test_table_with_defaults = 'INSERT INTO {} ("null") VALUES (1)'
  53. supports_default_keyword_in_insert = False
  54. supports_unlimited_charfield = True
  55. supports_no_precision_decimalfield = True
  56. can_return_columns_from_insert = True
  57. can_return_rows_from_bulk_insert = True
  58. can_return_rows_from_update = True
  59. supports_uuid4_function = True
  60. @cached_property
  61. def supports_uuid7_function(self):
  62. return PY314
  63. @cached_property
  64. def django_test_skips(self):
  65. skips = {
  66. "SQLite stores values rounded to 15 significant digits.": {
  67. "model_fields.test_decimalfield.DecimalFieldTests."
  68. "test_fetch_from_db_without_float_rounding",
  69. },
  70. "SQLite naively remakes the table on field alteration.": {
  71. "schema.tests.SchemaTests.test_unique_no_unnecessary_fk_drops",
  72. "schema.tests.SchemaTests.test_unique_and_reverse_m2m",
  73. "schema.tests.SchemaTests."
  74. "test_alter_field_default_doesnt_perform_queries",
  75. "schema.tests.SchemaTests."
  76. "test_rename_column_renames_deferred_sql_references",
  77. },
  78. "SQLite doesn't support negative precision for ROUND().": {
  79. "db_functions.math.test_round.RoundTests."
  80. "test_null_with_negative_precision",
  81. "db_functions.math.test_round.RoundTests."
  82. "test_decimal_with_negative_precision",
  83. "db_functions.math.test_round.RoundTests."
  84. "test_float_with_negative_precision",
  85. "db_functions.math.test_round.RoundTests."
  86. "test_integer_with_negative_precision",
  87. },
  88. "The actual query cannot be determined on SQLite": {
  89. "backends.base.test_base.ExecuteWrapperTests.test_wrapper_debug",
  90. },
  91. }
  92. if self.connection.is_in_memory_db():
  93. skips.update(
  94. {
  95. "the sqlite backend's close() method is a no-op when using an "
  96. "in-memory database": {
  97. "servers.test_liveserverthread.LiveServerThreadTest."
  98. "test_closes_connections",
  99. "servers.tests.LiveServerTestCloseConnectionTest."
  100. "test_closes_connections",
  101. },
  102. "For SQLite in-memory tests, closing the connection destroys "
  103. "the database.": {
  104. "test_utils.tests.AssertNumQueriesUponConnectionTests."
  105. "test_ignores_connection_configuration_queries",
  106. },
  107. }
  108. )
  109. else:
  110. skips.update(
  111. {
  112. "Only connections to in-memory SQLite databases are passed to the "
  113. "server thread.": {
  114. "servers.tests.LiveServerInMemoryDatabaseLockTest."
  115. "test_in_memory_database_lock",
  116. },
  117. "multiprocessing's start method is checked only for in-memory "
  118. "SQLite databases": {
  119. "backends.sqlite.test_creation.TestDbSignatureTests."
  120. "test_get_test_db_clone_settings_not_supported",
  121. },
  122. }
  123. )
  124. if Database.sqlite_version_info < (3, 47):
  125. skips.update(
  126. {
  127. "SQLite does not parse escaped double quotes in the JSON path "
  128. "notation": {
  129. "model_fields.test_jsonfield.TestQuerying."
  130. "test_lookups_special_chars_double_quotes",
  131. },
  132. }
  133. )
  134. return skips
  135. @cached_property
  136. def introspected_field_types(self):
  137. return {
  138. **super().introspected_field_types,
  139. "BigAutoField": "AutoField",
  140. "DurationField": "BigIntegerField",
  141. "GenericIPAddressField": "CharField",
  142. "SmallAutoField": "AutoField",
  143. }
  144. @property
  145. def max_query_params(self):
  146. """
  147. SQLite has a variable limit per query. The limit can be changed using
  148. the SQLITE_MAX_VARIABLE_NUMBER compile-time option (which defaults to
  149. 32766) or lowered per connection at run-time with
  150. setlimit(SQLITE_LIMIT_VARIABLE_NUMBER, N).
  151. """
  152. self.connection.ensure_connection()
  153. return self.connection.connection.getlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
  154. @cached_property
  155. def supports_json_field(self):
  156. with self.connection.cursor() as cursor:
  157. try:
  158. with transaction.atomic(self.connection.alias):
  159. cursor.execute('SELECT JSON(\'{"a": "b"}\')')
  160. except OperationalError:
  161. return False
  162. return True
  163. can_introspect_json_field = property(operator.attrgetter("supports_json_field"))
  164. has_json_object_function = property(operator.attrgetter("supports_json_field"))
  165. supports_uuid4_function_in_default = property(
  166. operator.attrgetter("supports_uuid4_function")
  167. )