features.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. from django.db import ProgrammingError
  2. from django.utils.functional import cached_property
  3. class BaseDatabaseFeatures:
  4. # An optional tuple indicating the minimum supported database version.
  5. minimum_database_version = None
  6. gis_enabled = False
  7. # Oracle can't group by LOB (large object) data types.
  8. allows_group_by_lob = True
  9. allows_group_by_selected_pks = False
  10. allows_group_by_select_index = True
  11. empty_fetchmany_value = []
  12. update_can_self_select = True
  13. # Does the backend distinguish between '' and None?
  14. interprets_empty_strings_as_nulls = False
  15. # Does the backend allow inserting duplicate NULL rows in a nullable
  16. # unique field? All core backends implement this correctly, but other
  17. # databases such as SQL Server do not.
  18. supports_nullable_unique_constraints = True
  19. # Does the backend allow inserting duplicate rows when a unique_together
  20. # constraint exists and some fields are nullable but not all of them?
  21. supports_partially_nullable_unique_constraints = True
  22. # Does the backend support initially deferrable unique constraints?
  23. supports_deferrable_unique_constraints = False
  24. can_use_chunked_reads = True
  25. can_return_columns_from_insert = False
  26. can_return_rows_from_bulk_insert = False
  27. has_bulk_insert = True
  28. uses_savepoints = True
  29. can_release_savepoints = False
  30. # If True, don't use integer foreign keys referring to, e.g., positive
  31. # integer primary keys.
  32. related_fields_match_type = False
  33. allow_sliced_subqueries_with_in = True
  34. has_select_for_update = False
  35. has_select_for_update_nowait = False
  36. has_select_for_update_skip_locked = False
  37. has_select_for_update_of = False
  38. has_select_for_no_key_update = False
  39. # Does the database's SELECT FOR UPDATE OF syntax require a column rather
  40. # than a table?
  41. select_for_update_of_column = False
  42. # Does the default test database allow multiple connections?
  43. # Usually an indication that the test database is in-memory
  44. test_db_allows_multiple_connections = True
  45. # Can an object be saved without an explicit primary key?
  46. supports_unspecified_pk = False
  47. # Can a fixture contain forward references? i.e., are
  48. # FK constraints checked at the end of transaction, or
  49. # at the end of each save operation?
  50. supports_forward_references = True
  51. # Does the backend truncate names properly when they are too long?
  52. truncates_names = False
  53. # Is there a REAL datatype in addition to floats/doubles?
  54. has_real_datatype = False
  55. supports_subqueries_in_group_by = True
  56. # Does the backend ignore unnecessary ORDER BY clauses in subqueries?
  57. ignores_unnecessary_order_by_in_subqueries = True
  58. # Is there a true datatype for uuid?
  59. has_native_uuid_field = False
  60. # Is there a true datatype for timedeltas?
  61. has_native_duration_field = False
  62. # Does the database driver supports same type temporal data subtraction
  63. # by returning the type used to store duration field?
  64. supports_temporal_subtraction = False
  65. # Does the __regex lookup support backreferencing and grouping?
  66. supports_regex_backreferencing = True
  67. # Can date/datetime lookups be performed using a string?
  68. supports_date_lookup_using_string = True
  69. # Can datetimes with timezones be used?
  70. supports_timezones = True
  71. # Does the database have a copy of the zoneinfo database?
  72. has_zoneinfo_database = True
  73. # When performing a GROUP BY, is an ORDER BY NULL required
  74. # to remove any ordering?
  75. requires_explicit_null_ordering_when_grouping = False
  76. # Does the backend order NULL values as largest or smallest?
  77. nulls_order_largest = False
  78. # Does the backend support NULLS FIRST and NULLS LAST in ORDER BY?
  79. supports_order_by_nulls_modifier = True
  80. # Does the backend orders NULLS FIRST by default?
  81. order_by_nulls_first = False
  82. # The database's limit on the number of query parameters.
  83. max_query_params = None
  84. # Can an object have an autoincrement primary key of 0?
  85. allows_auto_pk_0 = True
  86. # Do we need to NULL a ForeignKey out, or can the constraint check be
  87. # deferred
  88. can_defer_constraint_checks = False
  89. # Does the backend support tablespaces? Default to False because it isn't
  90. # in the SQL standard.
  91. supports_tablespaces = False
  92. # Does the backend reset sequences between tests?
  93. supports_sequence_reset = True
  94. # Can the backend introspect the default value of a column?
  95. can_introspect_default = True
  96. # Confirm support for introspected foreign keys
  97. # Every database can do this reliably, except MySQL,
  98. # which can't do it for MyISAM tables
  99. can_introspect_foreign_keys = True
  100. # Map fields which some backends may not be able to differentiate to the
  101. # field it's introspected as.
  102. introspected_field_types = {
  103. "AutoField": "AutoField",
  104. "BigAutoField": "BigAutoField",
  105. "BigIntegerField": "BigIntegerField",
  106. "BinaryField": "BinaryField",
  107. "BooleanField": "BooleanField",
  108. "CharField": "CharField",
  109. "DurationField": "DurationField",
  110. "GenericIPAddressField": "GenericIPAddressField",
  111. "IntegerField": "IntegerField",
  112. "PositiveBigIntegerField": "PositiveBigIntegerField",
  113. "PositiveIntegerField": "PositiveIntegerField",
  114. "PositiveSmallIntegerField": "PositiveSmallIntegerField",
  115. "SmallAutoField": "SmallAutoField",
  116. "SmallIntegerField": "SmallIntegerField",
  117. "TimeField": "TimeField",
  118. }
  119. # Can the backend introspect the column order (ASC/DESC) for indexes?
  120. supports_index_column_ordering = True
  121. # Does the backend support introspection of materialized views?
  122. can_introspect_materialized_views = False
  123. # Support for the DISTINCT ON clause
  124. can_distinct_on_fields = False
  125. # Does the backend prevent running SQL queries in broken transactions?
  126. atomic_transactions = True
  127. # Can we roll back DDL in a transaction?
  128. can_rollback_ddl = False
  129. schema_editor_uses_clientside_param_binding = False
  130. # Does it support operations requiring references rename in a transaction?
  131. supports_atomic_references_rename = True
  132. # Can we issue more than one ALTER COLUMN clause in an ALTER TABLE?
  133. supports_combined_alters = False
  134. # Does it support foreign keys?
  135. supports_foreign_keys = True
  136. # Can it create foreign key constraints inline when adding columns?
  137. can_create_inline_fk = True
  138. # Can an index be renamed?
  139. can_rename_index = False
  140. # Does it automatically index foreign keys?
  141. indexes_foreign_keys = True
  142. # Does it support CHECK constraints?
  143. supports_column_check_constraints = True
  144. supports_table_check_constraints = True
  145. # Does the backend support introspection of CHECK constraints?
  146. can_introspect_check_constraints = True
  147. # Does the backend support 'pyformat' style ("... %(name)s ...", {'name': value})
  148. # parameter passing? Note this can be provided by the backend even if not
  149. # supported by the Python driver
  150. supports_paramstyle_pyformat = True
  151. # Does the backend require literal defaults, rather than parameterized ones?
  152. requires_literal_defaults = False
  153. # Does the backend require a connection reset after each material schema change?
  154. connection_persists_old_columns = False
  155. # What kind of error does the backend throw when accessing closed cursor?
  156. closed_cursor_error_class = ProgrammingError
  157. # Does 'a' LIKE 'A' match?
  158. has_case_insensitive_like = False
  159. # Suffix for backends that don't support "SELECT xxx;" queries.
  160. bare_select_suffix = ""
  161. # If NULL is implied on columns without needing to be explicitly specified
  162. implied_column_null = False
  163. # Does the backend support "select for update" queries with limit (and offset)?
  164. supports_select_for_update_with_limit = True
  165. # Does the backend ignore null expressions in GREATEST and LEAST queries unless
  166. # every expression is null?
  167. greatest_least_ignores_nulls = False
  168. # Can the backend clone databases for parallel test execution?
  169. # Defaults to False to allow third-party backends to opt-in.
  170. can_clone_databases = False
  171. # Does the backend consider table names with different casing to
  172. # be equal?
  173. ignores_table_name_case = False
  174. # Place FOR UPDATE right after FROM clause. Used on MSSQL.
  175. for_update_after_from = False
  176. # Combinatorial flags
  177. supports_select_union = True
  178. supports_select_intersection = True
  179. supports_select_difference = True
  180. supports_slicing_ordering_in_compound = False
  181. supports_parentheses_in_compound = True
  182. requires_compound_order_by_subquery = False
  183. # Does the database support SQL 2003 FILTER (WHERE ...) in aggregate
  184. # expressions?
  185. supports_aggregate_filter_clause = False
  186. # Does the backend support indexing a TextField?
  187. supports_index_on_text_field = True
  188. # Does the backend support window expressions (expression OVER (...))?
  189. supports_over_clause = False
  190. supports_frame_range_fixed_distance = False
  191. only_supports_unbounded_with_preceding_and_following = False
  192. # Does the backend support CAST with precision?
  193. supports_cast_with_precision = True
  194. # How many second decimals does the database return when casting a value to
  195. # a type with time?
  196. time_cast_precision = 6
  197. # SQL to create a procedure for use by the Django test suite. The
  198. # functionality of the procedure isn't important.
  199. create_test_procedure_without_params_sql = None
  200. create_test_procedure_with_int_param_sql = None
  201. # SQL to create a table with a composite primary key for use by the Django
  202. # test suite.
  203. create_test_table_with_composite_primary_key = None
  204. # Does the backend support keyword parameters for cursor.callproc()?
  205. supports_callproc_kwargs = False
  206. # What formats does the backend EXPLAIN syntax support?
  207. supported_explain_formats = set()
  208. # Does the backend support the default parameter in lead() and lag()?
  209. supports_default_in_lead_lag = True
  210. # Does the backend support ignoring constraint or uniqueness errors during
  211. # INSERT?
  212. supports_ignore_conflicts = True
  213. # Does the backend support updating rows on constraint or uniqueness errors
  214. # during INSERT?
  215. supports_update_conflicts = False
  216. supports_update_conflicts_with_target = False
  217. # Does this backend require casting the results of CASE expressions used
  218. # in UPDATE statements to ensure the expression has the correct type?
  219. requires_casted_case_in_updates = False
  220. # Does the backend support partial indexes (CREATE INDEX ... WHERE ...)?
  221. supports_partial_indexes = True
  222. supports_functions_in_partial_indexes = True
  223. # Does the backend support covering indexes (CREATE INDEX ... INCLUDE ...)?
  224. supports_covering_indexes = False
  225. # Does the backend support indexes on expressions?
  226. supports_expression_indexes = True
  227. # Does the backend treat COLLATE as an indexed expression?
  228. collate_as_index_expression = False
  229. # Does the database allow more than one constraint or index on the same
  230. # field(s)?
  231. allows_multiple_constraints_on_same_fields = True
  232. # Does the backend support boolean expressions in SELECT and GROUP BY
  233. # clauses?
  234. supports_boolean_expr_in_select_clause = True
  235. # Does the backend support comparing boolean expressions in WHERE clauses?
  236. # Eg: WHERE (price > 0) IS NOT NULL
  237. supports_comparing_boolean_expr = True
  238. # Does the backend support JSONField?
  239. supports_json_field = True
  240. # Can the backend introspect a JSONField?
  241. can_introspect_json_field = True
  242. # Does the backend support primitives in JSONField?
  243. supports_primitives_in_json_field = True
  244. # Is there a true datatype for JSON?
  245. has_native_json_field = False
  246. # Does the backend use PostgreSQL-style JSON operators like '->'?
  247. has_json_operators = False
  248. # Does the backend support __contains and __contained_by lookups for
  249. # a JSONField?
  250. supports_json_field_contains = True
  251. # Does value__d__contains={'f': 'g'} (without a list around the dict) match
  252. # {'d': [{'f': 'g'}]}?
  253. json_key_contains_list_matching_requires_list = False
  254. # Does the backend support JSONObject() database function?
  255. has_json_object_function = True
  256. # Does the backend support column collations?
  257. supports_collation_on_charfield = True
  258. supports_collation_on_textfield = True
  259. # Does the backend support non-deterministic collations?
  260. supports_non_deterministic_collations = True
  261. # Does the backend support column and table comments?
  262. supports_comments = False
  263. # Does the backend support column comments in ADD COLUMN statements?
  264. supports_comments_inline = False
  265. # Does the backend support the logical XOR operator?
  266. supports_logical_xor = False
  267. # Set to (exception, message) if null characters in text are disallowed.
  268. prohibits_null_characters_in_text_exception = None
  269. # Does the backend support unlimited character columns?
  270. supports_unlimited_charfield = False
  271. # Collation names for use by the Django test suite.
  272. test_collations = {
  273. "ci": None, # Case-insensitive.
  274. "cs": None, # Case-sensitive.
  275. "non_default": None, # Non-default.
  276. "swedish_ci": None, # Swedish case-insensitive.
  277. }
  278. # SQL template override for tests.aggregation.tests.NowUTC
  279. test_now_utc_template = None
  280. # A set of dotted paths to tests in Django's test suite that are expected
  281. # to fail on this database.
  282. django_test_expected_failures = set()
  283. # A map of reasons to sets of dotted paths to tests in Django's test suite
  284. # that should be skipped for this database.
  285. django_test_skips = {}
  286. def __init__(self, connection):
  287. self.connection = connection
  288. @cached_property
  289. def supports_explaining_query_execution(self):
  290. """Does this backend support explaining query execution?"""
  291. return self.connection.ops.explain_prefix is not None
  292. @cached_property
  293. def supports_transactions(self):
  294. """Confirm support for transactions."""
  295. with self.connection.cursor() as cursor:
  296. cursor.execute("CREATE TABLE ROLLBACK_TEST (X INT)")
  297. self.connection.set_autocommit(False)
  298. cursor.execute("INSERT INTO ROLLBACK_TEST (X) VALUES (8)")
  299. self.connection.rollback()
  300. self.connection.set_autocommit(True)
  301. cursor.execute("SELECT COUNT(X) FROM ROLLBACK_TEST")
  302. (count,) = cursor.fetchone()
  303. cursor.execute("DROP TABLE ROLLBACK_TEST")
  304. return count == 0
  305. def allows_group_by_selected_pks_on_model(self, model):
  306. if not self.allows_group_by_selected_pks:
  307. return False
  308. return model._meta.managed