dml.py 64 KB

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