Development of an internal social media platform with personalised dashboards for students
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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. from django.db.models.aggregates import StdDev
  2. from django.db.utils import NotSupportedError, ProgrammingError
  3. from django.utils.functional import cached_property
  4. class BaseDatabaseFeatures:
  5. gis_enabled = False
  6. allows_group_by_pk = False
  7. allows_group_by_selected_pks = False
  8. empty_fetchmany_value = []
  9. update_can_self_select = True
  10. # Does the backend distinguish between '' and None?
  11. interprets_empty_strings_as_nulls = False
  12. # Does the backend allow inserting duplicate NULL rows in a nullable
  13. # unique field? All core backends implement this correctly, but other
  14. # databases such as SQL Server do not.
  15. supports_nullable_unique_constraints = True
  16. # Does the backend allow inserting duplicate rows when a unique_together
  17. # constraint exists and some fields are nullable but not all of them?
  18. supports_partially_nullable_unique_constraints = True
  19. can_use_chunked_reads = True
  20. can_return_id_from_insert = False
  21. can_return_ids_from_bulk_insert = False
  22. has_bulk_insert = True
  23. uses_savepoints = False
  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 an IPAddressField, instead of an CharField?
  99. can_introspect_ip_address_field = False
  100. # Can the backend introspect a PositiveIntegerField, instead of an IntegerField?
  101. can_introspect_positive_integer_field = False
  102. # Can the backend introspect a SmallIntegerField, instead of an IntegerField?
  103. can_introspect_small_integer_field = False
  104. # Can the backend introspect a TimeField, instead of a DateTimeField?
  105. can_introspect_time_field = True
  106. # Some backends may not be able to differentiate BooleanField from other
  107. # fields such as IntegerField.
  108. introspected_boolean_field_type = 'BooleanField'
  109. # Can the backend introspect the column order (ASC/DESC) for indexes?
  110. supports_index_column_ordering = True
  111. # Support for the DISTINCT ON clause
  112. can_distinct_on_fields = False
  113. # Does the backend decide to commit before SAVEPOINT statements
  114. # when autocommit is disabled? http://bugs.python.org/issue8145#msg109965
  115. autocommits_when_autocommit_is_off = False
  116. # Does the backend prevent running SQL queries in broken transactions?
  117. atomic_transactions = True
  118. # Can we roll back DDL in a transaction?
  119. can_rollback_ddl = False
  120. # Does it support operations requiring references rename in a transaction?
  121. supports_atomic_references_rename = True
  122. # Can we issue more than one ALTER COLUMN clause in an ALTER TABLE?
  123. supports_combined_alters = False
  124. # Does it support foreign keys?
  125. supports_foreign_keys = True
  126. # Does it support CHECK constraints?
  127. supports_column_check_constraints = True
  128. # Does the backend support 'pyformat' style ("... %(name)s ...", {'name': value})
  129. # parameter passing? Note this can be provided by the backend even if not
  130. # supported by the Python driver
  131. supports_paramstyle_pyformat = True
  132. # Does the backend require literal defaults, rather than parameterized ones?
  133. requires_literal_defaults = False
  134. # Does the backend require a connection reset after each material schema change?
  135. connection_persists_old_columns = False
  136. # What kind of error does the backend throw when accessing closed cursor?
  137. closed_cursor_error_class = ProgrammingError
  138. # Does 'a' LIKE 'A' match?
  139. has_case_insensitive_like = True
  140. # Does the backend require the sqlparse library for splitting multi-line
  141. # statements before executing them?
  142. requires_sqlparse_for_splitting = True
  143. # Suffix for backends that don't support "SELECT xxx;" queries.
  144. bare_select_suffix = ''
  145. # If NULL is implied on columns without needing to be explicitly specified
  146. implied_column_null = False
  147. uppercases_column_names = False
  148. # Does the backend support "select for update" queries with limit (and offset)?
  149. supports_select_for_update_with_limit = True
  150. # Does the backend ignore null expressions in GREATEST and LEAST queries unless
  151. # every expression is null?
  152. greatest_least_ignores_nulls = False
  153. # Can the backend clone databases for parallel test execution?
  154. # Defaults to False to allow third-party backends to opt-in.
  155. can_clone_databases = False
  156. # Does the backend consider table names with different casing to
  157. # be equal?
  158. ignores_table_name_case = False
  159. # Place FOR UPDATE right after FROM clause. Used on MSSQL.
  160. for_update_after_from = False
  161. # Combinatorial flags
  162. supports_select_union = True
  163. supports_select_intersection = True
  164. supports_select_difference = True
  165. supports_slicing_ordering_in_compound = False
  166. # Does the database support SQL 2003 FILTER (WHERE ...) in aggregate
  167. # expressions?
  168. supports_aggregate_filter_clause = False
  169. # Does the backend support indexing a TextField?
  170. supports_index_on_text_field = True
  171. # Does the backed support window expressions (expression OVER (...))?
  172. supports_over_clause = False
  173. # Does the backend support CAST with precision?
  174. supports_cast_with_precision = True
  175. # SQL to create a procedure for use by the Django test suite. The
  176. # functionality of the procedure isn't important.
  177. create_test_procedure_without_params_sql = None
  178. create_test_procedure_with_int_param_sql = None
  179. # Does the backend support keyword parameters for cursor.callproc()?
  180. supports_callproc_kwargs = False
  181. # Convert CharField results from bytes to str in database functions.
  182. db_functions_convert_bytes_to_str = False
  183. # What formats does the backend EXPLAIN syntax support?
  184. supported_explain_formats = set()
  185. # Does DatabaseOperations.explain_query_prefix() raise ValueError if
  186. # unknown kwargs are passed to QuerySet.explain()?
  187. validates_explain_options = True
  188. def __init__(self, connection):
  189. self.connection = connection
  190. @cached_property
  191. def supports_explaining_query_execution(self):
  192. """Does this backend support explaining query execution?"""
  193. return self.connection.ops.explain_prefix is not None
  194. @cached_property
  195. def supports_transactions(self):
  196. """Confirm support for transactions."""
  197. with self.connection.cursor() as cursor:
  198. cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
  199. self.connection.set_autocommit(False)
  200. cursor.execute('INSERT INTO ROLLBACK_TEST (X) VALUES (8)')
  201. self.connection.rollback()
  202. self.connection.set_autocommit(True)
  203. cursor.execute('SELECT COUNT(X) FROM ROLLBACK_TEST')
  204. count, = cursor.fetchone()
  205. cursor.execute('DROP TABLE ROLLBACK_TEST')
  206. return count == 0
  207. @cached_property
  208. def supports_stddev(self):
  209. """Confirm support for STDDEV and related stats functions."""
  210. try:
  211. self.connection.ops.check_expression_support(StdDev(1))
  212. except NotSupportedError:
  213. return False
  214. return True