crud.py 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. # sql/crud.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: allow-untyped-defs, allow-untyped-calls
  8. """Functions used by compiler.py to determine the parameters rendered
  9. within INSERT and UPDATE statements.
  10. """
  11. from __future__ import annotations
  12. import functools
  13. import operator
  14. from typing import Any
  15. from typing import Callable
  16. from typing import cast
  17. from typing import Dict
  18. from typing import Iterable
  19. from typing import List
  20. from typing import MutableMapping
  21. from typing import NamedTuple
  22. from typing import Optional
  23. from typing import overload
  24. from typing import Sequence
  25. from typing import Set
  26. from typing import Tuple
  27. from typing import TYPE_CHECKING
  28. from typing import Union
  29. from . import coercions
  30. from . import dml
  31. from . import elements
  32. from . import roles
  33. from .base import _DefaultDescriptionTuple
  34. from .dml import isinsert as _compile_state_isinsert
  35. from .elements import ColumnClause
  36. from .schema import default_is_clause_element
  37. from .schema import default_is_sequence
  38. from .selectable import Select
  39. from .selectable import TableClause
  40. from .. import exc
  41. from .. import util
  42. from ..util.typing import Literal
  43. if TYPE_CHECKING:
  44. from .compiler import _BindNameForColProtocol
  45. from .compiler import SQLCompiler
  46. from .dml import _DMLColumnElement
  47. from .dml import DMLState
  48. from .dml import ValuesBase
  49. from .elements import ColumnElement
  50. from .elements import KeyedColumnElement
  51. from .schema import _SQLExprDefault
  52. from .schema import Column
  53. REQUIRED = util.symbol(
  54. "REQUIRED",
  55. """
  56. Placeholder for the value within a :class:`.BindParameter`
  57. which is required to be present when the statement is passed
  58. to :meth:`_engine.Connection.execute`.
  59. This symbol is typically used when a :func:`_expression.insert`
  60. or :func:`_expression.update` statement is compiled without parameter
  61. values present.
  62. """,
  63. )
  64. def _as_dml_column(c: ColumnElement[Any]) -> ColumnClause[Any]:
  65. if not isinstance(c, ColumnClause):
  66. raise exc.CompileError(
  67. f"Can't create DML statement against column expression {c!r}"
  68. )
  69. return c
  70. _CrudParamElement = Tuple[
  71. "ColumnElement[Any]",
  72. str, # column name
  73. Optional[
  74. Union[str, "_SQLExprDefault"]
  75. ], # bound parameter string or SQL expression to apply
  76. Iterable[str],
  77. ]
  78. _CrudParamElementStr = Tuple[
  79. "KeyedColumnElement[Any]",
  80. str, # column name
  81. str, # bound parameter string
  82. Iterable[str],
  83. ]
  84. _CrudParamElementSQLExpr = Tuple[
  85. "ColumnClause[Any]",
  86. str,
  87. "_SQLExprDefault", # SQL expression to apply
  88. Iterable[str],
  89. ]
  90. _CrudParamSequence = List[_CrudParamElement]
  91. class _CrudParams(NamedTuple):
  92. single_params: _CrudParamSequence
  93. all_multi_params: List[Sequence[_CrudParamElementStr]]
  94. is_default_metavalue_only: bool = False
  95. use_insertmanyvalues: bool = False
  96. use_sentinel_columns: Optional[Sequence[Column[Any]]] = None
  97. def _get_crud_params(
  98. compiler: SQLCompiler,
  99. stmt: ValuesBase,
  100. compile_state: DMLState,
  101. toplevel: bool,
  102. **kw: Any,
  103. ) -> _CrudParams:
  104. """create a set of tuples representing column/string pairs for use
  105. in an INSERT or UPDATE statement.
  106. Also generates the Compiled object's postfetch, prefetch, and
  107. returning column collections, used for default handling and ultimately
  108. populating the CursorResult's prefetch_cols() and postfetch_cols()
  109. collections.
  110. """
  111. # note: the _get_crud_params() system was written with the notion in mind
  112. # that INSERT, UPDATE, DELETE are always the top level statement and
  113. # that there is only one of them. With the addition of CTEs that can
  114. # make use of DML, this assumption is no longer accurate; the DML
  115. # statement is not necessarily the top-level "row returning" thing
  116. # and it is also theoretically possible (fortunately nobody has asked yet)
  117. # to have a single statement with multiple DMLs inside of it via CTEs.
  118. # the current _get_crud_params() design doesn't accommodate these cases
  119. # right now. It "just works" for a CTE that has a single DML inside of
  120. # it, and for a CTE with multiple DML, it's not clear what would happen.
  121. # overall, the "compiler.XYZ" collections here would need to be in a
  122. # per-DML structure of some kind, and DefaultDialect would need to
  123. # navigate these collections on a per-statement basis, with additional
  124. # emphasis on the "toplevel returning data" statement. However we
  125. # still need to run through _get_crud_params() for all DML as we have
  126. # Python / SQL generated column defaults that need to be rendered.
  127. # if there is user need for this kind of thing, it's likely a post 2.0
  128. # kind of change as it would require deep changes to DefaultDialect
  129. # as well as here.
  130. compiler.postfetch = []
  131. compiler.insert_prefetch = []
  132. compiler.update_prefetch = []
  133. compiler.implicit_returning = []
  134. visiting_cte = kw.get("visiting_cte", None)
  135. if visiting_cte is not None:
  136. # for insert -> CTE -> insert, don't populate an incoming
  137. # _crud_accumulate_bind_names collection; the INSERT we process here
  138. # will not be inline within the VALUES of the enclosing INSERT as the
  139. # CTE is placed on the outside. See issue #9173
  140. kw.pop("accumulate_bind_names", None)
  141. assert (
  142. "accumulate_bind_names" not in kw
  143. ), "Don't know how to handle insert within insert without a CTE"
  144. # getters - these are normally just column.key,
  145. # but in the case of mysql multi-table update, the rules for
  146. # .key must conditionally take tablename into account
  147. (
  148. _column_as_key,
  149. _getattr_col_key,
  150. _col_bind_name,
  151. ) = _key_getters_for_crud_column(compiler, stmt, compile_state)
  152. compiler._get_bind_name_for_col = _col_bind_name
  153. if stmt._returning and stmt._return_defaults:
  154. raise exc.CompileError(
  155. "Can't compile statement that includes returning() and "
  156. "return_defaults() simultaneously"
  157. )
  158. if compile_state.isdelete:
  159. _setup_delete_return_defaults(
  160. compiler,
  161. stmt,
  162. compile_state,
  163. (),
  164. _getattr_col_key,
  165. _column_as_key,
  166. _col_bind_name,
  167. (),
  168. (),
  169. toplevel,
  170. kw,
  171. )
  172. return _CrudParams([], [])
  173. # no parameters in the statement, no parameters in the
  174. # compiled params - return binds for all columns
  175. if compiler.column_keys is None and compile_state._no_parameters:
  176. return _CrudParams(
  177. [
  178. (
  179. c,
  180. compiler.preparer.format_column(c),
  181. _create_bind_param(compiler, c, None, required=True),
  182. (c.key,),
  183. )
  184. for c in stmt.table.columns
  185. if not c._omit_from_statements
  186. ],
  187. [],
  188. )
  189. stmt_parameter_tuples: Optional[
  190. List[Tuple[Union[str, ColumnClause[Any]], Any]]
  191. ]
  192. spd: Optional[MutableMapping[_DMLColumnElement, Any]]
  193. if (
  194. _compile_state_isinsert(compile_state)
  195. and compile_state._has_multi_parameters
  196. ):
  197. mp = compile_state._multi_parameters
  198. assert mp is not None
  199. spd = mp[0]
  200. stmt_parameter_tuples = list(spd.items())
  201. spd_str_key = {_column_as_key(key) for key in spd}
  202. elif compile_state._ordered_values:
  203. spd = compile_state._dict_parameters
  204. stmt_parameter_tuples = compile_state._ordered_values
  205. assert spd is not None
  206. spd_str_key = {_column_as_key(key) for key in spd}
  207. elif compile_state._dict_parameters:
  208. spd = compile_state._dict_parameters
  209. stmt_parameter_tuples = list(spd.items())
  210. spd_str_key = {_column_as_key(key) for key in spd}
  211. else:
  212. stmt_parameter_tuples = spd_str_key = None
  213. # if we have statement parameters - set defaults in the
  214. # compiled params
  215. if compiler.column_keys is None:
  216. parameters = {}
  217. elif stmt_parameter_tuples:
  218. assert spd_str_key is not None
  219. parameters = {
  220. _column_as_key(key): REQUIRED
  221. for key in compiler.column_keys
  222. if key not in spd_str_key
  223. }
  224. else:
  225. parameters = {
  226. _column_as_key(key): REQUIRED for key in compiler.column_keys
  227. }
  228. # create a list of column assignment clauses as tuples
  229. values: List[_CrudParamElement] = []
  230. if stmt_parameter_tuples is not None:
  231. _get_stmt_parameter_tuples_params(
  232. compiler,
  233. compile_state,
  234. parameters,
  235. stmt_parameter_tuples,
  236. _column_as_key,
  237. values,
  238. kw,
  239. )
  240. check_columns: Dict[str, ColumnClause[Any]] = {}
  241. # special logic that only occurs for multi-table UPDATE
  242. # statements
  243. if dml.isupdate(compile_state) and compile_state.is_multitable:
  244. _get_update_multitable_params(
  245. compiler,
  246. stmt,
  247. compile_state,
  248. stmt_parameter_tuples,
  249. check_columns,
  250. _col_bind_name,
  251. _getattr_col_key,
  252. values,
  253. kw,
  254. )
  255. if _compile_state_isinsert(compile_state) and stmt._select_names:
  256. # is an insert from select, is not a multiparams
  257. assert not compile_state._has_multi_parameters
  258. _scan_insert_from_select_cols(
  259. compiler,
  260. stmt,
  261. compile_state,
  262. parameters,
  263. _getattr_col_key,
  264. _column_as_key,
  265. _col_bind_name,
  266. check_columns,
  267. values,
  268. toplevel,
  269. kw,
  270. )
  271. use_insertmanyvalues = False
  272. use_sentinel_columns = None
  273. else:
  274. use_insertmanyvalues, use_sentinel_columns = _scan_cols(
  275. compiler,
  276. stmt,
  277. compile_state,
  278. parameters,
  279. _getattr_col_key,
  280. _column_as_key,
  281. _col_bind_name,
  282. check_columns,
  283. values,
  284. toplevel,
  285. kw,
  286. )
  287. if parameters and stmt_parameter_tuples:
  288. check = (
  289. set(parameters)
  290. .intersection(_column_as_key(k) for k, v in stmt_parameter_tuples)
  291. .difference(check_columns)
  292. )
  293. if check:
  294. if dml.isupdate(compile_state):
  295. tables_mentioned = set(
  296. c.table
  297. for c, v in stmt_parameter_tuples
  298. if isinstance(c, ColumnClause) and c.table is not None
  299. ).difference([compile_state.dml_table])
  300. multi_not_in_from = tables_mentioned.difference(
  301. compile_state._extra_froms
  302. )
  303. if tables_mentioned and (
  304. not compile_state.is_multitable
  305. or not compiler.render_table_with_column_in_update_from
  306. ):
  307. if not compiler.render_table_with_column_in_update_from:
  308. preamble = (
  309. "Backend does not support additional "
  310. "tables in the SET clause"
  311. )
  312. else:
  313. preamble = (
  314. "Statement is not a multi-table UPDATE statement"
  315. )
  316. raise exc.CompileError(
  317. f"{preamble}; cannot "
  318. f"""include columns from table(s) {
  319. ", ".join(f"'{t.description}'"
  320. for t in tables_mentioned)
  321. } in SET clause"""
  322. )
  323. elif multi_not_in_from:
  324. assert compiler.render_table_with_column_in_update_from
  325. raise exc.CompileError(
  326. f"Multi-table UPDATE statement does not include "
  327. "table(s) "
  328. f"""{
  329. ", ".join(
  330. f"'{t.description}'" for
  331. t in multi_not_in_from)
  332. }"""
  333. )
  334. raise exc.CompileError(
  335. "Unconsumed column names: %s"
  336. % (", ".join("%s" % (c,) for c in check))
  337. )
  338. is_default_metavalue_only = False
  339. if (
  340. _compile_state_isinsert(compile_state)
  341. and compile_state._has_multi_parameters
  342. ):
  343. # is a multiparams, is not an insert from a select
  344. assert not stmt._select_names
  345. multi_extended_values = _extend_values_for_multiparams(
  346. compiler,
  347. stmt,
  348. compile_state,
  349. cast(
  350. "Sequence[_CrudParamElementStr]",
  351. values,
  352. ),
  353. cast("Callable[..., str]", _column_as_key),
  354. kw,
  355. )
  356. return _CrudParams(values, multi_extended_values)
  357. elif (
  358. not values
  359. and compiler.for_executemany
  360. and compiler.dialect.supports_default_metavalue
  361. ):
  362. # convert an "INSERT DEFAULT VALUES"
  363. # into INSERT (firstcol) VALUES (DEFAULT) which can be turned
  364. # into an in-place multi values. This supports
  365. # insert_executemany_returning mode :)
  366. values = [
  367. (
  368. _as_dml_column(stmt.table.columns[0]),
  369. compiler.preparer.format_column(stmt.table.columns[0]),
  370. compiler.dialect.default_metavalue_token,
  371. (),
  372. )
  373. ]
  374. is_default_metavalue_only = True
  375. return _CrudParams(
  376. values,
  377. [],
  378. is_default_metavalue_only=is_default_metavalue_only,
  379. use_insertmanyvalues=use_insertmanyvalues,
  380. use_sentinel_columns=use_sentinel_columns,
  381. )
  382. @overload
  383. def _create_bind_param(
  384. compiler: SQLCompiler,
  385. col: ColumnElement[Any],
  386. value: Any,
  387. process: Literal[True] = ...,
  388. required: bool = False,
  389. name: Optional[str] = None,
  390. force_anonymous: bool = False,
  391. **kw: Any,
  392. ) -> str: ...
  393. @overload
  394. def _create_bind_param(
  395. compiler: SQLCompiler,
  396. col: ColumnElement[Any],
  397. value: Any,
  398. **kw: Any,
  399. ) -> str: ...
  400. def _create_bind_param(
  401. compiler: SQLCompiler,
  402. col: ColumnElement[Any],
  403. value: Any,
  404. process: bool = True,
  405. required: bool = False,
  406. name: Optional[str] = None,
  407. force_anonymous: bool = False,
  408. **kw: Any,
  409. ) -> Union[str, elements.BindParameter[Any]]:
  410. if force_anonymous:
  411. name = None
  412. elif name is None:
  413. name = col.key
  414. bindparam = elements.BindParameter(
  415. name, value, type_=col.type, required=required
  416. )
  417. bindparam._is_crud = True
  418. if process:
  419. return bindparam._compiler_dispatch(compiler, **kw)
  420. else:
  421. return bindparam
  422. def _handle_values_anonymous_param(compiler, col, value, name, **kw):
  423. # the insert() and update() constructs as of 1.4 will now produce anonymous
  424. # bindparam() objects in the values() collections up front when given plain
  425. # literal values. This is so that cache key behaviors, which need to
  426. # produce bound parameters in deterministic order without invoking any
  427. # compilation here, can be applied to these constructs when they include
  428. # values() (but not yet multi-values, which are not included in caching
  429. # right now).
  430. #
  431. # in order to produce the desired "crud" style name for these parameters,
  432. # which will also be targetable in engine/default.py through the usual
  433. # conventions, apply our desired name to these unique parameters by
  434. # populating the compiler truncated names cache with the desired name,
  435. # rather than having
  436. # compiler.visit_bindparam()->compiler._truncated_identifier make up a
  437. # name. Saves on call counts also.
  438. # for INSERT/UPDATE that's a CTE, we don't need names to match to
  439. # external parameters and these would also conflict in the case where
  440. # multiple insert/update are combined together using CTEs
  441. is_cte = "visiting_cte" in kw
  442. if (
  443. not is_cte
  444. and value.unique
  445. and isinstance(value.key, elements._truncated_label)
  446. ):
  447. compiler.truncated_names[("bindparam", value.key)] = name
  448. if value.type._isnull:
  449. # either unique parameter, or other bound parameters that were
  450. # passed in directly
  451. # set type to that of the column unconditionally
  452. value = value._with_binary_element_type(col.type)
  453. return value._compiler_dispatch(compiler, **kw)
  454. def _key_getters_for_crud_column(
  455. compiler: SQLCompiler, stmt: ValuesBase, compile_state: DMLState
  456. ) -> Tuple[
  457. Callable[[Union[str, ColumnClause[Any]]], Union[str, Tuple[str, str]]],
  458. Callable[[ColumnClause[Any]], Union[str, Tuple[str, str]]],
  459. _BindNameForColProtocol,
  460. ]:
  461. if dml.isupdate(compile_state) and compile_state._extra_froms:
  462. # when extra tables are present, refer to the columns
  463. # in those extra tables as table-qualified, including in
  464. # dictionaries and when rendering bind param names.
  465. # the "main" table of the statement remains unqualified,
  466. # allowing the most compatibility with a non-multi-table
  467. # statement.
  468. _et = set(compile_state._extra_froms)
  469. c_key_role = functools.partial(
  470. coercions.expect_as_key, roles.DMLColumnRole
  471. )
  472. def _column_as_key(
  473. key: Union[ColumnClause[Any], str],
  474. ) -> Union[str, Tuple[str, str]]:
  475. str_key = c_key_role(key)
  476. if hasattr(key, "table") and key.table in _et:
  477. return (key.table.name, str_key) # type: ignore
  478. else:
  479. return str_key
  480. def _getattr_col_key(
  481. col: ColumnClause[Any],
  482. ) -> Union[str, Tuple[str, str]]:
  483. if col.table in _et:
  484. return (col.table.name, col.key) # type: ignore
  485. else:
  486. return col.key
  487. def _col_bind_name(col: ColumnClause[Any]) -> str:
  488. if col.table in _et:
  489. if TYPE_CHECKING:
  490. assert isinstance(col.table, TableClause)
  491. return "%s_%s" % (col.table.name, col.key)
  492. else:
  493. return col.key
  494. else:
  495. _column_as_key = functools.partial(
  496. coercions.expect_as_key, roles.DMLColumnRole
  497. )
  498. _getattr_col_key = _col_bind_name = operator.attrgetter("key") # type: ignore # noqa: E501
  499. return _column_as_key, _getattr_col_key, _col_bind_name
  500. def _scan_insert_from_select_cols(
  501. compiler,
  502. stmt,
  503. compile_state,
  504. parameters,
  505. _getattr_col_key,
  506. _column_as_key,
  507. _col_bind_name,
  508. check_columns,
  509. values,
  510. toplevel,
  511. kw,
  512. ):
  513. cols = [stmt.table.c[_column_as_key(name)] for name in stmt._select_names]
  514. assert compiler.stack[-1]["selectable"] is stmt
  515. compiler.stack[-1]["insert_from_select"] = stmt.select
  516. add_select_cols: List[_CrudParamElementSQLExpr] = []
  517. if stmt.include_insert_from_select_defaults:
  518. col_set = set(cols)
  519. for col in stmt.table.columns:
  520. # omit columns that were not in the SELECT statement.
  521. # this will omit columns marked as omit_from_statements naturally,
  522. # as long as that col was not explicit in the SELECT.
  523. # if an omit_from_statements col has a "default" on it, then
  524. # we need to include it, as these defaults should still fire off.
  525. # but, if it has that default and it's the "sentinel" default,
  526. # we don't do sentinel default operations for insert_from_select
  527. # here so we again omit it.
  528. if (
  529. col not in col_set
  530. and col.default
  531. and not col.default.is_sentinel
  532. ):
  533. cols.append(col)
  534. for c in cols:
  535. col_key = _getattr_col_key(c)
  536. if col_key in parameters and col_key not in check_columns:
  537. parameters.pop(col_key)
  538. values.append((c, compiler.preparer.format_column(c), None, ()))
  539. else:
  540. _append_param_insert_select_hasdefault(
  541. compiler, stmt, c, add_select_cols, kw
  542. )
  543. if add_select_cols:
  544. values.extend(add_select_cols)
  545. ins_from_select = compiler.stack[-1]["insert_from_select"]
  546. if not isinstance(ins_from_select, Select):
  547. raise exc.CompileError(
  548. f"Can't extend statement for INSERT..FROM SELECT to include "
  549. f"additional default-holding column(s) "
  550. f"""{
  551. ', '.join(repr(key) for _, key, _, _ in add_select_cols)
  552. }. Convert the selectable to a subquery() first, or pass """
  553. "include_defaults=False to Insert.from_select() to skip these "
  554. "columns."
  555. )
  556. ins_from_select = ins_from_select._generate()
  557. # copy raw_columns
  558. ins_from_select._raw_columns = list(ins_from_select._raw_columns) + [
  559. expr for _, _, expr, _ in add_select_cols
  560. ]
  561. compiler.stack[-1]["insert_from_select"] = ins_from_select
  562. def _scan_cols(
  563. compiler,
  564. stmt,
  565. compile_state,
  566. parameters,
  567. _getattr_col_key,
  568. _column_as_key,
  569. _col_bind_name,
  570. check_columns,
  571. values,
  572. toplevel,
  573. kw,
  574. ):
  575. (
  576. need_pks,
  577. implicit_returning,
  578. implicit_return_defaults,
  579. postfetch_lastrowid,
  580. use_insertmanyvalues,
  581. use_sentinel_columns,
  582. ) = _get_returning_modifiers(compiler, stmt, compile_state, toplevel)
  583. assert compile_state.isupdate or compile_state.isinsert
  584. if compile_state._parameter_ordering:
  585. parameter_ordering = [
  586. _column_as_key(key) for key in compile_state._parameter_ordering
  587. ]
  588. ordered_keys = set(parameter_ordering)
  589. cols = [
  590. stmt.table.c[key]
  591. for key in parameter_ordering
  592. if isinstance(key, str) and key in stmt.table.c
  593. ] + [c for c in stmt.table.c if c.key not in ordered_keys]
  594. else:
  595. cols = stmt.table.columns
  596. isinsert = _compile_state_isinsert(compile_state)
  597. if isinsert and not compile_state._has_multi_parameters:
  598. # new rules for #7998. fetch lastrowid or implicit returning
  599. # for autoincrement column even if parameter is NULL, for DBs that
  600. # override NULL param for primary key (sqlite, mysql/mariadb)
  601. autoincrement_col = stmt.table._autoincrement_column
  602. insert_null_pk_still_autoincrements = (
  603. compiler.dialect.insert_null_pk_still_autoincrements
  604. )
  605. else:
  606. autoincrement_col = insert_null_pk_still_autoincrements = None
  607. if stmt._supplemental_returning:
  608. supplemental_returning = set(stmt._supplemental_returning)
  609. else:
  610. supplemental_returning = set()
  611. compiler_implicit_returning = compiler.implicit_returning
  612. # TODO - see TODO(return_defaults_columns) below
  613. # cols_in_params = set()
  614. for c in cols:
  615. # scan through every column in the target table
  616. col_key = _getattr_col_key(c)
  617. if col_key in parameters and col_key not in check_columns:
  618. # parameter is present for the column. use that.
  619. _append_param_parameter(
  620. compiler,
  621. stmt,
  622. compile_state,
  623. c,
  624. col_key,
  625. parameters,
  626. _col_bind_name,
  627. implicit_returning,
  628. implicit_return_defaults,
  629. postfetch_lastrowid,
  630. values,
  631. autoincrement_col,
  632. insert_null_pk_still_autoincrements,
  633. kw,
  634. )
  635. # TODO - see TODO(return_defaults_columns) below
  636. # cols_in_params.add(c)
  637. elif isinsert:
  638. # no parameter is present and it's an insert.
  639. if c.primary_key and need_pks:
  640. # it's a primary key column, it will need to be generated by a
  641. # default generator of some kind, and the statement expects
  642. # inserted_primary_key to be available.
  643. if implicit_returning:
  644. # we can use RETURNING, find out how to invoke this
  645. # column and get the value where RETURNING is an option.
  646. # we can inline server-side functions in this case.
  647. _append_param_insert_pk_returning(
  648. compiler, stmt, c, values, kw
  649. )
  650. else:
  651. # otherwise, find out how to invoke this column
  652. # and get its value where RETURNING is not an option.
  653. # if we have to invoke a server-side function, we need
  654. # to pre-execute it. or if this is a straight
  655. # autoincrement column and the dialect supports it
  656. # we can use cursor.lastrowid.
  657. _append_param_insert_pk_no_returning(
  658. compiler, stmt, c, values, kw
  659. )
  660. elif c.default is not None:
  661. # column has a default, but it's not a pk column, or it is but
  662. # we don't need to get the pk back.
  663. if not c.default.is_sentinel or (
  664. use_sentinel_columns is not None
  665. ):
  666. _append_param_insert_hasdefault(
  667. compiler, stmt, c, implicit_return_defaults, values, kw
  668. )
  669. elif c.server_default is not None:
  670. # column has a DDL-level default, and is either not a pk
  671. # column or we don't need the pk.
  672. if implicit_return_defaults and c in implicit_return_defaults:
  673. compiler_implicit_returning.append(c)
  674. elif not c.primary_key:
  675. compiler.postfetch.append(c)
  676. elif implicit_return_defaults and c in implicit_return_defaults:
  677. compiler_implicit_returning.append(c)
  678. elif (
  679. c.primary_key
  680. and c is not stmt.table._autoincrement_column
  681. and not c.nullable
  682. ):
  683. _warn_pk_with_no_anticipated_value(c)
  684. elif compile_state.isupdate:
  685. # no parameter is present and it's an insert.
  686. _append_param_update(
  687. compiler,
  688. compile_state,
  689. stmt,
  690. c,
  691. implicit_return_defaults,
  692. values,
  693. kw,
  694. )
  695. # adding supplemental cols to implicit_returning in table
  696. # order so that order is maintained between multiple INSERT
  697. # statements which may have different parameters included, but all
  698. # have the same RETURNING clause
  699. if (
  700. c in supplemental_returning
  701. and c not in compiler_implicit_returning
  702. ):
  703. compiler_implicit_returning.append(c)
  704. if supplemental_returning:
  705. # we should have gotten every col into implicit_returning,
  706. # however supplemental returning can also have SQL functions etc.
  707. # in it
  708. remaining_supplemental = supplemental_returning.difference(
  709. compiler_implicit_returning
  710. )
  711. compiler_implicit_returning.extend(
  712. c
  713. for c in stmt._supplemental_returning
  714. if c in remaining_supplemental
  715. )
  716. # TODO(return_defaults_columns): there can still be more columns in
  717. # _return_defaults_columns in the case that they are from something like an
  718. # aliased of the table. we can add them here, however this breaks other ORM
  719. # things. so this is for another day. see
  720. # test/orm/dml/test_update_delete_where.py -> test_update_from_alias
  721. # if stmt._return_defaults_columns:
  722. # compiler_implicit_returning.extend(
  723. # set(stmt._return_defaults_columns)
  724. # .difference(compiler_implicit_returning)
  725. # .difference(cols_in_params)
  726. # )
  727. return (use_insertmanyvalues, use_sentinel_columns)
  728. def _setup_delete_return_defaults(
  729. compiler,
  730. stmt,
  731. compile_state,
  732. parameters,
  733. _getattr_col_key,
  734. _column_as_key,
  735. _col_bind_name,
  736. check_columns,
  737. values,
  738. toplevel,
  739. kw,
  740. ):
  741. (_, _, implicit_return_defaults, *_) = _get_returning_modifiers(
  742. compiler, stmt, compile_state, toplevel
  743. )
  744. if not implicit_return_defaults:
  745. return
  746. if stmt._return_defaults_columns:
  747. compiler.implicit_returning.extend(implicit_return_defaults)
  748. if stmt._supplemental_returning:
  749. ir_set = set(compiler.implicit_returning)
  750. compiler.implicit_returning.extend(
  751. c for c in stmt._supplemental_returning if c not in ir_set
  752. )
  753. def _append_param_parameter(
  754. compiler,
  755. stmt,
  756. compile_state,
  757. c,
  758. col_key,
  759. parameters,
  760. _col_bind_name,
  761. implicit_returning,
  762. implicit_return_defaults,
  763. postfetch_lastrowid,
  764. values,
  765. autoincrement_col,
  766. insert_null_pk_still_autoincrements,
  767. kw,
  768. ):
  769. value = parameters.pop(col_key)
  770. has_visiting_cte = kw.get("visiting_cte") is not None
  771. col_value = compiler.preparer.format_column(
  772. c, use_table=compile_state.include_table_with_column_exprs
  773. )
  774. accumulated_bind_names: Set[str] = set()
  775. if coercions._is_literal(value):
  776. if (
  777. insert_null_pk_still_autoincrements
  778. and c.primary_key
  779. and c is autoincrement_col
  780. ):
  781. # support use case for #7998, fetch autoincrement cols
  782. # even if value was given.
  783. if postfetch_lastrowid:
  784. compiler.postfetch_lastrowid = True
  785. elif implicit_returning:
  786. compiler.implicit_returning.append(c)
  787. value = _create_bind_param(
  788. compiler,
  789. c,
  790. value,
  791. required=value is REQUIRED,
  792. name=(
  793. _col_bind_name(c)
  794. if not _compile_state_isinsert(compile_state)
  795. or not compile_state._has_multi_parameters
  796. else "%s_m0" % _col_bind_name(c)
  797. ),
  798. accumulate_bind_names=accumulated_bind_names,
  799. force_anonymous=has_visiting_cte,
  800. **kw,
  801. )
  802. elif value._is_bind_parameter:
  803. if (
  804. insert_null_pk_still_autoincrements
  805. and value.value is None
  806. and c.primary_key
  807. and c is autoincrement_col
  808. ):
  809. # support use case for #7998, fetch autoincrement cols
  810. # even if value was given
  811. if implicit_returning:
  812. compiler.implicit_returning.append(c)
  813. elif compiler.dialect.postfetch_lastrowid:
  814. compiler.postfetch_lastrowid = True
  815. value = _handle_values_anonymous_param(
  816. compiler,
  817. c,
  818. value,
  819. name=(
  820. _col_bind_name(c)
  821. if not _compile_state_isinsert(compile_state)
  822. or not compile_state._has_multi_parameters
  823. else "%s_m0" % _col_bind_name(c)
  824. ),
  825. accumulate_bind_names=accumulated_bind_names,
  826. **kw,
  827. )
  828. else:
  829. # value is a SQL expression
  830. value = compiler.process(
  831. value.self_group(),
  832. accumulate_bind_names=accumulated_bind_names,
  833. **kw,
  834. )
  835. if compile_state.isupdate:
  836. if implicit_return_defaults and c in implicit_return_defaults:
  837. compiler.implicit_returning.append(c)
  838. else:
  839. compiler.postfetch.append(c)
  840. else:
  841. if c.primary_key:
  842. if implicit_returning:
  843. compiler.implicit_returning.append(c)
  844. elif compiler.dialect.postfetch_lastrowid:
  845. compiler.postfetch_lastrowid = True
  846. elif implicit_return_defaults and (c in implicit_return_defaults):
  847. compiler.implicit_returning.append(c)
  848. else:
  849. # postfetch specifically means, "we can SELECT the row we just
  850. # inserted by primary key to get back the server generated
  851. # defaults". so by definition this can't be used to get the
  852. # primary key value back, because we need to have it ahead of
  853. # time.
  854. compiler.postfetch.append(c)
  855. values.append((c, col_value, value, accumulated_bind_names))
  856. def _append_param_insert_pk_returning(compiler, stmt, c, values, kw):
  857. """Create a primary key expression in the INSERT statement where
  858. we want to populate result.inserted_primary_key and RETURNING
  859. is available.
  860. """
  861. if c.default is not None:
  862. if c.default.is_sequence:
  863. if compiler.dialect.supports_sequences and (
  864. not c.default.optional
  865. or not compiler.dialect.sequences_optional
  866. ):
  867. accumulated_bind_names: Set[str] = set()
  868. values.append(
  869. (
  870. c,
  871. compiler.preparer.format_column(c),
  872. compiler.process(
  873. c.default,
  874. accumulate_bind_names=accumulated_bind_names,
  875. **kw,
  876. ),
  877. accumulated_bind_names,
  878. )
  879. )
  880. compiler.implicit_returning.append(c)
  881. elif c.default.is_clause_element:
  882. accumulated_bind_names = set()
  883. values.append(
  884. (
  885. c,
  886. compiler.preparer.format_column(c),
  887. compiler.process(
  888. c.default.arg.self_group(),
  889. accumulate_bind_names=accumulated_bind_names,
  890. **kw,
  891. ),
  892. accumulated_bind_names,
  893. )
  894. )
  895. compiler.implicit_returning.append(c)
  896. else:
  897. # client side default. OK we can't use RETURNING, need to
  898. # do a "prefetch", which in fact fetches the default value
  899. # on the Python side
  900. values.append(
  901. (
  902. c,
  903. compiler.preparer.format_column(c),
  904. _create_insert_prefetch_bind_param(compiler, c, **kw),
  905. (c.key,),
  906. )
  907. )
  908. elif c is stmt.table._autoincrement_column or c.server_default is not None:
  909. compiler.implicit_returning.append(c)
  910. elif not c.nullable:
  911. # no .default, no .server_default, not autoincrement, we have
  912. # no indication this primary key column will have any value
  913. _warn_pk_with_no_anticipated_value(c)
  914. def _append_param_insert_pk_no_returning(compiler, stmt, c, values, kw):
  915. """Create a primary key expression in the INSERT statement where
  916. we want to populate result.inserted_primary_key and we cannot use
  917. RETURNING.
  918. Depending on the kind of default here we may create a bound parameter
  919. in the INSERT statement and pre-execute a default generation function,
  920. or we may use cursor.lastrowid if supported by the dialect.
  921. """
  922. if (
  923. # column has a Python-side default
  924. c.default is not None
  925. and (
  926. # and it either is not a sequence, or it is and we support
  927. # sequences and want to invoke it
  928. not c.default.is_sequence
  929. or (
  930. compiler.dialect.supports_sequences
  931. and (
  932. not c.default.optional
  933. or not compiler.dialect.sequences_optional
  934. )
  935. )
  936. )
  937. ) or (
  938. # column is the "autoincrement column"
  939. c is stmt.table._autoincrement_column
  940. and (
  941. # dialect can't use cursor.lastrowid
  942. not compiler.dialect.postfetch_lastrowid
  943. and (
  944. # column has a Sequence and we support those
  945. (
  946. c.default is not None
  947. and c.default.is_sequence
  948. and compiler.dialect.supports_sequences
  949. )
  950. or
  951. # column has no default on it, but dialect can run the
  952. # "autoincrement" mechanism explicitly, e.g. PostgreSQL
  953. # SERIAL we know the sequence name
  954. (
  955. c.default is None
  956. and compiler.dialect.preexecute_autoincrement_sequences
  957. )
  958. )
  959. )
  960. ):
  961. # do a pre-execute of the default
  962. values.append(
  963. (
  964. c,
  965. compiler.preparer.format_column(c),
  966. _create_insert_prefetch_bind_param(compiler, c, **kw),
  967. (c.key,),
  968. )
  969. )
  970. elif (
  971. c.default is None
  972. and c.server_default is None
  973. and not c.nullable
  974. and c is not stmt.table._autoincrement_column
  975. ):
  976. # no .default, no .server_default, not autoincrement, we have
  977. # no indication this primary key column will have any value
  978. _warn_pk_with_no_anticipated_value(c)
  979. elif compiler.dialect.postfetch_lastrowid:
  980. # finally, where it seems like there will be a generated primary key
  981. # value and we haven't set up any other way to fetch it, and the
  982. # dialect supports cursor.lastrowid, switch on the lastrowid flag so
  983. # that the DefaultExecutionContext calls upon cursor.lastrowid
  984. compiler.postfetch_lastrowid = True
  985. def _append_param_insert_hasdefault(
  986. compiler, stmt, c, implicit_return_defaults, values, kw
  987. ):
  988. if c.default.is_sequence:
  989. if compiler.dialect.supports_sequences and (
  990. not c.default.optional or not compiler.dialect.sequences_optional
  991. ):
  992. accumulated_bind_names: Set[str] = set()
  993. values.append(
  994. (
  995. c,
  996. compiler.preparer.format_column(c),
  997. compiler.process(
  998. c.default,
  999. accumulate_bind_names=accumulated_bind_names,
  1000. **kw,
  1001. ),
  1002. accumulated_bind_names,
  1003. )
  1004. )
  1005. if implicit_return_defaults and c in implicit_return_defaults:
  1006. compiler.implicit_returning.append(c)
  1007. elif not c.primary_key:
  1008. compiler.postfetch.append(c)
  1009. elif c.default.is_clause_element:
  1010. accumulated_bind_names = set()
  1011. values.append(
  1012. (
  1013. c,
  1014. compiler.preparer.format_column(c),
  1015. compiler.process(
  1016. c.default.arg.self_group(),
  1017. accumulate_bind_names=accumulated_bind_names,
  1018. **kw,
  1019. ),
  1020. accumulated_bind_names,
  1021. )
  1022. )
  1023. if implicit_return_defaults and c in implicit_return_defaults:
  1024. compiler.implicit_returning.append(c)
  1025. elif not c.primary_key:
  1026. # don't add primary key column to postfetch
  1027. compiler.postfetch.append(c)
  1028. else:
  1029. values.append(
  1030. (
  1031. c,
  1032. compiler.preparer.format_column(c),
  1033. _create_insert_prefetch_bind_param(compiler, c, **kw),
  1034. (c.key,),
  1035. )
  1036. )
  1037. def _append_param_insert_select_hasdefault(
  1038. compiler: SQLCompiler,
  1039. stmt: ValuesBase,
  1040. c: ColumnClause[Any],
  1041. values: List[_CrudParamElementSQLExpr],
  1042. kw: Dict[str, Any],
  1043. ) -> None:
  1044. if default_is_sequence(c.default):
  1045. if compiler.dialect.supports_sequences and (
  1046. not c.default.optional or not compiler.dialect.sequences_optional
  1047. ):
  1048. values.append(
  1049. (
  1050. c,
  1051. compiler.preparer.format_column(c),
  1052. c.default.next_value(),
  1053. (),
  1054. )
  1055. )
  1056. elif default_is_clause_element(c.default):
  1057. values.append(
  1058. (
  1059. c,
  1060. compiler.preparer.format_column(c),
  1061. c.default.arg.self_group(),
  1062. (),
  1063. )
  1064. )
  1065. else:
  1066. values.append(
  1067. (
  1068. c,
  1069. compiler.preparer.format_column(c),
  1070. _create_insert_prefetch_bind_param(
  1071. compiler, c, process=False, **kw
  1072. ),
  1073. (c.key,),
  1074. )
  1075. )
  1076. def _append_param_update(
  1077. compiler, compile_state, stmt, c, implicit_return_defaults, values, kw
  1078. ):
  1079. include_table = compile_state.include_table_with_column_exprs
  1080. if c.onupdate is not None and not c.onupdate.is_sequence:
  1081. if c.onupdate.is_clause_element:
  1082. values.append(
  1083. (
  1084. c,
  1085. compiler.preparer.format_column(
  1086. c,
  1087. use_table=include_table,
  1088. ),
  1089. compiler.process(c.onupdate.arg.self_group(), **kw),
  1090. (),
  1091. )
  1092. )
  1093. if implicit_return_defaults and c in implicit_return_defaults:
  1094. compiler.implicit_returning.append(c)
  1095. else:
  1096. compiler.postfetch.append(c)
  1097. else:
  1098. values.append(
  1099. (
  1100. c,
  1101. compiler.preparer.format_column(
  1102. c,
  1103. use_table=include_table,
  1104. ),
  1105. _create_update_prefetch_bind_param(compiler, c, **kw),
  1106. (c.key,),
  1107. )
  1108. )
  1109. elif c.server_onupdate is not None:
  1110. if implicit_return_defaults and c in implicit_return_defaults:
  1111. compiler.implicit_returning.append(c)
  1112. else:
  1113. compiler.postfetch.append(c)
  1114. elif (
  1115. implicit_return_defaults
  1116. and (stmt._return_defaults_columns or not stmt._return_defaults)
  1117. and c in implicit_return_defaults
  1118. ):
  1119. compiler.implicit_returning.append(c)
  1120. @overload
  1121. def _create_insert_prefetch_bind_param(
  1122. compiler: SQLCompiler,
  1123. c: ColumnElement[Any],
  1124. process: Literal[True] = ...,
  1125. **kw: Any,
  1126. ) -> str: ...
  1127. @overload
  1128. def _create_insert_prefetch_bind_param(
  1129. compiler: SQLCompiler,
  1130. c: ColumnElement[Any],
  1131. process: Literal[False],
  1132. **kw: Any,
  1133. ) -> elements.BindParameter[Any]: ...
  1134. def _create_insert_prefetch_bind_param(
  1135. compiler: SQLCompiler,
  1136. c: ColumnElement[Any],
  1137. process: bool = True,
  1138. name: Optional[str] = None,
  1139. **kw: Any,
  1140. ) -> Union[elements.BindParameter[Any], str]:
  1141. param = _create_bind_param(
  1142. compiler, c, None, process=process, name=name, **kw
  1143. )
  1144. compiler.insert_prefetch.append(c) # type: ignore
  1145. return param
  1146. @overload
  1147. def _create_update_prefetch_bind_param(
  1148. compiler: SQLCompiler,
  1149. c: ColumnElement[Any],
  1150. process: Literal[True] = ...,
  1151. **kw: Any,
  1152. ) -> str: ...
  1153. @overload
  1154. def _create_update_prefetch_bind_param(
  1155. compiler: SQLCompiler,
  1156. c: ColumnElement[Any],
  1157. process: Literal[False],
  1158. **kw: Any,
  1159. ) -> elements.BindParameter[Any]: ...
  1160. def _create_update_prefetch_bind_param(
  1161. compiler: SQLCompiler,
  1162. c: ColumnElement[Any],
  1163. process: bool = True,
  1164. name: Optional[str] = None,
  1165. **kw: Any,
  1166. ) -> Union[elements.BindParameter[Any], str]:
  1167. param = _create_bind_param(
  1168. compiler, c, None, process=process, name=name, **kw
  1169. )
  1170. compiler.update_prefetch.append(c) # type: ignore
  1171. return param
  1172. class _multiparam_column(elements.ColumnElement[Any]):
  1173. _is_multiparam_column = True
  1174. def __init__(self, original, index):
  1175. self.index = index
  1176. self.key = "%s_m%d" % (original.key, index + 1)
  1177. self.original = original
  1178. self.default = original.default
  1179. self.type = original.type
  1180. def compare(self, other, **kw):
  1181. raise NotImplementedError()
  1182. def _copy_internals(self, **kw):
  1183. raise NotImplementedError()
  1184. def __eq__(self, other):
  1185. return (
  1186. isinstance(other, _multiparam_column)
  1187. and other.key == self.key
  1188. and other.original == self.original
  1189. )
  1190. @util.memoized_property
  1191. def _default_description_tuple(self) -> _DefaultDescriptionTuple:
  1192. """used by default.py -> _process_execute_defaults()"""
  1193. return _DefaultDescriptionTuple._from_column_default(self.default)
  1194. @util.memoized_property
  1195. def _onupdate_description_tuple(self) -> _DefaultDescriptionTuple:
  1196. """used by default.py -> _process_execute_defaults()"""
  1197. return _DefaultDescriptionTuple._from_column_default(self.onupdate)
  1198. def _process_multiparam_default_bind(
  1199. compiler: SQLCompiler,
  1200. stmt: ValuesBase,
  1201. c: KeyedColumnElement[Any],
  1202. index: int,
  1203. kw: Dict[str, Any],
  1204. ) -> str:
  1205. if not c.default:
  1206. raise exc.CompileError(
  1207. "INSERT value for column %s is explicitly rendered as a bound"
  1208. "parameter in the VALUES clause; "
  1209. "a Python-side value or SQL expression is required" % c
  1210. )
  1211. elif default_is_clause_element(c.default):
  1212. return compiler.process(c.default.arg.self_group(), **kw)
  1213. elif c.default.is_sequence:
  1214. # these conditions would have been established
  1215. # by append_param_insert_(?:hasdefault|pk_returning|pk_no_returning)
  1216. # in order for us to be here, so these don't need to be
  1217. # checked
  1218. # assert compiler.dialect.supports_sequences and (
  1219. # not c.default.optional
  1220. # or not compiler.dialect.sequences_optional
  1221. # )
  1222. return compiler.process(c.default, **kw)
  1223. else:
  1224. col = _multiparam_column(c, index)
  1225. assert isinstance(stmt, dml.Insert)
  1226. return _create_insert_prefetch_bind_param(
  1227. compiler, col, process=True, **kw
  1228. )
  1229. def _get_update_multitable_params(
  1230. compiler,
  1231. stmt,
  1232. compile_state,
  1233. stmt_parameter_tuples,
  1234. check_columns,
  1235. _col_bind_name,
  1236. _getattr_col_key,
  1237. values,
  1238. kw,
  1239. ):
  1240. normalized_params = {
  1241. coercions.expect(roles.DMLColumnRole, c): param
  1242. for c, param in stmt_parameter_tuples or ()
  1243. }
  1244. include_table = compile_state.include_table_with_column_exprs
  1245. affected_tables = set()
  1246. for t in compile_state._extra_froms:
  1247. # extra gymnastics to support the probably-shouldnt-have-supported
  1248. # case of "UPDATE table AS alias SET table.foo = bar", but it's
  1249. # supported
  1250. we_shouldnt_be_here_if_columns_found = (
  1251. not include_table
  1252. and not compile_state.dml_table.is_derived_from(t)
  1253. )
  1254. for c in t.c:
  1255. if c in normalized_params:
  1256. if we_shouldnt_be_here_if_columns_found:
  1257. raise exc.CompileError(
  1258. "Backend does not support additional tables "
  1259. "in the SET "
  1260. "clause; cannot include columns from table(s) "
  1261. f"'{t.description}' in "
  1262. "SET clause"
  1263. )
  1264. affected_tables.add(t)
  1265. check_columns[_getattr_col_key(c)] = c
  1266. value = normalized_params[c]
  1267. col_value = compiler.process(c, include_table=include_table)
  1268. if coercions._is_literal(value):
  1269. value = _create_bind_param(
  1270. compiler,
  1271. c,
  1272. value,
  1273. required=value is REQUIRED,
  1274. name=_col_bind_name(c),
  1275. **kw, # TODO: no test coverage for literal binds here
  1276. )
  1277. accumulated_bind_names: Iterable[str] = (c.key,)
  1278. elif value._is_bind_parameter:
  1279. cbn = _col_bind_name(c)
  1280. value = _handle_values_anonymous_param(
  1281. compiler, c, value, name=cbn, **kw
  1282. )
  1283. accumulated_bind_names = (cbn,)
  1284. else:
  1285. compiler.postfetch.append(c)
  1286. value = compiler.process(value.self_group(), **kw)
  1287. accumulated_bind_names = ()
  1288. values.append((c, col_value, value, accumulated_bind_names))
  1289. # determine tables which are actually to be updated - process onupdate
  1290. # and server_onupdate for these
  1291. for t in affected_tables:
  1292. for c in t.c:
  1293. if c in normalized_params:
  1294. continue
  1295. elif c.onupdate is not None and not c.onupdate.is_sequence:
  1296. if c.onupdate.is_clause_element:
  1297. values.append(
  1298. (
  1299. c,
  1300. compiler.process(c, include_table=include_table),
  1301. compiler.process(
  1302. c.onupdate.arg.self_group(), **kw
  1303. ),
  1304. (),
  1305. )
  1306. )
  1307. compiler.postfetch.append(c)
  1308. else:
  1309. values.append(
  1310. (
  1311. c,
  1312. compiler.process(c, include_table=include_table),
  1313. _create_update_prefetch_bind_param(
  1314. compiler, c, name=_col_bind_name(c), **kw
  1315. ),
  1316. (c.key,),
  1317. )
  1318. )
  1319. elif c.server_onupdate is not None:
  1320. compiler.postfetch.append(c)
  1321. def _extend_values_for_multiparams(
  1322. compiler: SQLCompiler,
  1323. stmt: ValuesBase,
  1324. compile_state: DMLState,
  1325. initial_values: Sequence[_CrudParamElementStr],
  1326. _column_as_key: Callable[..., str],
  1327. kw: Dict[str, Any],
  1328. ) -> List[Sequence[_CrudParamElementStr]]:
  1329. values_0 = initial_values
  1330. values = [initial_values]
  1331. has_visiting_cte = kw.get("visiting_cte") is not None
  1332. mp = compile_state._multi_parameters
  1333. assert mp is not None
  1334. for i, row in enumerate(mp[1:]):
  1335. extension: List[_CrudParamElementStr] = []
  1336. row = {_column_as_key(key): v for key, v in row.items()}
  1337. for col, col_expr, param, accumulated_names in values_0:
  1338. if col.key in row:
  1339. key = col.key
  1340. if coercions._is_literal(row[key]):
  1341. new_param = _create_bind_param(
  1342. compiler,
  1343. col,
  1344. row[key],
  1345. name=("%s_m%d" % (col.key, i + 1)),
  1346. force_anonymous=has_visiting_cte,
  1347. **kw,
  1348. )
  1349. else:
  1350. new_param = compiler.process(row[key].self_group(), **kw)
  1351. else:
  1352. new_param = _process_multiparam_default_bind(
  1353. compiler, stmt, col, i, kw
  1354. )
  1355. extension.append((col, col_expr, new_param, accumulated_names))
  1356. values.append(extension)
  1357. return values
  1358. def _get_stmt_parameter_tuples_params(
  1359. compiler,
  1360. compile_state,
  1361. parameters,
  1362. stmt_parameter_tuples,
  1363. _column_as_key,
  1364. values,
  1365. kw,
  1366. ):
  1367. for k, v in stmt_parameter_tuples:
  1368. colkey = _column_as_key(k)
  1369. if colkey is not None:
  1370. parameters.setdefault(colkey, v)
  1371. else:
  1372. # a non-Column expression on the left side;
  1373. # add it to values() in an "as-is" state,
  1374. # coercing right side to bound param
  1375. # note one of the main use cases for this is array slice
  1376. # updates on PostgreSQL, as the left side is also an expression.
  1377. col_expr = compiler.process(
  1378. k, include_table=compile_state.include_table_with_column_exprs
  1379. )
  1380. if coercions._is_literal(v):
  1381. v = compiler.process(
  1382. elements.BindParameter(None, v, type_=k.type), **kw
  1383. )
  1384. else:
  1385. if v._is_bind_parameter and v.type._isnull:
  1386. # either unique parameter, or other bound parameters that
  1387. # were passed in directly
  1388. # set type to that of the column unconditionally
  1389. v = v._with_binary_element_type(k.type)
  1390. v = compiler.process(v.self_group(), **kw)
  1391. # TODO: not sure if accumulated_bind_names applies here
  1392. values.append((k, col_expr, v, ()))
  1393. def _get_returning_modifiers(compiler, stmt, compile_state, toplevel):
  1394. """determines RETURNING strategy, if any, for the statement.
  1395. This is where it's determined what we need to fetch from the
  1396. INSERT or UPDATE statement after it's invoked.
  1397. """
  1398. dialect = compiler.dialect
  1399. need_pks = (
  1400. toplevel
  1401. and _compile_state_isinsert(compile_state)
  1402. and not stmt._inline
  1403. and (
  1404. not compiler.for_executemany
  1405. or (dialect.insert_executemany_returning and stmt._return_defaults)
  1406. )
  1407. and not stmt._returning
  1408. # and (not stmt._returning or stmt._return_defaults)
  1409. and not compile_state._has_multi_parameters
  1410. )
  1411. # check if we have access to simple cursor.lastrowid. we can use that
  1412. # after the INSERT if that's all we need.
  1413. postfetch_lastrowid = (
  1414. need_pks
  1415. and dialect.postfetch_lastrowid
  1416. and stmt.table._autoincrement_column is not None
  1417. )
  1418. # see if we want to add RETURNING to an INSERT in order to get
  1419. # primary key columns back. This would be instead of postfetch_lastrowid
  1420. # if that's set.
  1421. implicit_returning = (
  1422. # statement itself can veto it
  1423. need_pks
  1424. # the dialect can veto it if it just doesnt support RETURNING
  1425. # with INSERT
  1426. and dialect.insert_returning
  1427. # user-defined implicit_returning on Table can veto it
  1428. and compile_state._primary_table.implicit_returning
  1429. # the compile_state can veto it (SQlite uses this to disable
  1430. # RETURNING for an ON CONFLICT insert, as SQLite does not return
  1431. # for rows that were updated, which is wrong)
  1432. and compile_state._supports_implicit_returning
  1433. and (
  1434. # since we support MariaDB and SQLite which also support lastrowid,
  1435. # decide if we should use lastrowid or RETURNING. for insert
  1436. # that didnt call return_defaults() and has just one set of
  1437. # parameters, we can use lastrowid. this is more "traditional"
  1438. # and a lot of weird use cases are supported by it.
  1439. # SQLite lastrowid times 3x faster than returning,
  1440. # Mariadb lastrowid 2x faster than returning
  1441. (not postfetch_lastrowid or dialect.favor_returning_over_lastrowid)
  1442. or compile_state._has_multi_parameters
  1443. or stmt._return_defaults
  1444. )
  1445. )
  1446. if implicit_returning:
  1447. postfetch_lastrowid = False
  1448. if _compile_state_isinsert(compile_state):
  1449. should_implicit_return_defaults = (
  1450. implicit_returning and stmt._return_defaults
  1451. )
  1452. explicit_returning = (
  1453. should_implicit_return_defaults
  1454. or stmt._returning
  1455. or stmt._supplemental_returning
  1456. )
  1457. use_insertmanyvalues = (
  1458. toplevel
  1459. and compiler.for_executemany
  1460. and dialect.use_insertmanyvalues
  1461. and (
  1462. explicit_returning or dialect.use_insertmanyvalues_wo_returning
  1463. )
  1464. )
  1465. use_sentinel_columns = None
  1466. if (
  1467. use_insertmanyvalues
  1468. and explicit_returning
  1469. and stmt._sort_by_parameter_order
  1470. ):
  1471. use_sentinel_columns = compiler._get_sentinel_column_for_table(
  1472. stmt.table
  1473. )
  1474. elif compile_state.isupdate:
  1475. should_implicit_return_defaults = (
  1476. stmt._return_defaults
  1477. and compile_state._primary_table.implicit_returning
  1478. and compile_state._supports_implicit_returning
  1479. and dialect.update_returning
  1480. )
  1481. use_insertmanyvalues = False
  1482. use_sentinel_columns = None
  1483. elif compile_state.isdelete:
  1484. should_implicit_return_defaults = (
  1485. stmt._return_defaults
  1486. and compile_state._primary_table.implicit_returning
  1487. and compile_state._supports_implicit_returning
  1488. and dialect.delete_returning
  1489. )
  1490. use_insertmanyvalues = False
  1491. use_sentinel_columns = None
  1492. else:
  1493. should_implicit_return_defaults = False # pragma: no cover
  1494. use_insertmanyvalues = False
  1495. use_sentinel_columns = None
  1496. if should_implicit_return_defaults:
  1497. if not stmt._return_defaults_columns:
  1498. # TODO: this is weird. See #9685 where we have to
  1499. # take an extra step to prevent this from happening. why
  1500. # would this ever be *all* columns? but if we set to blank, then
  1501. # that seems to break things also in the ORM. So we should
  1502. # try to clean this up and figure out what return_defaults
  1503. # needs to do w/ the ORM etc. here
  1504. implicit_return_defaults = set(stmt.table.c)
  1505. else:
  1506. implicit_return_defaults = set(stmt._return_defaults_columns)
  1507. else:
  1508. implicit_return_defaults = None
  1509. return (
  1510. need_pks,
  1511. implicit_returning or should_implicit_return_defaults,
  1512. implicit_return_defaults,
  1513. postfetch_lastrowid,
  1514. use_insertmanyvalues,
  1515. use_sentinel_columns,
  1516. )
  1517. def _warn_pk_with_no_anticipated_value(c):
  1518. msg = (
  1519. "Column '%s.%s' is marked as a member of the "
  1520. "primary key for table '%s', "
  1521. "but has no Python-side or server-side default generator indicated, "
  1522. "nor does it indicate 'autoincrement=True' or 'nullable=True', "
  1523. "and no explicit value is passed. "
  1524. "Primary key columns typically may not store NULL."
  1525. % (c.table.fullname, c.name, c.table.fullname)
  1526. )
  1527. if len(c.table.primary_key) > 1:
  1528. msg += (
  1529. " Note that as of SQLAlchemy 1.1, 'autoincrement=True' must be "
  1530. "indicated explicitly for composite (e.g. multicolumn) primary "
  1531. "keys if AUTO_INCREMENT/SERIAL/IDENTITY "
  1532. "behavior is expected for one of the columns in the primary key. "
  1533. "CREATE TABLE statements are impacted by this change as well on "
  1534. "most backends."
  1535. )
  1536. util.warn(msg)