Debian Codegraph @

django/db/backends/base/features.py

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