You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

features.py 11KB

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