dml.py 65 KB

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