operations.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import json
  2. from functools import lru_cache, partial
  3. from django.conf import settings
  4. from django.db.backends.base.operations import BaseDatabaseOperations
  5. from django.db.backends.postgresql.psycopg_any import (
  6. Inet,
  7. Jsonb,
  8. errors,
  9. is_psycopg3,
  10. mogrify,
  11. )
  12. from django.db.backends.utils import split_tzname_delta
  13. from django.db.models.constants import OnConflict
  14. from django.db.models.functions import Cast
  15. from django.utils.regex_helper import _lazy_re_compile
  16. @lru_cache
  17. def get_json_dumps(encoder):
  18. if encoder is None:
  19. return json.dumps
  20. return partial(json.dumps, cls=encoder)
  21. class DatabaseOperations(BaseDatabaseOperations):
  22. cast_char_field_without_max_length = "varchar"
  23. explain_prefix = "EXPLAIN"
  24. explain_options = frozenset(
  25. [
  26. "ANALYZE",
  27. "BUFFERS",
  28. "COSTS",
  29. "SETTINGS",
  30. "SUMMARY",
  31. "TIMING",
  32. "VERBOSE",
  33. "WAL",
  34. ]
  35. )
  36. cast_data_types = {
  37. "AutoField": "integer",
  38. "BigAutoField": "bigint",
  39. "SmallAutoField": "smallint",
  40. }
  41. if is_psycopg3:
  42. from psycopg.types import numeric
  43. integerfield_type_map = {
  44. "SmallIntegerField": numeric.Int2,
  45. "IntegerField": numeric.Int4,
  46. "BigIntegerField": numeric.Int8,
  47. "PositiveSmallIntegerField": numeric.Int2,
  48. "PositiveIntegerField": numeric.Int4,
  49. "PositiveBigIntegerField": numeric.Int8,
  50. }
  51. def unification_cast_sql(self, output_field):
  52. internal_type = output_field.get_internal_type()
  53. if internal_type in (
  54. "GenericIPAddressField",
  55. "IPAddressField",
  56. "TimeField",
  57. "UUIDField",
  58. ):
  59. # PostgreSQL will resolve a union as type 'text' if input types are
  60. # 'unknown'.
  61. # https://www.postgresql.org/docs/current/typeconv-union-case.html
  62. # These fields cannot be implicitly cast back in the default
  63. # PostgreSQL configuration so we need to explicitly cast them.
  64. # We must also remove components of the type within brackets:
  65. # varchar(255) -> varchar.
  66. return (
  67. "CAST(%%s AS %s)" % output_field.db_type(self.connection).split("(")[0]
  68. )
  69. return "%s"
  70. # EXTRACT format cannot be passed in parameters.
  71. _extract_format_re = _lazy_re_compile(r"[A-Z_]+")
  72. def date_extract_sql(self, lookup_type, sql, params):
  73. # https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
  74. if lookup_type == "week_day":
  75. # For consistency across backends, we return Sunday=1, Saturday=7.
  76. return f"EXTRACT(DOW FROM {sql}) + 1", params
  77. elif lookup_type == "iso_week_day":
  78. return f"EXTRACT(ISODOW FROM {sql})", params
  79. elif lookup_type == "iso_year":
  80. return f"EXTRACT(ISOYEAR FROM {sql})", params
  81. lookup_type = lookup_type.upper()
  82. if not self._extract_format_re.fullmatch(lookup_type):
  83. raise ValueError(f"Invalid lookup type: {lookup_type!r}")
  84. return f"EXTRACT({lookup_type} FROM {sql})", params
  85. def date_trunc_sql(self, lookup_type, sql, params, tzname=None):
  86. sql, params = self._convert_sql_to_tz(sql, params, tzname)
  87. # https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
  88. return f"DATE_TRUNC(%s, {sql})", (lookup_type, *params)
  89. def _prepare_tzname_delta(self, tzname):
  90. tzname, sign, offset = split_tzname_delta(tzname)
  91. if offset:
  92. sign = "-" if sign == "+" else "+"
  93. return f"{tzname}{sign}{offset}"
  94. return tzname
  95. def _convert_sql_to_tz(self, sql, params, tzname):
  96. if tzname and settings.USE_TZ:
  97. tzname_param = self._prepare_tzname_delta(tzname)
  98. return f"{sql} AT TIME ZONE %s", (*params, tzname_param)
  99. return sql, params
  100. def datetime_cast_date_sql(self, sql, params, tzname):
  101. sql, params = self._convert_sql_to_tz(sql, params, tzname)
  102. return f"({sql})::date", params
  103. def datetime_cast_time_sql(self, sql, params, tzname):
  104. sql, params = self._convert_sql_to_tz(sql, params, tzname)
  105. return f"({sql})::time", params
  106. def datetime_extract_sql(self, lookup_type, sql, params, tzname):
  107. sql, params = self._convert_sql_to_tz(sql, params, tzname)
  108. if lookup_type == "second":
  109. # Truncate fractional seconds.
  110. return f"EXTRACT(SECOND FROM DATE_TRUNC(%s, {sql}))", ("second", *params)
  111. return self.date_extract_sql(lookup_type, sql, params)
  112. def datetime_trunc_sql(self, lookup_type, sql, params, tzname):
  113. sql, params = self._convert_sql_to_tz(sql, params, tzname)
  114. # https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
  115. return f"DATE_TRUNC(%s, {sql})", (lookup_type, *params)
  116. def time_extract_sql(self, lookup_type, sql, params):
  117. if lookup_type == "second":
  118. # Truncate fractional seconds.
  119. return f"EXTRACT(SECOND FROM DATE_TRUNC(%s, {sql}))", ("second", *params)
  120. return self.date_extract_sql(lookup_type, sql, params)
  121. def time_trunc_sql(self, lookup_type, sql, params, tzname=None):
  122. sql, params = self._convert_sql_to_tz(sql, params, tzname)
  123. return f"DATE_TRUNC(%s, {sql})::time", (lookup_type, *params)
  124. def deferrable_sql(self):
  125. return " DEFERRABLE INITIALLY DEFERRED"
  126. def fetch_returned_insert_rows(self, cursor):
  127. """
  128. Given a cursor object that has just performed an INSERT...RETURNING
  129. statement into a table, return the tuple of returned data.
  130. """
  131. return cursor.fetchall()
  132. def lookup_cast(self, lookup_type, internal_type=None):
  133. lookup = "%s"
  134. if lookup_type == "isnull" and internal_type in (
  135. "CharField",
  136. "EmailField",
  137. "TextField",
  138. ):
  139. return "%s::text"
  140. # Cast text lookups to text to allow things like filter(x__contains=4)
  141. if lookup_type in (
  142. "iexact",
  143. "contains",
  144. "icontains",
  145. "startswith",
  146. "istartswith",
  147. "endswith",
  148. "iendswith",
  149. "regex",
  150. "iregex",
  151. ):
  152. if internal_type in ("IPAddressField", "GenericIPAddressField"):
  153. lookup = "HOST(%s)"
  154. else:
  155. lookup = "%s::text"
  156. # Use UPPER(x) for case-insensitive lookups; it's faster.
  157. if lookup_type in ("iexact", "icontains", "istartswith", "iendswith"):
  158. lookup = "UPPER(%s)" % lookup
  159. return lookup
  160. def no_limit_value(self):
  161. return None
  162. def prepare_sql_script(self, sql):
  163. return [sql]
  164. def quote_name(self, name):
  165. if name.startswith('"') and name.endswith('"'):
  166. return name # Quoting once is enough.
  167. return '"%s"' % name
  168. def compose_sql(self, sql, params):
  169. return mogrify(sql, params, self.connection)
  170. def set_time_zone_sql(self):
  171. return "SELECT set_config('TimeZone', %s, false)"
  172. def sql_flush(self, style, tables, *, reset_sequences=False, allow_cascade=False):
  173. if not tables:
  174. return []
  175. # Perform a single SQL 'TRUNCATE x, y, z...;' statement. It allows us
  176. # to truncate tables referenced by a foreign key in any other table.
  177. sql_parts = [
  178. style.SQL_KEYWORD("TRUNCATE"),
  179. ", ".join(style.SQL_FIELD(self.quote_name(table)) for table in tables),
  180. ]
  181. if reset_sequences:
  182. sql_parts.append(style.SQL_KEYWORD("RESTART IDENTITY"))
  183. if allow_cascade:
  184. sql_parts.append(style.SQL_KEYWORD("CASCADE"))
  185. return ["%s;" % " ".join(sql_parts)]
  186. def sequence_reset_by_name_sql(self, style, sequences):
  187. # 'ALTER SEQUENCE sequence_name RESTART WITH 1;'... style SQL statements
  188. # to reset sequence indices
  189. sql = []
  190. for sequence_info in sequences:
  191. table_name = sequence_info["table"]
  192. # 'id' will be the case if it's an m2m using an autogenerated
  193. # intermediate table (see BaseDatabaseIntrospection.sequence_list).
  194. column_name = sequence_info["column"] or "id"
  195. sql.append(
  196. "%s setval(pg_get_serial_sequence('%s','%s'), 1, false);"
  197. % (
  198. style.SQL_KEYWORD("SELECT"),
  199. style.SQL_TABLE(self.quote_name(table_name)),
  200. style.SQL_FIELD(column_name),
  201. )
  202. )
  203. return sql
  204. def tablespace_sql(self, tablespace, inline=False):
  205. if inline:
  206. return "USING INDEX TABLESPACE %s" % self.quote_name(tablespace)
  207. else:
  208. return "TABLESPACE %s" % self.quote_name(tablespace)
  209. def sequence_reset_sql(self, style, model_list):
  210. from django.db import models
  211. output = []
  212. qn = self.quote_name
  213. for model in model_list:
  214. # Use `coalesce` to set the sequence for each model to the max pk
  215. # value if there are records, or 1 if there are none. Set the
  216. # `is_called` property (the third argument to `setval`) to true if
  217. # there are records (as the max pk value is already in use),
  218. # otherwise set it to false. Use pg_get_serial_sequence to get the
  219. # underlying sequence name from the table name and column name.
  220. for f in model._meta.local_fields:
  221. if isinstance(f, models.AutoField):
  222. output.append(
  223. "%s setval(pg_get_serial_sequence('%s','%s'), "
  224. "coalesce(max(%s), 1), max(%s) %s null) %s %s;"
  225. % (
  226. style.SQL_KEYWORD("SELECT"),
  227. style.SQL_TABLE(qn(model._meta.db_table)),
  228. style.SQL_FIELD(f.column),
  229. style.SQL_FIELD(qn(f.column)),
  230. style.SQL_FIELD(qn(f.column)),
  231. style.SQL_KEYWORD("IS NOT"),
  232. style.SQL_KEYWORD("FROM"),
  233. style.SQL_TABLE(qn(model._meta.db_table)),
  234. )
  235. )
  236. # Only one AutoField is allowed per model, so don't bother
  237. # continuing.
  238. break
  239. return output
  240. def prep_for_iexact_query(self, x):
  241. return x
  242. def max_name_length(self):
  243. """
  244. Return the maximum length of an identifier.
  245. The maximum length of an identifier is 63 by default, but can be
  246. changed by recompiling PostgreSQL after editing the NAMEDATALEN
  247. macro in src/include/pg_config_manual.h.
  248. This implementation returns 63, but can be overridden by a custom
  249. database backend that inherits most of its behavior from this one.
  250. """
  251. return 63
  252. def distinct_sql(self, fields, params):
  253. if fields:
  254. params = [param for param_list in params for param in param_list]
  255. return (["DISTINCT ON (%s)" % ", ".join(fields)], params)
  256. else:
  257. return ["DISTINCT"], []
  258. if is_psycopg3:
  259. def last_executed_query(self, cursor, sql, params):
  260. try:
  261. return self.compose_sql(sql, params)
  262. except errors.DataError:
  263. return None
  264. else:
  265. def last_executed_query(self, cursor, sql, params):
  266. # https://www.psycopg.org/docs/cursor.html#cursor.query
  267. # The query attribute is a Psycopg extension to the DB API 2.0.
  268. if cursor.query is not None:
  269. return cursor.query.decode()
  270. return None
  271. def return_insert_columns(self, fields):
  272. if not fields:
  273. return "", ()
  274. columns = [
  275. "%s.%s"
  276. % (
  277. self.quote_name(field.model._meta.db_table),
  278. self.quote_name(field.column),
  279. )
  280. for field in fields
  281. ]
  282. return "RETURNING %s" % ", ".join(columns), ()
  283. def bulk_insert_sql(self, fields, placeholder_rows):
  284. placeholder_rows_sql = (", ".join(row) for row in placeholder_rows)
  285. values_sql = ", ".join("(%s)" % sql for sql in placeholder_rows_sql)
  286. return "VALUES " + values_sql
  287. if is_psycopg3:
  288. def adapt_integerfield_value(self, value, internal_type):
  289. if value is None or hasattr(value, "resolve_expression"):
  290. return value
  291. return self.integerfield_type_map[internal_type](value)
  292. def adapt_datefield_value(self, value):
  293. return value
  294. def adapt_datetimefield_value(self, value):
  295. return value
  296. def adapt_timefield_value(self, value):
  297. return value
  298. def adapt_decimalfield_value(self, value, max_digits=None, decimal_places=None):
  299. return value
  300. def adapt_ipaddressfield_value(self, value):
  301. if value:
  302. return Inet(value)
  303. return None
  304. def adapt_json_value(self, value, encoder):
  305. return Jsonb(value, dumps=get_json_dumps(encoder))
  306. def subtract_temporals(self, internal_type, lhs, rhs):
  307. if internal_type == "DateField":
  308. lhs_sql, lhs_params = lhs
  309. rhs_sql, rhs_params = rhs
  310. params = (*lhs_params, *rhs_params)
  311. return "(interval '1 day' * (%s - %s))" % (lhs_sql, rhs_sql), params
  312. return super().subtract_temporals(internal_type, lhs, rhs)
  313. def explain_query_prefix(self, format=None, **options):
  314. extra = {}
  315. # Normalize options.
  316. if options:
  317. options = {
  318. name.upper(): "true" if value else "false"
  319. for name, value in options.items()
  320. }
  321. for valid_option in self.explain_options:
  322. value = options.pop(valid_option, None)
  323. if value is not None:
  324. extra[valid_option] = value
  325. prefix = super().explain_query_prefix(format, **options)
  326. if format:
  327. extra["FORMAT"] = format
  328. if extra:
  329. prefix += " (%s)" % ", ".join("%s %s" % i for i in extra.items())
  330. return prefix
  331. def on_conflict_suffix_sql(self, fields, on_conflict, update_fields, unique_fields):
  332. if on_conflict == OnConflict.IGNORE:
  333. return "ON CONFLICT DO NOTHING"
  334. if on_conflict == OnConflict.UPDATE:
  335. return "ON CONFLICT(%s) DO UPDATE SET %s" % (
  336. ", ".join(map(self.quote_name, unique_fields)),
  337. ", ".join(
  338. [
  339. f"{field} = EXCLUDED.{field}"
  340. for field in map(self.quote_name, update_fields)
  341. ]
  342. ),
  343. )
  344. return super().on_conflict_suffix_sql(
  345. fields,
  346. on_conflict,
  347. update_fields,
  348. unique_fields,
  349. )
  350. def prepare_join_on_clause(self, lhs_table, lhs_field, rhs_table, rhs_field):
  351. lhs_expr, rhs_expr = super().prepare_join_on_clause(
  352. lhs_table, lhs_field, rhs_table, rhs_field
  353. )
  354. if lhs_field.db_type(self.connection) != rhs_field.db_type(self.connection):
  355. rhs_expr = Cast(rhs_expr, lhs_field)
  356. return lhs_expr, rhs_expr