_elements_constructors.py 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. # sql/_elements_constructors.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. from __future__ import annotations
  8. import typing
  9. from typing import Any
  10. from typing import Callable
  11. from typing import Mapping
  12. from typing import Optional
  13. from typing import overload
  14. from typing import Sequence
  15. from typing import Tuple as typing_Tuple
  16. from typing import TYPE_CHECKING
  17. from typing import TypeVar
  18. from typing import Union
  19. from . import coercions
  20. from . import roles
  21. from .base import _NoArg
  22. from .coercions import _document_text_coercion
  23. from .elements import BindParameter
  24. from .elements import BooleanClauseList
  25. from .elements import Case
  26. from .elements import Cast
  27. from .elements import CollationClause
  28. from .elements import CollectionAggregate
  29. from .elements import ColumnClause
  30. from .elements import ColumnElement
  31. from .elements import Extract
  32. from .elements import False_
  33. from .elements import FunctionFilter
  34. from .elements import Label
  35. from .elements import Null
  36. from .elements import Over
  37. from .elements import TextClause
  38. from .elements import True_
  39. from .elements import TryCast
  40. from .elements import Tuple
  41. from .elements import TypeCoerce
  42. from .elements import UnaryExpression
  43. from .elements import WithinGroup
  44. from .functions import FunctionElement
  45. from ..util.typing import Literal
  46. if typing.TYPE_CHECKING:
  47. from ._typing import _ByArgument
  48. from ._typing import _ColumnExpressionArgument
  49. from ._typing import _ColumnExpressionOrLiteralArgument
  50. from ._typing import _ColumnExpressionOrStrLabelArgument
  51. from ._typing import _TypeEngineArgument
  52. from .elements import BinaryExpression
  53. from .selectable import FromClause
  54. from .type_api import TypeEngine
  55. _T = TypeVar("_T")
  56. def all_(expr: _ColumnExpressionArgument[_T]) -> CollectionAggregate[bool]:
  57. """Produce an ALL expression.
  58. For dialects such as that of PostgreSQL, this operator applies
  59. to usage of the :class:`_types.ARRAY` datatype, for that of
  60. MySQL, it may apply to a subquery. e.g.::
  61. # renders on PostgreSQL:
  62. # '5 = ALL (somearray)'
  63. expr = 5 == all_(mytable.c.somearray)
  64. # renders on MySQL:
  65. # '5 = ALL (SELECT value FROM table)'
  66. expr = 5 == all_(select(table.c.value))
  67. Comparison to NULL may work using ``None``::
  68. None == all_(mytable.c.somearray)
  69. The any_() / all_() operators also feature a special "operand flipping"
  70. behavior such that if any_() / all_() are used on the left side of a
  71. comparison using a standalone operator such as ``==``, ``!=``, etc.
  72. (not including operator methods such as
  73. :meth:`_sql.ColumnOperators.is_`) the rendered expression is flipped::
  74. # would render '5 = ALL (column)`
  75. all_(mytable.c.column) == 5
  76. Or with ``None``, which note will not perform
  77. the usual step of rendering "IS" as is normally the case for NULL::
  78. # would render 'NULL = ALL(somearray)'
  79. all_(mytable.c.somearray) == None
  80. .. versionchanged:: 1.4.26 repaired the use of any_() / all_()
  81. comparing to NULL on the right side to be flipped to the left.
  82. The column-level :meth:`_sql.ColumnElement.all_` method (not to be
  83. confused with :class:`_types.ARRAY` level
  84. :meth:`_types.ARRAY.Comparator.all`) is shorthand for
  85. ``all_(col)``::
  86. 5 == mytable.c.somearray.all_()
  87. .. seealso::
  88. :meth:`_sql.ColumnOperators.all_`
  89. :func:`_expression.any_`
  90. """
  91. return CollectionAggregate._create_all(expr)
  92. def and_( # type: ignore[empty-body]
  93. initial_clause: Union[Literal[True], _ColumnExpressionArgument[bool]],
  94. *clauses: _ColumnExpressionArgument[bool],
  95. ) -> ColumnElement[bool]:
  96. r"""Produce a conjunction of expressions joined by ``AND``.
  97. E.g.::
  98. from sqlalchemy import and_
  99. stmt = select(users_table).where(
  100. and_(users_table.c.name == "wendy", users_table.c.enrolled == True)
  101. )
  102. The :func:`.and_` conjunction is also available using the
  103. Python ``&`` operator (though note that compound expressions
  104. need to be parenthesized in order to function with Python
  105. operator precedence behavior)::
  106. stmt = select(users_table).where(
  107. (users_table.c.name == "wendy") & (users_table.c.enrolled == True)
  108. )
  109. The :func:`.and_` operation is also implicit in some cases;
  110. the :meth:`_expression.Select.where`
  111. method for example can be invoked multiple
  112. times against a statement, which will have the effect of each
  113. clause being combined using :func:`.and_`::
  114. stmt = (
  115. select(users_table)
  116. .where(users_table.c.name == "wendy")
  117. .where(users_table.c.enrolled == True)
  118. )
  119. The :func:`.and_` construct must be given at least one positional
  120. argument in order to be valid; a :func:`.and_` construct with no
  121. arguments is ambiguous. To produce an "empty" or dynamically
  122. generated :func:`.and_` expression, from a given list of expressions,
  123. a "default" element of :func:`_sql.true` (or just ``True``) should be
  124. specified::
  125. from sqlalchemy import true
  126. criteria = and_(true(), *expressions)
  127. The above expression will compile to SQL as the expression ``true``
  128. or ``1 = 1``, depending on backend, if no other expressions are
  129. present. If expressions are present, then the :func:`_sql.true` value is
  130. ignored as it does not affect the outcome of an AND expression that
  131. has other elements.
  132. .. deprecated:: 1.4 The :func:`.and_` element now requires that at
  133. least one argument is passed; creating the :func:`.and_` construct
  134. with no arguments is deprecated, and will emit a deprecation warning
  135. while continuing to produce a blank SQL string.
  136. .. seealso::
  137. :func:`.or_`
  138. """
  139. ...
  140. if not TYPE_CHECKING:
  141. # handle deprecated case which allows zero-arguments
  142. def and_(*clauses): # noqa: F811
  143. r"""Produce a conjunction of expressions joined by ``AND``.
  144. E.g.::
  145. from sqlalchemy import and_
  146. stmt = select(users_table).where(
  147. and_(users_table.c.name == "wendy", users_table.c.enrolled == True)
  148. )
  149. The :func:`.and_` conjunction is also available using the
  150. Python ``&`` operator (though note that compound expressions
  151. need to be parenthesized in order to function with Python
  152. operator precedence behavior)::
  153. stmt = select(users_table).where(
  154. (users_table.c.name == "wendy") & (users_table.c.enrolled == True)
  155. )
  156. The :func:`.and_` operation is also implicit in some cases;
  157. the :meth:`_expression.Select.where`
  158. method for example can be invoked multiple
  159. times against a statement, which will have the effect of each
  160. clause being combined using :func:`.and_`::
  161. stmt = (
  162. select(users_table)
  163. .where(users_table.c.name == "wendy")
  164. .where(users_table.c.enrolled == True)
  165. )
  166. The :func:`.and_` construct must be given at least one positional
  167. argument in order to be valid; a :func:`.and_` construct with no
  168. arguments is ambiguous. To produce an "empty" or dynamically
  169. generated :func:`.and_` expression, from a given list of expressions,
  170. a "default" element of :func:`_sql.true` (or just ``True``) should be
  171. specified::
  172. from sqlalchemy import true
  173. criteria = and_(true(), *expressions)
  174. The above expression will compile to SQL as the expression ``true``
  175. or ``1 = 1``, depending on backend, if no other expressions are
  176. present. If expressions are present, then the :func:`_sql.true` value
  177. is ignored as it does not affect the outcome of an AND expression that
  178. has other elements.
  179. .. deprecated:: 1.4 The :func:`.and_` element now requires that at
  180. least one argument is passed; creating the :func:`.and_` construct
  181. with no arguments is deprecated, and will emit a deprecation warning
  182. while continuing to produce a blank SQL string.
  183. .. seealso::
  184. :func:`.or_`
  185. """ # noqa: E501
  186. return BooleanClauseList.and_(*clauses)
  187. def any_(expr: _ColumnExpressionArgument[_T]) -> CollectionAggregate[bool]:
  188. """Produce an ANY expression.
  189. For dialects such as that of PostgreSQL, this operator applies
  190. to usage of the :class:`_types.ARRAY` datatype, for that of
  191. MySQL, it may apply to a subquery. e.g.::
  192. # renders on PostgreSQL:
  193. # '5 = ANY (somearray)'
  194. expr = 5 == any_(mytable.c.somearray)
  195. # renders on MySQL:
  196. # '5 = ANY (SELECT value FROM table)'
  197. expr = 5 == any_(select(table.c.value))
  198. Comparison to NULL may work using ``None`` or :func:`_sql.null`::
  199. None == any_(mytable.c.somearray)
  200. The any_() / all_() operators also feature a special "operand flipping"
  201. behavior such that if any_() / all_() are used on the left side of a
  202. comparison using a standalone operator such as ``==``, ``!=``, etc.
  203. (not including operator methods such as
  204. :meth:`_sql.ColumnOperators.is_`) the rendered expression is flipped::
  205. # would render '5 = ANY (column)`
  206. any_(mytable.c.column) == 5
  207. Or with ``None``, which note will not perform
  208. the usual step of rendering "IS" as is normally the case for NULL::
  209. # would render 'NULL = ANY(somearray)'
  210. any_(mytable.c.somearray) == None
  211. .. versionchanged:: 1.4.26 repaired the use of any_() / all_()
  212. comparing to NULL on the right side to be flipped to the left.
  213. The column-level :meth:`_sql.ColumnElement.any_` method (not to be
  214. confused with :class:`_types.ARRAY` level
  215. :meth:`_types.ARRAY.Comparator.any`) is shorthand for
  216. ``any_(col)``::
  217. 5 = mytable.c.somearray.any_()
  218. .. seealso::
  219. :meth:`_sql.ColumnOperators.any_`
  220. :func:`_expression.all_`
  221. """
  222. return CollectionAggregate._create_any(expr)
  223. def asc(
  224. column: _ColumnExpressionOrStrLabelArgument[_T],
  225. ) -> UnaryExpression[_T]:
  226. """Produce an ascending ``ORDER BY`` clause element.
  227. e.g.::
  228. from sqlalchemy import asc
  229. stmt = select(users_table).order_by(asc(users_table.c.name))
  230. will produce SQL as:
  231. .. sourcecode:: sql
  232. SELECT id, name FROM user ORDER BY name ASC
  233. The :func:`.asc` function is a standalone version of the
  234. :meth:`_expression.ColumnElement.asc`
  235. method available on all SQL expressions,
  236. e.g.::
  237. stmt = select(users_table).order_by(users_table.c.name.asc())
  238. :param column: A :class:`_expression.ColumnElement` (e.g.
  239. scalar SQL expression)
  240. with which to apply the :func:`.asc` operation.
  241. .. seealso::
  242. :func:`.desc`
  243. :func:`.nulls_first`
  244. :func:`.nulls_last`
  245. :meth:`_expression.Select.order_by`
  246. """
  247. return UnaryExpression._create_asc(column)
  248. def collate(
  249. expression: _ColumnExpressionArgument[str], collation: str
  250. ) -> BinaryExpression[str]:
  251. """Return the clause ``expression COLLATE collation``.
  252. e.g.::
  253. collate(mycolumn, "utf8_bin")
  254. produces:
  255. .. sourcecode:: sql
  256. mycolumn COLLATE utf8_bin
  257. The collation expression is also quoted if it is a case sensitive
  258. identifier, e.g. contains uppercase characters.
  259. .. versionchanged:: 1.2 quoting is automatically applied to COLLATE
  260. expressions if they are case sensitive.
  261. """
  262. return CollationClause._create_collation_expression(expression, collation)
  263. def between(
  264. expr: _ColumnExpressionOrLiteralArgument[_T],
  265. lower_bound: Any,
  266. upper_bound: Any,
  267. symmetric: bool = False,
  268. ) -> BinaryExpression[bool]:
  269. """Produce a ``BETWEEN`` predicate clause.
  270. E.g.::
  271. from sqlalchemy import between
  272. stmt = select(users_table).where(between(users_table.c.id, 5, 7))
  273. Would produce SQL resembling:
  274. .. sourcecode:: sql
  275. SELECT id, name FROM user WHERE id BETWEEN :id_1 AND :id_2
  276. The :func:`.between` function is a standalone version of the
  277. :meth:`_expression.ColumnElement.between` method available on all
  278. SQL expressions, as in::
  279. stmt = select(users_table).where(users_table.c.id.between(5, 7))
  280. All arguments passed to :func:`.between`, including the left side
  281. column expression, are coerced from Python scalar values if a
  282. the value is not a :class:`_expression.ColumnElement` subclass.
  283. For example,
  284. three fixed values can be compared as in::
  285. print(between(5, 3, 7))
  286. Which would produce::
  287. :param_1 BETWEEN :param_2 AND :param_3
  288. :param expr: a column expression, typically a
  289. :class:`_expression.ColumnElement`
  290. instance or alternatively a Python scalar expression to be coerced
  291. into a column expression, serving as the left side of the ``BETWEEN``
  292. expression.
  293. :param lower_bound: a column or Python scalar expression serving as the
  294. lower bound of the right side of the ``BETWEEN`` expression.
  295. :param upper_bound: a column or Python scalar expression serving as the
  296. upper bound of the right side of the ``BETWEEN`` expression.
  297. :param symmetric: if True, will render " BETWEEN SYMMETRIC ". Note
  298. that not all databases support this syntax.
  299. .. seealso::
  300. :meth:`_expression.ColumnElement.between`
  301. """
  302. col_expr = coercions.expect(roles.ExpressionElementRole, expr)
  303. return col_expr.between(lower_bound, upper_bound, symmetric=symmetric)
  304. def outparam(
  305. key: str, type_: Optional[TypeEngine[_T]] = None
  306. ) -> BindParameter[_T]:
  307. """Create an 'OUT' parameter for usage in functions (stored procedures),
  308. for databases which support them.
  309. The ``outparam`` can be used like a regular function parameter.
  310. The "output" value will be available from the
  311. :class:`~sqlalchemy.engine.CursorResult` object via its ``out_parameters``
  312. attribute, which returns a dictionary containing the values.
  313. """
  314. return BindParameter(key, None, type_=type_, unique=False, isoutparam=True)
  315. @overload
  316. def not_(clause: BinaryExpression[_T]) -> BinaryExpression[_T]: ...
  317. @overload
  318. def not_(clause: _ColumnExpressionArgument[_T]) -> ColumnElement[_T]: ...
  319. def not_(clause: _ColumnExpressionArgument[_T]) -> ColumnElement[_T]:
  320. """Return a negation of the given clause, i.e. ``NOT(clause)``.
  321. The ``~`` operator is also overloaded on all
  322. :class:`_expression.ColumnElement` subclasses to produce the
  323. same result.
  324. """
  325. return coercions.expect(roles.ExpressionElementRole, clause).__invert__()
  326. def bindparam(
  327. key: Optional[str],
  328. value: Any = _NoArg.NO_ARG,
  329. type_: Optional[_TypeEngineArgument[_T]] = None,
  330. unique: bool = False,
  331. required: Union[bool, Literal[_NoArg.NO_ARG]] = _NoArg.NO_ARG,
  332. quote: Optional[bool] = None,
  333. callable_: Optional[Callable[[], Any]] = None,
  334. expanding: bool = False,
  335. isoutparam: bool = False,
  336. literal_execute: bool = False,
  337. ) -> BindParameter[_T]:
  338. r"""Produce a "bound expression".
  339. The return value is an instance of :class:`.BindParameter`; this
  340. is a :class:`_expression.ColumnElement`
  341. subclass which represents a so-called
  342. "placeholder" value in a SQL expression, the value of which is
  343. supplied at the point at which the statement in executed against a
  344. database connection.
  345. In SQLAlchemy, the :func:`.bindparam` construct has
  346. the ability to carry along the actual value that will be ultimately
  347. used at expression time. In this way, it serves not just as
  348. a "placeholder" for eventual population, but also as a means of
  349. representing so-called "unsafe" values which should not be rendered
  350. directly in a SQL statement, but rather should be passed along
  351. to the :term:`DBAPI` as values which need to be correctly escaped
  352. and potentially handled for type-safety.
  353. When using :func:`.bindparam` explicitly, the use case is typically
  354. one of traditional deferment of parameters; the :func:`.bindparam`
  355. construct accepts a name which can then be referred to at execution
  356. time::
  357. from sqlalchemy import bindparam
  358. stmt = select(users_table).where(
  359. users_table.c.name == bindparam("username")
  360. )
  361. The above statement, when rendered, will produce SQL similar to:
  362. .. sourcecode:: sql
  363. SELECT id, name FROM user WHERE name = :username
  364. In order to populate the value of ``:username`` above, the value
  365. would typically be applied at execution time to a method
  366. like :meth:`_engine.Connection.execute`::
  367. result = connection.execute(stmt, {"username": "wendy"})
  368. Explicit use of :func:`.bindparam` is also common when producing
  369. UPDATE or DELETE statements that are to be invoked multiple times,
  370. where the WHERE criterion of the statement is to change on each
  371. invocation, such as::
  372. stmt = (
  373. users_table.update()
  374. .where(user_table.c.name == bindparam("username"))
  375. .values(fullname=bindparam("fullname"))
  376. )
  377. connection.execute(
  378. stmt,
  379. [
  380. {"username": "wendy", "fullname": "Wendy Smith"},
  381. {"username": "jack", "fullname": "Jack Jones"},
  382. ],
  383. )
  384. SQLAlchemy's Core expression system makes wide use of
  385. :func:`.bindparam` in an implicit sense. It is typical that Python
  386. literal values passed to virtually all SQL expression functions are
  387. coerced into fixed :func:`.bindparam` constructs. For example, given
  388. a comparison operation such as::
  389. expr = users_table.c.name == "Wendy"
  390. The above expression will produce a :class:`.BinaryExpression`
  391. construct, where the left side is the :class:`_schema.Column` object
  392. representing the ``name`` column, and the right side is a
  393. :class:`.BindParameter` representing the literal value::
  394. print(repr(expr.right))
  395. BindParameter("%(4327771088 name)s", "Wendy", type_=String())
  396. The expression above will render SQL such as:
  397. .. sourcecode:: sql
  398. user.name = :name_1
  399. Where the ``:name_1`` parameter name is an anonymous name. The
  400. actual string ``Wendy`` is not in the rendered string, but is carried
  401. along where it is later used within statement execution. If we
  402. invoke a statement like the following::
  403. stmt = select(users_table).where(users_table.c.name == "Wendy")
  404. result = connection.execute(stmt)
  405. We would see SQL logging output as:
  406. .. sourcecode:: sql
  407. SELECT "user".id, "user".name
  408. FROM "user"
  409. WHERE "user".name = %(name_1)s
  410. {'name_1': 'Wendy'}
  411. Above, we see that ``Wendy`` is passed as a parameter to the database,
  412. while the placeholder ``:name_1`` is rendered in the appropriate form
  413. for the target database, in this case the PostgreSQL database.
  414. Similarly, :func:`.bindparam` is invoked automatically when working
  415. with :term:`CRUD` statements as far as the "VALUES" portion is
  416. concerned. The :func:`_expression.insert` construct produces an
  417. ``INSERT`` expression which will, at statement execution time, generate
  418. bound placeholders based on the arguments passed, as in::
  419. stmt = users_table.insert()
  420. result = connection.execute(stmt, {"name": "Wendy"})
  421. The above will produce SQL output as:
  422. .. sourcecode:: sql
  423. INSERT INTO "user" (name) VALUES (%(name)s)
  424. {'name': 'Wendy'}
  425. The :class:`_expression.Insert` construct, at
  426. compilation/execution time, rendered a single :func:`.bindparam`
  427. mirroring the column name ``name`` as a result of the single ``name``
  428. parameter we passed to the :meth:`_engine.Connection.execute` method.
  429. :param key:
  430. the key (e.g. the name) for this bind param.
  431. Will be used in the generated
  432. SQL statement for dialects that use named parameters. This
  433. value may be modified when part of a compilation operation,
  434. if other :class:`BindParameter` objects exist with the same
  435. key, or if its length is too long and truncation is
  436. required.
  437. If omitted, an "anonymous" name is generated for the bound parameter;
  438. when given a value to bind, the end result is equivalent to calling upon
  439. the :func:`.literal` function with a value to bind, particularly
  440. if the :paramref:`.bindparam.unique` parameter is also provided.
  441. :param value:
  442. Initial value for this bind param. Will be used at statement
  443. execution time as the value for this parameter passed to the
  444. DBAPI, if no other value is indicated to the statement execution
  445. method for this particular parameter name. Defaults to ``None``.
  446. :param callable\_:
  447. A callable function that takes the place of "value". The function
  448. will be called at statement execution time to determine the
  449. ultimate value. Used for scenarios where the actual bind
  450. value cannot be determined at the point at which the clause
  451. construct is created, but embedded bind values are still desirable.
  452. :param type\_:
  453. A :class:`.TypeEngine` class or instance representing an optional
  454. datatype for this :func:`.bindparam`. If not passed, a type
  455. may be determined automatically for the bind, based on the given
  456. value; for example, trivial Python types such as ``str``,
  457. ``int``, ``bool``
  458. may result in the :class:`.String`, :class:`.Integer` or
  459. :class:`.Boolean` types being automatically selected.
  460. The type of a :func:`.bindparam` is significant especially in that
  461. the type will apply pre-processing to the value before it is
  462. passed to the database. For example, a :func:`.bindparam` which
  463. refers to a datetime value, and is specified as holding the
  464. :class:`.DateTime` type, may apply conversion needed to the
  465. value (such as stringification on SQLite) before passing the value
  466. to the database.
  467. :param unique:
  468. if True, the key name of this :class:`.BindParameter` will be
  469. modified if another :class:`.BindParameter` of the same name
  470. already has been located within the containing
  471. expression. This flag is used generally by the internals
  472. when producing so-called "anonymous" bound expressions, it
  473. isn't generally applicable to explicitly-named :func:`.bindparam`
  474. constructs.
  475. :param required:
  476. If ``True``, a value is required at execution time. If not passed,
  477. it defaults to ``True`` if neither :paramref:`.bindparam.value`
  478. or :paramref:`.bindparam.callable` were passed. If either of these
  479. parameters are present, then :paramref:`.bindparam.required`
  480. defaults to ``False``.
  481. :param quote:
  482. True if this parameter name requires quoting and is not
  483. currently known as a SQLAlchemy reserved word; this currently
  484. only applies to the Oracle Database backends, where bound names must
  485. sometimes be quoted.
  486. :param isoutparam:
  487. if True, the parameter should be treated like a stored procedure
  488. "OUT" parameter. This applies to backends such as Oracle Database which
  489. support OUT parameters.
  490. :param expanding:
  491. if True, this parameter will be treated as an "expanding" parameter
  492. at execution time; the parameter value is expected to be a sequence,
  493. rather than a scalar value, and the string SQL statement will
  494. be transformed on a per-execution basis to accommodate the sequence
  495. with a variable number of parameter slots passed to the DBAPI.
  496. This is to allow statement caching to be used in conjunction with
  497. an IN clause.
  498. .. seealso::
  499. :meth:`.ColumnOperators.in_`
  500. :ref:`baked_in` - with baked queries
  501. .. note:: The "expanding" feature does not support "executemany"-
  502. style parameter sets.
  503. .. versionadded:: 1.2
  504. .. versionchanged:: 1.3 the "expanding" bound parameter feature now
  505. supports empty lists.
  506. :param literal_execute:
  507. if True, the bound parameter will be rendered in the compile phase
  508. with a special "POSTCOMPILE" token, and the SQLAlchemy compiler will
  509. render the final value of the parameter into the SQL statement at
  510. statement execution time, omitting the value from the parameter
  511. dictionary / list passed to DBAPI ``cursor.execute()``. This
  512. produces a similar effect as that of using the ``literal_binds``,
  513. compilation flag, however takes place as the statement is sent to
  514. the DBAPI ``cursor.execute()`` method, rather than when the statement
  515. is compiled. The primary use of this
  516. capability is for rendering LIMIT / OFFSET clauses for database
  517. drivers that can't accommodate for bound parameters in these
  518. contexts, while allowing SQL constructs to be cacheable at the
  519. compilation level.
  520. .. versionadded:: 1.4 Added "post compile" bound parameters
  521. .. seealso::
  522. :ref:`change_4808`.
  523. .. seealso::
  524. :ref:`tutorial_sending_parameters` - in the
  525. :ref:`unified_tutorial`
  526. """
  527. return BindParameter(
  528. key,
  529. value,
  530. type_,
  531. unique,
  532. required,
  533. quote,
  534. callable_,
  535. expanding,
  536. isoutparam,
  537. literal_execute,
  538. )
  539. def case(
  540. *whens: Union[
  541. typing_Tuple[_ColumnExpressionArgument[bool], Any], Mapping[Any, Any]
  542. ],
  543. value: Optional[Any] = None,
  544. else_: Optional[Any] = None,
  545. ) -> Case[Any]:
  546. r"""Produce a ``CASE`` expression.
  547. The ``CASE`` construct in SQL is a conditional object that
  548. acts somewhat analogously to an "if/then" construct in other
  549. languages. It returns an instance of :class:`.Case`.
  550. :func:`.case` in its usual form is passed a series of "when"
  551. constructs, that is, a list of conditions and results as tuples::
  552. from sqlalchemy import case
  553. stmt = select(users_table).where(
  554. case(
  555. (users_table.c.name == "wendy", "W"),
  556. (users_table.c.name == "jack", "J"),
  557. else_="E",
  558. )
  559. )
  560. The above statement will produce SQL resembling:
  561. .. sourcecode:: sql
  562. SELECT id, name FROM user
  563. WHERE CASE
  564. WHEN (name = :name_1) THEN :param_1
  565. WHEN (name = :name_2) THEN :param_2
  566. ELSE :param_3
  567. END
  568. When simple equality expressions of several values against a single
  569. parent column are needed, :func:`.case` also has a "shorthand" format
  570. used via the
  571. :paramref:`.case.value` parameter, which is passed a column
  572. expression to be compared. In this form, the :paramref:`.case.whens`
  573. parameter is passed as a dictionary containing expressions to be
  574. compared against keyed to result expressions. The statement below is
  575. equivalent to the preceding statement::
  576. stmt = select(users_table).where(
  577. case({"wendy": "W", "jack": "J"}, value=users_table.c.name, else_="E")
  578. )
  579. The values which are accepted as result values in
  580. :paramref:`.case.whens` as well as with :paramref:`.case.else_` are
  581. coerced from Python literals into :func:`.bindparam` constructs.
  582. SQL expressions, e.g. :class:`_expression.ColumnElement` constructs,
  583. are accepted
  584. as well. To coerce a literal string expression into a constant
  585. expression rendered inline, use the :func:`_expression.literal_column`
  586. construct,
  587. as in::
  588. from sqlalchemy import case, literal_column
  589. case(
  590. (orderline.c.qty > 100, literal_column("'greaterthan100'")),
  591. (orderline.c.qty > 10, literal_column("'greaterthan10'")),
  592. else_=literal_column("'lessthan10'"),
  593. )
  594. The above will render the given constants without using bound
  595. parameters for the result values (but still for the comparison
  596. values), as in:
  597. .. sourcecode:: sql
  598. CASE
  599. WHEN (orderline.qty > :qty_1) THEN 'greaterthan100'
  600. WHEN (orderline.qty > :qty_2) THEN 'greaterthan10'
  601. ELSE 'lessthan10'
  602. END
  603. :param \*whens: The criteria to be compared against,
  604. :paramref:`.case.whens` accepts two different forms, based on
  605. whether or not :paramref:`.case.value` is used.
  606. .. versionchanged:: 1.4 the :func:`_sql.case`
  607. function now accepts the series of WHEN conditions positionally
  608. In the first form, it accepts multiple 2-tuples passed as positional
  609. arguments; each 2-tuple consists of ``(<sql expression>, <value>)``,
  610. where the SQL expression is a boolean expression and "value" is a
  611. resulting value, e.g.::
  612. case(
  613. (users_table.c.name == "wendy", "W"),
  614. (users_table.c.name == "jack", "J"),
  615. )
  616. In the second form, it accepts a Python dictionary of comparison
  617. values mapped to a resulting value; this form requires
  618. :paramref:`.case.value` to be present, and values will be compared
  619. using the ``==`` operator, e.g.::
  620. case({"wendy": "W", "jack": "J"}, value=users_table.c.name)
  621. :param value: An optional SQL expression which will be used as a
  622. fixed "comparison point" for candidate values within a dictionary
  623. passed to :paramref:`.case.whens`.
  624. :param else\_: An optional SQL expression which will be the evaluated
  625. result of the ``CASE`` construct if all expressions within
  626. :paramref:`.case.whens` evaluate to false. When omitted, most
  627. databases will produce a result of NULL if none of the "when"
  628. expressions evaluate to true.
  629. """ # noqa: E501
  630. return Case(*whens, value=value, else_=else_)
  631. def cast(
  632. expression: _ColumnExpressionOrLiteralArgument[Any],
  633. type_: _TypeEngineArgument[_T],
  634. ) -> Cast[_T]:
  635. r"""Produce a ``CAST`` expression.
  636. :func:`.cast` returns an instance of :class:`.Cast`.
  637. E.g.::
  638. from sqlalchemy import cast, Numeric
  639. stmt = select(cast(product_table.c.unit_price, Numeric(10, 4)))
  640. The above statement will produce SQL resembling:
  641. .. sourcecode:: sql
  642. SELECT CAST(unit_price AS NUMERIC(10, 4)) FROM product
  643. The :func:`.cast` function performs two distinct functions when
  644. used. The first is that it renders the ``CAST`` expression within
  645. the resulting SQL string. The second is that it associates the given
  646. type (e.g. :class:`.TypeEngine` class or instance) with the column
  647. expression on the Python side, which means the expression will take
  648. on the expression operator behavior associated with that type,
  649. as well as the bound-value handling and result-row-handling behavior
  650. of the type.
  651. An alternative to :func:`.cast` is the :func:`.type_coerce` function.
  652. This function performs the second task of associating an expression
  653. with a specific type, but does not render the ``CAST`` expression
  654. in SQL.
  655. :param expression: A SQL expression, such as a
  656. :class:`_expression.ColumnElement`
  657. expression or a Python string which will be coerced into a bound
  658. literal value.
  659. :param type\_: A :class:`.TypeEngine` class or instance indicating
  660. the type to which the ``CAST`` should apply.
  661. .. seealso::
  662. :ref:`tutorial_casts`
  663. :func:`.try_cast` - an alternative to CAST that results in
  664. NULLs when the cast fails, instead of raising an error.
  665. Only supported by some dialects.
  666. :func:`.type_coerce` - an alternative to CAST that coerces the type
  667. on the Python side only, which is often sufficient to generate the
  668. correct SQL and data coercion.
  669. """
  670. return Cast(expression, type_)
  671. def try_cast(
  672. expression: _ColumnExpressionOrLiteralArgument[Any],
  673. type_: _TypeEngineArgument[_T],
  674. ) -> TryCast[_T]:
  675. """Produce a ``TRY_CAST`` expression for backends which support it;
  676. this is a ``CAST`` which returns NULL for un-castable conversions.
  677. In SQLAlchemy, this construct is supported **only** by the SQL Server
  678. dialect, and will raise a :class:`.CompileError` if used on other
  679. included backends. However, third party backends may also support
  680. this construct.
  681. .. tip:: As :func:`_sql.try_cast` originates from the SQL Server dialect,
  682. it's importable both from ``sqlalchemy.`` as well as from
  683. ``sqlalchemy.dialects.mssql``.
  684. :func:`_sql.try_cast` returns an instance of :class:`.TryCast` and
  685. generally behaves similarly to the :class:`.Cast` construct;
  686. at the SQL level, the difference between ``CAST`` and ``TRY_CAST``
  687. is that ``TRY_CAST`` returns NULL for an un-castable expression,
  688. such as attempting to cast a string ``"hi"`` to an integer value.
  689. E.g.::
  690. from sqlalchemy import select, try_cast, Numeric
  691. stmt = select(try_cast(product_table.c.unit_price, Numeric(10, 4)))
  692. The above would render on Microsoft SQL Server as:
  693. .. sourcecode:: sql
  694. SELECT TRY_CAST (product_table.unit_price AS NUMERIC(10, 4))
  695. FROM product_table
  696. .. versionadded:: 2.0.14 :func:`.try_cast` has been
  697. generalized from the SQL Server dialect into a general use
  698. construct that may be supported by additional dialects.
  699. """
  700. return TryCast(expression, type_)
  701. def column(
  702. text: str,
  703. type_: Optional[_TypeEngineArgument[_T]] = None,
  704. is_literal: bool = False,
  705. _selectable: Optional[FromClause] = None,
  706. ) -> ColumnClause[_T]:
  707. """Produce a :class:`.ColumnClause` object.
  708. The :class:`.ColumnClause` is a lightweight analogue to the
  709. :class:`_schema.Column` class. The :func:`_expression.column`
  710. function can
  711. be invoked with just a name alone, as in::
  712. from sqlalchemy import column
  713. id, name = column("id"), column("name")
  714. stmt = select(id, name).select_from("user")
  715. The above statement would produce SQL like:
  716. .. sourcecode:: sql
  717. SELECT id, name FROM user
  718. Once constructed, :func:`_expression.column`
  719. may be used like any other SQL
  720. expression element such as within :func:`_expression.select`
  721. constructs::
  722. from sqlalchemy.sql import column
  723. id, name = column("id"), column("name")
  724. stmt = select(id, name).select_from("user")
  725. The text handled by :func:`_expression.column`
  726. is assumed to be handled
  727. like the name of a database column; if the string contains mixed case,
  728. special characters, or matches a known reserved word on the target
  729. backend, the column expression will render using the quoting
  730. behavior determined by the backend. To produce a textual SQL
  731. expression that is rendered exactly without any quoting,
  732. use :func:`_expression.literal_column` instead,
  733. or pass ``True`` as the
  734. value of :paramref:`_expression.column.is_literal`. Additionally,
  735. full SQL
  736. statements are best handled using the :func:`_expression.text`
  737. construct.
  738. :func:`_expression.column` can be used in a table-like
  739. fashion by combining it with the :func:`.table` function
  740. (which is the lightweight analogue to :class:`_schema.Table`
  741. ) to produce
  742. a working table construct with minimal boilerplate::
  743. from sqlalchemy import table, column, select
  744. user = table(
  745. "user",
  746. column("id"),
  747. column("name"),
  748. column("description"),
  749. )
  750. stmt = select(user.c.description).where(user.c.name == "wendy")
  751. A :func:`_expression.column` / :func:`.table`
  752. construct like that illustrated
  753. above can be created in an
  754. ad-hoc fashion and is not associated with any
  755. :class:`_schema.MetaData`, DDL, or events, unlike its
  756. :class:`_schema.Table` counterpart.
  757. :param text: the text of the element.
  758. :param type: :class:`_types.TypeEngine` object which can associate
  759. this :class:`.ColumnClause` with a type.
  760. :param is_literal: if True, the :class:`.ColumnClause` is assumed to
  761. be an exact expression that will be delivered to the output with no
  762. quoting rules applied regardless of case sensitive settings. the
  763. :func:`_expression.literal_column()` function essentially invokes
  764. :func:`_expression.column` while passing ``is_literal=True``.
  765. .. seealso::
  766. :class:`_schema.Column`
  767. :func:`_expression.literal_column`
  768. :func:`.table`
  769. :func:`_expression.text`
  770. :ref:`tutorial_select_arbitrary_text`
  771. """
  772. return ColumnClause(text, type_, is_literal, _selectable)
  773. def desc(
  774. column: _ColumnExpressionOrStrLabelArgument[_T],
  775. ) -> UnaryExpression[_T]:
  776. """Produce a descending ``ORDER BY`` clause element.
  777. e.g.::
  778. from sqlalchemy import desc
  779. stmt = select(users_table).order_by(desc(users_table.c.name))
  780. will produce SQL as:
  781. .. sourcecode:: sql
  782. SELECT id, name FROM user ORDER BY name DESC
  783. The :func:`.desc` function is a standalone version of the
  784. :meth:`_expression.ColumnElement.desc`
  785. method available on all SQL expressions,
  786. e.g.::
  787. stmt = select(users_table).order_by(users_table.c.name.desc())
  788. :param column: A :class:`_expression.ColumnElement` (e.g.
  789. scalar SQL expression)
  790. with which to apply the :func:`.desc` operation.
  791. .. seealso::
  792. :func:`.asc`
  793. :func:`.nulls_first`
  794. :func:`.nulls_last`
  795. :meth:`_expression.Select.order_by`
  796. """
  797. return UnaryExpression._create_desc(column)
  798. def distinct(expr: _ColumnExpressionArgument[_T]) -> UnaryExpression[_T]:
  799. """Produce an column-expression-level unary ``DISTINCT`` clause.
  800. This applies the ``DISTINCT`` keyword to an **individual column
  801. expression** (e.g. not the whole statement), and renders **specifically
  802. in that column position**; this is used for containment within
  803. an aggregate function, as in::
  804. from sqlalchemy import distinct, func
  805. stmt = select(users_table.c.id, func.count(distinct(users_table.c.name)))
  806. The above would produce an statement resembling:
  807. .. sourcecode:: sql
  808. SELECT user.id, count(DISTINCT user.name) FROM user
  809. .. tip:: The :func:`_sql.distinct` function does **not** apply DISTINCT
  810. to the full SELECT statement, instead applying a DISTINCT modifier
  811. to **individual column expressions**. For general ``SELECT DISTINCT``
  812. support, use the
  813. :meth:`_sql.Select.distinct` method on :class:`_sql.Select`.
  814. The :func:`.distinct` function is also available as a column-level
  815. method, e.g. :meth:`_expression.ColumnElement.distinct`, as in::
  816. stmt = select(func.count(users_table.c.name.distinct()))
  817. The :func:`.distinct` operator is different from the
  818. :meth:`_expression.Select.distinct` method of
  819. :class:`_expression.Select`,
  820. which produces a ``SELECT`` statement
  821. with ``DISTINCT`` applied to the result set as a whole,
  822. e.g. a ``SELECT DISTINCT`` expression. See that method for further
  823. information.
  824. .. seealso::
  825. :meth:`_expression.ColumnElement.distinct`
  826. :meth:`_expression.Select.distinct`
  827. :data:`.func`
  828. """ # noqa: E501
  829. return UnaryExpression._create_distinct(expr)
  830. def bitwise_not(expr: _ColumnExpressionArgument[_T]) -> UnaryExpression[_T]:
  831. """Produce a unary bitwise NOT clause, typically via the ``~`` operator.
  832. Not to be confused with boolean negation :func:`_sql.not_`.
  833. .. versionadded:: 2.0.2
  834. .. seealso::
  835. :ref:`operators_bitwise`
  836. """
  837. return UnaryExpression._create_bitwise_not(expr)
  838. def extract(field: str, expr: _ColumnExpressionArgument[Any]) -> Extract:
  839. """Return a :class:`.Extract` construct.
  840. This is typically available as :func:`.extract`
  841. as well as ``func.extract`` from the
  842. :data:`.func` namespace.
  843. :param field: The field to extract.
  844. .. warning:: This field is used as a literal SQL string.
  845. **DO NOT PASS UNTRUSTED INPUT TO THIS STRING**.
  846. :param expr: A column or Python scalar expression serving as the
  847. right side of the ``EXTRACT`` expression.
  848. E.g.::
  849. from sqlalchemy import extract
  850. from sqlalchemy import table, column
  851. logged_table = table(
  852. "user",
  853. column("id"),
  854. column("date_created"),
  855. )
  856. stmt = select(logged_table.c.id).where(
  857. extract("YEAR", logged_table.c.date_created) == 2021
  858. )
  859. In the above example, the statement is used to select ids from the
  860. database where the ``YEAR`` component matches a specific value.
  861. Similarly, one can also select an extracted component::
  862. stmt = select(extract("YEAR", logged_table.c.date_created)).where(
  863. logged_table.c.id == 1
  864. )
  865. The implementation of ``EXTRACT`` may vary across database backends.
  866. Users are reminded to consult their database documentation.
  867. """
  868. return Extract(field, expr)
  869. def false() -> False_:
  870. """Return a :class:`.False_` construct.
  871. E.g.:
  872. .. sourcecode:: pycon+sql
  873. >>> from sqlalchemy import false
  874. >>> print(select(t.c.x).where(false()))
  875. {printsql}SELECT x FROM t WHERE false
  876. A backend which does not support true/false constants will render as
  877. an expression against 1 or 0:
  878. .. sourcecode:: pycon+sql
  879. >>> print(select(t.c.x).where(false()))
  880. {printsql}SELECT x FROM t WHERE 0 = 1
  881. The :func:`.true` and :func:`.false` constants also feature
  882. "short circuit" operation within an :func:`.and_` or :func:`.or_`
  883. conjunction:
  884. .. sourcecode:: pycon+sql
  885. >>> print(select(t.c.x).where(or_(t.c.x > 5, true())))
  886. {printsql}SELECT x FROM t WHERE true{stop}
  887. >>> print(select(t.c.x).where(and_(t.c.x > 5, false())))
  888. {printsql}SELECT x FROM t WHERE false{stop}
  889. .. seealso::
  890. :func:`.true`
  891. """
  892. return False_._instance()
  893. def funcfilter(
  894. func: FunctionElement[_T], *criterion: _ColumnExpressionArgument[bool]
  895. ) -> FunctionFilter[_T]:
  896. """Produce a :class:`.FunctionFilter` object against a function.
  897. Used against aggregate and window functions,
  898. for database backends that support the "FILTER" clause.
  899. E.g.::
  900. from sqlalchemy import funcfilter
  901. funcfilter(func.count(1), MyClass.name == "some name")
  902. Would produce "COUNT(1) FILTER (WHERE myclass.name = 'some name')".
  903. This function is also available from the :data:`~.expression.func`
  904. construct itself via the :meth:`.FunctionElement.filter` method.
  905. .. seealso::
  906. :ref:`tutorial_functions_within_group` - in the
  907. :ref:`unified_tutorial`
  908. :meth:`.FunctionElement.filter`
  909. """
  910. return FunctionFilter(func, *criterion)
  911. def label(
  912. name: str,
  913. element: _ColumnExpressionArgument[_T],
  914. type_: Optional[_TypeEngineArgument[_T]] = None,
  915. ) -> Label[_T]:
  916. """Return a :class:`Label` object for the
  917. given :class:`_expression.ColumnElement`.
  918. A label changes the name of an element in the columns clause of a
  919. ``SELECT`` statement, typically via the ``AS`` SQL keyword.
  920. This functionality is more conveniently available via the
  921. :meth:`_expression.ColumnElement.label` method on
  922. :class:`_expression.ColumnElement`.
  923. :param name: label name
  924. :param obj: a :class:`_expression.ColumnElement`.
  925. """
  926. return Label(name, element, type_)
  927. def null() -> Null:
  928. """Return a constant :class:`.Null` construct."""
  929. return Null._instance()
  930. def nulls_first(column: _ColumnExpressionArgument[_T]) -> UnaryExpression[_T]:
  931. """Produce the ``NULLS FIRST`` modifier for an ``ORDER BY`` expression.
  932. :func:`.nulls_first` is intended to modify the expression produced
  933. by :func:`.asc` or :func:`.desc`, and indicates how NULL values
  934. should be handled when they are encountered during ordering::
  935. from sqlalchemy import desc, nulls_first
  936. stmt = select(users_table).order_by(nulls_first(desc(users_table.c.name)))
  937. The SQL expression from the above would resemble:
  938. .. sourcecode:: sql
  939. SELECT id, name FROM user ORDER BY name DESC NULLS FIRST
  940. Like :func:`.asc` and :func:`.desc`, :func:`.nulls_first` is typically
  941. invoked from the column expression itself using
  942. :meth:`_expression.ColumnElement.nulls_first`,
  943. rather than as its standalone
  944. function version, as in::
  945. stmt = select(users_table).order_by(
  946. users_table.c.name.desc().nulls_first()
  947. )
  948. .. versionchanged:: 1.4 :func:`.nulls_first` is renamed from
  949. :func:`.nullsfirst` in previous releases.
  950. The previous name remains available for backwards compatibility.
  951. .. seealso::
  952. :func:`.asc`
  953. :func:`.desc`
  954. :func:`.nulls_last`
  955. :meth:`_expression.Select.order_by`
  956. """ # noqa: E501
  957. return UnaryExpression._create_nulls_first(column)
  958. def nulls_last(column: _ColumnExpressionArgument[_T]) -> UnaryExpression[_T]:
  959. """Produce the ``NULLS LAST`` modifier for an ``ORDER BY`` expression.
  960. :func:`.nulls_last` is intended to modify the expression produced
  961. by :func:`.asc` or :func:`.desc`, and indicates how NULL values
  962. should be handled when they are encountered during ordering::
  963. from sqlalchemy import desc, nulls_last
  964. stmt = select(users_table).order_by(nulls_last(desc(users_table.c.name)))
  965. The SQL expression from the above would resemble:
  966. .. sourcecode:: sql
  967. SELECT id, name FROM user ORDER BY name DESC NULLS LAST
  968. Like :func:`.asc` and :func:`.desc`, :func:`.nulls_last` is typically
  969. invoked from the column expression itself using
  970. :meth:`_expression.ColumnElement.nulls_last`,
  971. rather than as its standalone
  972. function version, as in::
  973. stmt = select(users_table).order_by(users_table.c.name.desc().nulls_last())
  974. .. versionchanged:: 1.4 :func:`.nulls_last` is renamed from
  975. :func:`.nullslast` in previous releases.
  976. The previous name remains available for backwards compatibility.
  977. .. seealso::
  978. :func:`.asc`
  979. :func:`.desc`
  980. :func:`.nulls_first`
  981. :meth:`_expression.Select.order_by`
  982. """ # noqa: E501
  983. return UnaryExpression._create_nulls_last(column)
  984. def or_( # type: ignore[empty-body]
  985. initial_clause: Union[Literal[False], _ColumnExpressionArgument[bool]],
  986. *clauses: _ColumnExpressionArgument[bool],
  987. ) -> ColumnElement[bool]:
  988. """Produce a conjunction of expressions joined by ``OR``.
  989. E.g.::
  990. from sqlalchemy import or_
  991. stmt = select(users_table).where(
  992. or_(users_table.c.name == "wendy", users_table.c.name == "jack")
  993. )
  994. The :func:`.or_` conjunction is also available using the
  995. Python ``|`` operator (though note that compound expressions
  996. need to be parenthesized in order to function with Python
  997. operator precedence behavior)::
  998. stmt = select(users_table).where(
  999. (users_table.c.name == "wendy") | (users_table.c.name == "jack")
  1000. )
  1001. The :func:`.or_` construct must be given at least one positional
  1002. argument in order to be valid; a :func:`.or_` construct with no
  1003. arguments is ambiguous. To produce an "empty" or dynamically
  1004. generated :func:`.or_` expression, from a given list of expressions,
  1005. a "default" element of :func:`_sql.false` (or just ``False``) should be
  1006. specified::
  1007. from sqlalchemy import false
  1008. or_criteria = or_(false(), *expressions)
  1009. The above expression will compile to SQL as the expression ``false``
  1010. or ``0 = 1``, depending on backend, if no other expressions are
  1011. present. If expressions are present, then the :func:`_sql.false` value is
  1012. ignored as it does not affect the outcome of an OR expression which
  1013. has other elements.
  1014. .. deprecated:: 1.4 The :func:`.or_` element now requires that at
  1015. least one argument is passed; creating the :func:`.or_` construct
  1016. with no arguments is deprecated, and will emit a deprecation warning
  1017. while continuing to produce a blank SQL string.
  1018. .. seealso::
  1019. :func:`.and_`
  1020. """
  1021. ...
  1022. if not TYPE_CHECKING:
  1023. # handle deprecated case which allows zero-arguments
  1024. def or_(*clauses): # noqa: F811
  1025. """Produce a conjunction of expressions joined by ``OR``.
  1026. E.g.::
  1027. from sqlalchemy import or_
  1028. stmt = select(users_table).where(
  1029. or_(users_table.c.name == "wendy", users_table.c.name == "jack")
  1030. )
  1031. The :func:`.or_` conjunction is also available using the
  1032. Python ``|`` operator (though note that compound expressions
  1033. need to be parenthesized in order to function with Python
  1034. operator precedence behavior)::
  1035. stmt = select(users_table).where(
  1036. (users_table.c.name == "wendy") | (users_table.c.name == "jack")
  1037. )
  1038. The :func:`.or_` construct must be given at least one positional
  1039. argument in order to be valid; a :func:`.or_` construct with no
  1040. arguments is ambiguous. To produce an "empty" or dynamically
  1041. generated :func:`.or_` expression, from a given list of expressions,
  1042. a "default" element of :func:`_sql.false` (or just ``False``) should be
  1043. specified::
  1044. from sqlalchemy import false
  1045. or_criteria = or_(false(), *expressions)
  1046. The above expression will compile to SQL as the expression ``false``
  1047. or ``0 = 1``, depending on backend, if no other expressions are
  1048. present. If expressions are present, then the :func:`_sql.false` value
  1049. is ignored as it does not affect the outcome of an OR expression which
  1050. has other elements.
  1051. .. deprecated:: 1.4 The :func:`.or_` element now requires that at
  1052. least one argument is passed; creating the :func:`.or_` construct
  1053. with no arguments is deprecated, and will emit a deprecation warning
  1054. while continuing to produce a blank SQL string.
  1055. .. seealso::
  1056. :func:`.and_`
  1057. """ # noqa: E501
  1058. return BooleanClauseList.or_(*clauses)
  1059. def over(
  1060. element: FunctionElement[_T],
  1061. partition_by: Optional[_ByArgument] = None,
  1062. order_by: Optional[_ByArgument] = None,
  1063. range_: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  1064. rows: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  1065. groups: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  1066. ) -> Over[_T]:
  1067. r"""Produce an :class:`.Over` object against a function.
  1068. Used against aggregate or so-called "window" functions,
  1069. for database backends that support window functions.
  1070. :func:`_expression.over` is usually called using
  1071. the :meth:`.FunctionElement.over` method, e.g.::
  1072. func.row_number().over(order_by=mytable.c.some_column)
  1073. Would produce:
  1074. .. sourcecode:: sql
  1075. ROW_NUMBER() OVER(ORDER BY some_column)
  1076. Ranges are also possible using the :paramref:`.expression.over.range_`,
  1077. :paramref:`.expression.over.rows`, and :paramref:`.expression.over.groups`
  1078. parameters. These
  1079. mutually-exclusive parameters each accept a 2-tuple, which contains
  1080. a combination of integers and None::
  1081. func.row_number().over(order_by=my_table.c.some_column, range_=(None, 0))
  1082. The above would produce:
  1083. .. sourcecode:: sql
  1084. ROW_NUMBER() OVER(ORDER BY some_column
  1085. RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  1086. A value of ``None`` indicates "unbounded", a
  1087. value of zero indicates "current row", and negative / positive
  1088. integers indicate "preceding" and "following":
  1089. * RANGE BETWEEN 5 PRECEDING AND 10 FOLLOWING::
  1090. func.row_number().over(order_by="x", range_=(-5, 10))
  1091. * ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW::
  1092. func.row_number().over(order_by="x", rows=(None, 0))
  1093. * RANGE BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING::
  1094. func.row_number().over(order_by="x", range_=(-2, None))
  1095. * RANGE BETWEEN 1 FOLLOWING AND 3 FOLLOWING::
  1096. func.row_number().over(order_by="x", range_=(1, 3))
  1097. * GROUPS BETWEEN 1 FOLLOWING AND 3 FOLLOWING::
  1098. func.row_number().over(order_by="x", groups=(1, 3))
  1099. :param element: a :class:`.FunctionElement`, :class:`.WithinGroup`,
  1100. or other compatible construct.
  1101. :param partition_by: a column element or string, or a list
  1102. of such, that will be used as the PARTITION BY clause
  1103. of the OVER construct.
  1104. :param order_by: a column element or string, or a list
  1105. of such, that will be used as the ORDER BY clause
  1106. of the OVER construct.
  1107. :param range\_: optional range clause for the window. This is a
  1108. tuple value which can contain integer values or ``None``,
  1109. and will render a RANGE BETWEEN PRECEDING / FOLLOWING clause.
  1110. :param rows: optional rows clause for the window. This is a tuple
  1111. value which can contain integer values or None, and will render
  1112. a ROWS BETWEEN PRECEDING / FOLLOWING clause.
  1113. :param groups: optional groups clause for the window. This is a
  1114. tuple value which can contain integer values or ``None``,
  1115. and will render a GROUPS BETWEEN PRECEDING / FOLLOWING clause.
  1116. .. versionadded:: 2.0.40
  1117. This function is also available from the :data:`~.expression.func`
  1118. construct itself via the :meth:`.FunctionElement.over` method.
  1119. .. seealso::
  1120. :ref:`tutorial_window_functions` - in the :ref:`unified_tutorial`
  1121. :data:`.expression.func`
  1122. :func:`_expression.within_group`
  1123. """ # noqa: E501
  1124. return Over(element, partition_by, order_by, range_, rows, groups)
  1125. @_document_text_coercion("text", ":func:`.text`", ":paramref:`.text.text`")
  1126. def text(text: str) -> TextClause:
  1127. r"""Construct a new :class:`_expression.TextClause` clause,
  1128. representing
  1129. a textual SQL string directly.
  1130. E.g.::
  1131. from sqlalchemy import text
  1132. t = text("SELECT * FROM users")
  1133. result = connection.execute(t)
  1134. The advantages :func:`_expression.text`
  1135. provides over a plain string are
  1136. backend-neutral support for bind parameters, per-statement
  1137. execution options, as well as
  1138. bind parameter and result-column typing behavior, allowing
  1139. SQLAlchemy type constructs to play a role when executing
  1140. a statement that is specified literally. The construct can also
  1141. be provided with a ``.c`` collection of column elements, allowing
  1142. it to be embedded in other SQL expression constructs as a subquery.
  1143. Bind parameters are specified by name, using the format ``:name``.
  1144. E.g.::
  1145. t = text("SELECT * FROM users WHERE id=:user_id")
  1146. result = connection.execute(t, {"user_id": 12})
  1147. For SQL statements where a colon is required verbatim, as within
  1148. an inline string, use a backslash to escape::
  1149. t = text(r"SELECT * FROM users WHERE name='\:username'")
  1150. The :class:`_expression.TextClause`
  1151. construct includes methods which can
  1152. provide information about the bound parameters as well as the column
  1153. values which would be returned from the textual statement, assuming
  1154. it's an executable SELECT type of statement. The
  1155. :meth:`_expression.TextClause.bindparams`
  1156. method is used to provide bound
  1157. parameter detail, and :meth:`_expression.TextClause.columns`
  1158. method allows
  1159. specification of return columns including names and types::
  1160. t = (
  1161. text("SELECT * FROM users WHERE id=:user_id")
  1162. .bindparams(user_id=7)
  1163. .columns(id=Integer, name=String)
  1164. )
  1165. for id, name in connection.execute(t):
  1166. print(id, name)
  1167. The :func:`_expression.text` construct is used in cases when
  1168. a literal string SQL fragment is specified as part of a larger query,
  1169. such as for the WHERE clause of a SELECT statement::
  1170. s = select(users.c.id, users.c.name).where(text("id=:user_id"))
  1171. result = connection.execute(s, {"user_id": 12})
  1172. :func:`_expression.text` is also used for the construction
  1173. of a full, standalone statement using plain text.
  1174. As such, SQLAlchemy refers
  1175. to it as an :class:`.Executable` object and may be used
  1176. like any other statement passed to an ``.execute()`` method.
  1177. :param text:
  1178. the text of the SQL statement to be created. Use ``:<param>``
  1179. to specify bind parameters; they will be compiled to their
  1180. engine-specific format.
  1181. .. seealso::
  1182. :ref:`tutorial_select_arbitrary_text`
  1183. """
  1184. return TextClause(text)
  1185. def true() -> True_:
  1186. """Return a constant :class:`.True_` construct.
  1187. E.g.:
  1188. .. sourcecode:: pycon+sql
  1189. >>> from sqlalchemy import true
  1190. >>> print(select(t.c.x).where(true()))
  1191. {printsql}SELECT x FROM t WHERE true
  1192. A backend which does not support true/false constants will render as
  1193. an expression against 1 or 0:
  1194. .. sourcecode:: pycon+sql
  1195. >>> print(select(t.c.x).where(true()))
  1196. {printsql}SELECT x FROM t WHERE 1 = 1
  1197. The :func:`.true` and :func:`.false` constants also feature
  1198. "short circuit" operation within an :func:`.and_` or :func:`.or_`
  1199. conjunction:
  1200. .. sourcecode:: pycon+sql
  1201. >>> print(select(t.c.x).where(or_(t.c.x > 5, true())))
  1202. {printsql}SELECT x FROM t WHERE true{stop}
  1203. >>> print(select(t.c.x).where(and_(t.c.x > 5, false())))
  1204. {printsql}SELECT x FROM t WHERE false{stop}
  1205. .. seealso::
  1206. :func:`.false`
  1207. """
  1208. return True_._instance()
  1209. def tuple_(
  1210. *clauses: _ColumnExpressionArgument[Any],
  1211. types: Optional[Sequence[_TypeEngineArgument[Any]]] = None,
  1212. ) -> Tuple:
  1213. """Return a :class:`.Tuple`.
  1214. Main usage is to produce a composite IN construct using
  1215. :meth:`.ColumnOperators.in_` ::
  1216. from sqlalchemy import tuple_
  1217. tuple_(table.c.col1, table.c.col2).in_([(1, 2), (5, 12), (10, 19)])
  1218. .. versionchanged:: 1.3.6 Added support for SQLite IN tuples.
  1219. .. warning::
  1220. The composite IN construct is not supported by all backends, and is
  1221. currently known to work on PostgreSQL, MySQL, and SQLite.
  1222. Unsupported backends will raise a subclass of
  1223. :class:`~sqlalchemy.exc.DBAPIError` when such an expression is
  1224. invoked.
  1225. """
  1226. return Tuple(*clauses, types=types)
  1227. def type_coerce(
  1228. expression: _ColumnExpressionOrLiteralArgument[Any],
  1229. type_: _TypeEngineArgument[_T],
  1230. ) -> TypeCoerce[_T]:
  1231. r"""Associate a SQL expression with a particular type, without rendering
  1232. ``CAST``.
  1233. E.g.::
  1234. from sqlalchemy import type_coerce
  1235. stmt = select(type_coerce(log_table.date_string, StringDateTime()))
  1236. The above construct will produce a :class:`.TypeCoerce` object, which
  1237. does not modify the rendering in any way on the SQL side, with the
  1238. possible exception of a generated label if used in a columns clause
  1239. context:
  1240. .. sourcecode:: sql
  1241. SELECT date_string AS date_string FROM log
  1242. When result rows are fetched, the ``StringDateTime`` type processor
  1243. will be applied to result rows on behalf of the ``date_string`` column.
  1244. .. note:: the :func:`.type_coerce` construct does not render any
  1245. SQL syntax of its own, including that it does not imply
  1246. parenthesization. Please use :meth:`.TypeCoerce.self_group`
  1247. if explicit parenthesization is required.
  1248. In order to provide a named label for the expression, use
  1249. :meth:`_expression.ColumnElement.label`::
  1250. stmt = select(
  1251. type_coerce(log_table.date_string, StringDateTime()).label("date")
  1252. )
  1253. A type that features bound-value handling will also have that behavior
  1254. take effect when literal values or :func:`.bindparam` constructs are
  1255. passed to :func:`.type_coerce` as targets.
  1256. For example, if a type implements the
  1257. :meth:`.TypeEngine.bind_expression`
  1258. method or :meth:`.TypeEngine.bind_processor` method or equivalent,
  1259. these functions will take effect at statement compilation/execution
  1260. time when a literal value is passed, as in::
  1261. # bound-value handling of MyStringType will be applied to the
  1262. # literal value "some string"
  1263. stmt = select(type_coerce("some string", MyStringType))
  1264. When using :func:`.type_coerce` with composed expressions, note that
  1265. **parenthesis are not applied**. If :func:`.type_coerce` is being
  1266. used in an operator context where the parenthesis normally present from
  1267. CAST are necessary, use the :meth:`.TypeCoerce.self_group` method:
  1268. .. sourcecode:: pycon+sql
  1269. >>> some_integer = column("someint", Integer)
  1270. >>> some_string = column("somestr", String)
  1271. >>> expr = type_coerce(some_integer + 5, String) + some_string
  1272. >>> print(expr)
  1273. {printsql}someint + :someint_1 || somestr{stop}
  1274. >>> expr = type_coerce(some_integer + 5, String).self_group() + some_string
  1275. >>> print(expr)
  1276. {printsql}(someint + :someint_1) || somestr{stop}
  1277. :param expression: A SQL expression, such as a
  1278. :class:`_expression.ColumnElement`
  1279. expression or a Python string which will be coerced into a bound
  1280. literal value.
  1281. :param type\_: A :class:`.TypeEngine` class or instance indicating
  1282. the type to which the expression is coerced.
  1283. .. seealso::
  1284. :ref:`tutorial_casts`
  1285. :func:`.cast`
  1286. """ # noqa
  1287. return TypeCoerce(expression, type_)
  1288. def within_group(
  1289. element: FunctionElement[_T], *order_by: _ColumnExpressionArgument[Any]
  1290. ) -> WithinGroup[_T]:
  1291. r"""Produce a :class:`.WithinGroup` object against a function.
  1292. Used against so-called "ordered set aggregate" and "hypothetical
  1293. set aggregate" functions, including :class:`.percentile_cont`,
  1294. :class:`.rank`, :class:`.dense_rank`, etc.
  1295. :func:`_expression.within_group` is usually called using
  1296. the :meth:`.FunctionElement.within_group` method, e.g.::
  1297. from sqlalchemy import within_group
  1298. stmt = select(
  1299. department.c.id,
  1300. func.percentile_cont(0.5).within_group(department.c.salary.desc()),
  1301. )
  1302. The above statement would produce SQL similar to
  1303. ``SELECT department.id, percentile_cont(0.5)
  1304. WITHIN GROUP (ORDER BY department.salary DESC)``.
  1305. :param element: a :class:`.FunctionElement` construct, typically
  1306. generated by :data:`~.expression.func`.
  1307. :param \*order_by: one or more column elements that will be used
  1308. as the ORDER BY clause of the WITHIN GROUP construct.
  1309. .. seealso::
  1310. :ref:`tutorial_functions_within_group` - in the
  1311. :ref:`unified_tutorial`
  1312. :data:`.expression.func`
  1313. :func:`_expression.over`
  1314. """
  1315. return WithinGroup(element, *order_by)