dml.py 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. # sql/dml.py
  2. # Copyright (C) 2009-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """
  8. Provide :class:`_expression.Insert`, :class:`_expression.Update` and
  9. :class:`_expression.Delete`.
  10. """
  11. from __future__ import annotations
  12. import collections.abc as collections_abc
  13. import operator
  14. from typing import Any
  15. from typing import cast
  16. from typing import Dict
  17. from typing import Iterable
  18. from typing import List
  19. from typing import MutableMapping
  20. from typing import NoReturn
  21. from typing import Optional
  22. from typing import overload
  23. from typing import Sequence
  24. from typing import Set
  25. from typing import Tuple
  26. from typing import Type
  27. from typing import TYPE_CHECKING
  28. from typing import TypeVar
  29. from typing import Union
  30. from . import coercions
  31. from . import roles
  32. from . import util as sql_util
  33. from ._typing import _TP
  34. from ._typing import _unexpected_kw
  35. from ._typing import is_column_element
  36. from ._typing import is_named_from_clause
  37. from .base import _entity_namespace_key
  38. from .base import _exclusive_against
  39. from .base import _from_objects
  40. from .base import _generative
  41. from .base import _select_iterables
  42. from .base import ColumnCollection
  43. from .base import ColumnSet
  44. from .base import CompileState
  45. from .base import DialectKWArgs
  46. from .base import Executable
  47. from .base import Generative
  48. from .base import HasCompileState
  49. from .elements import BooleanClauseList
  50. from .elements import ClauseElement
  51. from .elements import ColumnClause
  52. from .elements import ColumnElement
  53. from .elements import Null
  54. from .selectable import Alias
  55. from .selectable import ExecutableReturnsRows
  56. from .selectable import FromClause
  57. from .selectable import HasCTE
  58. from .selectable import HasPrefixes
  59. from .selectable import Join
  60. from .selectable import SelectLabelStyle
  61. from .selectable import TableClause
  62. from .selectable import TypedReturnsRows
  63. from .sqltypes import NullType
  64. from .visitors import InternalTraversal
  65. from .. import exc
  66. from .. import util
  67. from ..util.typing import Self
  68. from ..util.typing import TypeGuard
  69. if TYPE_CHECKING:
  70. from ._typing import _ColumnExpressionArgument
  71. from ._typing import _ColumnsClauseArgument
  72. from ._typing import _DMLColumnArgument
  73. from ._typing import _DMLColumnKeyMapping
  74. from ._typing import _DMLTableArgument
  75. from ._typing import _T0 # noqa
  76. from ._typing import _T1 # noqa
  77. from ._typing import _T2 # noqa
  78. from ._typing import _T3 # noqa
  79. from ._typing import _T4 # noqa
  80. from ._typing import _T5 # noqa
  81. from ._typing import _T6 # noqa
  82. from ._typing import _T7 # noqa
  83. from ._typing import _TypedColumnClauseArgument as _TCCA # noqa
  84. from .base import ReadOnlyColumnCollection
  85. from .compiler import SQLCompiler
  86. from .elements import KeyedColumnElement
  87. from .selectable import _ColumnsClauseElement
  88. from .selectable import _SelectIterable
  89. from .selectable import Select
  90. from .selectable import Selectable
  91. def isupdate(dml: DMLState) -> TypeGuard[UpdateDMLState]: ...
  92. def isdelete(dml: DMLState) -> TypeGuard[DeleteDMLState]: ...
  93. def isinsert(dml: DMLState) -> TypeGuard[InsertDMLState]: ...
  94. else:
  95. isupdate = operator.attrgetter("isupdate")
  96. isdelete = operator.attrgetter("isdelete")
  97. isinsert = operator.attrgetter("isinsert")
  98. _T = TypeVar("_T", bound=Any)
  99. _DMLColumnElement = Union[str, ColumnClause[Any]]
  100. _DMLTableElement = Union[TableClause, Alias, Join]
  101. class DMLState(CompileState):
  102. _no_parameters = True
  103. _dict_parameters: Optional[MutableMapping[_DMLColumnElement, Any]] = None
  104. _multi_parameters: Optional[
  105. List[MutableMapping[_DMLColumnElement, Any]]
  106. ] = None
  107. _ordered_values: Optional[List[Tuple[_DMLColumnElement, Any]]] = None
  108. _parameter_ordering: Optional[List[_DMLColumnElement]] = None
  109. _primary_table: FromClause
  110. _supports_implicit_returning = True
  111. isupdate = False
  112. isdelete = False
  113. isinsert = False
  114. statement: UpdateBase
  115. def __init__(
  116. self, statement: UpdateBase, compiler: SQLCompiler, **kw: Any
  117. ):
  118. raise NotImplementedError()
  119. @classmethod
  120. def get_entity_description(cls, statement: UpdateBase) -> Dict[str, Any]:
  121. return {
  122. "name": (
  123. statement.table.name
  124. if is_named_from_clause(statement.table)
  125. else None
  126. ),
  127. "table": statement.table,
  128. }
  129. @classmethod
  130. def get_returning_column_descriptions(
  131. cls, statement: UpdateBase
  132. ) -> List[Dict[str, Any]]:
  133. return [
  134. {
  135. "name": c.key,
  136. "type": c.type,
  137. "expr": c,
  138. }
  139. for c in statement._all_selected_columns
  140. ]
  141. @property
  142. def dml_table(self) -> _DMLTableElement:
  143. return self.statement.table
  144. if TYPE_CHECKING:
  145. @classmethod
  146. def get_plugin_class(cls, statement: Executable) -> Type[DMLState]: ...
  147. @classmethod
  148. def _get_multi_crud_kv_pairs(
  149. cls,
  150. statement: UpdateBase,
  151. multi_kv_iterator: Iterable[Dict[_DMLColumnArgument, Any]],
  152. ) -> List[Dict[_DMLColumnElement, Any]]:
  153. return [
  154. {
  155. coercions.expect(roles.DMLColumnRole, k): v
  156. for k, v in mapping.items()
  157. }
  158. for mapping in multi_kv_iterator
  159. ]
  160. @classmethod
  161. def _get_crud_kv_pairs(
  162. cls,
  163. statement: UpdateBase,
  164. kv_iterator: Iterable[Tuple[_DMLColumnArgument, Any]],
  165. needs_to_be_cacheable: bool,
  166. ) -> List[Tuple[_DMLColumnElement, Any]]:
  167. return [
  168. (
  169. coercions.expect(roles.DMLColumnRole, k),
  170. (
  171. v
  172. if not needs_to_be_cacheable
  173. else coercions.expect(
  174. roles.ExpressionElementRole,
  175. v,
  176. type_=NullType(),
  177. is_crud=True,
  178. )
  179. ),
  180. )
  181. for k, v in kv_iterator
  182. ]
  183. def _make_extra_froms(
  184. self, statement: DMLWhereBase
  185. ) -> Tuple[FromClause, List[FromClause]]:
  186. froms: List[FromClause] = []
  187. all_tables = list(sql_util.tables_from_leftmost(statement.table))
  188. primary_table = all_tables[0]
  189. seen = {primary_table}
  190. consider = statement._where_criteria
  191. if self._dict_parameters:
  192. consider += tuple(self._dict_parameters.values())
  193. for crit in consider:
  194. for item in _from_objects(crit):
  195. if not seen.intersection(item._cloned_set):
  196. froms.append(item)
  197. seen.update(item._cloned_set)
  198. froms.extend(all_tables[1:])
  199. return primary_table, froms
  200. def _process_values(self, statement: ValuesBase) -> None:
  201. if self._no_parameters:
  202. self._dict_parameters = statement._values
  203. self._no_parameters = False
  204. def _process_select_values(self, statement: ValuesBase) -> None:
  205. assert statement._select_names is not None
  206. parameters: MutableMapping[_DMLColumnElement, Any] = {
  207. name: Null() for name in statement._select_names
  208. }
  209. if self._no_parameters:
  210. self._no_parameters = False
  211. self._dict_parameters = parameters
  212. else:
  213. # this condition normally not reachable as the Insert
  214. # does not allow this construction to occur
  215. assert False, "This statement already has parameters"
  216. def _no_multi_values_supported(self, statement: ValuesBase) -> NoReturn:
  217. raise exc.InvalidRequestError(
  218. "%s construct does not support "
  219. "multiple parameter sets." % statement.__visit_name__.upper()
  220. )
  221. def _cant_mix_formats_error(self) -> NoReturn:
  222. raise exc.InvalidRequestError(
  223. "Can't mix single and multiple VALUES "
  224. "formats in one INSERT statement; one style appends to a "
  225. "list while the other replaces values, so the intent is "
  226. "ambiguous."
  227. )
  228. @CompileState.plugin_for("default", "insert")
  229. class InsertDMLState(DMLState):
  230. isinsert = True
  231. include_table_with_column_exprs = False
  232. _has_multi_parameters = False
  233. def __init__(
  234. self,
  235. statement: Insert,
  236. compiler: SQLCompiler,
  237. disable_implicit_returning: bool = False,
  238. **kw: Any,
  239. ):
  240. self.statement = statement
  241. self._primary_table = statement.table
  242. if disable_implicit_returning:
  243. self._supports_implicit_returning = False
  244. self.isinsert = True
  245. if statement._select_names:
  246. self._process_select_values(statement)
  247. if statement._values is not None:
  248. self._process_values(statement)
  249. if statement._multi_values:
  250. self._process_multi_values(statement)
  251. @util.memoized_property
  252. def _insert_col_keys(self) -> List[str]:
  253. # this is also done in crud.py -> _key_getters_for_crud_column
  254. return [
  255. coercions.expect(roles.DMLColumnRole, col, as_key=True)
  256. for col in self._dict_parameters or ()
  257. ]
  258. def _process_values(self, statement: ValuesBase) -> None:
  259. if self._no_parameters:
  260. self._has_multi_parameters = False
  261. self._dict_parameters = statement._values
  262. self._no_parameters = False
  263. elif self._has_multi_parameters:
  264. self._cant_mix_formats_error()
  265. def _process_multi_values(self, statement: ValuesBase) -> None:
  266. for parameters in statement._multi_values:
  267. multi_parameters: List[MutableMapping[_DMLColumnElement, Any]] = [
  268. (
  269. {
  270. c.key: value
  271. for c, value in zip(statement.table.c, parameter_set)
  272. }
  273. if isinstance(parameter_set, collections_abc.Sequence)
  274. else parameter_set
  275. )
  276. for parameter_set in parameters
  277. ]
  278. if self._no_parameters:
  279. self._no_parameters = False
  280. self._has_multi_parameters = True
  281. self._multi_parameters = multi_parameters
  282. self._dict_parameters = self._multi_parameters[0]
  283. elif not self._has_multi_parameters:
  284. self._cant_mix_formats_error()
  285. else:
  286. assert self._multi_parameters
  287. self._multi_parameters.extend(multi_parameters)
  288. @CompileState.plugin_for("default", "update")
  289. class UpdateDMLState(DMLState):
  290. isupdate = True
  291. include_table_with_column_exprs = False
  292. def __init__(self, statement: Update, compiler: SQLCompiler, **kw: Any):
  293. self.statement = statement
  294. self.isupdate = True
  295. if statement._ordered_values is not None:
  296. self._process_ordered_values(statement)
  297. elif statement._values is not None:
  298. self._process_values(statement)
  299. elif statement._multi_values:
  300. self._no_multi_values_supported(statement)
  301. t, ef = self._make_extra_froms(statement)
  302. self._primary_table = t
  303. self._extra_froms = ef
  304. self.is_multitable = mt = ef
  305. self.include_table_with_column_exprs = bool(
  306. mt and compiler.render_table_with_column_in_update_from
  307. )
  308. def _process_ordered_values(self, statement: ValuesBase) -> None:
  309. parameters = statement._ordered_values
  310. if self._no_parameters:
  311. self._no_parameters = False
  312. assert parameters is not None
  313. self._dict_parameters = dict(parameters)
  314. self._ordered_values = parameters
  315. self._parameter_ordering = [key for key, value in parameters]
  316. else:
  317. raise exc.InvalidRequestError(
  318. "Can only invoke ordered_values() once, and not mixed "
  319. "with any other values() call"
  320. )
  321. @CompileState.plugin_for("default", "delete")
  322. class DeleteDMLState(DMLState):
  323. isdelete = True
  324. def __init__(self, statement: Delete, compiler: SQLCompiler, **kw: Any):
  325. self.statement = statement
  326. self.isdelete = True
  327. t, ef = self._make_extra_froms(statement)
  328. self._primary_table = t
  329. self._extra_froms = ef
  330. self.is_multitable = ef
  331. class UpdateBase(
  332. roles.DMLRole,
  333. HasCTE,
  334. HasCompileState,
  335. DialectKWArgs,
  336. HasPrefixes,
  337. Generative,
  338. ExecutableReturnsRows,
  339. ClauseElement,
  340. ):
  341. """Form the base for ``INSERT``, ``UPDATE``, and ``DELETE`` statements."""
  342. __visit_name__ = "update_base"
  343. _hints: util.immutabledict[Tuple[_DMLTableElement, str], str] = (
  344. util.EMPTY_DICT
  345. )
  346. named_with_column = False
  347. _label_style: SelectLabelStyle = (
  348. SelectLabelStyle.LABEL_STYLE_DISAMBIGUATE_ONLY
  349. )
  350. table: _DMLTableElement
  351. _return_defaults = False
  352. _return_defaults_columns: Optional[Tuple[_ColumnsClauseElement, ...]] = (
  353. None
  354. )
  355. _supplemental_returning: Optional[Tuple[_ColumnsClauseElement, ...]] = None
  356. _returning: Tuple[_ColumnsClauseElement, ...] = ()
  357. is_dml = True
  358. def _generate_fromclause_column_proxies(
  359. self,
  360. fromclause: FromClause,
  361. columns: ColumnCollection[str, KeyedColumnElement[Any]],
  362. primary_key: ColumnSet,
  363. foreign_keys: Set[KeyedColumnElement[Any]],
  364. ) -> None:
  365. columns._populate_separate_keys(
  366. col._make_proxy(
  367. fromclause, primary_key=primary_key, foreign_keys=foreign_keys
  368. )
  369. for col in self._all_selected_columns
  370. if is_column_element(col)
  371. )
  372. def params(self, *arg: Any, **kw: Any) -> NoReturn:
  373. """Set the parameters for the statement.
  374. This method raises ``NotImplementedError`` on the base class,
  375. and is overridden by :class:`.ValuesBase` to provide the
  376. SET/VALUES clause of UPDATE and INSERT.
  377. """
  378. raise NotImplementedError(
  379. "params() is not supported for INSERT/UPDATE/DELETE statements."
  380. " To set the values for an INSERT or UPDATE statement, use"
  381. " stmt.values(**parameters)."
  382. )
  383. @_generative
  384. def with_dialect_options(self, **opt: Any) -> Self:
  385. """Add dialect options to this INSERT/UPDATE/DELETE object.
  386. e.g.::
  387. upd = table.update().dialect_options(mysql_limit=10)
  388. .. versionadded: 1.4 - this method supersedes the dialect options
  389. associated with the constructor.
  390. """
  391. self._validate_dialect_kwargs(opt)
  392. return self
  393. @_generative
  394. def return_defaults(
  395. self,
  396. *cols: _DMLColumnArgument,
  397. supplemental_cols: Optional[Iterable[_DMLColumnArgument]] = None,
  398. sort_by_parameter_order: bool = False,
  399. ) -> Self:
  400. """Make use of a :term:`RETURNING` clause for the purpose
  401. of fetching server-side expressions and defaults, for supporting
  402. backends only.
  403. .. deepalchemy::
  404. The :meth:`.UpdateBase.return_defaults` method is used by the ORM
  405. for its internal work in fetching newly generated primary key
  406. and server default values, in particular to provide the underyling
  407. implementation of the :paramref:`_orm.Mapper.eager_defaults`
  408. ORM feature as well as to allow RETURNING support with bulk
  409. ORM inserts. Its behavior is fairly idiosyncratic
  410. and is not really intended for general use. End users should
  411. stick with using :meth:`.UpdateBase.returning` in order to
  412. add RETURNING clauses to their INSERT, UPDATE and DELETE
  413. statements.
  414. Normally, a single row INSERT statement will automatically populate the
  415. :attr:`.CursorResult.inserted_primary_key` attribute when executed,
  416. which stores the primary key of the row that was just inserted in the
  417. form of a :class:`.Row` object with column names as named tuple keys
  418. (and the :attr:`.Row._mapping` view fully populated as well). The
  419. dialect in use chooses the strategy to use in order to populate this
  420. data; if it was generated using server-side defaults and / or SQL
  421. expressions, dialect-specific approaches such as ``cursor.lastrowid``
  422. or ``RETURNING`` are typically used to acquire the new primary key
  423. value.
  424. However, when the statement is modified by calling
  425. :meth:`.UpdateBase.return_defaults` before executing the statement,
  426. additional behaviors take place **only** for backends that support
  427. RETURNING and for :class:`.Table` objects that maintain the
  428. :paramref:`.Table.implicit_returning` parameter at its default value of
  429. ``True``. In these cases, when the :class:`.CursorResult` is returned
  430. from the statement's execution, not only will
  431. :attr:`.CursorResult.inserted_primary_key` be populated as always, the
  432. :attr:`.CursorResult.returned_defaults` attribute will also be
  433. populated with a :class:`.Row` named-tuple representing the full range
  434. of server generated
  435. values from that single row, including values for any columns that
  436. specify :paramref:`_schema.Column.server_default` or which make use of
  437. :paramref:`_schema.Column.default` using a SQL expression.
  438. When invoking INSERT statements with multiple rows using
  439. :ref:`insertmanyvalues <engine_insertmanyvalues>`, the
  440. :meth:`.UpdateBase.return_defaults` modifier will have the effect of
  441. the :attr:`_engine.CursorResult.inserted_primary_key_rows` and
  442. :attr:`_engine.CursorResult.returned_defaults_rows` attributes being
  443. fully populated with lists of :class:`.Row` objects representing newly
  444. inserted primary key values as well as newly inserted server generated
  445. values for each row inserted. The
  446. :attr:`.CursorResult.inserted_primary_key` and
  447. :attr:`.CursorResult.returned_defaults` attributes will also continue
  448. to be populated with the first row of these two collections.
  449. If the backend does not support RETURNING or the :class:`.Table` in use
  450. has disabled :paramref:`.Table.implicit_returning`, then no RETURNING
  451. clause is added and no additional data is fetched, however the
  452. INSERT, UPDATE or DELETE statement proceeds normally.
  453. E.g.::
  454. stmt = table.insert().values(data="newdata").return_defaults()
  455. result = connection.execute(stmt)
  456. server_created_at = result.returned_defaults["created_at"]
  457. When used against an UPDATE statement
  458. :meth:`.UpdateBase.return_defaults` instead looks for columns that
  459. include :paramref:`_schema.Column.onupdate` or
  460. :paramref:`_schema.Column.server_onupdate` parameters assigned, when
  461. constructing the columns that will be included in the RETURNING clause
  462. by default if explicit columns were not specified. When used against a
  463. DELETE statement, no columns are included in RETURNING by default, they
  464. instead must be specified explicitly as there are no columns that
  465. normally change values when a DELETE statement proceeds.
  466. .. versionadded:: 2.0 :meth:`.UpdateBase.return_defaults` is supported
  467. for DELETE statements also and has been moved from
  468. :class:`.ValuesBase` to :class:`.UpdateBase`.
  469. The :meth:`.UpdateBase.return_defaults` method is mutually exclusive
  470. against the :meth:`.UpdateBase.returning` method and errors will be
  471. raised during the SQL compilation process if both are used at the same
  472. time on one statement. The RETURNING clause of the INSERT, UPDATE or
  473. DELETE statement is therefore controlled by only one of these methods
  474. at a time.
  475. The :meth:`.UpdateBase.return_defaults` method differs from
  476. :meth:`.UpdateBase.returning` in these ways:
  477. 1. :meth:`.UpdateBase.return_defaults` method causes the
  478. :attr:`.CursorResult.returned_defaults` collection to be populated
  479. with the first row from the RETURNING result. This attribute is not
  480. populated when using :meth:`.UpdateBase.returning`.
  481. 2. :meth:`.UpdateBase.return_defaults` is compatible with existing
  482. logic used to fetch auto-generated primary key values that are then
  483. populated into the :attr:`.CursorResult.inserted_primary_key`
  484. attribute. By contrast, using :meth:`.UpdateBase.returning` will
  485. have the effect of the :attr:`.CursorResult.inserted_primary_key`
  486. attribute being left unpopulated.
  487. 3. :meth:`.UpdateBase.return_defaults` can be called against any
  488. backend. Backends that don't support RETURNING will skip the usage
  489. of the feature, rather than raising an exception, *unless*
  490. ``supplemental_cols`` is passed. The return value
  491. of :attr:`_engine.CursorResult.returned_defaults` will be ``None``
  492. for backends that don't support RETURNING or for which the target
  493. :class:`.Table` sets :paramref:`.Table.implicit_returning` to
  494. ``False``.
  495. 4. An INSERT statement invoked with executemany() is supported if the
  496. backend database driver supports the
  497. :ref:`insertmanyvalues <engine_insertmanyvalues>`
  498. feature which is now supported by most SQLAlchemy-included backends.
  499. When executemany is used, the
  500. :attr:`_engine.CursorResult.returned_defaults_rows` and
  501. :attr:`_engine.CursorResult.inserted_primary_key_rows` accessors
  502. will return the inserted defaults and primary keys.
  503. .. versionadded:: 1.4 Added
  504. :attr:`_engine.CursorResult.returned_defaults_rows` and
  505. :attr:`_engine.CursorResult.inserted_primary_key_rows` accessors.
  506. In version 2.0, the underlying implementation which fetches and
  507. populates the data for these attributes was generalized to be
  508. supported by most backends, whereas in 1.4 they were only
  509. supported by the ``psycopg2`` driver.
  510. :param cols: optional list of column key names or
  511. :class:`_schema.Column` that acts as a filter for those columns that
  512. will be fetched.
  513. :param supplemental_cols: optional list of RETURNING expressions,
  514. in the same form as one would pass to the
  515. :meth:`.UpdateBase.returning` method. When present, the additional
  516. columns will be included in the RETURNING clause, and the
  517. :class:`.CursorResult` object will be "rewound" when returned, so
  518. that methods like :meth:`.CursorResult.all` will return new rows
  519. mostly as though the statement used :meth:`.UpdateBase.returning`
  520. directly. However, unlike when using :meth:`.UpdateBase.returning`
  521. directly, the **order of the columns is undefined**, so can only be
  522. targeted using names or :attr:`.Row._mapping` keys; they cannot
  523. reliably be targeted positionally.
  524. .. versionadded:: 2.0
  525. :param sort_by_parameter_order: for a batch INSERT that is being
  526. executed against multiple parameter sets, organize the results of
  527. RETURNING so that the returned rows correspond to the order of
  528. parameter sets passed in. This applies only to an :term:`executemany`
  529. execution for supporting dialects and typically makes use of the
  530. :term:`insertmanyvalues` feature.
  531. .. versionadded:: 2.0.10
  532. .. seealso::
  533. :ref:`engine_insertmanyvalues_returning_order` - background on
  534. sorting of RETURNING rows for bulk INSERT
  535. .. seealso::
  536. :meth:`.UpdateBase.returning`
  537. :attr:`_engine.CursorResult.returned_defaults`
  538. :attr:`_engine.CursorResult.returned_defaults_rows`
  539. :attr:`_engine.CursorResult.inserted_primary_key`
  540. :attr:`_engine.CursorResult.inserted_primary_key_rows`
  541. """
  542. if self._return_defaults:
  543. # note _return_defaults_columns = () means return all columns,
  544. # so if we have been here before, only update collection if there
  545. # are columns in the collection
  546. if self._return_defaults_columns and cols:
  547. self._return_defaults_columns = tuple(
  548. util.OrderedSet(self._return_defaults_columns).union(
  549. coercions.expect(roles.ColumnsClauseRole, c)
  550. for c in cols
  551. )
  552. )
  553. else:
  554. # set for all columns
  555. self._return_defaults_columns = ()
  556. else:
  557. self._return_defaults_columns = tuple(
  558. coercions.expect(roles.ColumnsClauseRole, c) for c in cols
  559. )
  560. self._return_defaults = True
  561. if sort_by_parameter_order:
  562. if not self.is_insert:
  563. raise exc.ArgumentError(
  564. "The 'sort_by_parameter_order' argument to "
  565. "return_defaults() only applies to INSERT statements"
  566. )
  567. self._sort_by_parameter_order = True
  568. if supplemental_cols:
  569. # uniquifying while also maintaining order (the maintain of order
  570. # is for test suites but also for vertical splicing
  571. supplemental_col_tup = (
  572. coercions.expect(roles.ColumnsClauseRole, c)
  573. for c in supplemental_cols
  574. )
  575. if self._supplemental_returning is None:
  576. self._supplemental_returning = tuple(
  577. util.unique_list(supplemental_col_tup)
  578. )
  579. else:
  580. self._supplemental_returning = tuple(
  581. util.unique_list(
  582. self._supplemental_returning
  583. + tuple(supplemental_col_tup)
  584. )
  585. )
  586. return self
  587. def is_derived_from(self, fromclause: Optional[FromClause]) -> bool:
  588. """Return ``True`` if this :class:`.ReturnsRows` is
  589. 'derived' from the given :class:`.FromClause`.
  590. Since these are DMLs, we dont want such statements ever being adapted
  591. so we return False for derives.
  592. """
  593. return False
  594. @_generative
  595. def returning(
  596. self,
  597. *cols: _ColumnsClauseArgument[Any],
  598. sort_by_parameter_order: bool = False,
  599. **__kw: Any,
  600. ) -> UpdateBase:
  601. r"""Add a :term:`RETURNING` or equivalent clause to this statement.
  602. e.g.:
  603. .. sourcecode:: pycon+sql
  604. >>> stmt = (
  605. ... table.update()
  606. ... .where(table.c.data == "value")
  607. ... .values(status="X")
  608. ... .returning(table.c.server_flag, table.c.updated_timestamp)
  609. ... )
  610. >>> print(stmt)
  611. {printsql}UPDATE some_table SET status=:status
  612. WHERE some_table.data = :data_1
  613. RETURNING some_table.server_flag, some_table.updated_timestamp
  614. The method may be invoked multiple times to add new entries to the
  615. list of expressions to be returned.
  616. .. versionadded:: 1.4.0b2 The method may be invoked multiple times to
  617. add new entries to the list of expressions to be returned.
  618. The given collection of column expressions should be derived from the
  619. table that is the target of the INSERT, UPDATE, or DELETE. While
  620. :class:`_schema.Column` objects are typical, the elements can also be
  621. expressions:
  622. .. sourcecode:: pycon+sql
  623. >>> stmt = table.insert().returning(
  624. ... (table.c.first_name + " " + table.c.last_name).label("fullname")
  625. ... )
  626. >>> print(stmt)
  627. {printsql}INSERT INTO some_table (first_name, last_name)
  628. VALUES (:first_name, :last_name)
  629. RETURNING some_table.first_name || :first_name_1 || some_table.last_name AS fullname
  630. Upon compilation, a RETURNING clause, or database equivalent,
  631. will be rendered within the statement. For INSERT and UPDATE,
  632. the values are the newly inserted/updated values. For DELETE,
  633. the values are those of the rows which were deleted.
  634. Upon execution, the values of the columns to be returned are made
  635. available via the result set and can be iterated using
  636. :meth:`_engine.CursorResult.fetchone` and similar.
  637. For DBAPIs which do not
  638. natively support returning values (i.e. cx_oracle), SQLAlchemy will
  639. approximate this behavior at the result level so that a reasonable
  640. amount of behavioral neutrality is provided.
  641. Note that not all databases/DBAPIs
  642. support RETURNING. For those backends with no support,
  643. an exception is raised upon compilation and/or execution.
  644. For those who do support it, the functionality across backends
  645. varies greatly, including restrictions on executemany()
  646. and other statements which return multiple rows. Please
  647. read the documentation notes for the database in use in
  648. order to determine the availability of RETURNING.
  649. :param \*cols: series of columns, SQL expressions, or whole tables
  650. entities to be returned.
  651. :param sort_by_parameter_order: for a batch INSERT that is being
  652. executed against multiple parameter sets, organize the results of
  653. RETURNING so that the returned rows correspond to the order of
  654. parameter sets passed in. This applies only to an :term:`executemany`
  655. execution for supporting dialects and typically makes use of the
  656. :term:`insertmanyvalues` feature.
  657. .. versionadded:: 2.0.10
  658. .. seealso::
  659. :ref:`engine_insertmanyvalues_returning_order` - background on
  660. sorting of RETURNING rows for bulk INSERT (Core level discussion)
  661. :ref:`orm_queryguide_bulk_insert_returning_ordered` - example of
  662. use with :ref:`orm_queryguide_bulk_insert` (ORM level discussion)
  663. .. seealso::
  664. :meth:`.UpdateBase.return_defaults` - an alternative method tailored
  665. towards efficient fetching of server-side defaults and triggers
  666. for single-row INSERTs or UPDATEs.
  667. :ref:`tutorial_insert_returning` - in the :ref:`unified_tutorial`
  668. """ # noqa: E501
  669. if __kw:
  670. raise _unexpected_kw("UpdateBase.returning()", __kw)
  671. if self._return_defaults:
  672. raise exc.InvalidRequestError(
  673. "return_defaults() is already configured on this statement"
  674. )
  675. self._returning += tuple(
  676. coercions.expect(roles.ColumnsClauseRole, c) for c in cols
  677. )
  678. if sort_by_parameter_order:
  679. if not self.is_insert:
  680. raise exc.ArgumentError(
  681. "The 'sort_by_parameter_order' argument to returning() "
  682. "only applies to INSERT statements"
  683. )
  684. self._sort_by_parameter_order = True
  685. return self
  686. def corresponding_column(
  687. self, column: KeyedColumnElement[Any], require_embedded: bool = False
  688. ) -> Optional[ColumnElement[Any]]:
  689. return self.exported_columns.corresponding_column(
  690. column, require_embedded=require_embedded
  691. )
  692. @util.ro_memoized_property
  693. def _all_selected_columns(self) -> _SelectIterable:
  694. return [c for c in _select_iterables(self._returning)]
  695. @util.ro_memoized_property
  696. def exported_columns(
  697. self,
  698. ) -> ReadOnlyColumnCollection[Optional[str], ColumnElement[Any]]:
  699. """Return the RETURNING columns as a column collection for this
  700. statement.
  701. .. versionadded:: 1.4
  702. """
  703. return ColumnCollection(
  704. (c.key, c)
  705. for c in self._all_selected_columns
  706. if is_column_element(c)
  707. ).as_readonly()
  708. @_generative
  709. def with_hint(
  710. self,
  711. text: str,
  712. selectable: Optional[_DMLTableArgument] = None,
  713. dialect_name: str = "*",
  714. ) -> Self:
  715. """Add a table hint for a single table to this
  716. INSERT/UPDATE/DELETE statement.
  717. .. note::
  718. :meth:`.UpdateBase.with_hint` currently applies only to
  719. Microsoft SQL Server. For MySQL INSERT/UPDATE/DELETE hints, use
  720. :meth:`.UpdateBase.prefix_with`.
  721. The text of the hint is rendered in the appropriate
  722. location for the database backend in use, relative
  723. to the :class:`_schema.Table` that is the subject of this
  724. statement, or optionally to that of the given
  725. :class:`_schema.Table` passed as the ``selectable`` argument.
  726. The ``dialect_name`` option will limit the rendering of a particular
  727. hint to a particular backend. Such as, to add a hint
  728. that only takes effect for SQL Server::
  729. mytable.insert().with_hint("WITH (PAGLOCK)", dialect_name="mssql")
  730. :param text: Text of the hint.
  731. :param selectable: optional :class:`_schema.Table` that specifies
  732. an element of the FROM clause within an UPDATE or DELETE
  733. to be the subject of the hint - applies only to certain backends.
  734. :param dialect_name: defaults to ``*``, if specified as the name
  735. of a particular dialect, will apply these hints only when
  736. that dialect is in use.
  737. """
  738. if selectable is None:
  739. selectable = self.table
  740. else:
  741. selectable = coercions.expect(roles.DMLTableRole, selectable)
  742. self._hints = self._hints.union({(selectable, dialect_name): text})
  743. return self
  744. @property
  745. def entity_description(self) -> Dict[str, Any]:
  746. """Return a :term:`plugin-enabled` description of the table and/or
  747. entity which this DML construct is operating against.
  748. This attribute is generally useful when using the ORM, as an
  749. extended structure which includes information about mapped
  750. entities is returned. The section :ref:`queryguide_inspection`
  751. contains more background.
  752. For a Core statement, the structure returned by this accessor
  753. is derived from the :attr:`.UpdateBase.table` attribute, and
  754. refers to the :class:`.Table` being inserted, updated, or deleted::
  755. >>> stmt = insert(user_table)
  756. >>> stmt.entity_description
  757. {
  758. "name": "user_table",
  759. "table": Table("user_table", ...)
  760. }
  761. .. versionadded:: 1.4.33
  762. .. seealso::
  763. :attr:`.UpdateBase.returning_column_descriptions`
  764. :attr:`.Select.column_descriptions` - entity information for
  765. a :func:`.select` construct
  766. :ref:`queryguide_inspection` - ORM background
  767. """
  768. meth = DMLState.get_plugin_class(self).get_entity_description
  769. return meth(self)
  770. @property
  771. def returning_column_descriptions(self) -> List[Dict[str, Any]]:
  772. """Return a :term:`plugin-enabled` description of the columns
  773. which this DML construct is RETURNING against, in other words
  774. the expressions established as part of :meth:`.UpdateBase.returning`.
  775. This attribute is generally useful when using the ORM, as an
  776. extended structure which includes information about mapped
  777. entities is returned. The section :ref:`queryguide_inspection`
  778. contains more background.
  779. For a Core statement, the structure returned by this accessor is
  780. derived from the same objects that are returned by the
  781. :attr:`.UpdateBase.exported_columns` accessor::
  782. >>> stmt = insert(user_table).returning(user_table.c.id, user_table.c.name)
  783. >>> stmt.entity_description
  784. [
  785. {
  786. "name": "id",
  787. "type": Integer,
  788. "expr": Column("id", Integer(), table=<user>, ...)
  789. },
  790. {
  791. "name": "name",
  792. "type": String(),
  793. "expr": Column("name", String(), table=<user>, ...)
  794. },
  795. ]
  796. .. versionadded:: 1.4.33
  797. .. seealso::
  798. :attr:`.UpdateBase.entity_description`
  799. :attr:`.Select.column_descriptions` - entity information for
  800. a :func:`.select` construct
  801. :ref:`queryguide_inspection` - ORM background
  802. """ # noqa: E501
  803. meth = DMLState.get_plugin_class(
  804. self
  805. ).get_returning_column_descriptions
  806. return meth(self)
  807. class ValuesBase(UpdateBase):
  808. """Supplies support for :meth:`.ValuesBase.values` to
  809. INSERT and UPDATE constructs."""
  810. __visit_name__ = "values_base"
  811. _supports_multi_parameters = False
  812. select: Optional[Select[Any]] = None
  813. """SELECT statement for INSERT .. FROM SELECT"""
  814. _post_values_clause: Optional[ClauseElement] = None
  815. """used by extensions to Insert etc. to add additional syntacitcal
  816. constructs, e.g. ON CONFLICT etc."""
  817. _values: Optional[util.immutabledict[_DMLColumnElement, Any]] = None
  818. _multi_values: Tuple[
  819. Union[
  820. Sequence[Dict[_DMLColumnElement, Any]],
  821. Sequence[Sequence[Any]],
  822. ],
  823. ...,
  824. ] = ()
  825. _ordered_values: Optional[List[Tuple[_DMLColumnElement, Any]]] = None
  826. _select_names: Optional[List[str]] = None
  827. _inline: bool = False
  828. def __init__(self, table: _DMLTableArgument):
  829. self.table = coercions.expect(
  830. roles.DMLTableRole, table, apply_propagate_attrs=self
  831. )
  832. @_generative
  833. @_exclusive_against(
  834. "_select_names",
  835. "_ordered_values",
  836. msgs={
  837. "_select_names": "This construct already inserts from a SELECT",
  838. "_ordered_values": "This statement already has ordered "
  839. "values present",
  840. },
  841. )
  842. def values(
  843. self,
  844. *args: Union[
  845. _DMLColumnKeyMapping[Any],
  846. Sequence[Any],
  847. ],
  848. **kwargs: Any,
  849. ) -> Self:
  850. r"""Specify a fixed VALUES clause for an INSERT statement, or the SET
  851. clause for an UPDATE.
  852. Note that the :class:`_expression.Insert` and
  853. :class:`_expression.Update`
  854. constructs support
  855. per-execution time formatting of the VALUES and/or SET clauses,
  856. based on the arguments passed to :meth:`_engine.Connection.execute`.
  857. However, the :meth:`.ValuesBase.values` method can be used to "fix" a
  858. particular set of parameters into the statement.
  859. Multiple calls to :meth:`.ValuesBase.values` will produce a new
  860. construct, each one with the parameter list modified to include
  861. the new parameters sent. In the typical case of a single
  862. dictionary of parameters, the newly passed keys will replace
  863. the same keys in the previous construct. In the case of a list-based
  864. "multiple values" construct, each new list of values is extended
  865. onto the existing list of values.
  866. :param \**kwargs: key value pairs representing the string key
  867. of a :class:`_schema.Column`
  868. mapped to the value to be rendered into the
  869. VALUES or SET clause::
  870. users.insert().values(name="some name")
  871. users.update().where(users.c.id == 5).values(name="some name")
  872. :param \*args: As an alternative to passing key/value parameters,
  873. a dictionary, tuple, or list of dictionaries or tuples can be passed
  874. as a single positional argument in order to form the VALUES or
  875. SET clause of the statement. The forms that are accepted vary
  876. based on whether this is an :class:`_expression.Insert` or an
  877. :class:`_expression.Update` construct.
  878. For either an :class:`_expression.Insert` or
  879. :class:`_expression.Update`
  880. construct, a single dictionary can be passed, which works the same as
  881. that of the kwargs form::
  882. users.insert().values({"name": "some name"})
  883. users.update().values({"name": "some new name"})
  884. Also for either form but more typically for the
  885. :class:`_expression.Insert` construct, a tuple that contains an
  886. entry for every column in the table is also accepted::
  887. users.insert().values((5, "some name"))
  888. The :class:`_expression.Insert` construct also supports being
  889. passed a list of dictionaries or full-table-tuples, which on the
  890. server will render the less common SQL syntax of "multiple values" -
  891. this syntax is supported on backends such as SQLite, PostgreSQL,
  892. MySQL, but not necessarily others::
  893. users.insert().values(
  894. [
  895. {"name": "some name"},
  896. {"name": "some other name"},
  897. {"name": "yet another name"},
  898. ]
  899. )
  900. The above form would render a multiple VALUES statement similar to:
  901. .. sourcecode:: sql
  902. INSERT INTO users (name) VALUES
  903. (:name_1),
  904. (:name_2),
  905. (:name_3)
  906. It is essential to note that **passing multiple values is
  907. NOT the same as using traditional executemany() form**. The above
  908. syntax is a **special** syntax not typically used. To emit an
  909. INSERT statement against multiple rows, the normal method is
  910. to pass a multiple values list to the
  911. :meth:`_engine.Connection.execute`
  912. method, which is supported by all database backends and is generally
  913. more efficient for a very large number of parameters.
  914. .. seealso::
  915. :ref:`tutorial_multiple_parameters` - an introduction to
  916. the traditional Core method of multiple parameter set
  917. invocation for INSERTs and other statements.
  918. The UPDATE construct also supports rendering the SET parameters
  919. in a specific order. For this feature refer to the
  920. :meth:`_expression.Update.ordered_values` method.
  921. .. seealso::
  922. :meth:`_expression.Update.ordered_values`
  923. """
  924. if args:
  925. # positional case. this is currently expensive. we don't
  926. # yet have positional-only args so we have to check the length.
  927. # then we need to check multiparams vs. single dictionary.
  928. # since the parameter format is needed in order to determine
  929. # a cache key, we need to determine this up front.
  930. arg = args[0]
  931. if kwargs:
  932. raise exc.ArgumentError(
  933. "Can't pass positional and kwargs to values() "
  934. "simultaneously"
  935. )
  936. elif len(args) > 1:
  937. raise exc.ArgumentError(
  938. "Only a single dictionary/tuple or list of "
  939. "dictionaries/tuples is accepted positionally."
  940. )
  941. elif isinstance(arg, collections_abc.Sequence):
  942. if arg and isinstance(arg[0], dict):
  943. multi_kv_generator = DMLState.get_plugin_class(
  944. self
  945. )._get_multi_crud_kv_pairs
  946. self._multi_values += (multi_kv_generator(self, arg),)
  947. return self
  948. if arg and isinstance(arg[0], (list, tuple)):
  949. self._multi_values += (arg,)
  950. return self
  951. if TYPE_CHECKING:
  952. # crud.py raises during compilation if this is not the
  953. # case
  954. assert isinstance(self, Insert)
  955. # tuple values
  956. arg = {c.key: value for c, value in zip(self.table.c, arg)}
  957. else:
  958. # kwarg path. this is the most common path for non-multi-params
  959. # so this is fairly quick.
  960. arg = cast("Dict[_DMLColumnArgument, Any]", kwargs)
  961. if args:
  962. raise exc.ArgumentError(
  963. "Only a single dictionary/tuple or list of "
  964. "dictionaries/tuples is accepted positionally."
  965. )
  966. # for top level values(), convert literals to anonymous bound
  967. # parameters at statement construction time, so that these values can
  968. # participate in the cache key process like any other ClauseElement.
  969. # crud.py now intercepts bound parameters with unique=True from here
  970. # and ensures they get the "crud"-style name when rendered.
  971. kv_generator = DMLState.get_plugin_class(self)._get_crud_kv_pairs
  972. coerced_arg = dict(kv_generator(self, arg.items(), True))
  973. if self._values:
  974. self._values = self._values.union(coerced_arg)
  975. else:
  976. self._values = util.immutabledict(coerced_arg)
  977. return self
  978. class Insert(ValuesBase):
  979. """Represent an INSERT construct.
  980. The :class:`_expression.Insert` object is created using the
  981. :func:`_expression.insert()` function.
  982. """
  983. __visit_name__ = "insert"
  984. _supports_multi_parameters = True
  985. select = None
  986. include_insert_from_select_defaults = False
  987. _sort_by_parameter_order: bool = False
  988. is_insert = True
  989. table: TableClause
  990. _traverse_internals = (
  991. [
  992. ("table", InternalTraversal.dp_clauseelement),
  993. ("_inline", InternalTraversal.dp_boolean),
  994. ("_select_names", InternalTraversal.dp_string_list),
  995. ("_values", InternalTraversal.dp_dml_values),
  996. ("_multi_values", InternalTraversal.dp_dml_multi_values),
  997. ("select", InternalTraversal.dp_clauseelement),
  998. ("_post_values_clause", InternalTraversal.dp_clauseelement),
  999. ("_returning", InternalTraversal.dp_clauseelement_tuple),
  1000. ("_hints", InternalTraversal.dp_table_hint_list),
  1001. ("_return_defaults", InternalTraversal.dp_boolean),
  1002. (
  1003. "_return_defaults_columns",
  1004. InternalTraversal.dp_clauseelement_tuple,
  1005. ),
  1006. ("_sort_by_parameter_order", InternalTraversal.dp_boolean),
  1007. ]
  1008. + HasPrefixes._has_prefixes_traverse_internals
  1009. + DialectKWArgs._dialect_kwargs_traverse_internals
  1010. + Executable._executable_traverse_internals
  1011. + HasCTE._has_ctes_traverse_internals
  1012. )
  1013. def __init__(self, table: _DMLTableArgument):
  1014. super().__init__(table)
  1015. @_generative
  1016. def inline(self) -> Self:
  1017. """Make this :class:`_expression.Insert` construct "inline" .
  1018. When set, no attempt will be made to retrieve the
  1019. SQL-generated default values to be provided within the statement;
  1020. in particular,
  1021. this allows SQL expressions to be rendered 'inline' within the
  1022. statement without the need to pre-execute them beforehand; for
  1023. backends that support "returning", this turns off the "implicit
  1024. returning" feature for the statement.
  1025. .. versionchanged:: 1.4 the :paramref:`_expression.Insert.inline`
  1026. parameter
  1027. is now superseded by the :meth:`_expression.Insert.inline` method.
  1028. """
  1029. self._inline = True
  1030. return self
  1031. @_generative
  1032. def from_select(
  1033. self,
  1034. names: Sequence[_DMLColumnArgument],
  1035. select: Selectable,
  1036. include_defaults: bool = True,
  1037. ) -> Self:
  1038. """Return a new :class:`_expression.Insert` construct which represents
  1039. an ``INSERT...FROM SELECT`` statement.
  1040. e.g.::
  1041. sel = select(table1.c.a, table1.c.b).where(table1.c.c > 5)
  1042. ins = table2.insert().from_select(["a", "b"], sel)
  1043. :param names: a sequence of string column names or
  1044. :class:`_schema.Column`
  1045. objects representing the target columns.
  1046. :param select: a :func:`_expression.select` construct,
  1047. :class:`_expression.FromClause`
  1048. or other construct which resolves into a
  1049. :class:`_expression.FromClause`,
  1050. such as an ORM :class:`_query.Query` object, etc. The order of
  1051. columns returned from this FROM clause should correspond to the
  1052. order of columns sent as the ``names`` parameter; while this
  1053. is not checked before passing along to the database, the database
  1054. would normally raise an exception if these column lists don't
  1055. correspond.
  1056. :param include_defaults: if True, non-server default values and
  1057. SQL expressions as specified on :class:`_schema.Column` objects
  1058. (as documented in :ref:`metadata_defaults_toplevel`) not
  1059. otherwise specified in the list of names will be rendered
  1060. into the INSERT and SELECT statements, so that these values are also
  1061. included in the data to be inserted.
  1062. .. note:: A Python-side default that uses a Python callable function
  1063. will only be invoked **once** for the whole statement, and **not
  1064. per row**.
  1065. """
  1066. if self._values:
  1067. raise exc.InvalidRequestError(
  1068. "This construct already inserts value expressions"
  1069. )
  1070. self._select_names = [
  1071. coercions.expect(roles.DMLColumnRole, name, as_key=True)
  1072. for name in names
  1073. ]
  1074. self._inline = True
  1075. self.include_insert_from_select_defaults = include_defaults
  1076. self.select = coercions.expect(roles.DMLSelectRole, select)
  1077. return self
  1078. if TYPE_CHECKING:
  1079. # START OVERLOADED FUNCTIONS self.returning ReturningInsert 1-8 ", *, sort_by_parameter_order: bool = False" # noqa: E501
  1080. # code within this block is **programmatically,
  1081. # statically generated** by tools/generate_tuple_map_overloads.py
  1082. @overload
  1083. def returning(
  1084. self, __ent0: _TCCA[_T0], *, sort_by_parameter_order: bool = False
  1085. ) -> ReturningInsert[Tuple[_T0]]: ...
  1086. @overload
  1087. def returning(
  1088. self,
  1089. __ent0: _TCCA[_T0],
  1090. __ent1: _TCCA[_T1],
  1091. *,
  1092. sort_by_parameter_order: bool = False,
  1093. ) -> ReturningInsert[Tuple[_T0, _T1]]: ...
  1094. @overload
  1095. def returning(
  1096. self,
  1097. __ent0: _TCCA[_T0],
  1098. __ent1: _TCCA[_T1],
  1099. __ent2: _TCCA[_T2],
  1100. *,
  1101. sort_by_parameter_order: bool = False,
  1102. ) -> ReturningInsert[Tuple[_T0, _T1, _T2]]: ...
  1103. @overload
  1104. def returning(
  1105. self,
  1106. __ent0: _TCCA[_T0],
  1107. __ent1: _TCCA[_T1],
  1108. __ent2: _TCCA[_T2],
  1109. __ent3: _TCCA[_T3],
  1110. *,
  1111. sort_by_parameter_order: bool = False,
  1112. ) -> ReturningInsert[Tuple[_T0, _T1, _T2, _T3]]: ...
  1113. @overload
  1114. def returning(
  1115. self,
  1116. __ent0: _TCCA[_T0],
  1117. __ent1: _TCCA[_T1],
  1118. __ent2: _TCCA[_T2],
  1119. __ent3: _TCCA[_T3],
  1120. __ent4: _TCCA[_T4],
  1121. *,
  1122. sort_by_parameter_order: bool = False,
  1123. ) -> ReturningInsert[Tuple[_T0, _T1, _T2, _T3, _T4]]: ...
  1124. @overload
  1125. def returning(
  1126. self,
  1127. __ent0: _TCCA[_T0],
  1128. __ent1: _TCCA[_T1],
  1129. __ent2: _TCCA[_T2],
  1130. __ent3: _TCCA[_T3],
  1131. __ent4: _TCCA[_T4],
  1132. __ent5: _TCCA[_T5],
  1133. *,
  1134. sort_by_parameter_order: bool = False,
  1135. ) -> ReturningInsert[Tuple[_T0, _T1, _T2, _T3, _T4, _T5]]: ...
  1136. @overload
  1137. def returning(
  1138. self,
  1139. __ent0: _TCCA[_T0],
  1140. __ent1: _TCCA[_T1],
  1141. __ent2: _TCCA[_T2],
  1142. __ent3: _TCCA[_T3],
  1143. __ent4: _TCCA[_T4],
  1144. __ent5: _TCCA[_T5],
  1145. __ent6: _TCCA[_T6],
  1146. *,
  1147. sort_by_parameter_order: bool = False,
  1148. ) -> ReturningInsert[Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6]]: ...
  1149. @overload
  1150. def returning(
  1151. self,
  1152. __ent0: _TCCA[_T0],
  1153. __ent1: _TCCA[_T1],
  1154. __ent2: _TCCA[_T2],
  1155. __ent3: _TCCA[_T3],
  1156. __ent4: _TCCA[_T4],
  1157. __ent5: _TCCA[_T5],
  1158. __ent6: _TCCA[_T6],
  1159. __ent7: _TCCA[_T7],
  1160. *,
  1161. sort_by_parameter_order: bool = False,
  1162. ) -> ReturningInsert[
  1163. Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7]
  1164. ]: ...
  1165. # END OVERLOADED FUNCTIONS self.returning
  1166. @overload
  1167. def returning(
  1168. self,
  1169. *cols: _ColumnsClauseArgument[Any],
  1170. sort_by_parameter_order: bool = False,
  1171. **__kw: Any,
  1172. ) -> ReturningInsert[Any]: ...
  1173. def returning(
  1174. self,
  1175. *cols: _ColumnsClauseArgument[Any],
  1176. sort_by_parameter_order: bool = False,
  1177. **__kw: Any,
  1178. ) -> ReturningInsert[Any]: ...
  1179. class ReturningInsert(Insert, TypedReturnsRows[_TP]):
  1180. """Typing-only class that establishes a generic type form of
  1181. :class:`.Insert` which tracks returned column types.
  1182. This datatype is delivered when calling the
  1183. :meth:`.Insert.returning` method.
  1184. .. versionadded:: 2.0
  1185. """
  1186. class DMLWhereBase:
  1187. table: _DMLTableElement
  1188. _where_criteria: Tuple[ColumnElement[Any], ...] = ()
  1189. @_generative
  1190. def where(self, *whereclause: _ColumnExpressionArgument[bool]) -> Self:
  1191. """Return a new construct with the given expression(s) added to
  1192. its WHERE clause, joined to the existing clause via AND, if any.
  1193. Both :meth:`_dml.Update.where` and :meth:`_dml.Delete.where`
  1194. support multiple-table forms, including database-specific
  1195. ``UPDATE...FROM`` as well as ``DELETE..USING``. For backends that
  1196. don't have multiple-table support, a backend agnostic approach
  1197. to using multiple tables is to make use of correlated subqueries.
  1198. See the linked tutorial sections below for examples.
  1199. .. seealso::
  1200. :ref:`tutorial_correlated_updates`
  1201. :ref:`tutorial_update_from`
  1202. :ref:`tutorial_multi_table_deletes`
  1203. """
  1204. for criterion in whereclause:
  1205. where_criteria: ColumnElement[Any] = coercions.expect(
  1206. roles.WhereHavingRole, criterion, apply_propagate_attrs=self
  1207. )
  1208. self._where_criteria += (where_criteria,)
  1209. return self
  1210. def filter(self, *criteria: roles.ExpressionElementRole[Any]) -> Self:
  1211. """A synonym for the :meth:`_dml.DMLWhereBase.where` method.
  1212. .. versionadded:: 1.4
  1213. """
  1214. return self.where(*criteria)
  1215. def _filter_by_zero(self) -> _DMLTableElement:
  1216. return self.table
  1217. def filter_by(self, **kwargs: Any) -> Self:
  1218. r"""apply the given filtering criterion as a WHERE clause
  1219. to this select.
  1220. """
  1221. from_entity = self._filter_by_zero()
  1222. clauses = [
  1223. _entity_namespace_key(from_entity, key) == value
  1224. for key, value in kwargs.items()
  1225. ]
  1226. return self.filter(*clauses)
  1227. @property
  1228. def whereclause(self) -> Optional[ColumnElement[Any]]:
  1229. """Return the completed WHERE clause for this :class:`.DMLWhereBase`
  1230. statement.
  1231. This assembles the current collection of WHERE criteria
  1232. into a single :class:`_expression.BooleanClauseList` construct.
  1233. .. versionadded:: 1.4
  1234. """
  1235. return BooleanClauseList._construct_for_whereclause(
  1236. self._where_criteria
  1237. )
  1238. class Update(DMLWhereBase, ValuesBase):
  1239. """Represent an Update construct.
  1240. The :class:`_expression.Update` object is created using the
  1241. :func:`_expression.update()` function.
  1242. """
  1243. __visit_name__ = "update"
  1244. is_update = True
  1245. _traverse_internals = (
  1246. [
  1247. ("table", InternalTraversal.dp_clauseelement),
  1248. ("_where_criteria", InternalTraversal.dp_clauseelement_tuple),
  1249. ("_inline", InternalTraversal.dp_boolean),
  1250. ("_ordered_values", InternalTraversal.dp_dml_ordered_values),
  1251. ("_values", InternalTraversal.dp_dml_values),
  1252. ("_returning", InternalTraversal.dp_clauseelement_tuple),
  1253. ("_hints", InternalTraversal.dp_table_hint_list),
  1254. ("_return_defaults", InternalTraversal.dp_boolean),
  1255. (
  1256. "_return_defaults_columns",
  1257. InternalTraversal.dp_clauseelement_tuple,
  1258. ),
  1259. ]
  1260. + HasPrefixes._has_prefixes_traverse_internals
  1261. + DialectKWArgs._dialect_kwargs_traverse_internals
  1262. + Executable._executable_traverse_internals
  1263. + HasCTE._has_ctes_traverse_internals
  1264. )
  1265. def __init__(self, table: _DMLTableArgument):
  1266. super().__init__(table)
  1267. @_generative
  1268. def ordered_values(self, *args: Tuple[_DMLColumnArgument, Any]) -> Self:
  1269. """Specify the VALUES clause of this UPDATE statement with an explicit
  1270. parameter ordering that will be maintained in the SET clause of the
  1271. resulting UPDATE statement.
  1272. E.g.::
  1273. stmt = table.update().ordered_values(("name", "ed"), ("ident", "foo"))
  1274. .. seealso::
  1275. :ref:`tutorial_parameter_ordered_updates` - full example of the
  1276. :meth:`_expression.Update.ordered_values` method.
  1277. .. versionchanged:: 1.4 The :meth:`_expression.Update.ordered_values`
  1278. method
  1279. supersedes the
  1280. :paramref:`_expression.update.preserve_parameter_order`
  1281. parameter, which will be removed in SQLAlchemy 2.0.
  1282. """ # noqa: E501
  1283. if self._values:
  1284. raise exc.ArgumentError(
  1285. "This statement already has values present"
  1286. )
  1287. elif self._ordered_values:
  1288. raise exc.ArgumentError(
  1289. "This statement already has ordered values present"
  1290. )
  1291. kv_generator = DMLState.get_plugin_class(self)._get_crud_kv_pairs
  1292. self._ordered_values = kv_generator(self, args, True)
  1293. return self
  1294. @_generative
  1295. def inline(self) -> Self:
  1296. """Make this :class:`_expression.Update` construct "inline" .
  1297. When set, SQL defaults present on :class:`_schema.Column`
  1298. objects via the
  1299. ``default`` keyword will be compiled 'inline' into the statement and
  1300. not pre-executed. This means that their values will not be available
  1301. in the dictionary returned from
  1302. :meth:`_engine.CursorResult.last_updated_params`.
  1303. .. versionchanged:: 1.4 the :paramref:`_expression.update.inline`
  1304. parameter
  1305. is now superseded by the :meth:`_expression.Update.inline` method.
  1306. """
  1307. self._inline = True
  1308. return self
  1309. if TYPE_CHECKING:
  1310. # START OVERLOADED FUNCTIONS self.returning ReturningUpdate 1-8
  1311. # code within this block is **programmatically,
  1312. # statically generated** by tools/generate_tuple_map_overloads.py
  1313. @overload
  1314. def returning(
  1315. self, __ent0: _TCCA[_T0]
  1316. ) -> ReturningUpdate[Tuple[_T0]]: ...
  1317. @overload
  1318. def returning(
  1319. self, __ent0: _TCCA[_T0], __ent1: _TCCA[_T1]
  1320. ) -> ReturningUpdate[Tuple[_T0, _T1]]: ...
  1321. @overload
  1322. def returning(
  1323. self, __ent0: _TCCA[_T0], __ent1: _TCCA[_T1], __ent2: _TCCA[_T2]
  1324. ) -> ReturningUpdate[Tuple[_T0, _T1, _T2]]: ...
  1325. @overload
  1326. def returning(
  1327. self,
  1328. __ent0: _TCCA[_T0],
  1329. __ent1: _TCCA[_T1],
  1330. __ent2: _TCCA[_T2],
  1331. __ent3: _TCCA[_T3],
  1332. ) -> ReturningUpdate[Tuple[_T0, _T1, _T2, _T3]]: ...
  1333. @overload
  1334. def returning(
  1335. self,
  1336. __ent0: _TCCA[_T0],
  1337. __ent1: _TCCA[_T1],
  1338. __ent2: _TCCA[_T2],
  1339. __ent3: _TCCA[_T3],
  1340. __ent4: _TCCA[_T4],
  1341. ) -> ReturningUpdate[Tuple[_T0, _T1, _T2, _T3, _T4]]: ...
  1342. @overload
  1343. def returning(
  1344. self,
  1345. __ent0: _TCCA[_T0],
  1346. __ent1: _TCCA[_T1],
  1347. __ent2: _TCCA[_T2],
  1348. __ent3: _TCCA[_T3],
  1349. __ent4: _TCCA[_T4],
  1350. __ent5: _TCCA[_T5],
  1351. ) -> ReturningUpdate[Tuple[_T0, _T1, _T2, _T3, _T4, _T5]]: ...
  1352. @overload
  1353. def returning(
  1354. self,
  1355. __ent0: _TCCA[_T0],
  1356. __ent1: _TCCA[_T1],
  1357. __ent2: _TCCA[_T2],
  1358. __ent3: _TCCA[_T3],
  1359. __ent4: _TCCA[_T4],
  1360. __ent5: _TCCA[_T5],
  1361. __ent6: _TCCA[_T6],
  1362. ) -> ReturningUpdate[Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6]]: ...
  1363. @overload
  1364. def returning(
  1365. self,
  1366. __ent0: _TCCA[_T0],
  1367. __ent1: _TCCA[_T1],
  1368. __ent2: _TCCA[_T2],
  1369. __ent3: _TCCA[_T3],
  1370. __ent4: _TCCA[_T4],
  1371. __ent5: _TCCA[_T5],
  1372. __ent6: _TCCA[_T6],
  1373. __ent7: _TCCA[_T7],
  1374. ) -> ReturningUpdate[
  1375. Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7]
  1376. ]: ...
  1377. # END OVERLOADED FUNCTIONS self.returning
  1378. @overload
  1379. def returning(
  1380. self, *cols: _ColumnsClauseArgument[Any], **__kw: Any
  1381. ) -> ReturningUpdate[Any]: ...
  1382. def returning(
  1383. self, *cols: _ColumnsClauseArgument[Any], **__kw: Any
  1384. ) -> ReturningUpdate[Any]: ...
  1385. class ReturningUpdate(Update, TypedReturnsRows[_TP]):
  1386. """Typing-only class that establishes a generic type form of
  1387. :class:`.Update` which tracks returned column types.
  1388. This datatype is delivered when calling the
  1389. :meth:`.Update.returning` method.
  1390. .. versionadded:: 2.0
  1391. """
  1392. class Delete(DMLWhereBase, UpdateBase):
  1393. """Represent a DELETE construct.
  1394. The :class:`_expression.Delete` object is created using the
  1395. :func:`_expression.delete()` function.
  1396. """
  1397. __visit_name__ = "delete"
  1398. is_delete = True
  1399. _traverse_internals = (
  1400. [
  1401. ("table", InternalTraversal.dp_clauseelement),
  1402. ("_where_criteria", InternalTraversal.dp_clauseelement_tuple),
  1403. ("_returning", InternalTraversal.dp_clauseelement_tuple),
  1404. ("_hints", InternalTraversal.dp_table_hint_list),
  1405. ]
  1406. + HasPrefixes._has_prefixes_traverse_internals
  1407. + DialectKWArgs._dialect_kwargs_traverse_internals
  1408. + Executable._executable_traverse_internals
  1409. + HasCTE._has_ctes_traverse_internals
  1410. )
  1411. def __init__(self, table: _DMLTableArgument):
  1412. self.table = coercions.expect(
  1413. roles.DMLTableRole, table, apply_propagate_attrs=self
  1414. )
  1415. if TYPE_CHECKING:
  1416. # START OVERLOADED FUNCTIONS self.returning ReturningDelete 1-8
  1417. # code within this block is **programmatically,
  1418. # statically generated** by tools/generate_tuple_map_overloads.py
  1419. @overload
  1420. def returning(
  1421. self, __ent0: _TCCA[_T0]
  1422. ) -> ReturningDelete[Tuple[_T0]]: ...
  1423. @overload
  1424. def returning(
  1425. self, __ent0: _TCCA[_T0], __ent1: _TCCA[_T1]
  1426. ) -> ReturningDelete[Tuple[_T0, _T1]]: ...
  1427. @overload
  1428. def returning(
  1429. self, __ent0: _TCCA[_T0], __ent1: _TCCA[_T1], __ent2: _TCCA[_T2]
  1430. ) -> ReturningDelete[Tuple[_T0, _T1, _T2]]: ...
  1431. @overload
  1432. def returning(
  1433. self,
  1434. __ent0: _TCCA[_T0],
  1435. __ent1: _TCCA[_T1],
  1436. __ent2: _TCCA[_T2],
  1437. __ent3: _TCCA[_T3],
  1438. ) -> ReturningDelete[Tuple[_T0, _T1, _T2, _T3]]: ...
  1439. @overload
  1440. def returning(
  1441. self,
  1442. __ent0: _TCCA[_T0],
  1443. __ent1: _TCCA[_T1],
  1444. __ent2: _TCCA[_T2],
  1445. __ent3: _TCCA[_T3],
  1446. __ent4: _TCCA[_T4],
  1447. ) -> ReturningDelete[Tuple[_T0, _T1, _T2, _T3, _T4]]: ...
  1448. @overload
  1449. def returning(
  1450. self,
  1451. __ent0: _TCCA[_T0],
  1452. __ent1: _TCCA[_T1],
  1453. __ent2: _TCCA[_T2],
  1454. __ent3: _TCCA[_T3],
  1455. __ent4: _TCCA[_T4],
  1456. __ent5: _TCCA[_T5],
  1457. ) -> ReturningDelete[Tuple[_T0, _T1, _T2, _T3, _T4, _T5]]: ...
  1458. @overload
  1459. def returning(
  1460. self,
  1461. __ent0: _TCCA[_T0],
  1462. __ent1: _TCCA[_T1],
  1463. __ent2: _TCCA[_T2],
  1464. __ent3: _TCCA[_T3],
  1465. __ent4: _TCCA[_T4],
  1466. __ent5: _TCCA[_T5],
  1467. __ent6: _TCCA[_T6],
  1468. ) -> ReturningDelete[Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6]]: ...
  1469. @overload
  1470. def returning(
  1471. self,
  1472. __ent0: _TCCA[_T0],
  1473. __ent1: _TCCA[_T1],
  1474. __ent2: _TCCA[_T2],
  1475. __ent3: _TCCA[_T3],
  1476. __ent4: _TCCA[_T4],
  1477. __ent5: _TCCA[_T5],
  1478. __ent6: _TCCA[_T6],
  1479. __ent7: _TCCA[_T7],
  1480. ) -> ReturningDelete[
  1481. Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7]
  1482. ]: ...
  1483. # END OVERLOADED FUNCTIONS self.returning
  1484. @overload
  1485. def returning(
  1486. self, *cols: _ColumnsClauseArgument[Any], **__kw: Any
  1487. ) -> ReturningDelete[Any]: ...
  1488. def returning(
  1489. self, *cols: _ColumnsClauseArgument[Any], **__kw: Any
  1490. ) -> ReturningDelete[Any]: ...
  1491. class ReturningDelete(Update, TypedReturnsRows[_TP]):
  1492. """Typing-only class that establishes a generic type form of
  1493. :class:`.Delete` which tracks returned column types.
  1494. This datatype is delivered when calling the
  1495. :meth:`.Delete.returning` method.
  1496. .. versionadded:: 2.0
  1497. """