features.py 12 KB

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