features.py 13 KB

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