features.py 15 KB

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