elements.py 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553
  1. # sql/elements.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. """Core SQL expression elements, including :class:`_expression.ClauseElement`,
  9. :class:`_expression.ColumnElement`, and derived classes.
  10. """
  11. from __future__ import annotations
  12. from decimal import Decimal
  13. from enum import Enum
  14. import itertools
  15. import operator
  16. import re
  17. import typing
  18. from typing import AbstractSet
  19. from typing import Any
  20. from typing import Callable
  21. from typing import cast
  22. from typing import Dict
  23. from typing import FrozenSet
  24. from typing import Generic
  25. from typing import Iterable
  26. from typing import Iterator
  27. from typing import List
  28. from typing import Mapping
  29. from typing import Optional
  30. from typing import overload
  31. from typing import Sequence
  32. from typing import Set
  33. from typing import Tuple as typing_Tuple
  34. from typing import Type
  35. from typing import TYPE_CHECKING
  36. from typing import TypeVar
  37. from typing import Union
  38. from . import coercions
  39. from . import operators
  40. from . import roles
  41. from . import traversals
  42. from . import type_api
  43. from ._typing import has_schema_attr
  44. from ._typing import is_named_from_clause
  45. from ._typing import is_quoted_name
  46. from ._typing import is_tuple_type
  47. from .annotation import Annotated
  48. from .annotation import SupportsWrappingAnnotations
  49. from .base import _clone
  50. from .base import _expand_cloned
  51. from .base import _generative
  52. from .base import _NoArg
  53. from .base import Executable
  54. from .base import Generative
  55. from .base import HasMemoized
  56. from .base import Immutable
  57. from .base import NO_ARG
  58. from .base import SingletonConstant
  59. from .cache_key import MemoizedHasCacheKey
  60. from .cache_key import NO_CACHE
  61. from .coercions import _document_text_coercion # noqa
  62. from .operators import ColumnOperators
  63. from .traversals import HasCopyInternals
  64. from .visitors import cloned_traverse
  65. from .visitors import ExternallyTraversible
  66. from .visitors import InternalTraversal
  67. from .visitors import traverse
  68. from .visitors import Visitable
  69. from .. import exc
  70. from .. import inspection
  71. from .. import util
  72. from ..util import HasMemoized_ro_memoized_attribute
  73. from ..util import TypingOnly
  74. from ..util.typing import Literal
  75. from ..util.typing import ParamSpec
  76. from ..util.typing import Self
  77. if typing.TYPE_CHECKING:
  78. from ._typing import _ByArgument
  79. from ._typing import _ColumnExpressionArgument
  80. from ._typing import _ColumnExpressionOrStrLabelArgument
  81. from ._typing import _HasDialect
  82. from ._typing import _InfoType
  83. from ._typing import _PropagateAttrsType
  84. from ._typing import _TypeEngineArgument
  85. from .base import _EntityNamespace
  86. from .base import ColumnSet
  87. from .cache_key import _CacheKeyTraversalType
  88. from .cache_key import CacheKey
  89. from .compiler import Compiled
  90. from .compiler import SQLCompiler
  91. from .functions import FunctionElement
  92. from .operators import OperatorType
  93. from .schema import Column
  94. from .schema import DefaultGenerator
  95. from .schema import FetchedValue
  96. from .schema import ForeignKey
  97. from .selectable import _SelectIterable
  98. from .selectable import FromClause
  99. from .selectable import NamedFromClause
  100. from .selectable import TextualSelect
  101. from .sqltypes import TupleType
  102. from .type_api import TypeEngine
  103. from .visitors import _CloneCallableType
  104. from .visitors import _TraverseInternalsType
  105. from .visitors import anon_map
  106. from ..engine import Connection
  107. from ..engine import Dialect
  108. from ..engine.interfaces import _CoreMultiExecuteParams
  109. from ..engine.interfaces import CacheStats
  110. from ..engine.interfaces import CompiledCacheType
  111. from ..engine.interfaces import CoreExecuteOptionsParameter
  112. from ..engine.interfaces import SchemaTranslateMapType
  113. from ..engine.result import Result
  114. _NUMERIC = Union[float, Decimal]
  115. _NUMBER = Union[float, int, Decimal]
  116. _T = TypeVar("_T", bound="Any")
  117. _T_co = TypeVar("_T_co", bound=Any, covariant=True)
  118. _OPT = TypeVar("_OPT", bound="Any")
  119. _NT = TypeVar("_NT", bound="_NUMERIC")
  120. _NMT = TypeVar("_NMT", bound="_NUMBER")
  121. @overload
  122. def literal(
  123. value: Any,
  124. type_: _TypeEngineArgument[_T],
  125. literal_execute: bool = False,
  126. ) -> BindParameter[_T]: ...
  127. @overload
  128. def literal(
  129. value: _T,
  130. type_: None = None,
  131. literal_execute: bool = False,
  132. ) -> BindParameter[_T]: ...
  133. @overload
  134. def literal(
  135. value: Any,
  136. type_: Optional[_TypeEngineArgument[Any]] = None,
  137. literal_execute: bool = False,
  138. ) -> BindParameter[Any]: ...
  139. def literal(
  140. value: Any,
  141. type_: Optional[_TypeEngineArgument[Any]] = None,
  142. literal_execute: bool = False,
  143. ) -> BindParameter[Any]:
  144. r"""Return a literal clause, bound to a bind parameter.
  145. Literal clauses are created automatically when non-
  146. :class:`_expression.ClauseElement` objects (such as strings, ints, dates,
  147. etc.) are
  148. used in a comparison operation with a :class:`_expression.ColumnElement`
  149. subclass,
  150. such as a :class:`~sqlalchemy.schema.Column` object. Use this function
  151. to force the generation of a literal clause, which will be created as a
  152. :class:`BindParameter` with a bound value.
  153. :param value: the value to be bound. Can be any Python object supported by
  154. the underlying DB-API, or is translatable via the given type argument.
  155. :param type\_: an optional :class:`~sqlalchemy.types.TypeEngine` which will
  156. provide bind-parameter translation for this literal.
  157. :param literal_execute: optional bool, when True, the SQL engine will
  158. attempt to render the bound value directly in the SQL statement at
  159. execution time rather than providing as a parameter value.
  160. .. versionadded:: 2.0
  161. """
  162. return coercions.expect(
  163. roles.LiteralValueRole,
  164. value,
  165. type_=type_,
  166. literal_execute=literal_execute,
  167. )
  168. def literal_column(
  169. text: str, type_: Optional[_TypeEngineArgument[_T]] = None
  170. ) -> ColumnClause[_T]:
  171. r"""Produce a :class:`.ColumnClause` object that has the
  172. :paramref:`_expression.column.is_literal` flag set to True.
  173. :func:`_expression.literal_column` is similar to
  174. :func:`_expression.column`, except that
  175. it is more often used as a "standalone" column expression that renders
  176. exactly as stated; while :func:`_expression.column`
  177. stores a string name that
  178. will be assumed to be part of a table and may be quoted as such,
  179. :func:`_expression.literal_column` can be that,
  180. or any other arbitrary column-oriented
  181. expression.
  182. :param text: the text of the expression; can be any SQL expression.
  183. Quoting rules will not be applied. To specify a column-name expression
  184. which should be subject to quoting rules, use the :func:`column`
  185. function.
  186. :param type\_: an optional :class:`~sqlalchemy.types.TypeEngine`
  187. object which will
  188. provide result-set translation and additional expression semantics for
  189. this column. If left as ``None`` the type will be :class:`.NullType`.
  190. .. seealso::
  191. :func:`_expression.column`
  192. :func:`_expression.text`
  193. :ref:`tutorial_select_arbitrary_text`
  194. """
  195. return ColumnClause(text, type_=type_, is_literal=True)
  196. class CompilerElement(Visitable):
  197. """base class for SQL elements that can be compiled to produce a
  198. SQL string.
  199. .. versionadded:: 2.0
  200. """
  201. __slots__ = ()
  202. __visit_name__ = "compiler_element"
  203. supports_execution = False
  204. stringify_dialect = "default"
  205. @util.preload_module("sqlalchemy.engine.default")
  206. @util.preload_module("sqlalchemy.engine.url")
  207. def compile(
  208. self,
  209. bind: Optional[_HasDialect] = None,
  210. dialect: Optional[Dialect] = None,
  211. **kw: Any,
  212. ) -> Compiled:
  213. """Compile this SQL expression.
  214. The return value is a :class:`~.Compiled` object.
  215. Calling ``str()`` or ``unicode()`` on the returned value will yield a
  216. string representation of the result. The
  217. :class:`~.Compiled` object also can return a
  218. dictionary of bind parameter names and values
  219. using the ``params`` accessor.
  220. :param bind: An :class:`.Connection` or :class:`.Engine` which
  221. can provide a :class:`.Dialect` in order to generate a
  222. :class:`.Compiled` object. If the ``bind`` and
  223. ``dialect`` parameters are both omitted, a default SQL compiler
  224. is used.
  225. :param column_keys: Used for INSERT and UPDATE statements, a list of
  226. column names which should be present in the VALUES clause of the
  227. compiled statement. If ``None``, all columns from the target table
  228. object are rendered.
  229. :param dialect: A :class:`.Dialect` instance which can generate
  230. a :class:`.Compiled` object. This argument takes precedence over
  231. the ``bind`` argument.
  232. :param compile_kwargs: optional dictionary of additional parameters
  233. that will be passed through to the compiler within all "visit"
  234. methods. This allows any custom flag to be passed through to
  235. a custom compilation construct, for example. It is also used
  236. for the case of passing the ``literal_binds`` flag through::
  237. from sqlalchemy.sql import table, column, select
  238. t = table("t", column("x"))
  239. s = select(t).where(t.c.x == 5)
  240. print(s.compile(compile_kwargs={"literal_binds": True}))
  241. .. seealso::
  242. :ref:`faq_sql_expression_string`
  243. """
  244. if dialect is None:
  245. if bind:
  246. dialect = bind.dialect
  247. elif self.stringify_dialect == "default":
  248. dialect = self._default_dialect()
  249. else:
  250. url = util.preloaded.engine_url
  251. dialect = url.URL.create(
  252. self.stringify_dialect
  253. ).get_dialect()()
  254. return self._compiler(dialect, **kw)
  255. def _default_dialect(self):
  256. default = util.preloaded.engine_default
  257. return default.StrCompileDialect()
  258. def _compiler(self, dialect: Dialect, **kw: Any) -> Compiled:
  259. """Return a compiler appropriate for this ClauseElement, given a
  260. Dialect."""
  261. if TYPE_CHECKING:
  262. assert isinstance(self, ClauseElement)
  263. return dialect.statement_compiler(dialect, self, **kw)
  264. def __str__(self) -> str:
  265. return str(self.compile())
  266. @inspection._self_inspects
  267. class ClauseElement(
  268. SupportsWrappingAnnotations,
  269. MemoizedHasCacheKey,
  270. HasCopyInternals,
  271. ExternallyTraversible,
  272. CompilerElement,
  273. ):
  274. """Base class for elements of a programmatically constructed SQL
  275. expression.
  276. """
  277. __visit_name__ = "clause"
  278. if TYPE_CHECKING:
  279. @util.memoized_property
  280. def _propagate_attrs(self) -> _PropagateAttrsType:
  281. """like annotations, however these propagate outwards liberally
  282. as SQL constructs are built, and are set up at construction time.
  283. """
  284. ...
  285. else:
  286. _propagate_attrs = util.EMPTY_DICT
  287. @util.ro_memoized_property
  288. def description(self) -> Optional[str]:
  289. return None
  290. _is_clone_of: Optional[Self] = None
  291. is_clause_element = True
  292. is_selectable = False
  293. is_dml = False
  294. _is_column_element = False
  295. _is_keyed_column_element = False
  296. _is_table = False
  297. _gen_static_annotations_cache_key = False
  298. _is_textual = False
  299. _is_from_clause = False
  300. _is_returns_rows = False
  301. _is_text_clause = False
  302. _is_from_container = False
  303. _is_select_container = False
  304. _is_select_base = False
  305. _is_select_statement = False
  306. _is_bind_parameter = False
  307. _is_clause_list = False
  308. _is_lambda_element = False
  309. _is_singleton_constant = False
  310. _is_immutable = False
  311. _is_star = False
  312. @property
  313. def _order_by_label_element(self) -> Optional[Label[Any]]:
  314. return None
  315. _cache_key_traversal: _CacheKeyTraversalType = None
  316. negation_clause: ColumnElement[bool]
  317. if typing.TYPE_CHECKING:
  318. def get_children(
  319. self, *, omit_attrs: typing_Tuple[str, ...] = ..., **kw: Any
  320. ) -> Iterable[ClauseElement]: ...
  321. @util.ro_non_memoized_property
  322. def _from_objects(self) -> List[FromClause]:
  323. return []
  324. def _set_propagate_attrs(self, values: Mapping[str, Any]) -> Self:
  325. # usually, self._propagate_attrs is empty here. one case where it's
  326. # not is a subquery against ORM select, that is then pulled as a
  327. # property of an aliased class. should all be good
  328. # assert not self._propagate_attrs
  329. self._propagate_attrs = util.immutabledict(values)
  330. return self
  331. def _default_compiler(self) -> SQLCompiler:
  332. dialect = self._default_dialect()
  333. return dialect.statement_compiler(dialect, self) # type: ignore
  334. def _clone(self, **kw: Any) -> Self:
  335. """Create a shallow copy of this ClauseElement.
  336. This method may be used by a generative API. Its also used as
  337. part of the "deep" copy afforded by a traversal that combines
  338. the _copy_internals() method.
  339. """
  340. skip = self._memoized_keys
  341. c = self.__class__.__new__(self.__class__)
  342. if skip:
  343. # ensure this iteration remains atomic
  344. c.__dict__ = {
  345. k: v for k, v in self.__dict__.copy().items() if k not in skip
  346. }
  347. else:
  348. c.__dict__ = self.__dict__.copy()
  349. # this is a marker that helps to "equate" clauses to each other
  350. # when a Select returns its list of FROM clauses. the cloning
  351. # process leaves around a lot of remnants of the previous clause
  352. # typically in the form of column expressions still attached to the
  353. # old table.
  354. cc = self._is_clone_of
  355. c._is_clone_of = cc if cc is not None else self
  356. return c
  357. def _negate_in_binary(self, negated_op, original_op):
  358. """a hook to allow the right side of a binary expression to respond
  359. to a negation of the binary expression.
  360. Used for the special case of expanding bind parameter with IN.
  361. """
  362. return self
  363. def _with_binary_element_type(self, type_):
  364. """in the context of binary expression, convert the type of this
  365. object to the one given.
  366. applies only to :class:`_expression.ColumnElement` classes.
  367. """
  368. return self
  369. @property
  370. def _constructor(self): # type: ignore[override]
  371. """return the 'constructor' for this ClauseElement.
  372. This is for the purposes for creating a new object of
  373. this type. Usually, its just the element's __class__.
  374. However, the "Annotated" version of the object overrides
  375. to return the class of its proxied element.
  376. """
  377. return self.__class__
  378. @HasMemoized.memoized_attribute
  379. def _cloned_set(self):
  380. """Return the set consisting all cloned ancestors of this
  381. ClauseElement.
  382. Includes this ClauseElement. This accessor tends to be used for
  383. FromClause objects to identify 'equivalent' FROM clauses, regardless
  384. of transformative operations.
  385. """
  386. s = util.column_set()
  387. f: Optional[ClauseElement] = self
  388. # note this creates a cycle, asserted in test_memusage. however,
  389. # turning this into a plain @property adds tends of thousands of method
  390. # calls to Core / ORM performance tests, so the small overhead
  391. # introduced by the relatively small amount of short term cycles
  392. # produced here is preferable
  393. while f is not None:
  394. s.add(f)
  395. f = f._is_clone_of
  396. return s
  397. def _de_clone(self):
  398. while self._is_clone_of is not None:
  399. self = self._is_clone_of
  400. return self
  401. @util.ro_non_memoized_property
  402. def entity_namespace(self) -> _EntityNamespace:
  403. raise AttributeError(
  404. "This SQL expression has no entity namespace "
  405. "with which to filter from."
  406. )
  407. def __getstate__(self):
  408. d = self.__dict__.copy()
  409. d.pop("_is_clone_of", None)
  410. d.pop("_generate_cache_key", None)
  411. return d
  412. def _execute_on_connection(
  413. self,
  414. connection: Connection,
  415. distilled_params: _CoreMultiExecuteParams,
  416. execution_options: CoreExecuteOptionsParameter,
  417. ) -> Result[Any]:
  418. if self.supports_execution:
  419. if TYPE_CHECKING:
  420. assert isinstance(self, Executable)
  421. return connection._execute_clauseelement(
  422. self, distilled_params, execution_options
  423. )
  424. else:
  425. raise exc.ObjectNotExecutableError(self)
  426. def _execute_on_scalar(
  427. self,
  428. connection: Connection,
  429. distilled_params: _CoreMultiExecuteParams,
  430. execution_options: CoreExecuteOptionsParameter,
  431. ) -> Any:
  432. """an additional hook for subclasses to provide a different
  433. implementation for connection.scalar() vs. connection.execute().
  434. .. versionadded:: 2.0
  435. """
  436. return self._execute_on_connection(
  437. connection, distilled_params, execution_options
  438. ).scalar()
  439. def _get_embedded_bindparams(self) -> Sequence[BindParameter[Any]]:
  440. """Return the list of :class:`.BindParameter` objects embedded in the
  441. object.
  442. This accomplishes the same purpose as ``visitors.traverse()`` or
  443. similar would provide, however by making use of the cache key
  444. it takes advantage of memoization of the key to result in fewer
  445. net method calls, assuming the statement is also going to be
  446. executed.
  447. """
  448. key = self._generate_cache_key()
  449. if key is None:
  450. bindparams: List[BindParameter[Any]] = []
  451. traverse(self, {}, {"bindparam": bindparams.append})
  452. return bindparams
  453. else:
  454. return key.bindparams
  455. def unique_params(
  456. self,
  457. __optionaldict: Optional[Dict[str, Any]] = None,
  458. **kwargs: Any,
  459. ) -> Self:
  460. """Return a copy with :func:`_expression.bindparam` elements
  461. replaced.
  462. Same functionality as :meth:`_expression.ClauseElement.params`,
  463. except adds `unique=True`
  464. to affected bind parameters so that multiple statements can be
  465. used.
  466. """
  467. return self._replace_params(True, __optionaldict, kwargs)
  468. def params(
  469. self,
  470. __optionaldict: Optional[Mapping[str, Any]] = None,
  471. **kwargs: Any,
  472. ) -> Self:
  473. """Return a copy with :func:`_expression.bindparam` elements
  474. replaced.
  475. Returns a copy of this ClauseElement with
  476. :func:`_expression.bindparam`
  477. elements replaced with values taken from the given dictionary::
  478. >>> clause = column("x") + bindparam("foo")
  479. >>> print(clause.compile().params)
  480. {'foo':None}
  481. >>> print(clause.params({"foo": 7}).compile().params)
  482. {'foo':7}
  483. """
  484. return self._replace_params(False, __optionaldict, kwargs)
  485. def _replace_params(
  486. self,
  487. unique: bool,
  488. optionaldict: Optional[Mapping[str, Any]],
  489. kwargs: Dict[str, Any],
  490. ) -> Self:
  491. if optionaldict:
  492. kwargs.update(optionaldict)
  493. def visit_bindparam(bind: BindParameter[Any]) -> None:
  494. if bind.key in kwargs:
  495. bind.value = kwargs[bind.key]
  496. bind.required = False
  497. if unique:
  498. bind._convert_to_unique()
  499. return cloned_traverse(
  500. self,
  501. {"maintain_key": True, "detect_subquery_cols": True},
  502. {"bindparam": visit_bindparam},
  503. )
  504. def compare(self, other: ClauseElement, **kw: Any) -> bool:
  505. r"""Compare this :class:`_expression.ClauseElement` to
  506. the given :class:`_expression.ClauseElement`.
  507. Subclasses should override the default behavior, which is a
  508. straight identity comparison.
  509. \**kw are arguments consumed by subclass ``compare()`` methods and
  510. may be used to modify the criteria for comparison
  511. (see :class:`_expression.ColumnElement`).
  512. """
  513. return traversals.compare(self, other, **kw)
  514. def self_group(
  515. self, against: Optional[OperatorType] = None
  516. ) -> ClauseElement:
  517. """Apply a 'grouping' to this :class:`_expression.ClauseElement`.
  518. This method is overridden by subclasses to return a "grouping"
  519. construct, i.e. parenthesis. In particular it's used by "binary"
  520. expressions to provide a grouping around themselves when placed into a
  521. larger expression, as well as by :func:`_expression.select`
  522. constructs when placed into the FROM clause of another
  523. :func:`_expression.select`. (Note that subqueries should be
  524. normally created using the :meth:`_expression.Select.alias` method,
  525. as many
  526. platforms require nested SELECT statements to be named).
  527. As expressions are composed together, the application of
  528. :meth:`self_group` is automatic - end-user code should never
  529. need to use this method directly. Note that SQLAlchemy's
  530. clause constructs take operator precedence into account -
  531. so parenthesis might not be needed, for example, in
  532. an expression like ``x OR (y AND z)`` - AND takes precedence
  533. over OR.
  534. The base :meth:`self_group` method of
  535. :class:`_expression.ClauseElement`
  536. just returns self.
  537. """
  538. return self
  539. def _ungroup(self) -> ClauseElement:
  540. """Return this :class:`_expression.ClauseElement`
  541. without any groupings.
  542. """
  543. return self
  544. def _compile_w_cache(
  545. self,
  546. dialect: Dialect,
  547. *,
  548. compiled_cache: Optional[CompiledCacheType],
  549. column_keys: List[str],
  550. for_executemany: bool = False,
  551. schema_translate_map: Optional[SchemaTranslateMapType] = None,
  552. **kw: Any,
  553. ) -> typing_Tuple[
  554. Compiled, Optional[Sequence[BindParameter[Any]]], CacheStats
  555. ]:
  556. elem_cache_key: Optional[CacheKey]
  557. if compiled_cache is not None and dialect._supports_statement_cache:
  558. elem_cache_key = self._generate_cache_key()
  559. else:
  560. elem_cache_key = None
  561. extracted_params: Optional[Sequence[BindParameter[Any]]]
  562. if elem_cache_key is not None:
  563. if TYPE_CHECKING:
  564. assert compiled_cache is not None
  565. cache_key, extracted_params = elem_cache_key
  566. key = (
  567. dialect,
  568. cache_key,
  569. tuple(column_keys),
  570. bool(schema_translate_map),
  571. for_executemany,
  572. )
  573. compiled_sql = compiled_cache.get(key)
  574. if compiled_sql is None:
  575. cache_hit = dialect.CACHE_MISS
  576. compiled_sql = self._compiler(
  577. dialect,
  578. cache_key=elem_cache_key,
  579. column_keys=column_keys,
  580. for_executemany=for_executemany,
  581. schema_translate_map=schema_translate_map,
  582. **kw,
  583. )
  584. compiled_cache[key] = compiled_sql
  585. else:
  586. cache_hit = dialect.CACHE_HIT
  587. else:
  588. extracted_params = None
  589. compiled_sql = self._compiler(
  590. dialect,
  591. cache_key=elem_cache_key,
  592. column_keys=column_keys,
  593. for_executemany=for_executemany,
  594. schema_translate_map=schema_translate_map,
  595. **kw,
  596. )
  597. if not dialect._supports_statement_cache:
  598. cache_hit = dialect.NO_DIALECT_SUPPORT
  599. elif compiled_cache is None:
  600. cache_hit = dialect.CACHING_DISABLED
  601. else:
  602. cache_hit = dialect.NO_CACHE_KEY
  603. return compiled_sql, extracted_params, cache_hit
  604. def __invert__(self):
  605. # undocumented element currently used by the ORM for
  606. # relationship.contains()
  607. if hasattr(self, "negation_clause"):
  608. return self.negation_clause
  609. else:
  610. return self._negate()
  611. def _negate(self) -> ClauseElement:
  612. # TODO: this code is uncovered and in all likelihood is not included
  613. # in any codepath. So this should raise NotImplementedError in 2.1
  614. grouped = self.self_group(against=operators.inv)
  615. assert isinstance(grouped, ColumnElement)
  616. return UnaryExpression(grouped, operator=operators.inv)
  617. def __bool__(self):
  618. raise TypeError("Boolean value of this clause is not defined")
  619. def __repr__(self):
  620. friendly = self.description
  621. if friendly is None:
  622. return object.__repr__(self)
  623. else:
  624. return "<%s.%s at 0x%x; %s>" % (
  625. self.__module__,
  626. self.__class__.__name__,
  627. id(self),
  628. friendly,
  629. )
  630. class DQLDMLClauseElement(ClauseElement):
  631. """represents a :class:`.ClauseElement` that compiles to a DQL or DML
  632. expression, not DDL.
  633. .. versionadded:: 2.0
  634. """
  635. if typing.TYPE_CHECKING:
  636. def _compiler(self, dialect: Dialect, **kw: Any) -> SQLCompiler:
  637. """Return a compiler appropriate for this ClauseElement, given a
  638. Dialect."""
  639. ...
  640. def compile( # noqa: A001
  641. self,
  642. bind: Optional[_HasDialect] = None,
  643. dialect: Optional[Dialect] = None,
  644. **kw: Any,
  645. ) -> SQLCompiler: ...
  646. class CompilerColumnElement(
  647. roles.DMLColumnRole,
  648. roles.DDLConstraintColumnRole,
  649. roles.ColumnsClauseRole,
  650. CompilerElement,
  651. ):
  652. """A compiler-only column element used for ad-hoc string compilations.
  653. .. versionadded:: 2.0
  654. """
  655. __slots__ = ()
  656. _propagate_attrs = util.EMPTY_DICT
  657. _is_collection_aggregate = False
  658. # SQLCoreOperations should be suiting the ExpressionElementRole
  659. # and ColumnsClauseRole. however the MRO issues become too elaborate
  660. # at the moment.
  661. class SQLCoreOperations(Generic[_T_co], ColumnOperators, TypingOnly):
  662. __slots__ = ()
  663. # annotations for comparison methods
  664. # these are from operators->Operators / ColumnOperators,
  665. # redefined with the specific types returned by ColumnElement hierarchies
  666. if typing.TYPE_CHECKING:
  667. @util.non_memoized_property
  668. def _propagate_attrs(self) -> _PropagateAttrsType: ...
  669. def operate(
  670. self, op: OperatorType, *other: Any, **kwargs: Any
  671. ) -> ColumnElement[Any]: ...
  672. def reverse_operate(
  673. self, op: OperatorType, other: Any, **kwargs: Any
  674. ) -> ColumnElement[Any]: ...
  675. @overload
  676. def op(
  677. self,
  678. opstring: str,
  679. precedence: int = ...,
  680. is_comparison: bool = ...,
  681. *,
  682. return_type: _TypeEngineArgument[_OPT],
  683. python_impl: Optional[Callable[..., Any]] = None,
  684. ) -> Callable[[Any], BinaryExpression[_OPT]]: ...
  685. @overload
  686. def op(
  687. self,
  688. opstring: str,
  689. precedence: int = ...,
  690. is_comparison: bool = ...,
  691. return_type: Optional[_TypeEngineArgument[Any]] = ...,
  692. python_impl: Optional[Callable[..., Any]] = ...,
  693. ) -> Callable[[Any], BinaryExpression[Any]]: ...
  694. def op(
  695. self,
  696. opstring: str,
  697. precedence: int = 0,
  698. is_comparison: bool = False,
  699. return_type: Optional[_TypeEngineArgument[Any]] = None,
  700. python_impl: Optional[Callable[..., Any]] = None,
  701. ) -> Callable[[Any], BinaryExpression[Any]]: ...
  702. def bool_op(
  703. self,
  704. opstring: str,
  705. precedence: int = 0,
  706. python_impl: Optional[Callable[..., Any]] = None,
  707. ) -> Callable[[Any], BinaryExpression[bool]]: ...
  708. def __and__(self, other: Any) -> BooleanClauseList: ...
  709. def __or__(self, other: Any) -> BooleanClauseList: ...
  710. def __invert__(self) -> ColumnElement[_T_co]: ...
  711. def __lt__(self, other: Any) -> ColumnElement[bool]: ...
  712. def __le__(self, other: Any) -> ColumnElement[bool]: ...
  713. # declare also that this class has an hash method otherwise
  714. # it may be assumed to be None by type checkers since the
  715. # object defines __eq__ and python sets it to None in that case:
  716. # https://docs.python.org/3/reference/datamodel.html#object.__hash__
  717. def __hash__(self) -> int: ...
  718. def __eq__(self, other: Any) -> ColumnElement[bool]: # type: ignore[override] # noqa: E501
  719. ...
  720. def __ne__(self, other: Any) -> ColumnElement[bool]: # type: ignore[override] # noqa: E501
  721. ...
  722. def is_distinct_from(self, other: Any) -> ColumnElement[bool]: ...
  723. def is_not_distinct_from(self, other: Any) -> ColumnElement[bool]: ...
  724. def __gt__(self, other: Any) -> ColumnElement[bool]: ...
  725. def __ge__(self, other: Any) -> ColumnElement[bool]: ...
  726. def __neg__(self) -> UnaryExpression[_T_co]: ...
  727. def __contains__(self, other: Any) -> ColumnElement[bool]: ...
  728. def __getitem__(self, index: Any) -> ColumnElement[Any]: ...
  729. @overload
  730. def __lshift__(self: _SQO[int], other: Any) -> ColumnElement[int]: ...
  731. @overload
  732. def __lshift__(self, other: Any) -> ColumnElement[Any]: ...
  733. def __lshift__(self, other: Any) -> ColumnElement[Any]: ...
  734. @overload
  735. def __rshift__(self: _SQO[int], other: Any) -> ColumnElement[int]: ...
  736. @overload
  737. def __rshift__(self, other: Any) -> ColumnElement[Any]: ...
  738. def __rshift__(self, other: Any) -> ColumnElement[Any]: ...
  739. @overload
  740. def concat(self: _SQO[str], other: Any) -> ColumnElement[str]: ...
  741. @overload
  742. def concat(self, other: Any) -> ColumnElement[Any]: ...
  743. def concat(self, other: Any) -> ColumnElement[Any]: ...
  744. def like(
  745. self, other: Any, escape: Optional[str] = None
  746. ) -> BinaryExpression[bool]: ...
  747. def ilike(
  748. self, other: Any, escape: Optional[str] = None
  749. ) -> BinaryExpression[bool]: ...
  750. def bitwise_xor(self, other: Any) -> BinaryExpression[Any]: ...
  751. def bitwise_or(self, other: Any) -> BinaryExpression[Any]: ...
  752. def bitwise_and(self, other: Any) -> BinaryExpression[Any]: ...
  753. def bitwise_not(self) -> UnaryExpression[_T_co]: ...
  754. def bitwise_lshift(self, other: Any) -> BinaryExpression[Any]: ...
  755. def bitwise_rshift(self, other: Any) -> BinaryExpression[Any]: ...
  756. def in_(
  757. self,
  758. other: Union[
  759. Iterable[Any], BindParameter[Any], roles.InElementRole
  760. ],
  761. ) -> BinaryExpression[bool]: ...
  762. def not_in(
  763. self,
  764. other: Union[
  765. Iterable[Any], BindParameter[Any], roles.InElementRole
  766. ],
  767. ) -> BinaryExpression[bool]: ...
  768. def notin_(
  769. self,
  770. other: Union[
  771. Iterable[Any], BindParameter[Any], roles.InElementRole
  772. ],
  773. ) -> BinaryExpression[bool]: ...
  774. def not_like(
  775. self, other: Any, escape: Optional[str] = None
  776. ) -> BinaryExpression[bool]: ...
  777. def notlike(
  778. self, other: Any, escape: Optional[str] = None
  779. ) -> BinaryExpression[bool]: ...
  780. def not_ilike(
  781. self, other: Any, escape: Optional[str] = None
  782. ) -> BinaryExpression[bool]: ...
  783. def notilike(
  784. self, other: Any, escape: Optional[str] = None
  785. ) -> BinaryExpression[bool]: ...
  786. def is_(self, other: Any) -> BinaryExpression[bool]: ...
  787. def is_not(self, other: Any) -> BinaryExpression[bool]: ...
  788. def isnot(self, other: Any) -> BinaryExpression[bool]: ...
  789. def startswith(
  790. self,
  791. other: Any,
  792. escape: Optional[str] = None,
  793. autoescape: bool = False,
  794. ) -> ColumnElement[bool]: ...
  795. def istartswith(
  796. self,
  797. other: Any,
  798. escape: Optional[str] = None,
  799. autoescape: bool = False,
  800. ) -> ColumnElement[bool]: ...
  801. def endswith(
  802. self,
  803. other: Any,
  804. escape: Optional[str] = None,
  805. autoescape: bool = False,
  806. ) -> ColumnElement[bool]: ...
  807. def iendswith(
  808. self,
  809. other: Any,
  810. escape: Optional[str] = None,
  811. autoescape: bool = False,
  812. ) -> ColumnElement[bool]: ...
  813. def contains(self, other: Any, **kw: Any) -> ColumnElement[bool]: ...
  814. def icontains(self, other: Any, **kw: Any) -> ColumnElement[bool]: ...
  815. def match(self, other: Any, **kwargs: Any) -> ColumnElement[bool]: ...
  816. def regexp_match(
  817. self, pattern: Any, flags: Optional[str] = None
  818. ) -> ColumnElement[bool]: ...
  819. def regexp_replace(
  820. self, pattern: Any, replacement: Any, flags: Optional[str] = None
  821. ) -> ColumnElement[str]: ...
  822. def desc(self) -> UnaryExpression[_T_co]: ...
  823. def asc(self) -> UnaryExpression[_T_co]: ...
  824. def nulls_first(self) -> UnaryExpression[_T_co]: ...
  825. def nullsfirst(self) -> UnaryExpression[_T_co]: ...
  826. def nulls_last(self) -> UnaryExpression[_T_co]: ...
  827. def nullslast(self) -> UnaryExpression[_T_co]: ...
  828. def collate(self, collation: str) -> CollationClause: ...
  829. def between(
  830. self, cleft: Any, cright: Any, symmetric: bool = False
  831. ) -> BinaryExpression[bool]: ...
  832. def distinct(self: _SQO[_T_co]) -> UnaryExpression[_T_co]: ...
  833. def any_(self) -> CollectionAggregate[Any]: ...
  834. def all_(self) -> CollectionAggregate[Any]: ...
  835. # numeric overloads. These need more tweaking
  836. # in particular they all need to have a variant for Optiona[_T]
  837. # because Optional only applies to the data side, not the expression
  838. # side
  839. @overload
  840. def __add__(
  841. self: _SQO[_NMT],
  842. other: Any,
  843. ) -> ColumnElement[_NMT]: ...
  844. @overload
  845. def __add__(
  846. self: _SQO[str],
  847. other: Any,
  848. ) -> ColumnElement[str]: ...
  849. @overload
  850. def __add__(self, other: Any) -> ColumnElement[Any]: ...
  851. def __add__(self, other: Any) -> ColumnElement[Any]: ...
  852. @overload
  853. def __radd__(self: _SQO[_NMT], other: Any) -> ColumnElement[_NMT]: ...
  854. @overload
  855. def __radd__(self: _SQO[str], other: Any) -> ColumnElement[str]: ...
  856. def __radd__(self, other: Any) -> ColumnElement[Any]: ...
  857. @overload
  858. def __sub__(
  859. self: _SQO[_NMT],
  860. other: Any,
  861. ) -> ColumnElement[_NMT]: ...
  862. @overload
  863. def __sub__(self, other: Any) -> ColumnElement[Any]: ...
  864. def __sub__(self, other: Any) -> ColumnElement[Any]: ...
  865. @overload
  866. def __rsub__(
  867. self: _SQO[_NMT],
  868. other: Any,
  869. ) -> ColumnElement[_NMT]: ...
  870. @overload
  871. def __rsub__(self, other: Any) -> ColumnElement[Any]: ...
  872. def __rsub__(self, other: Any) -> ColumnElement[Any]: ...
  873. @overload
  874. def __mul__(
  875. self: _SQO[_NMT],
  876. other: Any,
  877. ) -> ColumnElement[_NMT]: ...
  878. @overload
  879. def __mul__(self, other: Any) -> ColumnElement[Any]: ...
  880. def __mul__(self, other: Any) -> ColumnElement[Any]: ...
  881. @overload
  882. def __rmul__(
  883. self: _SQO[_NMT],
  884. other: Any,
  885. ) -> ColumnElement[_NMT]: ...
  886. @overload
  887. def __rmul__(self, other: Any) -> ColumnElement[Any]: ...
  888. def __rmul__(self, other: Any) -> ColumnElement[Any]: ...
  889. @overload
  890. def __mod__(self: _SQO[_NMT], other: Any) -> ColumnElement[_NMT]: ...
  891. @overload
  892. def __mod__(self, other: Any) -> ColumnElement[Any]: ...
  893. def __mod__(self, other: Any) -> ColumnElement[Any]: ...
  894. @overload
  895. def __rmod__(self: _SQO[_NMT], other: Any) -> ColumnElement[_NMT]: ...
  896. @overload
  897. def __rmod__(self, other: Any) -> ColumnElement[Any]: ...
  898. def __rmod__(self, other: Any) -> ColumnElement[Any]: ...
  899. @overload
  900. def __truediv__(
  901. self: _SQO[int], other: Any
  902. ) -> ColumnElement[_NUMERIC]: ...
  903. @overload
  904. def __truediv__(self: _SQO[_NT], other: Any) -> ColumnElement[_NT]: ...
  905. @overload
  906. def __truediv__(self, other: Any) -> ColumnElement[Any]: ...
  907. def __truediv__(self, other: Any) -> ColumnElement[Any]: ...
  908. @overload
  909. def __rtruediv__(
  910. self: _SQO[_NMT], other: Any
  911. ) -> ColumnElement[_NUMERIC]: ...
  912. @overload
  913. def __rtruediv__(self, other: Any) -> ColumnElement[Any]: ...
  914. def __rtruediv__(self, other: Any) -> ColumnElement[Any]: ...
  915. @overload
  916. def __floordiv__(
  917. self: _SQO[_NMT], other: Any
  918. ) -> ColumnElement[_NMT]: ...
  919. @overload
  920. def __floordiv__(self, other: Any) -> ColumnElement[Any]: ...
  921. def __floordiv__(self, other: Any) -> ColumnElement[Any]: ...
  922. @overload
  923. def __rfloordiv__(
  924. self: _SQO[_NMT], other: Any
  925. ) -> ColumnElement[_NMT]: ...
  926. @overload
  927. def __rfloordiv__(self, other: Any) -> ColumnElement[Any]: ...
  928. def __rfloordiv__(self, other: Any) -> ColumnElement[Any]: ...
  929. class SQLColumnExpression(
  930. SQLCoreOperations[_T_co], roles.ExpressionElementRole[_T_co], TypingOnly
  931. ):
  932. """A type that may be used to indicate any SQL column element or object
  933. that acts in place of one.
  934. :class:`.SQLColumnExpression` is a base of
  935. :class:`.ColumnElement`, as well as within the bases of ORM elements
  936. such as :class:`.InstrumentedAttribute`, and may be used in :pep:`484`
  937. typing to indicate arguments or return values that should behave
  938. as column expressions.
  939. .. versionadded:: 2.0.0b4
  940. """
  941. __slots__ = ()
  942. _SQO = SQLCoreOperations
  943. class ColumnElement(
  944. roles.ColumnArgumentOrKeyRole,
  945. roles.StatementOptionRole,
  946. roles.WhereHavingRole,
  947. roles.BinaryElementRole[_T],
  948. roles.OrderByRole,
  949. roles.ColumnsClauseRole,
  950. roles.LimitOffsetRole,
  951. roles.DMLColumnRole,
  952. roles.DDLConstraintColumnRole,
  953. roles.DDLExpressionRole,
  954. SQLColumnExpression[_T],
  955. DQLDMLClauseElement,
  956. ):
  957. """Represent a column-oriented SQL expression suitable for usage in the
  958. "columns" clause, WHERE clause etc. of a statement.
  959. While the most familiar kind of :class:`_expression.ColumnElement` is the
  960. :class:`_schema.Column` object, :class:`_expression.ColumnElement`
  961. serves as the basis
  962. for any unit that may be present in a SQL expression, including
  963. the expressions themselves, SQL functions, bound parameters,
  964. literal expressions, keywords such as ``NULL``, etc.
  965. :class:`_expression.ColumnElement`
  966. is the ultimate base class for all such elements.
  967. A wide variety of SQLAlchemy Core functions work at the SQL expression
  968. level, and are intended to accept instances of
  969. :class:`_expression.ColumnElement` as
  970. arguments. These functions will typically document that they accept a
  971. "SQL expression" as an argument. What this means in terms of SQLAlchemy
  972. usually refers to an input which is either already in the form of a
  973. :class:`_expression.ColumnElement` object,
  974. or a value which can be **coerced** into
  975. one. The coercion rules followed by most, but not all, SQLAlchemy Core
  976. functions with regards to SQL expressions are as follows:
  977. * a literal Python value, such as a string, integer or floating
  978. point value, boolean, datetime, ``Decimal`` object, or virtually
  979. any other Python object, will be coerced into a "literal bound
  980. value". This generally means that a :func:`.bindparam` will be
  981. produced featuring the given value embedded into the construct; the
  982. resulting :class:`.BindParameter` object is an instance of
  983. :class:`_expression.ColumnElement`.
  984. The Python value will ultimately be sent
  985. to the DBAPI at execution time as a parameterized argument to the
  986. ``execute()`` or ``executemany()`` methods, after SQLAlchemy
  987. type-specific converters (e.g. those provided by any associated
  988. :class:`.TypeEngine` objects) are applied to the value.
  989. * any special object value, typically ORM-level constructs, which
  990. feature an accessor called ``__clause_element__()``. The Core
  991. expression system looks for this method when an object of otherwise
  992. unknown type is passed to a function that is looking to coerce the
  993. argument into a :class:`_expression.ColumnElement` and sometimes a
  994. :class:`_expression.SelectBase` expression.
  995. It is used within the ORM to
  996. convert from ORM-specific objects like mapped classes and
  997. mapped attributes into Core expression objects.
  998. * The Python ``None`` value is typically interpreted as ``NULL``,
  999. which in SQLAlchemy Core produces an instance of :func:`.null`.
  1000. A :class:`_expression.ColumnElement` provides the ability to generate new
  1001. :class:`_expression.ColumnElement`
  1002. objects using Python expressions. This means that Python operators
  1003. such as ``==``, ``!=`` and ``<`` are overloaded to mimic SQL operations,
  1004. and allow the instantiation of further :class:`_expression.ColumnElement`
  1005. instances
  1006. which are composed from other, more fundamental
  1007. :class:`_expression.ColumnElement`
  1008. objects. For example, two :class:`.ColumnClause` objects can be added
  1009. together with the addition operator ``+`` to produce
  1010. a :class:`.BinaryExpression`.
  1011. Both :class:`.ColumnClause` and :class:`.BinaryExpression` are subclasses
  1012. of :class:`_expression.ColumnElement`:
  1013. .. sourcecode:: pycon+sql
  1014. >>> from sqlalchemy.sql import column
  1015. >>> column("a") + column("b")
  1016. <sqlalchemy.sql.expression.BinaryExpression object at 0x101029dd0>
  1017. >>> print(column("a") + column("b"))
  1018. {printsql}a + b
  1019. .. seealso::
  1020. :class:`_schema.Column`
  1021. :func:`_expression.column`
  1022. """
  1023. __visit_name__ = "column_element"
  1024. primary_key: bool = False
  1025. _is_clone_of: Optional[ColumnElement[_T]]
  1026. _is_column_element = True
  1027. _insert_sentinel: bool = False
  1028. _omit_from_statements = False
  1029. _is_collection_aggregate = False
  1030. foreign_keys: AbstractSet[ForeignKey] = frozenset()
  1031. @util.memoized_property
  1032. def _proxies(self) -> List[ColumnElement[Any]]:
  1033. return []
  1034. @util.non_memoized_property
  1035. def _tq_label(self) -> Optional[str]:
  1036. """The named label that can be used to target
  1037. this column in a result set in a "table qualified" context.
  1038. This label is almost always the label used when
  1039. rendering <expr> AS <label> in a SELECT statement when using
  1040. the LABEL_STYLE_TABLENAME_PLUS_COL label style, which is what the
  1041. legacy ORM ``Query`` object uses as well.
  1042. For a regular Column bound to a Table, this is typically the label
  1043. <tablename>_<columnname>. For other constructs, different rules
  1044. may apply, such as anonymized labels and others.
  1045. .. versionchanged:: 1.4.21 renamed from ``._label``
  1046. """
  1047. return None
  1048. key: Optional[str] = None
  1049. """The 'key' that in some circumstances refers to this object in a
  1050. Python namespace.
  1051. This typically refers to the "key" of the column as present in the
  1052. ``.c`` collection of a selectable, e.g. ``sometable.c["somekey"]`` would
  1053. return a :class:`_schema.Column` with a ``.key`` of "somekey".
  1054. """
  1055. @HasMemoized.memoized_attribute
  1056. def _tq_key_label(self) -> Optional[str]:
  1057. """A label-based version of 'key' that in some circumstances refers
  1058. to this object in a Python namespace.
  1059. _tq_key_label comes into play when a select() statement is constructed
  1060. with apply_labels(); in this case, all Column objects in the ``.c``
  1061. collection are rendered as <tablename>_<columnname> in SQL; this is
  1062. essentially the value of ._label. But to locate those columns in the
  1063. ``.c`` collection, the name is along the lines of <tablename>_<key>;
  1064. that's the typical value of .key_label.
  1065. .. versionchanged:: 1.4.21 renamed from ``._key_label``
  1066. """
  1067. return self._proxy_key
  1068. @property
  1069. def _key_label(self) -> Optional[str]:
  1070. """legacy; renamed to _tq_key_label"""
  1071. return self._tq_key_label
  1072. @property
  1073. def _label(self) -> Optional[str]:
  1074. """legacy; renamed to _tq_label"""
  1075. return self._tq_label
  1076. @property
  1077. def _non_anon_label(self) -> Optional[str]:
  1078. """the 'name' that naturally applies this element when rendered in
  1079. SQL.
  1080. Concretely, this is the "name" of a column or a label in a
  1081. SELECT statement; ``<columnname>`` and ``<labelname>`` below:
  1082. .. sourcecode:: sql
  1083. SELECT <columnmame> FROM table
  1084. SELECT column AS <labelname> FROM table
  1085. Above, the two names noted will be what's present in the DBAPI
  1086. ``cursor.description`` as the names.
  1087. If this attribute returns ``None``, it means that the SQL element as
  1088. written does not have a 100% fully predictable "name" that would appear
  1089. in the ``cursor.description``. Examples include SQL functions, CAST
  1090. functions, etc. While such things do return names in
  1091. ``cursor.description``, they are only predictable on a
  1092. database-specific basis; e.g. an expression like ``MAX(table.col)`` may
  1093. appear as the string ``max`` on one database (like PostgreSQL) or may
  1094. appear as the whole expression ``max(table.col)`` on SQLite.
  1095. The default implementation looks for a ``.name`` attribute on the
  1096. object, as has been the precedent established in SQLAlchemy for many
  1097. years. An exception is made on the ``FunctionElement`` subclass
  1098. so that the return value is always ``None``.
  1099. .. versionadded:: 1.4.21
  1100. """
  1101. return getattr(self, "name", None)
  1102. _render_label_in_columns_clause = True
  1103. """A flag used by select._columns_plus_names that helps to determine
  1104. we are actually going to render in terms of "SELECT <col> AS <label>".
  1105. This flag can be returned as False for some Column objects that want
  1106. to be rendered as simple "SELECT <col>"; typically columns that don't have
  1107. any parent table and are named the same as what the label would be
  1108. in any case.
  1109. """
  1110. _allow_label_resolve = True
  1111. """A flag that can be flipped to prevent a column from being resolvable
  1112. by string label name.
  1113. The joined eager loader strategy in the ORM uses this, for example.
  1114. """
  1115. _is_implicitly_boolean = False
  1116. _alt_names: Sequence[str] = ()
  1117. if TYPE_CHECKING:
  1118. def _ungroup(self) -> ColumnElement[_T]: ...
  1119. @overload
  1120. def self_group(self, against: None = None) -> ColumnElement[_T]: ...
  1121. @overload
  1122. def self_group(
  1123. self, against: Optional[OperatorType] = None
  1124. ) -> ColumnElement[Any]: ...
  1125. def self_group(
  1126. self, against: Optional[OperatorType] = None
  1127. ) -> ColumnElement[Any]:
  1128. if (
  1129. against in (operators.and_, operators.or_, operators._asbool)
  1130. and self.type._type_affinity is type_api.BOOLEANTYPE._type_affinity
  1131. ):
  1132. return AsBoolean(self, operators.is_true, operators.is_false)
  1133. elif against in (operators.any_op, operators.all_op):
  1134. return Grouping(self)
  1135. else:
  1136. return self
  1137. @overload
  1138. def _negate(self: ColumnElement[bool]) -> ColumnElement[bool]: ...
  1139. @overload
  1140. def _negate(self: ColumnElement[_T]) -> ColumnElement[_T]: ...
  1141. def _negate(self) -> ColumnElement[Any]:
  1142. if self.type._type_affinity is type_api.BOOLEANTYPE._type_affinity:
  1143. return AsBoolean(self, operators.is_false, operators.is_true)
  1144. else:
  1145. grouped = self.self_group(against=operators.inv)
  1146. assert isinstance(grouped, ColumnElement)
  1147. return UnaryExpression(
  1148. grouped,
  1149. operator=operators.inv,
  1150. )
  1151. type: TypeEngine[_T]
  1152. if not TYPE_CHECKING:
  1153. @util.memoized_property
  1154. def type(self) -> TypeEngine[_T]: # noqa: A001
  1155. # used for delayed setup of
  1156. # type_api
  1157. return type_api.NULLTYPE
  1158. @HasMemoized.memoized_attribute
  1159. def comparator(self) -> TypeEngine.Comparator[_T]:
  1160. try:
  1161. comparator_factory = self.type.comparator_factory
  1162. except AttributeError as err:
  1163. raise TypeError(
  1164. "Object %r associated with '.type' attribute "
  1165. "is not a TypeEngine class or object" % self.type
  1166. ) from err
  1167. else:
  1168. return comparator_factory(self)
  1169. def __setstate__(self, state):
  1170. self.__dict__.update(state)
  1171. def __getattr__(self, key: str) -> Any:
  1172. try:
  1173. return getattr(self.comparator, key)
  1174. except AttributeError as err:
  1175. raise AttributeError(
  1176. "Neither %r object nor %r object has an attribute %r"
  1177. % (
  1178. type(self).__name__,
  1179. type(self.comparator).__name__,
  1180. key,
  1181. )
  1182. ) from err
  1183. def operate(
  1184. self,
  1185. op: operators.OperatorType,
  1186. *other: Any,
  1187. **kwargs: Any,
  1188. ) -> ColumnElement[Any]:
  1189. return op(self.comparator, *other, **kwargs) # type: ignore[no-any-return] # noqa: E501
  1190. def reverse_operate(
  1191. self, op: operators.OperatorType, other: Any, **kwargs: Any
  1192. ) -> ColumnElement[Any]:
  1193. return op(other, self.comparator, **kwargs) # type: ignore[no-any-return] # noqa: E501
  1194. def _bind_param(
  1195. self,
  1196. operator: operators.OperatorType,
  1197. obj: Any,
  1198. type_: Optional[TypeEngine[_T]] = None,
  1199. expanding: bool = False,
  1200. ) -> BindParameter[_T]:
  1201. return BindParameter(
  1202. None,
  1203. obj,
  1204. _compared_to_operator=operator,
  1205. type_=type_,
  1206. _compared_to_type=self.type,
  1207. unique=True,
  1208. expanding=expanding,
  1209. )
  1210. @property
  1211. def expression(self) -> ColumnElement[Any]:
  1212. """Return a column expression.
  1213. Part of the inspection interface; returns self.
  1214. """
  1215. return self
  1216. @property
  1217. def _select_iterable(self) -> _SelectIterable:
  1218. return (self,)
  1219. @util.memoized_property
  1220. def base_columns(self) -> FrozenSet[ColumnElement[Any]]:
  1221. return frozenset(c for c in self.proxy_set if not c._proxies)
  1222. @util.memoized_property
  1223. def proxy_set(self) -> FrozenSet[ColumnElement[Any]]:
  1224. """set of all columns we are proxying
  1225. as of 2.0 this is explicitly deannotated columns. previously it was
  1226. effectively deannotated columns but wasn't enforced. annotated
  1227. columns should basically not go into sets if at all possible because
  1228. their hashing behavior is very non-performant.
  1229. """
  1230. return frozenset([self._deannotate()]).union(
  1231. itertools.chain(*[c.proxy_set for c in self._proxies])
  1232. )
  1233. @util.memoized_property
  1234. def _expanded_proxy_set(self) -> FrozenSet[ColumnElement[Any]]:
  1235. return frozenset(_expand_cloned(self.proxy_set))
  1236. def _uncached_proxy_list(self) -> List[ColumnElement[Any]]:
  1237. """An 'uncached' version of proxy set.
  1238. This list includes annotated columns which perform very poorly in
  1239. set operations.
  1240. """
  1241. return [self] + list(
  1242. itertools.chain(*[c._uncached_proxy_list() for c in self._proxies])
  1243. )
  1244. def shares_lineage(self, othercolumn: ColumnElement[Any]) -> bool:
  1245. """Return True if the given :class:`_expression.ColumnElement`
  1246. has a common ancestor to this :class:`_expression.ColumnElement`."""
  1247. return bool(self.proxy_set.intersection(othercolumn.proxy_set))
  1248. def _compare_name_for_result(self, other: ColumnElement[Any]) -> bool:
  1249. """Return True if the given column element compares to this one
  1250. when targeting within a result row."""
  1251. return (
  1252. hasattr(other, "name")
  1253. and hasattr(self, "name")
  1254. and other.name == self.name
  1255. )
  1256. @HasMemoized.memoized_attribute
  1257. def _proxy_key(self) -> Optional[str]:
  1258. if self._annotations and "proxy_key" in self._annotations:
  1259. return cast(str, self._annotations["proxy_key"])
  1260. name = self.key
  1261. if not name:
  1262. # there's a bit of a seeming contradiction which is that the
  1263. # "_non_anon_label" of a column can in fact be an
  1264. # "_anonymous_label"; this is when it's on a column that is
  1265. # proxying for an anonymous expression in a subquery.
  1266. name = self._non_anon_label
  1267. if isinstance(name, _anonymous_label):
  1268. return None
  1269. else:
  1270. return name
  1271. @HasMemoized.memoized_attribute
  1272. def _expression_label(self) -> Optional[str]:
  1273. """a suggested label to use in the case that the column has no name,
  1274. which should be used if possible as the explicit 'AS <label>'
  1275. where this expression would normally have an anon label.
  1276. this is essentially mostly what _proxy_key does except it returns
  1277. None if the column has a normal name that can be used.
  1278. """
  1279. if getattr(self, "name", None) is not None:
  1280. return None
  1281. elif self._annotations and "proxy_key" in self._annotations:
  1282. return cast(str, self._annotations["proxy_key"])
  1283. else:
  1284. return None
  1285. def _make_proxy(
  1286. self,
  1287. selectable: FromClause,
  1288. *,
  1289. primary_key: ColumnSet,
  1290. foreign_keys: Set[KeyedColumnElement[Any]],
  1291. name: Optional[str] = None,
  1292. key: Optional[str] = None,
  1293. name_is_truncatable: bool = False,
  1294. compound_select_cols: Optional[Sequence[ColumnElement[Any]]] = None,
  1295. **kw: Any,
  1296. ) -> typing_Tuple[str, ColumnClause[_T]]:
  1297. """Create a new :class:`_expression.ColumnElement` representing this
  1298. :class:`_expression.ColumnElement` as it appears in the select list of
  1299. a descending selectable.
  1300. """
  1301. if name is None:
  1302. name = self._anon_name_label
  1303. if key is None:
  1304. key = self._proxy_key
  1305. else:
  1306. key = name
  1307. assert key is not None
  1308. co: ColumnClause[_T] = ColumnClause(
  1309. (
  1310. coercions.expect(roles.TruncatedLabelRole, name)
  1311. if name_is_truncatable
  1312. else name
  1313. ),
  1314. type_=getattr(self, "type", None),
  1315. _selectable=selectable,
  1316. )
  1317. co._propagate_attrs = selectable._propagate_attrs
  1318. if compound_select_cols:
  1319. co._proxies = list(compound_select_cols)
  1320. else:
  1321. co._proxies = [self]
  1322. if selectable._is_clone_of is not None:
  1323. co._is_clone_of = selectable._is_clone_of.columns.get(key)
  1324. return key, co
  1325. def cast(self, type_: _TypeEngineArgument[_OPT]) -> Cast[_OPT]:
  1326. """Produce a type cast, i.e. ``CAST(<expression> AS <type>)``.
  1327. This is a shortcut to the :func:`_expression.cast` function.
  1328. .. seealso::
  1329. :ref:`tutorial_casts`
  1330. :func:`_expression.cast`
  1331. :func:`_expression.type_coerce`
  1332. """
  1333. return Cast(self, type_)
  1334. def label(self, name: Optional[str]) -> Label[_T]:
  1335. """Produce a column label, i.e. ``<columnname> AS <name>``.
  1336. This is a shortcut to the :func:`_expression.label` function.
  1337. If 'name' is ``None``, an anonymous label name will be generated.
  1338. """
  1339. return Label(name, self, self.type)
  1340. def _anon_label(
  1341. self, seed: Optional[str], add_hash: Optional[int] = None
  1342. ) -> _anonymous_label:
  1343. while self._is_clone_of is not None:
  1344. self = self._is_clone_of
  1345. # as of 1.4 anonymous label for ColumnElement uses hash(), not id(),
  1346. # as the identifier, because a column and its annotated version are
  1347. # the same thing in a SQL statement
  1348. hash_value = hash(self)
  1349. if add_hash:
  1350. # this path is used for disambiguating anon labels that would
  1351. # otherwise be the same name for the same element repeated.
  1352. # an additional numeric value is factored in for each label.
  1353. # shift hash(self) (which is id(self), typically 8 byte integer)
  1354. # 16 bits leftward. fill extra add_hash on right
  1355. assert add_hash < (2 << 15)
  1356. assert seed
  1357. hash_value = (hash_value << 16) | add_hash
  1358. # extra underscore is added for labels with extra hash
  1359. # values, to isolate the "deduped anon" namespace from the
  1360. # regular namespace. eliminates chance of these
  1361. # manufactured hash values overlapping with regular ones for some
  1362. # undefined python interpreter
  1363. seed = seed + "_"
  1364. if isinstance(seed, _anonymous_label):
  1365. return _anonymous_label.safe_construct(
  1366. hash_value, "", enclosing_label=seed
  1367. )
  1368. return _anonymous_label.safe_construct(hash_value, seed or "anon")
  1369. @util.memoized_property
  1370. def _anon_name_label(self) -> str:
  1371. """Provides a constant 'anonymous label' for this ColumnElement.
  1372. This is a label() expression which will be named at compile time.
  1373. The same label() is returned each time ``anon_label`` is called so
  1374. that expressions can reference ``anon_label`` multiple times,
  1375. producing the same label name at compile time.
  1376. The compiler uses this function automatically at compile time
  1377. for expressions that are known to be 'unnamed' like binary
  1378. expressions and function calls.
  1379. .. versionchanged:: 1.4.9 - this attribute was not intended to be
  1380. public and is renamed to _anon_name_label. anon_name exists
  1381. for backwards compat
  1382. """
  1383. name = getattr(self, "name", None)
  1384. return self._anon_label(name)
  1385. @util.memoized_property
  1386. def _anon_key_label(self) -> _anonymous_label:
  1387. """Provides a constant 'anonymous key label' for this ColumnElement.
  1388. Compare to ``anon_label``, except that the "key" of the column,
  1389. if available, is used to generate the label.
  1390. This is used when a deduplicating key is placed into the columns
  1391. collection of a selectable.
  1392. .. versionchanged:: 1.4.9 - this attribute was not intended to be
  1393. public and is renamed to _anon_key_label. anon_key_label exists
  1394. for backwards compat
  1395. """
  1396. return self._anon_label(self._proxy_key)
  1397. @property
  1398. @util.deprecated(
  1399. "1.4",
  1400. "The :attr:`_expression.ColumnElement.anon_label` attribute is now "
  1401. "private, and the public accessor is deprecated.",
  1402. )
  1403. def anon_label(self) -> str:
  1404. return self._anon_name_label
  1405. @property
  1406. @util.deprecated(
  1407. "1.4",
  1408. "The :attr:`_expression.ColumnElement.anon_key_label` attribute is "
  1409. "now private, and the public accessor is deprecated.",
  1410. )
  1411. def anon_key_label(self) -> str:
  1412. return self._anon_key_label
  1413. def _dedupe_anon_label_idx(self, idx: int) -> str:
  1414. """label to apply to a column that is anon labeled, but repeated
  1415. in the SELECT, so that we have to make an "extra anon" label that
  1416. disambiguates it from the previous appearance.
  1417. these labels come out like "foo_bar_id__1" and have double underscores
  1418. in them.
  1419. """
  1420. label = getattr(self, "name", None)
  1421. # current convention is that if the element doesn't have a
  1422. # ".name" (usually because it is not NamedColumn), we try to
  1423. # use a "table qualified" form for the "dedupe anon" label,
  1424. # based on the notion that a label like
  1425. # "CAST(casttest.v1 AS DECIMAL) AS casttest_v1__1" looks better than
  1426. # "CAST(casttest.v1 AS DECIMAL) AS anon__1"
  1427. if label is None:
  1428. return self._dedupe_anon_tq_label_idx(idx)
  1429. else:
  1430. return self._anon_label(label, add_hash=idx)
  1431. @util.memoized_property
  1432. def _anon_tq_label(self) -> _anonymous_label:
  1433. return self._anon_label(getattr(self, "_tq_label", None))
  1434. @util.memoized_property
  1435. def _anon_tq_key_label(self) -> _anonymous_label:
  1436. return self._anon_label(getattr(self, "_tq_key_label", None))
  1437. def _dedupe_anon_tq_label_idx(self, idx: int) -> _anonymous_label:
  1438. label = getattr(self, "_tq_label", None) or "anon"
  1439. return self._anon_label(label, add_hash=idx)
  1440. class KeyedColumnElement(ColumnElement[_T]):
  1441. """ColumnElement where ``.key`` is non-None."""
  1442. _is_keyed_column_element = True
  1443. key: str
  1444. class WrapsColumnExpression(ColumnElement[_T]):
  1445. """Mixin that defines a :class:`_expression.ColumnElement`
  1446. as a wrapper with special
  1447. labeling behavior for an expression that already has a name.
  1448. .. versionadded:: 1.4
  1449. .. seealso::
  1450. :ref:`change_4449`
  1451. """
  1452. @property
  1453. def wrapped_column_expression(self) -> ColumnElement[_T]:
  1454. raise NotImplementedError()
  1455. @util.non_memoized_property
  1456. def _tq_label(self) -> Optional[str]:
  1457. wce = self.wrapped_column_expression
  1458. if hasattr(wce, "_tq_label"):
  1459. return wce._tq_label
  1460. else:
  1461. return None
  1462. @property
  1463. def _label(self) -> Optional[str]:
  1464. return self._tq_label
  1465. @property
  1466. def _non_anon_label(self) -> Optional[str]:
  1467. return None
  1468. @util.non_memoized_property
  1469. def _anon_name_label(self) -> str:
  1470. wce = self.wrapped_column_expression
  1471. # this logic tries to get the WrappedColumnExpression to render
  1472. # with "<expr> AS <name>", where "<name>" is the natural name
  1473. # within the expression itself. e.g. "CAST(table.foo) AS foo".
  1474. if not wce._is_text_clause:
  1475. nal = wce._non_anon_label
  1476. if nal:
  1477. return nal
  1478. elif hasattr(wce, "_anon_name_label"):
  1479. return wce._anon_name_label
  1480. return super()._anon_name_label
  1481. def _dedupe_anon_label_idx(self, idx: int) -> str:
  1482. wce = self.wrapped_column_expression
  1483. nal = wce._non_anon_label
  1484. if nal:
  1485. return self._anon_label(nal + "_")
  1486. else:
  1487. return self._dedupe_anon_tq_label_idx(idx)
  1488. @property
  1489. def _proxy_key(self):
  1490. wce = self.wrapped_column_expression
  1491. if not wce._is_text_clause:
  1492. return wce._proxy_key
  1493. return super()._proxy_key
  1494. class BindParameter(roles.InElementRole, KeyedColumnElement[_T]):
  1495. r"""Represent a "bound expression".
  1496. :class:`.BindParameter` is invoked explicitly using the
  1497. :func:`.bindparam` function, as in::
  1498. from sqlalchemy import bindparam
  1499. stmt = select(users_table).where(
  1500. users_table.c.name == bindparam("username")
  1501. )
  1502. Detailed discussion of how :class:`.BindParameter` is used is
  1503. at :func:`.bindparam`.
  1504. .. seealso::
  1505. :func:`.bindparam`
  1506. """
  1507. __visit_name__ = "bindparam"
  1508. _traverse_internals: _TraverseInternalsType = [
  1509. ("key", InternalTraversal.dp_anon_name),
  1510. ("type", InternalTraversal.dp_type),
  1511. ("callable", InternalTraversal.dp_plain_dict),
  1512. ("value", InternalTraversal.dp_plain_obj),
  1513. ("literal_execute", InternalTraversal.dp_boolean),
  1514. ]
  1515. key: str
  1516. type: TypeEngine[_T]
  1517. value: Optional[_T]
  1518. _is_crud = False
  1519. _is_bind_parameter = True
  1520. _key_is_anon = False
  1521. # bindparam implements its own _gen_cache_key() method however
  1522. # we check subclasses for this flag, else no cache key is generated
  1523. inherit_cache = True
  1524. def __init__(
  1525. self,
  1526. key: Optional[str],
  1527. value: Any = _NoArg.NO_ARG,
  1528. type_: Optional[_TypeEngineArgument[_T]] = None,
  1529. unique: bool = False,
  1530. required: Union[bool, Literal[_NoArg.NO_ARG]] = _NoArg.NO_ARG,
  1531. quote: Optional[bool] = None,
  1532. callable_: Optional[Callable[[], Any]] = None,
  1533. expanding: bool = False,
  1534. isoutparam: bool = False,
  1535. literal_execute: bool = False,
  1536. _compared_to_operator: Optional[OperatorType] = None,
  1537. _compared_to_type: Optional[TypeEngine[Any]] = None,
  1538. _is_crud: bool = False,
  1539. ):
  1540. if required is _NoArg.NO_ARG:
  1541. required = value is _NoArg.NO_ARG and callable_ is None
  1542. if value is _NoArg.NO_ARG:
  1543. value = None
  1544. if quote is not None:
  1545. key = quoted_name.construct(key, quote)
  1546. if unique:
  1547. self.key = _anonymous_label.safe_construct(
  1548. id(self),
  1549. (
  1550. key
  1551. if key is not None
  1552. and not isinstance(key, _anonymous_label)
  1553. else "param"
  1554. ),
  1555. sanitize_key=True,
  1556. )
  1557. self._key_is_anon = True
  1558. elif key:
  1559. self.key = key
  1560. else:
  1561. self.key = _anonymous_label.safe_construct(id(self), "param")
  1562. self._key_is_anon = True
  1563. # identifying key that won't change across
  1564. # clones, used to identify the bind's logical
  1565. # identity
  1566. self._identifying_key = self.key
  1567. # key that was passed in the first place, used to
  1568. # generate new keys
  1569. self._orig_key = key or "param"
  1570. self.unique = unique
  1571. self.value = value
  1572. self.callable = callable_
  1573. self.isoutparam = isoutparam
  1574. self.required = required
  1575. # indicate an "expanding" parameter; the compiler sets this
  1576. # automatically in the compiler _render_in_expr_w_bindparam method
  1577. # for an IN expression
  1578. self.expanding = expanding
  1579. # this is another hint to help w/ expanding and is typically
  1580. # set in the compiler _render_in_expr_w_bindparam method for an
  1581. # IN expression
  1582. self.expand_op = None
  1583. self.literal_execute = literal_execute
  1584. if _is_crud:
  1585. self._is_crud = True
  1586. if type_ is None:
  1587. if expanding:
  1588. if value:
  1589. check_value = value[0]
  1590. else:
  1591. check_value = type_api._NO_VALUE_IN_LIST
  1592. else:
  1593. check_value = value
  1594. if _compared_to_type is not None:
  1595. self.type = _compared_to_type.coerce_compared_value(
  1596. _compared_to_operator, check_value
  1597. )
  1598. else:
  1599. self.type = type_api._resolve_value_to_type(check_value)
  1600. elif isinstance(type_, type):
  1601. self.type = type_()
  1602. elif is_tuple_type(type_):
  1603. if value:
  1604. if expanding:
  1605. check_value = value[0]
  1606. else:
  1607. check_value = value
  1608. cast("BindParameter[typing_Tuple[Any, ...]]", self).type = (
  1609. type_._resolve_values_to_types(check_value)
  1610. )
  1611. else:
  1612. cast("BindParameter[typing_Tuple[Any, ...]]", self).type = (
  1613. type_
  1614. )
  1615. else:
  1616. self.type = type_
  1617. def _with_value(self, value, maintain_key=False, required=NO_ARG):
  1618. """Return a copy of this :class:`.BindParameter` with the given value
  1619. set.
  1620. """
  1621. cloned = self._clone(maintain_key=maintain_key)
  1622. cloned.value = value
  1623. cloned.callable = None
  1624. cloned.required = required if required is not NO_ARG else self.required
  1625. if cloned.type is type_api.NULLTYPE:
  1626. cloned.type = type_api._resolve_value_to_type(value)
  1627. return cloned
  1628. @property
  1629. def effective_value(self) -> Optional[_T]:
  1630. """Return the value of this bound parameter,
  1631. taking into account if the ``callable`` parameter
  1632. was set.
  1633. The ``callable`` value will be evaluated
  1634. and returned if present, else ``value``.
  1635. """
  1636. if self.callable:
  1637. # TODO: set up protocol for bind parameter callable
  1638. return self.callable() # type: ignore
  1639. else:
  1640. return self.value
  1641. def render_literal_execute(self) -> BindParameter[_T]:
  1642. """Produce a copy of this bound parameter that will enable the
  1643. :paramref:`_sql.BindParameter.literal_execute` flag.
  1644. The :paramref:`_sql.BindParameter.literal_execute` flag will
  1645. have the effect of the parameter rendered in the compiled SQL
  1646. string using ``[POSTCOMPILE]`` form, which is a special form that
  1647. is converted to be a rendering of the literal value of the parameter
  1648. at SQL execution time. The rationale is to support caching
  1649. of SQL statement strings that can embed per-statement literal values,
  1650. such as LIMIT and OFFSET parameters, in the final SQL string that
  1651. is passed to the DBAPI. Dialects in particular may want to use
  1652. this method within custom compilation schemes.
  1653. .. versionadded:: 1.4.5
  1654. .. seealso::
  1655. :ref:`engine_thirdparty_caching`
  1656. """
  1657. c = ClauseElement._clone(self)
  1658. c.literal_execute = True
  1659. return c
  1660. def _negate_in_binary(self, negated_op, original_op):
  1661. if self.expand_op is original_op:
  1662. bind = self._clone()
  1663. bind.expand_op = negated_op
  1664. return bind
  1665. else:
  1666. return self
  1667. def _with_binary_element_type(self, type_: TypeEngine[Any]) -> Self:
  1668. c: Self = ClauseElement._clone(self)
  1669. c.type = type_
  1670. return c
  1671. def _clone(self, maintain_key: bool = False, **kw: Any) -> Self:
  1672. c = ClauseElement._clone(self, **kw)
  1673. # ensure all the BindParameter objects stay in cloned set.
  1674. # in #7823, we changed "clone" so that a clone only keeps a reference
  1675. # to the "original" element, since for column correspondence, that's
  1676. # all we need. However, for BindParam, _cloned_set is used by
  1677. # the "cache key bind match" lookup, which means if any of those
  1678. # interim BindParameter objects became part of a cache key in the
  1679. # cache, we need it. So here, make sure all clones keep carrying
  1680. # forward.
  1681. c._cloned_set.update(self._cloned_set)
  1682. if not maintain_key and self.unique:
  1683. c.key = _anonymous_label.safe_construct(
  1684. id(c), c._orig_key or "param", sanitize_key=True
  1685. )
  1686. return c
  1687. def _gen_cache_key(self, anon_map, bindparams):
  1688. _gen_cache_ok = self.__class__.__dict__.get("inherit_cache", False)
  1689. if not _gen_cache_ok:
  1690. if anon_map is not None:
  1691. anon_map[NO_CACHE] = True
  1692. return None
  1693. id_, found = anon_map.get_anon(self)
  1694. if found:
  1695. return (id_, self.__class__)
  1696. if bindparams is not None:
  1697. bindparams.append(self)
  1698. return (
  1699. id_,
  1700. self.__class__,
  1701. self.type._static_cache_key,
  1702. self.key % anon_map if self._key_is_anon else self.key,
  1703. self.literal_execute,
  1704. )
  1705. def _convert_to_unique(self):
  1706. if not self.unique:
  1707. self.unique = True
  1708. self.key = _anonymous_label.safe_construct(
  1709. id(self), self._orig_key or "param", sanitize_key=True
  1710. )
  1711. def __getstate__(self):
  1712. """execute a deferred value for serialization purposes."""
  1713. d = self.__dict__.copy()
  1714. v = self.value
  1715. if self.callable:
  1716. v = self.callable()
  1717. d["callable"] = None
  1718. d["value"] = v
  1719. return d
  1720. def __setstate__(self, state):
  1721. if state.get("unique", False):
  1722. state["key"] = _anonymous_label.safe_construct(
  1723. id(self), state.get("_orig_key", "param"), sanitize_key=True
  1724. )
  1725. self.__dict__.update(state)
  1726. def __repr__(self):
  1727. return "%s(%r, %r, type_=%r)" % (
  1728. self.__class__.__name__,
  1729. self.key,
  1730. self.value,
  1731. self.type,
  1732. )
  1733. class TypeClause(DQLDMLClauseElement):
  1734. """Handle a type keyword in a SQL statement.
  1735. Used by the ``Case`` statement.
  1736. """
  1737. __visit_name__ = "typeclause"
  1738. _traverse_internals: _TraverseInternalsType = [
  1739. ("type", InternalTraversal.dp_type)
  1740. ]
  1741. type: TypeEngine[Any]
  1742. def __init__(self, type_: TypeEngine[Any]):
  1743. self.type = type_
  1744. class TextClause(
  1745. roles.DDLConstraintColumnRole,
  1746. roles.DDLExpressionRole,
  1747. roles.StatementOptionRole,
  1748. roles.WhereHavingRole,
  1749. roles.OrderByRole,
  1750. roles.FromClauseRole,
  1751. roles.SelectStatementRole,
  1752. roles.InElementRole,
  1753. Generative,
  1754. Executable,
  1755. DQLDMLClauseElement,
  1756. roles.BinaryElementRole[Any],
  1757. inspection.Inspectable["TextClause"],
  1758. ):
  1759. """Represent a literal SQL text fragment.
  1760. E.g.::
  1761. from sqlalchemy import text
  1762. t = text("SELECT * FROM users")
  1763. result = connection.execute(t)
  1764. The :class:`_expression.TextClause` construct is produced using the
  1765. :func:`_expression.text`
  1766. function; see that function for full documentation.
  1767. .. seealso::
  1768. :func:`_expression.text`
  1769. """
  1770. __visit_name__ = "textclause"
  1771. _traverse_internals: _TraverseInternalsType = [
  1772. ("_bindparams", InternalTraversal.dp_string_clauseelement_dict),
  1773. ("text", InternalTraversal.dp_string),
  1774. ]
  1775. _is_text_clause = True
  1776. _is_textual = True
  1777. _bind_params_regex = re.compile(r"(?<![:\w\x5c]):(\w+)(?!:)", re.UNICODE)
  1778. _is_implicitly_boolean = False
  1779. _render_label_in_columns_clause = False
  1780. _omit_from_statements = False
  1781. _is_collection_aggregate = False
  1782. @property
  1783. def _hide_froms(self) -> Iterable[FromClause]:
  1784. return ()
  1785. def __and__(self, other):
  1786. # support use in select.where(), query.filter()
  1787. return and_(self, other)
  1788. @property
  1789. def _select_iterable(self) -> _SelectIterable:
  1790. return (self,)
  1791. # help in those cases where text() is
  1792. # interpreted in a column expression situation
  1793. key: Optional[str] = None
  1794. _label: Optional[str] = None
  1795. _allow_label_resolve = False
  1796. @property
  1797. def _is_star(self): # type: ignore[override]
  1798. return self.text == "*"
  1799. def __init__(self, text: str):
  1800. self._bindparams: Dict[str, BindParameter[Any]] = {}
  1801. def repl(m):
  1802. self._bindparams[m.group(1)] = BindParameter(m.group(1))
  1803. return ":%s" % m.group(1)
  1804. # scan the string and search for bind parameter names, add them
  1805. # to the list of bindparams
  1806. self.text = self._bind_params_regex.sub(repl, text)
  1807. @_generative
  1808. def bindparams(
  1809. self,
  1810. *binds: BindParameter[Any],
  1811. **names_to_values: Any,
  1812. ) -> Self:
  1813. """Establish the values and/or types of bound parameters within
  1814. this :class:`_expression.TextClause` construct.
  1815. Given a text construct such as::
  1816. from sqlalchemy import text
  1817. stmt = text(
  1818. "SELECT id, name FROM user WHERE name=:name AND timestamp=:timestamp"
  1819. )
  1820. the :meth:`_expression.TextClause.bindparams`
  1821. method can be used to establish
  1822. the initial value of ``:name`` and ``:timestamp``,
  1823. using simple keyword arguments::
  1824. stmt = stmt.bindparams(
  1825. name="jack", timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5)
  1826. )
  1827. Where above, new :class:`.BindParameter` objects
  1828. will be generated with the names ``name`` and ``timestamp``, and
  1829. values of ``jack`` and ``datetime.datetime(2012, 10, 8, 15, 12, 5)``,
  1830. respectively. The types will be
  1831. inferred from the values given, in this case :class:`.String` and
  1832. :class:`.DateTime`.
  1833. When specific typing behavior is needed, the positional ``*binds``
  1834. argument can be used in which to specify :func:`.bindparam` constructs
  1835. directly. These constructs must include at least the ``key``
  1836. argument, then an optional value and type::
  1837. from sqlalchemy import bindparam
  1838. stmt = stmt.bindparams(
  1839. bindparam("name", value="jack", type_=String),
  1840. bindparam("timestamp", type_=DateTime),
  1841. )
  1842. Above, we specified the type of :class:`.DateTime` for the
  1843. ``timestamp`` bind, and the type of :class:`.String` for the ``name``
  1844. bind. In the case of ``name`` we also set the default value of
  1845. ``"jack"``.
  1846. Additional bound parameters can be supplied at statement execution
  1847. time, e.g.::
  1848. result = connection.execute(
  1849. stmt, timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5)
  1850. )
  1851. The :meth:`_expression.TextClause.bindparams`
  1852. method can be called repeatedly,
  1853. where it will re-use existing :class:`.BindParameter` objects to add
  1854. new information. For example, we can call
  1855. :meth:`_expression.TextClause.bindparams`
  1856. first with typing information, and a
  1857. second time with value information, and it will be combined::
  1858. stmt = text(
  1859. "SELECT id, name FROM user WHERE name=:name "
  1860. "AND timestamp=:timestamp"
  1861. )
  1862. stmt = stmt.bindparams(
  1863. bindparam("name", type_=String), bindparam("timestamp", type_=DateTime)
  1864. )
  1865. stmt = stmt.bindparams(
  1866. name="jack", timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5)
  1867. )
  1868. The :meth:`_expression.TextClause.bindparams`
  1869. method also supports the concept of
  1870. **unique** bound parameters. These are parameters that are
  1871. "uniquified" on name at statement compilation time, so that multiple
  1872. :func:`_expression.text`
  1873. constructs may be combined together without the names
  1874. conflicting. To use this feature, specify the
  1875. :paramref:`.BindParameter.unique` flag on each :func:`.bindparam`
  1876. object::
  1877. stmt1 = text("select id from table where name=:name").bindparams(
  1878. bindparam("name", value="name1", unique=True)
  1879. )
  1880. stmt2 = text("select id from table where name=:name").bindparams(
  1881. bindparam("name", value="name2", unique=True)
  1882. )
  1883. union = union_all(stmt1.columns(column("id")), stmt2.columns(column("id")))
  1884. The above statement will render as:
  1885. .. sourcecode:: sql
  1886. select id from table where name=:name_1
  1887. UNION ALL select id from table where name=:name_2
  1888. .. versionadded:: 1.3.11 Added support for the
  1889. :paramref:`.BindParameter.unique` flag to work with
  1890. :func:`_expression.text`
  1891. constructs.
  1892. """ # noqa: E501
  1893. self._bindparams = new_params = self._bindparams.copy()
  1894. for bind in binds:
  1895. try:
  1896. # the regex used for text() currently will not match
  1897. # a unique/anonymous key in any case, so use the _orig_key
  1898. # so that a text() construct can support unique parameters
  1899. existing = new_params[bind._orig_key]
  1900. except KeyError as err:
  1901. raise exc.ArgumentError(
  1902. "This text() construct doesn't define a "
  1903. "bound parameter named %r" % bind._orig_key
  1904. ) from err
  1905. else:
  1906. new_params[existing._orig_key] = bind
  1907. for key, value in names_to_values.items():
  1908. try:
  1909. existing = new_params[key]
  1910. except KeyError as err:
  1911. raise exc.ArgumentError(
  1912. "This text() construct doesn't define a "
  1913. "bound parameter named %r" % key
  1914. ) from err
  1915. else:
  1916. new_params[key] = existing._with_value(value, required=False)
  1917. return self
  1918. @util.preload_module("sqlalchemy.sql.selectable")
  1919. def columns(
  1920. self,
  1921. *cols: _ColumnExpressionArgument[Any],
  1922. **types: _TypeEngineArgument[Any],
  1923. ) -> TextualSelect:
  1924. r"""Turn this :class:`_expression.TextClause` object into a
  1925. :class:`_expression.TextualSelect`
  1926. object that serves the same role as a SELECT
  1927. statement.
  1928. The :class:`_expression.TextualSelect` is part of the
  1929. :class:`_expression.SelectBase`
  1930. hierarchy and can be embedded into another statement by using the
  1931. :meth:`_expression.TextualSelect.subquery` method to produce a
  1932. :class:`.Subquery`
  1933. object, which can then be SELECTed from.
  1934. This function essentially bridges the gap between an entirely
  1935. textual SELECT statement and the SQL expression language concept
  1936. of a "selectable"::
  1937. from sqlalchemy.sql import column, text
  1938. stmt = text("SELECT id, name FROM some_table")
  1939. stmt = stmt.columns(column("id"), column("name")).subquery("st")
  1940. stmt = (
  1941. select(mytable)
  1942. .select_from(mytable.join(stmt, mytable.c.name == stmt.c.name))
  1943. .where(stmt.c.id > 5)
  1944. )
  1945. Above, we pass a series of :func:`_expression.column` elements to the
  1946. :meth:`_expression.TextClause.columns` method positionally. These
  1947. :func:`_expression.column`
  1948. elements now become first class elements upon the
  1949. :attr:`_expression.TextualSelect.selected_columns` column collection,
  1950. which then
  1951. become part of the :attr:`.Subquery.c` collection after
  1952. :meth:`_expression.TextualSelect.subquery` is invoked.
  1953. The column expressions we pass to
  1954. :meth:`_expression.TextClause.columns` may
  1955. also be typed; when we do so, these :class:`.TypeEngine` objects become
  1956. the effective return type of the column, so that SQLAlchemy's
  1957. result-set-processing systems may be used on the return values.
  1958. This is often needed for types such as date or boolean types, as well
  1959. as for unicode processing on some dialect configurations::
  1960. stmt = text("SELECT id, name, timestamp FROM some_table")
  1961. stmt = stmt.columns(
  1962. column("id", Integer),
  1963. column("name", Unicode),
  1964. column("timestamp", DateTime),
  1965. )
  1966. for id, name, timestamp in connection.execute(stmt):
  1967. print(id, name, timestamp)
  1968. As a shortcut to the above syntax, keyword arguments referring to
  1969. types alone may be used, if only type conversion is needed::
  1970. stmt = text("SELECT id, name, timestamp FROM some_table")
  1971. stmt = stmt.columns(id=Integer, name=Unicode, timestamp=DateTime)
  1972. for id, name, timestamp in connection.execute(stmt):
  1973. print(id, name, timestamp)
  1974. The positional form of :meth:`_expression.TextClause.columns`
  1975. also provides the
  1976. unique feature of **positional column targeting**, which is
  1977. particularly useful when using the ORM with complex textual queries. If
  1978. we specify the columns from our model to
  1979. :meth:`_expression.TextClause.columns`,
  1980. the result set will match to those columns positionally, meaning the
  1981. name or origin of the column in the textual SQL doesn't matter::
  1982. stmt = text(
  1983. "SELECT users.id, addresses.id, users.id, "
  1984. "users.name, addresses.email_address AS email "
  1985. "FROM users JOIN addresses ON users.id=addresses.user_id "
  1986. "WHERE users.id = 1"
  1987. ).columns(
  1988. User.id,
  1989. Address.id,
  1990. Address.user_id,
  1991. User.name,
  1992. Address.email_address,
  1993. )
  1994. query = (
  1995. session.query(User)
  1996. .from_statement(stmt)
  1997. .options(contains_eager(User.addresses))
  1998. )
  1999. The :meth:`_expression.TextClause.columns` method provides a direct
  2000. route to calling :meth:`_expression.FromClause.subquery` as well as
  2001. :meth:`_expression.SelectBase.cte`
  2002. against a textual SELECT statement::
  2003. stmt = stmt.columns(id=Integer, name=String).cte("st")
  2004. stmt = select(sometable).where(sometable.c.id == stmt.c.id)
  2005. :param \*cols: A series of :class:`_expression.ColumnElement` objects,
  2006. typically
  2007. :class:`_schema.Column` objects from a :class:`_schema.Table`
  2008. or ORM level
  2009. column-mapped attributes, representing a set of columns that this
  2010. textual string will SELECT from.
  2011. :param \**types: A mapping of string names to :class:`.TypeEngine`
  2012. type objects indicating the datatypes to use for names that are
  2013. SELECTed from the textual string. Prefer to use the ``*cols``
  2014. argument as it also indicates positional ordering.
  2015. """
  2016. selectable = util.preloaded.sql_selectable
  2017. input_cols: List[NamedColumn[Any]] = [
  2018. coercions.expect(roles.LabeledColumnExprRole, col) for col in cols
  2019. ]
  2020. positional_input_cols = [
  2021. (
  2022. ColumnClause(col.key, types.pop(col.key))
  2023. if col.key in types
  2024. else col
  2025. )
  2026. for col in input_cols
  2027. ]
  2028. keyed_input_cols: List[NamedColumn[Any]] = [
  2029. ColumnClause(key, type_) for key, type_ in types.items()
  2030. ]
  2031. elem = selectable.TextualSelect.__new__(selectable.TextualSelect)
  2032. elem._init(
  2033. self,
  2034. positional_input_cols + keyed_input_cols,
  2035. positional=bool(positional_input_cols) and not keyed_input_cols,
  2036. )
  2037. return elem
  2038. @property
  2039. def type(self) -> TypeEngine[Any]:
  2040. return type_api.NULLTYPE
  2041. @property
  2042. def comparator(self):
  2043. # TODO: this seems wrong, it seems like we might not
  2044. # be using this method.
  2045. return self.type.comparator_factory(self) # type: ignore
  2046. def self_group(
  2047. self, against: Optional[OperatorType] = None
  2048. ) -> Union[Self, Grouping[Any]]:
  2049. if against is operators.in_op:
  2050. return Grouping(self)
  2051. else:
  2052. return self
  2053. class Null(SingletonConstant, roles.ConstExprRole[None], ColumnElement[None]):
  2054. """Represent the NULL keyword in a SQL statement.
  2055. :class:`.Null` is accessed as a constant via the
  2056. :func:`.null` function.
  2057. """
  2058. __visit_name__ = "null"
  2059. _traverse_internals: _TraverseInternalsType = []
  2060. _singleton: Null
  2061. if not TYPE_CHECKING:
  2062. @util.memoized_property
  2063. def type(self) -> TypeEngine[_T]: # noqa: A001
  2064. return type_api.NULLTYPE
  2065. @classmethod
  2066. def _instance(cls) -> Null:
  2067. """Return a constant :class:`.Null` construct."""
  2068. return Null._singleton
  2069. Null._create_singleton()
  2070. class False_(
  2071. SingletonConstant, roles.ConstExprRole[bool], ColumnElement[bool]
  2072. ):
  2073. """Represent the ``false`` keyword, or equivalent, in a SQL statement.
  2074. :class:`.False_` is accessed as a constant via the
  2075. :func:`.false` function.
  2076. """
  2077. __visit_name__ = "false"
  2078. _traverse_internals: _TraverseInternalsType = []
  2079. _singleton: False_
  2080. if not TYPE_CHECKING:
  2081. @util.memoized_property
  2082. def type(self) -> TypeEngine[_T]: # noqa: A001
  2083. return type_api.BOOLEANTYPE
  2084. def _negate(self) -> True_:
  2085. return True_._singleton
  2086. @classmethod
  2087. def _instance(cls) -> False_:
  2088. return False_._singleton
  2089. False_._create_singleton()
  2090. class True_(SingletonConstant, roles.ConstExprRole[bool], ColumnElement[bool]):
  2091. """Represent the ``true`` keyword, or equivalent, in a SQL statement.
  2092. :class:`.True_` is accessed as a constant via the
  2093. :func:`.true` function.
  2094. """
  2095. __visit_name__ = "true"
  2096. _traverse_internals: _TraverseInternalsType = []
  2097. _singleton: True_
  2098. if not TYPE_CHECKING:
  2099. @util.memoized_property
  2100. def type(self) -> TypeEngine[_T]: # noqa: A001
  2101. return type_api.BOOLEANTYPE
  2102. def _negate(self) -> False_:
  2103. return False_._singleton
  2104. @classmethod
  2105. def _ifnone(
  2106. cls, other: Optional[ColumnElement[Any]]
  2107. ) -> ColumnElement[Any]:
  2108. if other is None:
  2109. return cls._instance()
  2110. else:
  2111. return other
  2112. @classmethod
  2113. def _instance(cls) -> True_:
  2114. return True_._singleton
  2115. True_._create_singleton()
  2116. class ClauseList(
  2117. roles.InElementRole,
  2118. roles.OrderByRole,
  2119. roles.ColumnsClauseRole,
  2120. roles.DMLColumnRole,
  2121. DQLDMLClauseElement,
  2122. ):
  2123. """Describe a list of clauses, separated by an operator.
  2124. By default, is comma-separated, such as a column listing.
  2125. """
  2126. __visit_name__ = "clauselist"
  2127. # this is used only by the ORM in a legacy use case for
  2128. # composite attributes
  2129. _is_clause_list = True
  2130. _traverse_internals: _TraverseInternalsType = [
  2131. ("clauses", InternalTraversal.dp_clauseelement_list),
  2132. ("operator", InternalTraversal.dp_operator),
  2133. ]
  2134. clauses: List[ColumnElement[Any]]
  2135. def __init__(
  2136. self,
  2137. *clauses: _ColumnExpressionArgument[Any],
  2138. operator: OperatorType = operators.comma_op,
  2139. group: bool = True,
  2140. group_contents: bool = True,
  2141. _literal_as_text_role: Type[roles.SQLRole] = roles.WhereHavingRole,
  2142. ):
  2143. self.operator = operator
  2144. self.group = group
  2145. self.group_contents = group_contents
  2146. clauses_iterator: Iterable[_ColumnExpressionArgument[Any]] = clauses
  2147. text_converter_role: Type[roles.SQLRole] = _literal_as_text_role
  2148. self._text_converter_role = text_converter_role
  2149. if self.group_contents:
  2150. self.clauses = [
  2151. coercions.expect(
  2152. text_converter_role, clause, apply_propagate_attrs=self
  2153. ).self_group(against=self.operator)
  2154. for clause in clauses_iterator
  2155. ]
  2156. else:
  2157. self.clauses = [
  2158. coercions.expect(
  2159. text_converter_role, clause, apply_propagate_attrs=self
  2160. )
  2161. for clause in clauses_iterator
  2162. ]
  2163. self._is_implicitly_boolean = operators.is_boolean(self.operator)
  2164. @classmethod
  2165. def _construct_raw(
  2166. cls,
  2167. operator: OperatorType,
  2168. clauses: Optional[Sequence[ColumnElement[Any]]] = None,
  2169. ) -> ClauseList:
  2170. self = cls.__new__(cls)
  2171. self.clauses = list(clauses) if clauses else []
  2172. self.group = True
  2173. self.operator = operator
  2174. self.group_contents = True
  2175. self._is_implicitly_boolean = False
  2176. return self
  2177. def __iter__(self) -> Iterator[ColumnElement[Any]]:
  2178. return iter(self.clauses)
  2179. def __len__(self) -> int:
  2180. return len(self.clauses)
  2181. @property
  2182. def _select_iterable(self) -> _SelectIterable:
  2183. return itertools.chain.from_iterable(
  2184. [elem._select_iterable for elem in self.clauses]
  2185. )
  2186. def append(self, clause):
  2187. if self.group_contents:
  2188. self.clauses.append(
  2189. coercions.expect(self._text_converter_role, clause).self_group(
  2190. against=self.operator
  2191. )
  2192. )
  2193. else:
  2194. self.clauses.append(
  2195. coercions.expect(self._text_converter_role, clause)
  2196. )
  2197. @util.ro_non_memoized_property
  2198. def _from_objects(self) -> List[FromClause]:
  2199. return list(itertools.chain(*[c._from_objects for c in self.clauses]))
  2200. def self_group(
  2201. self, against: Optional[OperatorType] = None
  2202. ) -> Union[Self, Grouping[Any]]:
  2203. if self.group and operators.is_precedent(self.operator, against):
  2204. return Grouping(self)
  2205. else:
  2206. return self
  2207. class OperatorExpression(ColumnElement[_T]):
  2208. """base for expressions that contain an operator and operands
  2209. .. versionadded:: 2.0
  2210. """
  2211. operator: OperatorType
  2212. type: TypeEngine[_T]
  2213. group: bool = True
  2214. @property
  2215. def is_comparison(self):
  2216. return operators.is_comparison(self.operator)
  2217. def self_group(
  2218. self, against: Optional[OperatorType] = None
  2219. ) -> Union[Self, Grouping[_T]]:
  2220. if (
  2221. self.group
  2222. and operators.is_precedent(self.operator, against)
  2223. or (
  2224. # a negate against a non-boolean operator
  2225. # doesn't make too much sense but we should
  2226. # group for that
  2227. against is operators.inv
  2228. and not operators.is_boolean(self.operator)
  2229. )
  2230. ):
  2231. return Grouping(self)
  2232. else:
  2233. return self
  2234. @property
  2235. def _flattened_operator_clauses(
  2236. self,
  2237. ) -> typing_Tuple[ColumnElement[Any], ...]:
  2238. raise NotImplementedError()
  2239. @classmethod
  2240. def _construct_for_op(
  2241. cls,
  2242. left: ColumnElement[Any],
  2243. right: ColumnElement[Any],
  2244. op: OperatorType,
  2245. *,
  2246. type_: TypeEngine[_T],
  2247. negate: Optional[OperatorType] = None,
  2248. modifiers: Optional[Mapping[str, Any]] = None,
  2249. ) -> OperatorExpression[_T]:
  2250. if operators.is_associative(op):
  2251. assert (
  2252. negate is None
  2253. ), f"negate not supported for associative operator {op}"
  2254. multi = False
  2255. if getattr(
  2256. left, "operator", None
  2257. ) is op and type_._compare_type_affinity(left.type):
  2258. multi = True
  2259. left_flattened = left._flattened_operator_clauses
  2260. else:
  2261. left_flattened = (left,)
  2262. if getattr(
  2263. right, "operator", None
  2264. ) is op and type_._compare_type_affinity(right.type):
  2265. multi = True
  2266. right_flattened = right._flattened_operator_clauses
  2267. else:
  2268. right_flattened = (right,)
  2269. if multi:
  2270. return ExpressionClauseList._construct_for_list(
  2271. op,
  2272. type_,
  2273. *(left_flattened + right_flattened),
  2274. )
  2275. if right._is_collection_aggregate:
  2276. negate = None
  2277. return BinaryExpression(
  2278. left, right, op, type_=type_, negate=negate, modifiers=modifiers
  2279. )
  2280. class ExpressionClauseList(OperatorExpression[_T]):
  2281. """Describe a list of clauses, separated by an operator,
  2282. in a column expression context.
  2283. :class:`.ExpressionClauseList` differs from :class:`.ClauseList` in that
  2284. it represents a column-oriented DQL expression only, not an open ended
  2285. list of anything comma separated.
  2286. .. versionadded:: 2.0
  2287. """
  2288. __visit_name__ = "expression_clauselist"
  2289. _traverse_internals: _TraverseInternalsType = [
  2290. ("clauses", InternalTraversal.dp_clauseelement_tuple),
  2291. ("operator", InternalTraversal.dp_operator),
  2292. ]
  2293. clauses: typing_Tuple[ColumnElement[Any], ...]
  2294. group: bool
  2295. def __init__(
  2296. self,
  2297. operator: OperatorType,
  2298. *clauses: _ColumnExpressionArgument[Any],
  2299. type_: Optional[_TypeEngineArgument[_T]] = None,
  2300. ):
  2301. self.operator = operator
  2302. self.clauses = tuple(
  2303. coercions.expect(
  2304. roles.ExpressionElementRole, clause, apply_propagate_attrs=self
  2305. )
  2306. for clause in clauses
  2307. )
  2308. self._is_implicitly_boolean = operators.is_boolean(self.operator)
  2309. self.type = type_api.to_instance(type_) # type: ignore
  2310. @property
  2311. def _flattened_operator_clauses(
  2312. self,
  2313. ) -> typing_Tuple[ColumnElement[Any], ...]:
  2314. return self.clauses
  2315. def __iter__(self) -> Iterator[ColumnElement[Any]]:
  2316. return iter(self.clauses)
  2317. def __len__(self) -> int:
  2318. return len(self.clauses)
  2319. @property
  2320. def _select_iterable(self) -> _SelectIterable:
  2321. return (self,)
  2322. @util.ro_non_memoized_property
  2323. def _from_objects(self) -> List[FromClause]:
  2324. return list(itertools.chain(*[c._from_objects for c in self.clauses]))
  2325. def _append_inplace(self, clause: ColumnElement[Any]) -> None:
  2326. self.clauses += (clause,)
  2327. @classmethod
  2328. def _construct_for_list(
  2329. cls,
  2330. operator: OperatorType,
  2331. type_: TypeEngine[_T],
  2332. *clauses: ColumnElement[Any],
  2333. group: bool = True,
  2334. ) -> ExpressionClauseList[_T]:
  2335. self = cls.__new__(cls)
  2336. self.group = group
  2337. if group:
  2338. self.clauses = tuple(
  2339. c.self_group(against=operator) for c in clauses
  2340. )
  2341. else:
  2342. self.clauses = clauses
  2343. self.operator = operator
  2344. self.type = type_
  2345. for c in clauses:
  2346. if c._propagate_attrs:
  2347. self._propagate_attrs = c._propagate_attrs
  2348. break
  2349. return self
  2350. def _negate(self) -> Any:
  2351. grouped = self.self_group(against=operators.inv)
  2352. assert isinstance(grouped, ColumnElement)
  2353. return UnaryExpression(grouped, operator=operators.inv)
  2354. class BooleanClauseList(ExpressionClauseList[bool]):
  2355. __visit_name__ = "expression_clauselist"
  2356. inherit_cache = True
  2357. def __init__(self, *arg, **kw):
  2358. raise NotImplementedError(
  2359. "BooleanClauseList has a private constructor"
  2360. )
  2361. @classmethod
  2362. def _process_clauses_for_boolean(
  2363. cls,
  2364. operator: OperatorType,
  2365. continue_on: Any,
  2366. skip_on: Any,
  2367. clauses: Iterable[ColumnElement[Any]],
  2368. ) -> typing_Tuple[int, List[ColumnElement[Any]]]:
  2369. has_continue_on = None
  2370. convert_clauses = []
  2371. against = operators._asbool
  2372. lcc = 0
  2373. for clause in clauses:
  2374. if clause is continue_on:
  2375. # instance of continue_on, like and_(x, y, True, z), store it
  2376. # if we didn't find one already, we will use it if there
  2377. # are no other expressions here.
  2378. has_continue_on = clause
  2379. elif clause is skip_on:
  2380. # instance of skip_on, e.g. and_(x, y, False, z), cancels
  2381. # the rest out
  2382. convert_clauses = [clause]
  2383. lcc = 1
  2384. break
  2385. else:
  2386. if not lcc:
  2387. lcc = 1
  2388. else:
  2389. against = operator
  2390. # technically this would be len(convert_clauses) + 1
  2391. # however this only needs to indicate "greater than one"
  2392. lcc = 2
  2393. convert_clauses.append(clause)
  2394. if not convert_clauses and has_continue_on is not None:
  2395. convert_clauses = [has_continue_on]
  2396. lcc = 1
  2397. return lcc, [c.self_group(against=against) for c in convert_clauses]
  2398. @classmethod
  2399. def _construct(
  2400. cls,
  2401. operator: OperatorType,
  2402. continue_on: Any,
  2403. skip_on: Any,
  2404. initial_clause: Any = _NoArg.NO_ARG,
  2405. *clauses: Any,
  2406. **kw: Any,
  2407. ) -> ColumnElement[Any]:
  2408. if initial_clause is _NoArg.NO_ARG:
  2409. # no elements period. deprecated use case. return an empty
  2410. # ClauseList construct that generates nothing unless it has
  2411. # elements added to it.
  2412. name = operator.__name__
  2413. util.warn_deprecated(
  2414. f"Invoking {name}() without arguments is deprecated, and "
  2415. f"will be disallowed in a future release. For an empty "
  2416. f"""{name}() construct, use '{name}({
  2417. 'true()' if continue_on is True_._singleton else 'false()'
  2418. }, *args)' """
  2419. f"""or '{name}({
  2420. 'True' if continue_on is True_._singleton else 'False'
  2421. }, *args)'.""",
  2422. version="1.4",
  2423. )
  2424. return cls._construct_raw(operator)
  2425. lcc, convert_clauses = cls._process_clauses_for_boolean(
  2426. operator,
  2427. continue_on,
  2428. skip_on,
  2429. [
  2430. coercions.expect(roles.WhereHavingRole, clause)
  2431. for clause in util.coerce_generator_arg(
  2432. (initial_clause,) + clauses
  2433. )
  2434. ],
  2435. )
  2436. if lcc > 1:
  2437. # multiple elements. Return regular BooleanClauseList
  2438. # which will link elements against the operator.
  2439. flattened_clauses = itertools.chain.from_iterable(
  2440. (
  2441. (c for c in to_flat._flattened_operator_clauses)
  2442. if getattr(to_flat, "operator", None) is operator
  2443. else (to_flat,)
  2444. )
  2445. for to_flat in convert_clauses
  2446. )
  2447. return cls._construct_raw(operator, flattened_clauses) # type: ignore # noqa: E501
  2448. else:
  2449. assert lcc
  2450. # just one element. return it as a single boolean element,
  2451. # not a list and discard the operator.
  2452. return convert_clauses[0]
  2453. @classmethod
  2454. def _construct_for_whereclause(
  2455. cls, clauses: Iterable[ColumnElement[Any]]
  2456. ) -> Optional[ColumnElement[bool]]:
  2457. operator, continue_on, skip_on = (
  2458. operators.and_,
  2459. True_._singleton,
  2460. False_._singleton,
  2461. )
  2462. lcc, convert_clauses = cls._process_clauses_for_boolean(
  2463. operator,
  2464. continue_on,
  2465. skip_on,
  2466. clauses, # these are assumed to be coerced already
  2467. )
  2468. if lcc > 1:
  2469. # multiple elements. Return regular BooleanClauseList
  2470. # which will link elements against the operator.
  2471. return cls._construct_raw(operator, convert_clauses)
  2472. elif lcc == 1:
  2473. # just one element. return it as a single boolean element,
  2474. # not a list and discard the operator.
  2475. return convert_clauses[0]
  2476. else:
  2477. return None
  2478. @classmethod
  2479. def _construct_raw(
  2480. cls,
  2481. operator: OperatorType,
  2482. clauses: Optional[Sequence[ColumnElement[Any]]] = None,
  2483. ) -> BooleanClauseList:
  2484. self = cls.__new__(cls)
  2485. self.clauses = tuple(clauses) if clauses else ()
  2486. self.group = True
  2487. self.operator = operator
  2488. self.type = type_api.BOOLEANTYPE
  2489. self._is_implicitly_boolean = True
  2490. return self
  2491. @classmethod
  2492. def and_(
  2493. cls,
  2494. initial_clause: Union[
  2495. Literal[True], _ColumnExpressionArgument[bool], _NoArg
  2496. ] = _NoArg.NO_ARG,
  2497. *clauses: _ColumnExpressionArgument[bool],
  2498. ) -> ColumnElement[bool]:
  2499. r"""Produce a conjunction of expressions joined by ``AND``.
  2500. See :func:`_sql.and_` for full documentation.
  2501. """
  2502. return cls._construct(
  2503. operators.and_,
  2504. True_._singleton,
  2505. False_._singleton,
  2506. initial_clause,
  2507. *clauses,
  2508. )
  2509. @classmethod
  2510. def or_(
  2511. cls,
  2512. initial_clause: Union[
  2513. Literal[False], _ColumnExpressionArgument[bool], _NoArg
  2514. ] = _NoArg.NO_ARG,
  2515. *clauses: _ColumnExpressionArgument[bool],
  2516. ) -> ColumnElement[bool]:
  2517. """Produce a conjunction of expressions joined by ``OR``.
  2518. See :func:`_sql.or_` for full documentation.
  2519. """
  2520. return cls._construct(
  2521. operators.or_,
  2522. False_._singleton,
  2523. True_._singleton,
  2524. initial_clause,
  2525. *clauses,
  2526. )
  2527. @property
  2528. def _select_iterable(self) -> _SelectIterable:
  2529. return (self,)
  2530. def self_group(
  2531. self, against: Optional[OperatorType] = None
  2532. ) -> Union[Self, Grouping[bool]]:
  2533. if not self.clauses:
  2534. return self
  2535. else:
  2536. return super().self_group(against=against)
  2537. and_ = BooleanClauseList.and_
  2538. or_ = BooleanClauseList.or_
  2539. class Tuple(ClauseList, ColumnElement[typing_Tuple[Any, ...]]):
  2540. """Represent a SQL tuple."""
  2541. __visit_name__ = "tuple"
  2542. _traverse_internals: _TraverseInternalsType = (
  2543. ClauseList._traverse_internals + []
  2544. )
  2545. type: TupleType
  2546. @util.preload_module("sqlalchemy.sql.sqltypes")
  2547. def __init__(
  2548. self,
  2549. *clauses: _ColumnExpressionArgument[Any],
  2550. types: Optional[Sequence[_TypeEngineArgument[Any]]] = None,
  2551. ):
  2552. sqltypes = util.preloaded.sql_sqltypes
  2553. if types is None:
  2554. init_clauses: List[ColumnElement[Any]] = [
  2555. coercions.expect(roles.ExpressionElementRole, c)
  2556. for c in clauses
  2557. ]
  2558. else:
  2559. if len(types) != len(clauses):
  2560. raise exc.ArgumentError(
  2561. "Wrong number of elements for %d-tuple: %r "
  2562. % (len(types), clauses)
  2563. )
  2564. init_clauses = [
  2565. coercions.expect(
  2566. roles.ExpressionElementRole,
  2567. c,
  2568. type_=typ if not typ._isnull else None,
  2569. )
  2570. for typ, c in zip(types, clauses)
  2571. ]
  2572. self.type = sqltypes.TupleType(*[arg.type for arg in init_clauses])
  2573. super().__init__(*init_clauses)
  2574. @property
  2575. def _select_iterable(self) -> _SelectIterable:
  2576. return (self,)
  2577. def _bind_param(self, operator, obj, type_=None, expanding=False):
  2578. if expanding:
  2579. return BindParameter(
  2580. None,
  2581. value=obj,
  2582. _compared_to_operator=operator,
  2583. unique=True,
  2584. expanding=True,
  2585. type_=type_,
  2586. _compared_to_type=self.type,
  2587. )
  2588. else:
  2589. return Tuple(
  2590. *[
  2591. BindParameter(
  2592. None,
  2593. o,
  2594. _compared_to_operator=operator,
  2595. _compared_to_type=compared_to_type,
  2596. unique=True,
  2597. type_=type_,
  2598. )
  2599. for o, compared_to_type in zip(obj, self.type.types)
  2600. ]
  2601. )
  2602. def self_group(self, against: Optional[OperatorType] = None) -> Self:
  2603. # Tuple is parenthesized by definition.
  2604. return self
  2605. class Case(ColumnElement[_T]):
  2606. """Represent a ``CASE`` expression.
  2607. :class:`.Case` is produced using the :func:`.case` factory function,
  2608. as in::
  2609. from sqlalchemy import case
  2610. stmt = select(users_table).where(
  2611. case(
  2612. (users_table.c.name == "wendy", "W"),
  2613. (users_table.c.name == "jack", "J"),
  2614. else_="E",
  2615. )
  2616. )
  2617. Details on :class:`.Case` usage is at :func:`.case`.
  2618. .. seealso::
  2619. :func:`.case`
  2620. """
  2621. __visit_name__ = "case"
  2622. _traverse_internals: _TraverseInternalsType = [
  2623. ("value", InternalTraversal.dp_clauseelement),
  2624. ("whens", InternalTraversal.dp_clauseelement_tuples),
  2625. ("else_", InternalTraversal.dp_clauseelement),
  2626. ]
  2627. # for case(), the type is derived from the whens. so for the moment
  2628. # users would have to cast() the case to get a specific type
  2629. whens: List[typing_Tuple[ColumnElement[bool], ColumnElement[_T]]]
  2630. else_: Optional[ColumnElement[_T]]
  2631. value: Optional[ColumnElement[Any]]
  2632. def __init__(
  2633. self,
  2634. *whens: Union[
  2635. typing_Tuple[_ColumnExpressionArgument[bool], Any],
  2636. Mapping[Any, Any],
  2637. ],
  2638. value: Optional[Any] = None,
  2639. else_: Optional[Any] = None,
  2640. ):
  2641. new_whens: Iterable[Any] = coercions._expression_collection_was_a_list(
  2642. "whens", "case", whens
  2643. )
  2644. try:
  2645. new_whens = util.dictlike_iteritems(new_whens)
  2646. except TypeError:
  2647. pass
  2648. self.whens = [
  2649. (
  2650. coercions.expect(
  2651. roles.ExpressionElementRole,
  2652. c,
  2653. apply_propagate_attrs=self,
  2654. ).self_group(),
  2655. coercions.expect(roles.ExpressionElementRole, r),
  2656. )
  2657. for (c, r) in new_whens
  2658. ]
  2659. if value is None:
  2660. self.value = None
  2661. else:
  2662. self.value = coercions.expect(roles.ExpressionElementRole, value)
  2663. if else_ is not None:
  2664. self.else_ = coercions.expect(roles.ExpressionElementRole, else_)
  2665. else:
  2666. self.else_ = None
  2667. type_ = next(
  2668. (
  2669. then.type
  2670. # Iterate `whens` in reverse to match previous behaviour
  2671. # where type of final element took priority
  2672. for *_, then in reversed(self.whens)
  2673. if not then.type._isnull
  2674. ),
  2675. self.else_.type if self.else_ is not None else type_api.NULLTYPE,
  2676. )
  2677. self.type = cast(_T, type_)
  2678. @util.ro_non_memoized_property
  2679. def _from_objects(self) -> List[FromClause]:
  2680. return list(
  2681. itertools.chain(*[x._from_objects for x in self.get_children()])
  2682. )
  2683. class Cast(WrapsColumnExpression[_T]):
  2684. """Represent a ``CAST`` expression.
  2685. :class:`.Cast` is produced using the :func:`.cast` factory function,
  2686. as in::
  2687. from sqlalchemy import cast, Numeric
  2688. stmt = select(cast(product_table.c.unit_price, Numeric(10, 4)))
  2689. Details on :class:`.Cast` usage is at :func:`.cast`.
  2690. .. seealso::
  2691. :ref:`tutorial_casts`
  2692. :func:`.cast`
  2693. :func:`.try_cast`
  2694. :func:`.type_coerce` - an alternative to CAST that coerces the type
  2695. on the Python side only, which is often sufficient to generate the
  2696. correct SQL and data coercion.
  2697. """
  2698. __visit_name__ = "cast"
  2699. _traverse_internals: _TraverseInternalsType = [
  2700. ("clause", InternalTraversal.dp_clauseelement),
  2701. ("type", InternalTraversal.dp_type),
  2702. ]
  2703. clause: ColumnElement[Any]
  2704. type: TypeEngine[_T]
  2705. typeclause: TypeClause
  2706. def __init__(
  2707. self,
  2708. expression: _ColumnExpressionArgument[Any],
  2709. type_: _TypeEngineArgument[_T],
  2710. ):
  2711. self.type = type_api.to_instance(type_)
  2712. self.clause = coercions.expect(
  2713. roles.ExpressionElementRole,
  2714. expression,
  2715. type_=self.type,
  2716. apply_propagate_attrs=self,
  2717. )
  2718. self.typeclause = TypeClause(self.type)
  2719. @util.ro_non_memoized_property
  2720. def _from_objects(self) -> List[FromClause]:
  2721. return self.clause._from_objects
  2722. @property
  2723. def wrapped_column_expression(self):
  2724. return self.clause
  2725. class TryCast(Cast[_T]):
  2726. """Represent a TRY_CAST expression.
  2727. Details on :class:`.TryCast` usage is at :func:`.try_cast`.
  2728. .. seealso::
  2729. :func:`.try_cast`
  2730. :ref:`tutorial_casts`
  2731. """
  2732. __visit_name__ = "try_cast"
  2733. inherit_cache = True
  2734. class TypeCoerce(WrapsColumnExpression[_T]):
  2735. """Represent a Python-side type-coercion wrapper.
  2736. :class:`.TypeCoerce` supplies the :func:`_expression.type_coerce`
  2737. function; see that function for usage details.
  2738. .. seealso::
  2739. :func:`_expression.type_coerce`
  2740. :func:`.cast`
  2741. """
  2742. __visit_name__ = "type_coerce"
  2743. _traverse_internals: _TraverseInternalsType = [
  2744. ("clause", InternalTraversal.dp_clauseelement),
  2745. ("type", InternalTraversal.dp_type),
  2746. ]
  2747. clause: ColumnElement[Any]
  2748. type: TypeEngine[_T]
  2749. def __init__(
  2750. self,
  2751. expression: _ColumnExpressionArgument[Any],
  2752. type_: _TypeEngineArgument[_T],
  2753. ):
  2754. self.type = type_api.to_instance(type_)
  2755. self.clause = coercions.expect(
  2756. roles.ExpressionElementRole,
  2757. expression,
  2758. type_=self.type,
  2759. apply_propagate_attrs=self,
  2760. )
  2761. @util.ro_non_memoized_property
  2762. def _from_objects(self) -> List[FromClause]:
  2763. return self.clause._from_objects
  2764. @HasMemoized.memoized_attribute
  2765. def typed_expression(self):
  2766. if isinstance(self.clause, BindParameter):
  2767. bp = self.clause._clone()
  2768. bp.type = self.type
  2769. return bp
  2770. else:
  2771. return self.clause
  2772. @property
  2773. def wrapped_column_expression(self):
  2774. return self.clause
  2775. def self_group(
  2776. self, against: Optional[OperatorType] = None
  2777. ) -> TypeCoerce[_T]:
  2778. grouped = self.clause.self_group(against=against)
  2779. if grouped is not self.clause:
  2780. return TypeCoerce(grouped, self.type)
  2781. else:
  2782. return self
  2783. class Extract(ColumnElement[int]):
  2784. """Represent a SQL EXTRACT clause, ``extract(field FROM expr)``."""
  2785. __visit_name__ = "extract"
  2786. _traverse_internals: _TraverseInternalsType = [
  2787. ("expr", InternalTraversal.dp_clauseelement),
  2788. ("field", InternalTraversal.dp_string),
  2789. ]
  2790. expr: ColumnElement[Any]
  2791. field: str
  2792. def __init__(self, field: str, expr: _ColumnExpressionArgument[Any]):
  2793. self.type = type_api.INTEGERTYPE
  2794. self.field = field
  2795. self.expr = coercions.expect(roles.ExpressionElementRole, expr)
  2796. @util.ro_non_memoized_property
  2797. def _from_objects(self) -> List[FromClause]:
  2798. return self.expr._from_objects
  2799. class _label_reference(ColumnElement[_T]):
  2800. """Wrap a column expression as it appears in a 'reference' context.
  2801. This expression is any that includes an _order_by_label_element,
  2802. which is a Label, or a DESC / ASC construct wrapping a Label.
  2803. The production of _label_reference() should occur when an expression
  2804. is added to this context; this includes the ORDER BY or GROUP BY of a
  2805. SELECT statement, as well as a few other places, such as the ORDER BY
  2806. within an OVER clause.
  2807. """
  2808. __visit_name__ = "label_reference"
  2809. _traverse_internals: _TraverseInternalsType = [
  2810. ("element", InternalTraversal.dp_clauseelement)
  2811. ]
  2812. element: ColumnElement[_T]
  2813. def __init__(self, element: ColumnElement[_T]):
  2814. self.element = element
  2815. @util.ro_non_memoized_property
  2816. def _from_objects(self) -> List[FromClause]:
  2817. return []
  2818. class _textual_label_reference(ColumnElement[Any]):
  2819. __visit_name__ = "textual_label_reference"
  2820. _traverse_internals: _TraverseInternalsType = [
  2821. ("element", InternalTraversal.dp_string)
  2822. ]
  2823. def __init__(self, element: str):
  2824. self.element = element
  2825. @util.memoized_property
  2826. def _text_clause(self) -> TextClause:
  2827. return TextClause(self.element)
  2828. class UnaryExpression(ColumnElement[_T]):
  2829. """Define a 'unary' expression.
  2830. A unary expression has a single column expression
  2831. and an operator. The operator can be placed on the left
  2832. (where it is called the 'operator') or right (where it is called the
  2833. 'modifier') of the column expression.
  2834. :class:`.UnaryExpression` is the basis for several unary operators
  2835. including those used by :func:`.desc`, :func:`.asc`, :func:`.distinct`,
  2836. :func:`.nulls_first` and :func:`.nulls_last`.
  2837. """
  2838. __visit_name__ = "unary"
  2839. _traverse_internals: _TraverseInternalsType = [
  2840. ("element", InternalTraversal.dp_clauseelement),
  2841. ("operator", InternalTraversal.dp_operator),
  2842. ("modifier", InternalTraversal.dp_operator),
  2843. ]
  2844. element: ColumnElement[Any]
  2845. operator: Optional[OperatorType]
  2846. modifier: Optional[OperatorType]
  2847. def __init__(
  2848. self,
  2849. element: ColumnElement[Any],
  2850. *,
  2851. operator: Optional[OperatorType] = None,
  2852. modifier: Optional[OperatorType] = None,
  2853. type_: Optional[_TypeEngineArgument[_T]] = None,
  2854. wraps_column_expression: bool = False, # legacy, not used as of 2.0.42
  2855. ):
  2856. self.operator = operator
  2857. self.modifier = modifier
  2858. self._propagate_attrs = element._propagate_attrs
  2859. self.element = element.self_group(
  2860. against=self.operator or self.modifier
  2861. )
  2862. # if type is None, we get NULLTYPE, which is our _T. But I don't
  2863. # know how to get the overloads to express that correctly
  2864. self.type = type_api.to_instance(type_) # type: ignore
  2865. def _wraps_unnamed_column(self):
  2866. ungrouped = self.element._ungroup()
  2867. return (
  2868. not isinstance(ungrouped, NamedColumn)
  2869. or ungrouped._non_anon_label is None
  2870. )
  2871. @classmethod
  2872. def _create_nulls_first(
  2873. cls,
  2874. column: _ColumnExpressionArgument[_T],
  2875. ) -> UnaryExpression[_T]:
  2876. return UnaryExpression(
  2877. coercions.expect(roles.ByOfRole, column),
  2878. modifier=operators.nulls_first_op,
  2879. )
  2880. @classmethod
  2881. def _create_nulls_last(
  2882. cls,
  2883. column: _ColumnExpressionArgument[_T],
  2884. ) -> UnaryExpression[_T]:
  2885. return UnaryExpression(
  2886. coercions.expect(roles.ByOfRole, column),
  2887. modifier=operators.nulls_last_op,
  2888. )
  2889. @classmethod
  2890. def _create_desc(
  2891. cls, column: _ColumnExpressionOrStrLabelArgument[_T]
  2892. ) -> UnaryExpression[_T]:
  2893. return UnaryExpression(
  2894. coercions.expect(roles.ByOfRole, column),
  2895. modifier=operators.desc_op,
  2896. )
  2897. @classmethod
  2898. def _create_asc(
  2899. cls,
  2900. column: _ColumnExpressionOrStrLabelArgument[_T],
  2901. ) -> UnaryExpression[_T]:
  2902. return UnaryExpression(
  2903. coercions.expect(roles.ByOfRole, column),
  2904. modifier=operators.asc_op,
  2905. )
  2906. @classmethod
  2907. def _create_distinct(
  2908. cls,
  2909. expr: _ColumnExpressionArgument[_T],
  2910. ) -> UnaryExpression[_T]:
  2911. col_expr: ColumnElement[_T] = coercions.expect(
  2912. roles.ExpressionElementRole, expr
  2913. )
  2914. return UnaryExpression(
  2915. col_expr,
  2916. operator=operators.distinct_op,
  2917. type_=col_expr.type,
  2918. )
  2919. @classmethod
  2920. def _create_bitwise_not(
  2921. cls,
  2922. expr: _ColumnExpressionArgument[_T],
  2923. ) -> UnaryExpression[_T]:
  2924. col_expr: ColumnElement[_T] = coercions.expect(
  2925. roles.ExpressionElementRole, expr
  2926. )
  2927. return UnaryExpression(
  2928. col_expr,
  2929. operator=operators.bitwise_not_op,
  2930. type_=col_expr.type,
  2931. )
  2932. @property
  2933. def _order_by_label_element(self) -> Optional[Label[Any]]:
  2934. if operators.is_order_by_modifier(self.modifier):
  2935. return self.element._order_by_label_element
  2936. else:
  2937. return None
  2938. @util.ro_non_memoized_property
  2939. def _from_objects(self) -> List[FromClause]:
  2940. return self.element._from_objects
  2941. def _negate(self) -> ColumnElement[Any]:
  2942. if self.type._type_affinity is type_api.BOOLEANTYPE._type_affinity:
  2943. return UnaryExpression(
  2944. self.self_group(against=operators.inv),
  2945. operator=operators.inv,
  2946. type_=type_api.BOOLEANTYPE,
  2947. )
  2948. else:
  2949. return ColumnElement._negate(self)
  2950. def self_group(
  2951. self, against: Optional[OperatorType] = None
  2952. ) -> Union[Self, Grouping[_T]]:
  2953. if self.operator and operators.is_precedent(self.operator, against):
  2954. return Grouping(self)
  2955. else:
  2956. return self
  2957. class CollectionAggregate(UnaryExpression[_T]):
  2958. """Forms the basis for right-hand collection operator modifiers
  2959. ANY and ALL.
  2960. The ANY and ALL keywords are available in different ways on different
  2961. backends. On PostgreSQL, they only work for an ARRAY type. On
  2962. MySQL, they only work for subqueries.
  2963. """
  2964. inherit_cache = True
  2965. _is_collection_aggregate = True
  2966. @classmethod
  2967. def _create_any(
  2968. cls, expr: _ColumnExpressionArgument[_T]
  2969. ) -> CollectionAggregate[bool]:
  2970. col_expr: ColumnElement[_T] = coercions.expect(
  2971. roles.ExpressionElementRole,
  2972. expr,
  2973. )
  2974. col_expr = col_expr.self_group()
  2975. return CollectionAggregate(
  2976. col_expr,
  2977. operator=operators.any_op,
  2978. type_=type_api.BOOLEANTYPE,
  2979. )
  2980. @classmethod
  2981. def _create_all(
  2982. cls, expr: _ColumnExpressionArgument[_T]
  2983. ) -> CollectionAggregate[bool]:
  2984. col_expr: ColumnElement[_T] = coercions.expect(
  2985. roles.ExpressionElementRole,
  2986. expr,
  2987. )
  2988. col_expr = col_expr.self_group()
  2989. return CollectionAggregate(
  2990. col_expr,
  2991. operator=operators.all_op,
  2992. type_=type_api.BOOLEANTYPE,
  2993. )
  2994. # operate and reverse_operate are hardwired to
  2995. # dispatch onto the type comparator directly, so that we can
  2996. # ensure "reversed" behavior.
  2997. def operate(
  2998. self, op: OperatorType, *other: Any, **kwargs: Any
  2999. ) -> ColumnElement[_T]:
  3000. if not operators.is_comparison(op):
  3001. raise exc.ArgumentError(
  3002. "Only comparison operators may be used with ANY/ALL"
  3003. )
  3004. kwargs["reverse"] = True
  3005. return self.comparator.operate(operators.mirror(op), *other, **kwargs)
  3006. def reverse_operate(
  3007. self, op: OperatorType, other: Any, **kwargs: Any
  3008. ) -> ColumnElement[_T]:
  3009. # comparison operators should never call reverse_operate
  3010. assert not operators.is_comparison(op)
  3011. raise exc.ArgumentError(
  3012. "Only comparison operators may be used with ANY/ALL"
  3013. )
  3014. class AsBoolean(WrapsColumnExpression[bool], UnaryExpression[bool]):
  3015. inherit_cache = True
  3016. def __init__(self, element, operator, negate):
  3017. self.element = element
  3018. self.type = type_api.BOOLEANTYPE
  3019. self.operator = operator
  3020. self.negate = negate
  3021. self.modifier = None
  3022. self._is_implicitly_boolean = element._is_implicitly_boolean
  3023. @property
  3024. def wrapped_column_expression(self):
  3025. return self.element
  3026. def self_group(self, against: Optional[OperatorType] = None) -> Self:
  3027. return self
  3028. def _negate(self):
  3029. if isinstance(self.element, (True_, False_)):
  3030. return self.element._negate()
  3031. else:
  3032. return AsBoolean(self.element, self.negate, self.operator)
  3033. class BinaryExpression(OperatorExpression[_T]):
  3034. """Represent an expression that is ``LEFT <operator> RIGHT``.
  3035. A :class:`.BinaryExpression` is generated automatically
  3036. whenever two column expressions are used in a Python binary expression:
  3037. .. sourcecode:: pycon+sql
  3038. >>> from sqlalchemy.sql import column
  3039. >>> column("a") + column("b")
  3040. <sqlalchemy.sql.expression.BinaryExpression object at 0x101029dd0>
  3041. >>> print(column("a") + column("b"))
  3042. {printsql}a + b
  3043. """
  3044. __visit_name__ = "binary"
  3045. _traverse_internals: _TraverseInternalsType = [
  3046. ("left", InternalTraversal.dp_clauseelement),
  3047. ("right", InternalTraversal.dp_clauseelement),
  3048. ("operator", InternalTraversal.dp_operator),
  3049. ("negate", InternalTraversal.dp_operator),
  3050. ("modifiers", InternalTraversal.dp_plain_dict),
  3051. (
  3052. "type",
  3053. InternalTraversal.dp_type,
  3054. ),
  3055. ]
  3056. _cache_key_traversal = [
  3057. ("left", InternalTraversal.dp_clauseelement),
  3058. ("right", InternalTraversal.dp_clauseelement),
  3059. ("operator", InternalTraversal.dp_operator),
  3060. ("modifiers", InternalTraversal.dp_plain_dict),
  3061. # "type" affects JSON CAST operators, so while redundant in most cases,
  3062. # is needed for that one
  3063. (
  3064. "type",
  3065. InternalTraversal.dp_type,
  3066. ),
  3067. ]
  3068. _is_implicitly_boolean = True
  3069. """Indicates that any database will know this is a boolean expression
  3070. even if the database does not have an explicit boolean datatype.
  3071. """
  3072. left: ColumnElement[Any]
  3073. right: ColumnElement[Any]
  3074. modifiers: Mapping[str, Any]
  3075. def __init__(
  3076. self,
  3077. left: ColumnElement[Any],
  3078. right: ColumnElement[Any],
  3079. operator: OperatorType,
  3080. type_: Optional[_TypeEngineArgument[_T]] = None,
  3081. negate: Optional[OperatorType] = None,
  3082. modifiers: Optional[Mapping[str, Any]] = None,
  3083. ):
  3084. # allow compatibility with libraries that
  3085. # refer to BinaryExpression directly and pass strings
  3086. if isinstance(operator, str):
  3087. operator = operators.custom_op(operator)
  3088. self._orig = (left.__hash__(), right.__hash__())
  3089. self._propagate_attrs = left._propagate_attrs or right._propagate_attrs
  3090. self.left = left.self_group(against=operator)
  3091. self.right = right.self_group(against=operator)
  3092. self.operator = operator
  3093. # if type is None, we get NULLTYPE, which is our _T. But I don't
  3094. # know how to get the overloads to express that correctly
  3095. self.type = type_api.to_instance(type_) # type: ignore
  3096. self.negate = negate
  3097. self._is_implicitly_boolean = operators.is_boolean(operator)
  3098. if modifiers is None:
  3099. self.modifiers = {}
  3100. else:
  3101. self.modifiers = modifiers
  3102. @property
  3103. def _flattened_operator_clauses(
  3104. self,
  3105. ) -> typing_Tuple[ColumnElement[Any], ...]:
  3106. return (self.left, self.right)
  3107. def __bool__(self):
  3108. """Implement Python-side "bool" for BinaryExpression as a
  3109. simple "identity" check for the left and right attributes,
  3110. if the operator is "eq" or "ne". Otherwise the expression
  3111. continues to not support "bool" like all other column expressions.
  3112. The rationale here is so that ColumnElement objects can be hashable.
  3113. What? Well, suppose you do this::
  3114. c1, c2 = column("x"), column("y")
  3115. s1 = set([c1, c2])
  3116. We do that **a lot**, columns inside of sets is an extremely basic
  3117. thing all over the ORM for example.
  3118. So what happens if we do this? ::
  3119. c1 in s1
  3120. Hashing means it will normally use ``__hash__()`` of the object,
  3121. but in case of hash collision, it's going to also do ``c1 == c1``
  3122. and/or ``c1 == c2`` inside. Those operations need to return a
  3123. True/False value. But because we override ``==`` and ``!=``, they're
  3124. going to get a BinaryExpression. Hence we implement ``__bool__`` here
  3125. so that these comparisons behave in this particular context mostly
  3126. like regular object comparisons. Thankfully Python is OK with
  3127. that! Otherwise we'd have to use special set classes for columns
  3128. (which we used to do, decades ago).
  3129. """
  3130. if self.operator in (operators.eq, operators.ne):
  3131. # this is using the eq/ne operator given int hash values,
  3132. # rather than Operator, so that "bool" can be based on
  3133. # identity
  3134. return self.operator(*self._orig) # type: ignore
  3135. else:
  3136. raise TypeError("Boolean value of this clause is not defined")
  3137. if typing.TYPE_CHECKING:
  3138. def __invert__(
  3139. self: BinaryExpression[_T],
  3140. ) -> BinaryExpression[_T]: ...
  3141. @util.ro_non_memoized_property
  3142. def _from_objects(self) -> List[FromClause]:
  3143. return self.left._from_objects + self.right._from_objects
  3144. def _negate(self):
  3145. if self.negate is not None:
  3146. return BinaryExpression(
  3147. self.left,
  3148. self.right._negate_in_binary(self.negate, self.operator),
  3149. self.negate,
  3150. negate=self.operator,
  3151. type_=self.type,
  3152. modifiers=self.modifiers,
  3153. )
  3154. else:
  3155. return self.self_group()._negate()
  3156. class Slice(ColumnElement[Any]):
  3157. """Represent SQL for a Python array-slice object.
  3158. This is not a specific SQL construct at this level, but
  3159. may be interpreted by specific dialects, e.g. PostgreSQL.
  3160. """
  3161. __visit_name__ = "slice"
  3162. _traverse_internals: _TraverseInternalsType = [
  3163. ("start", InternalTraversal.dp_clauseelement),
  3164. ("stop", InternalTraversal.dp_clauseelement),
  3165. ("step", InternalTraversal.dp_clauseelement),
  3166. ]
  3167. def __init__(self, start, stop, step, _name=None):
  3168. self.start = coercions.expect(
  3169. roles.ExpressionElementRole,
  3170. start,
  3171. name=_name,
  3172. type_=type_api.INTEGERTYPE,
  3173. )
  3174. self.stop = coercions.expect(
  3175. roles.ExpressionElementRole,
  3176. stop,
  3177. name=_name,
  3178. type_=type_api.INTEGERTYPE,
  3179. )
  3180. self.step = coercions.expect(
  3181. roles.ExpressionElementRole,
  3182. step,
  3183. name=_name,
  3184. type_=type_api.INTEGERTYPE,
  3185. )
  3186. self.type = type_api.NULLTYPE
  3187. def self_group(self, against: Optional[OperatorType] = None) -> Self:
  3188. assert against is operator.getitem
  3189. return self
  3190. class IndexExpression(BinaryExpression[Any]):
  3191. """Represent the class of expressions that are like an "index"
  3192. operation."""
  3193. inherit_cache = True
  3194. class GroupedElement(DQLDMLClauseElement):
  3195. """Represent any parenthesized expression"""
  3196. __visit_name__ = "grouping"
  3197. def self_group(self, against: Optional[OperatorType] = None) -> Self:
  3198. return self
  3199. def _ungroup(self) -> ClauseElement:
  3200. raise NotImplementedError()
  3201. class Grouping(GroupedElement, ColumnElement[_T]):
  3202. """Represent a grouping within a column expression"""
  3203. _traverse_internals: _TraverseInternalsType = [
  3204. ("element", InternalTraversal.dp_clauseelement),
  3205. ("type", InternalTraversal.dp_type),
  3206. ]
  3207. _cache_key_traversal = [
  3208. ("element", InternalTraversal.dp_clauseelement),
  3209. ]
  3210. element: Union[TextClause, ClauseList, ColumnElement[_T]]
  3211. def __init__(
  3212. self, element: Union[TextClause, ClauseList, ColumnElement[_T]]
  3213. ):
  3214. self.element = element
  3215. # nulltype assignment issue
  3216. self.type = getattr(element, "type", type_api.NULLTYPE) # type: ignore
  3217. self._propagate_attrs = element._propagate_attrs
  3218. def _with_binary_element_type(self, type_):
  3219. return self.__class__(self.element._with_binary_element_type(type_))
  3220. def _ungroup(self) -> ColumnElement[_T]:
  3221. assert isinstance(self.element, ColumnElement)
  3222. return self.element._ungroup()
  3223. @util.memoized_property
  3224. def _is_implicitly_boolean(self):
  3225. return self.element._is_implicitly_boolean
  3226. @util.non_memoized_property
  3227. def _tq_label(self) -> Optional[str]:
  3228. return (
  3229. getattr(self.element, "_tq_label", None) or self._anon_name_label
  3230. )
  3231. @util.non_memoized_property
  3232. def _proxies(self) -> List[ColumnElement[Any]]:
  3233. if isinstance(self.element, ColumnElement):
  3234. return [self.element]
  3235. else:
  3236. return []
  3237. @util.ro_non_memoized_property
  3238. def _from_objects(self) -> List[FromClause]:
  3239. return self.element._from_objects
  3240. def __getattr__(self, attr):
  3241. return getattr(self.element, attr)
  3242. def __getstate__(self):
  3243. return {"element": self.element, "type": self.type}
  3244. def __setstate__(self, state):
  3245. self.element = state["element"]
  3246. self.type = state["type"]
  3247. if TYPE_CHECKING:
  3248. def self_group(
  3249. self, against: Optional[OperatorType] = None
  3250. ) -> Self: ...
  3251. class _OverrideBinds(Grouping[_T]):
  3252. """used by cache_key->_apply_params_to_element to allow compilation /
  3253. execution of a SQL element that's been cached, using an alternate set of
  3254. bound parameter values.
  3255. This is used by the ORM to swap new parameter values into expressions
  3256. that are embedded into loader options like with_expression(),
  3257. selectinload(). Previously, this task was accomplished using the
  3258. .params() method which would perform a deep-copy instead. This deep
  3259. copy proved to be too expensive for more complex expressions.
  3260. See #11085
  3261. """
  3262. __visit_name__ = "override_binds"
  3263. def __init__(
  3264. self,
  3265. element: ColumnElement[_T],
  3266. bindparams: Sequence[BindParameter[Any]],
  3267. replaces_params: Sequence[BindParameter[Any]],
  3268. ):
  3269. self.element = element
  3270. self.translate = {
  3271. k.key: v.value for k, v in zip(replaces_params, bindparams)
  3272. }
  3273. def _gen_cache_key(
  3274. self, anon_map: anon_map, bindparams: List[BindParameter[Any]]
  3275. ) -> Optional[typing_Tuple[Any, ...]]:
  3276. """generate a cache key for the given element, substituting its bind
  3277. values for the translation values present."""
  3278. existing_bps: List[BindParameter[Any]] = []
  3279. ck = self.element._gen_cache_key(anon_map, existing_bps)
  3280. bindparams.extend(
  3281. (
  3282. bp._with_value(
  3283. self.translate[bp.key], maintain_key=True, required=False
  3284. )
  3285. if bp.key in self.translate
  3286. else bp
  3287. )
  3288. for bp in existing_bps
  3289. )
  3290. return ck
  3291. class _OverRange(Enum):
  3292. RANGE_UNBOUNDED = 0
  3293. RANGE_CURRENT = 1
  3294. RANGE_UNBOUNDED = _OverRange.RANGE_UNBOUNDED
  3295. RANGE_CURRENT = _OverRange.RANGE_CURRENT
  3296. _IntOrRange = Union[int, _OverRange]
  3297. class Over(ColumnElement[_T]):
  3298. """Represent an OVER clause.
  3299. This is a special operator against a so-called
  3300. "window" function, as well as any aggregate function,
  3301. which produces results relative to the result set
  3302. itself. Most modern SQL backends now support window functions.
  3303. """
  3304. __visit_name__ = "over"
  3305. _traverse_internals: _TraverseInternalsType = [
  3306. ("element", InternalTraversal.dp_clauseelement),
  3307. ("order_by", InternalTraversal.dp_clauseelement),
  3308. ("partition_by", InternalTraversal.dp_clauseelement),
  3309. ("range_", InternalTraversal.dp_plain_obj),
  3310. ("rows", InternalTraversal.dp_plain_obj),
  3311. ("groups", InternalTraversal.dp_plain_obj),
  3312. ]
  3313. order_by: Optional[ClauseList] = None
  3314. partition_by: Optional[ClauseList] = None
  3315. element: ColumnElement[_T]
  3316. """The underlying expression object to which this :class:`.Over`
  3317. object refers."""
  3318. range_: Optional[typing_Tuple[_IntOrRange, _IntOrRange]]
  3319. rows: Optional[typing_Tuple[_IntOrRange, _IntOrRange]]
  3320. groups: Optional[typing_Tuple[_IntOrRange, _IntOrRange]]
  3321. def __init__(
  3322. self,
  3323. element: ColumnElement[_T],
  3324. partition_by: Optional[_ByArgument] = None,
  3325. order_by: Optional[_ByArgument] = None,
  3326. range_: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3327. rows: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3328. groups: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3329. ):
  3330. self.element = element
  3331. if order_by is not None:
  3332. self.order_by = ClauseList(
  3333. *util.to_list(order_by), _literal_as_text_role=roles.ByOfRole
  3334. )
  3335. if partition_by is not None:
  3336. self.partition_by = ClauseList(
  3337. *util.to_list(partition_by),
  3338. _literal_as_text_role=roles.ByOfRole,
  3339. )
  3340. if sum(bool(item) for item in (range_, rows, groups)) > 1:
  3341. raise exc.ArgumentError(
  3342. "only one of 'rows', 'range_', or 'groups' may be provided"
  3343. )
  3344. else:
  3345. self.range_ = self._interpret_range(range_) if range_ else None
  3346. self.rows = self._interpret_range(rows) if rows else None
  3347. self.groups = self._interpret_range(groups) if groups else None
  3348. def __reduce__(self):
  3349. return self.__class__, (
  3350. self.element,
  3351. self.partition_by,
  3352. self.order_by,
  3353. self.range_,
  3354. self.rows,
  3355. self.groups,
  3356. )
  3357. def _interpret_range(
  3358. self,
  3359. range_: typing_Tuple[Optional[_IntOrRange], Optional[_IntOrRange]],
  3360. ) -> typing_Tuple[_IntOrRange, _IntOrRange]:
  3361. if not isinstance(range_, tuple) or len(range_) != 2:
  3362. raise exc.ArgumentError("2-tuple expected for range/rows")
  3363. r0, r1 = range_
  3364. lower: _IntOrRange
  3365. upper: _IntOrRange
  3366. if r0 is None:
  3367. lower = RANGE_UNBOUNDED
  3368. elif isinstance(r0, _OverRange):
  3369. lower = r0
  3370. else:
  3371. try:
  3372. lower = int(r0)
  3373. except ValueError as err:
  3374. raise exc.ArgumentError(
  3375. "Integer or None expected for range value"
  3376. ) from err
  3377. else:
  3378. if lower == 0:
  3379. lower = RANGE_CURRENT
  3380. if r1 is None:
  3381. upper = RANGE_UNBOUNDED
  3382. elif isinstance(r1, _OverRange):
  3383. upper = r1
  3384. else:
  3385. try:
  3386. upper = int(r1)
  3387. except ValueError as err:
  3388. raise exc.ArgumentError(
  3389. "Integer or None expected for range value"
  3390. ) from err
  3391. else:
  3392. if upper == 0:
  3393. upper = RANGE_CURRENT
  3394. return lower, upper
  3395. if not TYPE_CHECKING:
  3396. @util.memoized_property
  3397. def type(self) -> TypeEngine[_T]: # noqa: A001
  3398. return self.element.type
  3399. @util.ro_non_memoized_property
  3400. def _from_objects(self) -> List[FromClause]:
  3401. return list(
  3402. itertools.chain(
  3403. *[
  3404. c._from_objects
  3405. for c in (self.element, self.partition_by, self.order_by)
  3406. if c is not None
  3407. ]
  3408. )
  3409. )
  3410. class WithinGroup(ColumnElement[_T]):
  3411. """Represent a WITHIN GROUP (ORDER BY) clause.
  3412. This is a special operator against so-called
  3413. "ordered set aggregate" and "hypothetical
  3414. set aggregate" functions, including ``percentile_cont()``,
  3415. ``rank()``, ``dense_rank()``, etc.
  3416. It's supported only by certain database backends, such as PostgreSQL,
  3417. Oracle Database and MS SQL Server.
  3418. The :class:`.WithinGroup` construct extracts its type from the
  3419. method :meth:`.FunctionElement.within_group_type`. If this returns
  3420. ``None``, the function's ``.type`` is used.
  3421. """
  3422. __visit_name__ = "withingroup"
  3423. _traverse_internals: _TraverseInternalsType = [
  3424. ("element", InternalTraversal.dp_clauseelement),
  3425. ("order_by", InternalTraversal.dp_clauseelement),
  3426. ]
  3427. order_by: Optional[ClauseList] = None
  3428. def __init__(
  3429. self,
  3430. element: Union[FunctionElement[_T], FunctionFilter[_T]],
  3431. *order_by: _ColumnExpressionArgument[Any],
  3432. ):
  3433. self.element = element
  3434. if order_by is not None:
  3435. self.order_by = ClauseList(
  3436. *util.to_list(order_by), _literal_as_text_role=roles.ByOfRole
  3437. )
  3438. def __reduce__(self):
  3439. return self.__class__, (self.element,) + (
  3440. tuple(self.order_by) if self.order_by is not None else ()
  3441. )
  3442. def over(
  3443. self,
  3444. *,
  3445. partition_by: Optional[_ByArgument] = None,
  3446. order_by: Optional[_ByArgument] = None,
  3447. rows: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3448. range_: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3449. groups: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3450. ) -> Over[_T]:
  3451. """Produce an OVER clause against this :class:`.WithinGroup`
  3452. construct.
  3453. This function has the same signature as that of
  3454. :meth:`.FunctionElement.over`.
  3455. """
  3456. return Over(
  3457. self,
  3458. partition_by=partition_by,
  3459. order_by=order_by,
  3460. range_=range_,
  3461. rows=rows,
  3462. groups=groups,
  3463. )
  3464. @overload
  3465. def filter(self) -> Self: ...
  3466. @overload
  3467. def filter(
  3468. self,
  3469. __criterion0: _ColumnExpressionArgument[bool],
  3470. *criterion: _ColumnExpressionArgument[bool],
  3471. ) -> FunctionFilter[_T]: ...
  3472. def filter(
  3473. self, *criterion: _ColumnExpressionArgument[bool]
  3474. ) -> Union[Self, FunctionFilter[_T]]:
  3475. """Produce a FILTER clause against this function."""
  3476. if not criterion:
  3477. return self
  3478. return FunctionFilter(self, *criterion)
  3479. if not TYPE_CHECKING:
  3480. @util.memoized_property
  3481. def type(self) -> TypeEngine[_T]: # noqa: A001
  3482. wgt = self.element.within_group_type(self)
  3483. if wgt is not None:
  3484. return wgt
  3485. else:
  3486. return self.element.type
  3487. @util.ro_non_memoized_property
  3488. def _from_objects(self) -> List[FromClause]:
  3489. return list(
  3490. itertools.chain(
  3491. *[
  3492. c._from_objects
  3493. for c in (self.element, self.order_by)
  3494. if c is not None
  3495. ]
  3496. )
  3497. )
  3498. class FunctionFilter(Generative, ColumnElement[_T]):
  3499. """Represent a function FILTER clause.
  3500. This is a special operator against aggregate and window functions,
  3501. which controls which rows are passed to it.
  3502. It's supported only by certain database backends.
  3503. Invocation of :class:`.FunctionFilter` is via
  3504. :meth:`.FunctionElement.filter`::
  3505. func.count(1).filter(True)
  3506. .. seealso::
  3507. :meth:`.FunctionElement.filter`
  3508. """
  3509. __visit_name__ = "funcfilter"
  3510. _traverse_internals: _TraverseInternalsType = [
  3511. ("func", InternalTraversal.dp_clauseelement),
  3512. ("criterion", InternalTraversal.dp_clauseelement),
  3513. ]
  3514. criterion: Optional[ColumnElement[bool]] = None
  3515. def __init__(
  3516. self,
  3517. func: Union[FunctionElement[_T], WithinGroup[_T]],
  3518. *criterion: _ColumnExpressionArgument[bool],
  3519. ):
  3520. self.func = func
  3521. self.filter.non_generative(self, *criterion) # type: ignore
  3522. @_generative
  3523. def filter(self, *criterion: _ColumnExpressionArgument[bool]) -> Self:
  3524. """Produce an additional FILTER against the function.
  3525. This method adds additional criteria to the initial criteria
  3526. set up by :meth:`.FunctionElement.filter`.
  3527. Multiple criteria are joined together at SQL render time
  3528. via ``AND``.
  3529. """
  3530. for crit in list(criterion):
  3531. crit = coercions.expect(roles.WhereHavingRole, crit)
  3532. if self.criterion is not None:
  3533. self.criterion = self.criterion & crit
  3534. else:
  3535. self.criterion = crit
  3536. return self
  3537. def over(
  3538. self,
  3539. partition_by: Optional[
  3540. Union[
  3541. Iterable[_ColumnExpressionArgument[Any]],
  3542. _ColumnExpressionArgument[Any],
  3543. ]
  3544. ] = None,
  3545. order_by: Optional[
  3546. Union[
  3547. Iterable[_ColumnExpressionArgument[Any]],
  3548. _ColumnExpressionArgument[Any],
  3549. ]
  3550. ] = None,
  3551. range_: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3552. rows: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3553. groups: Optional[typing_Tuple[Optional[int], Optional[int]]] = None,
  3554. ) -> Over[_T]:
  3555. """Produce an OVER clause against this filtered function.
  3556. Used against aggregate or so-called "window" functions,
  3557. for database backends that support window functions.
  3558. The expression::
  3559. func.rank().filter(MyClass.y > 5).over(order_by="x")
  3560. is shorthand for::
  3561. from sqlalchemy import over, funcfilter
  3562. over(funcfilter(func.rank(), MyClass.y > 5), order_by="x")
  3563. See :func:`_expression.over` for a full description.
  3564. """
  3565. return Over(
  3566. self,
  3567. partition_by=partition_by,
  3568. order_by=order_by,
  3569. range_=range_,
  3570. rows=rows,
  3571. groups=groups,
  3572. )
  3573. def within_group(
  3574. self, *order_by: _ColumnExpressionArgument[Any]
  3575. ) -> WithinGroup[_T]:
  3576. """Produce a WITHIN GROUP (ORDER BY expr) clause against
  3577. this function.
  3578. """
  3579. return WithinGroup(self, *order_by)
  3580. def within_group_type(
  3581. self, within_group: WithinGroup[_T]
  3582. ) -> Optional[TypeEngine[_T]]:
  3583. return None
  3584. def self_group(
  3585. self, against: Optional[OperatorType] = None
  3586. ) -> Union[Self, Grouping[_T]]:
  3587. if operators.is_precedent(operators.filter_op, against):
  3588. return Grouping(self)
  3589. else:
  3590. return self
  3591. if not TYPE_CHECKING:
  3592. @util.memoized_property
  3593. def type(self) -> TypeEngine[_T]: # noqa: A001
  3594. return self.func.type
  3595. @util.ro_non_memoized_property
  3596. def _from_objects(self) -> List[FromClause]:
  3597. return list(
  3598. itertools.chain(
  3599. *[
  3600. c._from_objects
  3601. for c in (self.func, self.criterion)
  3602. if c is not None
  3603. ]
  3604. )
  3605. )
  3606. class NamedColumn(KeyedColumnElement[_T]):
  3607. is_literal = False
  3608. table: Optional[FromClause] = None
  3609. name: str
  3610. key: str
  3611. def _compare_name_for_result(self, other):
  3612. return (hasattr(other, "name") and self.name == other.name) or (
  3613. hasattr(other, "_label") and self._label == other._label
  3614. )
  3615. @util.ro_memoized_property
  3616. def description(self) -> str:
  3617. return self.name
  3618. @HasMemoized.memoized_attribute
  3619. def _tq_key_label(self) -> Optional[str]:
  3620. """table qualified label based on column key.
  3621. for table-bound columns this is <tablename>_<column key/proxy key>;
  3622. all other expressions it resolves to key/proxy key.
  3623. """
  3624. proxy_key = self._proxy_key
  3625. if proxy_key and proxy_key != self.name:
  3626. return self._gen_tq_label(proxy_key)
  3627. else:
  3628. return self._tq_label
  3629. @HasMemoized.memoized_attribute
  3630. def _tq_label(self) -> Optional[str]:
  3631. """table qualified label based on column name.
  3632. for table-bound columns this is <tablename>_<columnname>; all other
  3633. expressions it resolves to .name.
  3634. """
  3635. return self._gen_tq_label(self.name)
  3636. @HasMemoized.memoized_attribute
  3637. def _render_label_in_columns_clause(self):
  3638. return True
  3639. @HasMemoized.memoized_attribute
  3640. def _non_anon_label(self):
  3641. return self.name
  3642. def _gen_tq_label(
  3643. self, name: str, dedupe_on_key: bool = True
  3644. ) -> Optional[str]:
  3645. return name
  3646. def _bind_param(
  3647. self,
  3648. operator: OperatorType,
  3649. obj: Any,
  3650. type_: Optional[TypeEngine[_T]] = None,
  3651. expanding: bool = False,
  3652. ) -> BindParameter[_T]:
  3653. return BindParameter(
  3654. self.key,
  3655. obj,
  3656. _compared_to_operator=operator,
  3657. _compared_to_type=self.type,
  3658. type_=type_,
  3659. unique=True,
  3660. expanding=expanding,
  3661. )
  3662. def _make_proxy(
  3663. self,
  3664. selectable: FromClause,
  3665. *,
  3666. primary_key: ColumnSet,
  3667. foreign_keys: Set[KeyedColumnElement[Any]],
  3668. name: Optional[str] = None,
  3669. key: Optional[str] = None,
  3670. name_is_truncatable: bool = False,
  3671. compound_select_cols: Optional[Sequence[ColumnElement[Any]]] = None,
  3672. disallow_is_literal: bool = False,
  3673. **kw: Any,
  3674. ) -> typing_Tuple[str, ColumnClause[_T]]:
  3675. c = ColumnClause(
  3676. (
  3677. coercions.expect(roles.TruncatedLabelRole, name or self.name)
  3678. if name_is_truncatable
  3679. else (name or self.name)
  3680. ),
  3681. type_=self.type,
  3682. _selectable=selectable,
  3683. is_literal=False,
  3684. )
  3685. c._propagate_attrs = selectable._propagate_attrs
  3686. if name is None:
  3687. c.key = self.key
  3688. if compound_select_cols:
  3689. c._proxies = list(compound_select_cols)
  3690. else:
  3691. c._proxies = [self]
  3692. if selectable._is_clone_of is not None:
  3693. c._is_clone_of = selectable._is_clone_of.columns.get(c.key)
  3694. return c.key, c
  3695. _PS = ParamSpec("_PS")
  3696. class Label(roles.LabeledColumnExprRole[_T], NamedColumn[_T]):
  3697. """Represents a column label (AS).
  3698. Represent a label, as typically applied to any column-level
  3699. element using the ``AS`` sql keyword.
  3700. """
  3701. __visit_name__ = "label"
  3702. _traverse_internals: _TraverseInternalsType = [
  3703. ("name", InternalTraversal.dp_anon_name),
  3704. ("type", InternalTraversal.dp_type),
  3705. ("_element", InternalTraversal.dp_clauseelement),
  3706. ]
  3707. _cache_key_traversal = [
  3708. ("name", InternalTraversal.dp_anon_name),
  3709. ("_element", InternalTraversal.dp_clauseelement),
  3710. ]
  3711. _element: ColumnElement[_T]
  3712. name: str
  3713. def __init__(
  3714. self,
  3715. name: Optional[str],
  3716. element: _ColumnExpressionArgument[_T],
  3717. type_: Optional[_TypeEngineArgument[_T]] = None,
  3718. ):
  3719. orig_element = element
  3720. element = coercions.expect(
  3721. roles.ExpressionElementRole,
  3722. element,
  3723. apply_propagate_attrs=self,
  3724. )
  3725. while isinstance(element, Label):
  3726. # TODO: this is only covered in test_text.py, but nothing
  3727. # fails if it's removed. determine rationale
  3728. element = element.element
  3729. if name:
  3730. self.name = name
  3731. else:
  3732. self.name = _anonymous_label.safe_construct(
  3733. id(self), getattr(element, "name", "anon")
  3734. )
  3735. if isinstance(orig_element, Label):
  3736. # TODO: no coverage for this block, again would be in
  3737. # test_text.py where the resolve_label concept is important
  3738. self._resolve_label = orig_element._label
  3739. self.key = self._tq_label = self._tq_key_label = self.name
  3740. self._element = element
  3741. self.type = (
  3742. type_api.to_instance(type_)
  3743. if type_ is not None
  3744. else self._element.type
  3745. )
  3746. self._proxies = [element]
  3747. def __reduce__(self):
  3748. return self.__class__, (self.name, self._element, self.type)
  3749. @HasMemoized.memoized_attribute
  3750. def _render_label_in_columns_clause(self):
  3751. return True
  3752. def _bind_param(self, operator, obj, type_=None, expanding=False):
  3753. return BindParameter(
  3754. None,
  3755. obj,
  3756. _compared_to_operator=operator,
  3757. type_=type_,
  3758. _compared_to_type=self.type,
  3759. unique=True,
  3760. expanding=expanding,
  3761. )
  3762. @util.memoized_property
  3763. def _is_implicitly_boolean(self):
  3764. return self.element._is_implicitly_boolean
  3765. @HasMemoized.memoized_attribute
  3766. def _allow_label_resolve(self):
  3767. return self.element._allow_label_resolve
  3768. @property
  3769. def _order_by_label_element(self):
  3770. return self
  3771. @HasMemoized.memoized_attribute
  3772. def element(self) -> ColumnElement[_T]:
  3773. return self._element.self_group(against=operators.as_)
  3774. def self_group(self, against: Optional[OperatorType] = None) -> Label[_T]:
  3775. return self._apply_to_inner(self._element.self_group, against=against)
  3776. def _negate(self):
  3777. return self._apply_to_inner(self._element._negate)
  3778. def _apply_to_inner(
  3779. self,
  3780. fn: Callable[_PS, ColumnElement[_T]],
  3781. *arg: _PS.args,
  3782. **kw: _PS.kwargs,
  3783. ) -> Label[_T]:
  3784. sub_element = fn(*arg, **kw)
  3785. if sub_element is not self._element:
  3786. return Label(self.name, sub_element, type_=self.type)
  3787. else:
  3788. return self
  3789. @property
  3790. def primary_key(self): # type: ignore[override]
  3791. return self.element.primary_key
  3792. @property
  3793. def foreign_keys(self): # type: ignore[override]
  3794. return self.element.foreign_keys
  3795. def _copy_internals(
  3796. self,
  3797. *,
  3798. clone: _CloneCallableType = _clone,
  3799. anonymize_labels: bool = False,
  3800. **kw: Any,
  3801. ) -> None:
  3802. self._reset_memoizations()
  3803. self._element = clone(self._element, **kw)
  3804. if anonymize_labels:
  3805. self.name = _anonymous_label.safe_construct(
  3806. id(self), getattr(self.element, "name", "anon")
  3807. )
  3808. self.key = self._tq_label = self._tq_key_label = self.name
  3809. @util.ro_non_memoized_property
  3810. def _from_objects(self) -> List[FromClause]:
  3811. return self.element._from_objects
  3812. def _make_proxy(
  3813. self,
  3814. selectable: FromClause,
  3815. *,
  3816. primary_key: ColumnSet,
  3817. foreign_keys: Set[KeyedColumnElement[Any]],
  3818. name: Optional[str] = None,
  3819. compound_select_cols: Optional[Sequence[ColumnElement[Any]]] = None,
  3820. **kw: Any,
  3821. ) -> typing_Tuple[str, ColumnClause[_T]]:
  3822. name = self.name if not name else name
  3823. key, e = self.element._make_proxy(
  3824. selectable,
  3825. name=name,
  3826. disallow_is_literal=True,
  3827. name_is_truncatable=isinstance(name, _truncated_label),
  3828. compound_select_cols=compound_select_cols,
  3829. primary_key=primary_key,
  3830. foreign_keys=foreign_keys,
  3831. )
  3832. # there was a note here to remove this assertion, which was here
  3833. # to determine if we later could support a use case where
  3834. # the key and name of a label are separate. But I don't know what
  3835. # that case was. For now, this is an unexpected case that occurs
  3836. # when a label name conflicts with other columns and select()
  3837. # is attempting to disambiguate an explicit label, which is not what
  3838. # the user would want. See issue #6090.
  3839. if key != self.name and not isinstance(self.name, _anonymous_label):
  3840. raise exc.InvalidRequestError(
  3841. "Label name %s is being renamed to an anonymous label due "
  3842. "to disambiguation "
  3843. "which is not supported right now. Please use unique names "
  3844. "for explicit labels." % (self.name)
  3845. )
  3846. e._propagate_attrs = selectable._propagate_attrs
  3847. e._proxies.append(self)
  3848. if self.type is not None:
  3849. e.type = self.type
  3850. return self.key, e
  3851. class ColumnClause(
  3852. roles.DDLReferredColumnRole,
  3853. roles.LabeledColumnExprRole[_T],
  3854. roles.StrAsPlainColumnRole,
  3855. Immutable,
  3856. NamedColumn[_T],
  3857. ):
  3858. """Represents a column expression from any textual string.
  3859. The :class:`.ColumnClause`, a lightweight analogue to the
  3860. :class:`_schema.Column` class, is typically invoked using the
  3861. :func:`_expression.column` function, as in::
  3862. from sqlalchemy import column
  3863. id, name = column("id"), column("name")
  3864. stmt = select(id, name).select_from("user")
  3865. The above statement would produce SQL like:
  3866. .. sourcecode:: sql
  3867. SELECT id, name FROM user
  3868. :class:`.ColumnClause` is the immediate superclass of the schema-specific
  3869. :class:`_schema.Column` object. While the :class:`_schema.Column`
  3870. class has all the
  3871. same capabilities as :class:`.ColumnClause`, the :class:`.ColumnClause`
  3872. class is usable by itself in those cases where behavioral requirements
  3873. are limited to simple SQL expression generation. The object has none of
  3874. the associations with schema-level metadata or with execution-time
  3875. behavior that :class:`_schema.Column` does,
  3876. so in that sense is a "lightweight"
  3877. version of :class:`_schema.Column`.
  3878. Full details on :class:`.ColumnClause` usage is at
  3879. :func:`_expression.column`.
  3880. .. seealso::
  3881. :func:`_expression.column`
  3882. :class:`_schema.Column`
  3883. """
  3884. table: Optional[FromClause]
  3885. is_literal: bool
  3886. __visit_name__ = "column"
  3887. _traverse_internals: _TraverseInternalsType = [
  3888. ("name", InternalTraversal.dp_anon_name),
  3889. ("type", InternalTraversal.dp_type),
  3890. ("table", InternalTraversal.dp_clauseelement),
  3891. ("is_literal", InternalTraversal.dp_boolean),
  3892. ]
  3893. onupdate: Optional[DefaultGenerator] = None
  3894. default: Optional[DefaultGenerator] = None
  3895. server_default: Optional[FetchedValue] = None
  3896. server_onupdate: Optional[FetchedValue] = None
  3897. _is_multiparam_column = False
  3898. @property
  3899. def _is_star(self): # type: ignore[override]
  3900. return self.is_literal and self.name == "*"
  3901. def __init__(
  3902. self,
  3903. text: str,
  3904. type_: Optional[_TypeEngineArgument[_T]] = None,
  3905. is_literal: bool = False,
  3906. _selectable: Optional[FromClause] = None,
  3907. ):
  3908. self.key = self.name = text
  3909. self.table = _selectable
  3910. # if type is None, we get NULLTYPE, which is our _T. But I don't
  3911. # know how to get the overloads to express that correctly
  3912. self.type = type_api.to_instance(type_) # type: ignore
  3913. self.is_literal = is_literal
  3914. def get_children(self, *, column_tables=False, **kw):
  3915. # override base get_children() to not return the Table
  3916. # or selectable that is parent to this column. Traversals
  3917. # expect the columns of tables and subqueries to be leaf nodes.
  3918. return []
  3919. @property
  3920. def entity_namespace(self):
  3921. if self.table is not None:
  3922. return self.table.entity_namespace
  3923. else:
  3924. return super().entity_namespace
  3925. def _clone(self, detect_subquery_cols=False, **kw):
  3926. if (
  3927. detect_subquery_cols
  3928. and self.table is not None
  3929. and self.table._is_subquery
  3930. ):
  3931. clone = kw.pop("clone")
  3932. table = clone(self.table, **kw)
  3933. new = table.c.corresponding_column(self)
  3934. return new
  3935. return super()._clone(**kw)
  3936. @HasMemoized_ro_memoized_attribute
  3937. def _from_objects(self) -> List[FromClause]:
  3938. t = self.table
  3939. if t is not None:
  3940. return [t]
  3941. else:
  3942. return []
  3943. @HasMemoized.memoized_attribute
  3944. def _render_label_in_columns_clause(self):
  3945. return self.table is not None
  3946. @property
  3947. def _ddl_label(self):
  3948. return self._gen_tq_label(self.name, dedupe_on_key=False)
  3949. def _compare_name_for_result(self, other):
  3950. if (
  3951. self.is_literal
  3952. or self.table is None
  3953. or self.table._is_textual
  3954. or not hasattr(other, "proxy_set")
  3955. or (
  3956. isinstance(other, ColumnClause)
  3957. and (
  3958. other.is_literal
  3959. or other.table is None
  3960. or other.table._is_textual
  3961. )
  3962. )
  3963. ):
  3964. return (hasattr(other, "name") and self.name == other.name) or (
  3965. hasattr(other, "_tq_label")
  3966. and self._tq_label == other._tq_label
  3967. )
  3968. else:
  3969. return other.proxy_set.intersection(self.proxy_set)
  3970. def _gen_tq_label(
  3971. self, name: str, dedupe_on_key: bool = True
  3972. ) -> Optional[str]:
  3973. """generate table-qualified label
  3974. for a table-bound column this is <tablename>_<columnname>.
  3975. used primarily for LABEL_STYLE_TABLENAME_PLUS_COL
  3976. as well as the .columns collection on a Join object.
  3977. """
  3978. label: str
  3979. t = self.table
  3980. if self.is_literal:
  3981. return None
  3982. elif t is not None and is_named_from_clause(t):
  3983. if has_schema_attr(t) and t.schema:
  3984. label = t.schema.replace(".", "_") + "_" + t.name + "_" + name
  3985. else:
  3986. assert not TYPE_CHECKING or isinstance(t, NamedFromClause)
  3987. label = t.name + "_" + name
  3988. # propagate name quoting rules for labels.
  3989. if is_quoted_name(name) and name.quote is not None:
  3990. if is_quoted_name(label):
  3991. label.quote = name.quote
  3992. else:
  3993. label = quoted_name(label, name.quote)
  3994. elif is_quoted_name(t.name) and t.name.quote is not None:
  3995. # can't get this situation to occur, so let's
  3996. # assert false on it for now
  3997. assert not isinstance(label, quoted_name)
  3998. label = quoted_name(label, t.name.quote)
  3999. if dedupe_on_key:
  4000. # ensure the label name doesn't conflict with that of an
  4001. # existing column. note that this implies that any Column
  4002. # must **not** set up its _label before its parent table has
  4003. # all of its other Column objects set up. There are several
  4004. # tables in the test suite which will fail otherwise; example:
  4005. # table "owner" has columns "name" and "owner_name". Therefore
  4006. # column owner.name cannot use the label "owner_name", it has
  4007. # to be "owner_name_1".
  4008. if label in t.c:
  4009. _label = label
  4010. counter = 1
  4011. while _label in t.c:
  4012. _label = label + "_" + str(counter)
  4013. counter += 1
  4014. label = _label
  4015. return coercions.expect(roles.TruncatedLabelRole, label)
  4016. else:
  4017. return name
  4018. def _make_proxy(
  4019. self,
  4020. selectable: FromClause,
  4021. *,
  4022. primary_key: ColumnSet,
  4023. foreign_keys: Set[KeyedColumnElement[Any]],
  4024. name: Optional[str] = None,
  4025. key: Optional[str] = None,
  4026. name_is_truncatable: bool = False,
  4027. compound_select_cols: Optional[Sequence[ColumnElement[Any]]] = None,
  4028. disallow_is_literal: bool = False,
  4029. **kw: Any,
  4030. ) -> typing_Tuple[str, ColumnClause[_T]]:
  4031. # the "is_literal" flag normally should never be propagated; a proxied
  4032. # column is always a SQL identifier and never the actual expression
  4033. # being evaluated. however, there is a case where the "is_literal" flag
  4034. # might be used to allow the given identifier to have a fixed quoting
  4035. # pattern already, so maintain the flag for the proxy unless a
  4036. # :class:`.Label` object is creating the proxy. See [ticket:4730].
  4037. is_literal = (
  4038. not disallow_is_literal
  4039. and self.is_literal
  4040. and (
  4041. # note this does not accommodate for quoted_name differences
  4042. # right now
  4043. name is None
  4044. or name == self.name
  4045. )
  4046. )
  4047. c = self._constructor(
  4048. (
  4049. coercions.expect(roles.TruncatedLabelRole, name or self.name)
  4050. if name_is_truncatable
  4051. else (name or self.name)
  4052. ),
  4053. type_=self.type,
  4054. _selectable=selectable,
  4055. is_literal=is_literal,
  4056. )
  4057. c._propagate_attrs = selectable._propagate_attrs
  4058. if name is None:
  4059. c.key = self.key
  4060. if compound_select_cols:
  4061. c._proxies = list(compound_select_cols)
  4062. else:
  4063. c._proxies = [self]
  4064. if selectable._is_clone_of is not None:
  4065. c._is_clone_of = selectable._is_clone_of.columns.get(c.key)
  4066. return c.key, c
  4067. class TableValuedColumn(NamedColumn[_T]):
  4068. __visit_name__ = "table_valued_column"
  4069. _traverse_internals: _TraverseInternalsType = [
  4070. ("name", InternalTraversal.dp_anon_name),
  4071. ("type", InternalTraversal.dp_type),
  4072. ("scalar_alias", InternalTraversal.dp_clauseelement),
  4073. ]
  4074. def __init__(self, scalar_alias: NamedFromClause, type_: TypeEngine[_T]):
  4075. self.scalar_alias = scalar_alias
  4076. self.key = self.name = scalar_alias.name
  4077. self.type = type_
  4078. def _copy_internals(
  4079. self, clone: _CloneCallableType = _clone, **kw: Any
  4080. ) -> None:
  4081. self.scalar_alias = clone(self.scalar_alias, **kw)
  4082. self.key = self.name = self.scalar_alias.name
  4083. @util.ro_non_memoized_property
  4084. def _from_objects(self) -> List[FromClause]:
  4085. return [self.scalar_alias]
  4086. class CollationClause(ColumnElement[str]):
  4087. __visit_name__ = "collation"
  4088. _traverse_internals: _TraverseInternalsType = [
  4089. ("collation", InternalTraversal.dp_string)
  4090. ]
  4091. @classmethod
  4092. @util.preload_module("sqlalchemy.sql.sqltypes")
  4093. def _create_collation_expression(
  4094. cls, expression: _ColumnExpressionArgument[str], collation: str
  4095. ) -> BinaryExpression[str]:
  4096. sqltypes = util.preloaded.sql_sqltypes
  4097. expr = coercions.expect(roles.ExpressionElementRole[str], expression)
  4098. if expr.type._type_affinity is sqltypes.String:
  4099. collate_type = expr.type._with_collation(collation)
  4100. else:
  4101. collate_type = expr.type
  4102. return BinaryExpression(
  4103. expr,
  4104. CollationClause(collation),
  4105. operators.collate,
  4106. type_=collate_type,
  4107. )
  4108. def __init__(self, collation):
  4109. self.collation = collation
  4110. class _IdentifiedClause(Executable, ClauseElement):
  4111. __visit_name__ = "identified"
  4112. def __init__(self, ident):
  4113. self.ident = ident
  4114. class SavepointClause(_IdentifiedClause):
  4115. __visit_name__ = "savepoint"
  4116. inherit_cache = False
  4117. class RollbackToSavepointClause(_IdentifiedClause):
  4118. __visit_name__ = "rollback_to_savepoint"
  4119. inherit_cache = False
  4120. class ReleaseSavepointClause(_IdentifiedClause):
  4121. __visit_name__ = "release_savepoint"
  4122. inherit_cache = False
  4123. class quoted_name(util.MemoizedSlots, str):
  4124. """Represent a SQL identifier combined with quoting preferences.
  4125. :class:`.quoted_name` is a Python unicode/str subclass which
  4126. represents a particular identifier name along with a
  4127. ``quote`` flag. This ``quote`` flag, when set to
  4128. ``True`` or ``False``, overrides automatic quoting behavior
  4129. for this identifier in order to either unconditionally quote
  4130. or to not quote the name. If left at its default of ``None``,
  4131. quoting behavior is applied to the identifier on a per-backend basis
  4132. based on an examination of the token itself.
  4133. A :class:`.quoted_name` object with ``quote=True`` is also
  4134. prevented from being modified in the case of a so-called
  4135. "name normalize" option. Certain database backends, such as
  4136. Oracle Database, Firebird, and DB2 "normalize" case-insensitive names
  4137. as uppercase. The SQLAlchemy dialects for these backends
  4138. convert from SQLAlchemy's lower-case-means-insensitive convention
  4139. to the upper-case-means-insensitive conventions of those backends.
  4140. The ``quote=True`` flag here will prevent this conversion from occurring
  4141. to support an identifier that's quoted as all lower case against
  4142. such a backend.
  4143. The :class:`.quoted_name` object is normally created automatically
  4144. when specifying the name for key schema constructs such as
  4145. :class:`_schema.Table`, :class:`_schema.Column`, and others.
  4146. The class can also be
  4147. passed explicitly as the name to any function that receives a name which
  4148. can be quoted. Such as to use the :meth:`_engine.Engine.has_table`
  4149. method with
  4150. an unconditionally quoted name::
  4151. from sqlalchemy import create_engine
  4152. from sqlalchemy import inspect
  4153. from sqlalchemy.sql import quoted_name
  4154. engine = create_engine("oracle+oracledb://some_dsn")
  4155. print(inspect(engine).has_table(quoted_name("some_table", True)))
  4156. The above logic will run the "has table" logic against the Oracle Database
  4157. backend, passing the name exactly as ``"some_table"`` without converting to
  4158. upper case.
  4159. .. versionchanged:: 1.2 The :class:`.quoted_name` construct is now
  4160. importable from ``sqlalchemy.sql``, in addition to the previous
  4161. location of ``sqlalchemy.sql.elements``.
  4162. """
  4163. __slots__ = "quote", "lower", "upper"
  4164. quote: Optional[bool]
  4165. @overload
  4166. @classmethod
  4167. def construct(cls, value: str, quote: Optional[bool]) -> quoted_name: ...
  4168. @overload
  4169. @classmethod
  4170. def construct(cls, value: None, quote: Optional[bool]) -> None: ...
  4171. @classmethod
  4172. def construct(
  4173. cls, value: Optional[str], quote: Optional[bool]
  4174. ) -> Optional[quoted_name]:
  4175. if value is None:
  4176. return None
  4177. else:
  4178. return quoted_name(value, quote)
  4179. def __new__(cls, value: str, quote: Optional[bool]) -> quoted_name:
  4180. assert (
  4181. value is not None
  4182. ), "use quoted_name.construct() for None passthrough"
  4183. if isinstance(value, cls) and (quote is None or value.quote == quote):
  4184. return value
  4185. self = super().__new__(cls, value)
  4186. self.quote = quote
  4187. return self
  4188. def __reduce__(self):
  4189. return quoted_name, (str(self), self.quote)
  4190. def _memoized_method_lower(self):
  4191. if self.quote:
  4192. return self
  4193. else:
  4194. return str(self).lower()
  4195. def _memoized_method_upper(self):
  4196. if self.quote:
  4197. return self
  4198. else:
  4199. return str(self).upper()
  4200. def _find_columns(clause: ClauseElement) -> Set[ColumnClause[Any]]:
  4201. """locate Column objects within the given expression."""
  4202. cols: Set[ColumnClause[Any]] = set()
  4203. traverse(clause, {}, {"column": cols.add})
  4204. return cols
  4205. def _type_from_args(args: Sequence[ColumnElement[_T]]) -> TypeEngine[_T]:
  4206. for a in args:
  4207. if not a.type._isnull:
  4208. return a.type
  4209. else:
  4210. return type_api.NULLTYPE # type: ignore
  4211. def _corresponding_column_or_error(fromclause, column, require_embedded=False):
  4212. c = fromclause.corresponding_column(
  4213. column, require_embedded=require_embedded
  4214. )
  4215. if c is None:
  4216. raise exc.InvalidRequestError(
  4217. "Given column '%s', attached to table '%s', "
  4218. "failed to locate a corresponding column from table '%s'"
  4219. % (column, getattr(column, "table", None), fromclause.description)
  4220. )
  4221. return c
  4222. class _memoized_property_but_not_nulltype(
  4223. util.memoized_property["TypeEngine[_T]"]
  4224. ):
  4225. """memoized property, but dont memoize NullType"""
  4226. def __get__(self, obj, cls):
  4227. if obj is None:
  4228. return self
  4229. result = self.fget(obj)
  4230. if not result._isnull:
  4231. obj.__dict__[self.__name__] = result
  4232. return result
  4233. class AnnotatedColumnElement(Annotated):
  4234. _Annotated__element: ColumnElement[Any]
  4235. def __init__(self, element, values):
  4236. Annotated.__init__(self, element, values)
  4237. for attr in (
  4238. "comparator",
  4239. "_proxy_key",
  4240. "_tq_key_label",
  4241. "_tq_label",
  4242. "_non_anon_label",
  4243. "type",
  4244. ):
  4245. self.__dict__.pop(attr, None)
  4246. for attr in ("name", "key", "table"):
  4247. if self.__dict__.get(attr, False) is None:
  4248. self.__dict__.pop(attr)
  4249. def _with_annotations(self, values):
  4250. clone = super()._with_annotations(values)
  4251. for attr in (
  4252. "comparator",
  4253. "_proxy_key",
  4254. "_tq_key_label",
  4255. "_tq_label",
  4256. "_non_anon_label",
  4257. ):
  4258. clone.__dict__.pop(attr, None)
  4259. return clone
  4260. @util.memoized_property
  4261. def name(self):
  4262. """pull 'name' from parent, if not present"""
  4263. return self._Annotated__element.name
  4264. @_memoized_property_but_not_nulltype
  4265. def type(self):
  4266. """pull 'type' from parent and don't cache if null.
  4267. type is routinely changed on existing columns within the
  4268. mapped_column() initialization process, and "type" is also consulted
  4269. during the creation of SQL expressions. Therefore it can change after
  4270. it was already retrieved. At the same time we don't want annotated
  4271. objects having overhead when expressions are produced, so continue
  4272. to memoize, but only when we have a non-null type.
  4273. """
  4274. return self._Annotated__element.type
  4275. @util.memoized_property
  4276. def table(self):
  4277. """pull 'table' from parent, if not present"""
  4278. return self._Annotated__element.table
  4279. @util.memoized_property
  4280. def key(self):
  4281. """pull 'key' from parent, if not present"""
  4282. return self._Annotated__element.key
  4283. @util.memoized_property
  4284. def info(self) -> _InfoType:
  4285. if TYPE_CHECKING:
  4286. assert isinstance(self._Annotated__element, Column)
  4287. return self._Annotated__element.info
  4288. @util.memoized_property
  4289. def _anon_name_label(self) -> str:
  4290. return self._Annotated__element._anon_name_label
  4291. class _truncated_label(quoted_name):
  4292. """A unicode subclass used to identify symbolic "
  4293. "names that may require truncation."""
  4294. __slots__ = ()
  4295. def __new__(cls, value: str, quote: Optional[bool] = None) -> Any:
  4296. quote = getattr(value, "quote", quote)
  4297. # return super(_truncated_label, cls).__new__(cls, value, quote, True)
  4298. return super().__new__(cls, value, quote)
  4299. def __reduce__(self) -> Any:
  4300. return self.__class__, (str(self), self.quote)
  4301. def apply_map(self, map_: Mapping[str, Any]) -> str:
  4302. return self
  4303. class conv(_truncated_label):
  4304. """Mark a string indicating that a name has already been converted
  4305. by a naming convention.
  4306. This is a string subclass that indicates a name that should not be
  4307. subject to any further naming conventions.
  4308. E.g. when we create a :class:`.Constraint` using a naming convention
  4309. as follows::
  4310. m = MetaData(
  4311. naming_convention={"ck": "ck_%(table_name)s_%(constraint_name)s"}
  4312. )
  4313. t = Table(
  4314. "t", m, Column("x", Integer), CheckConstraint("x > 5", name="x5")
  4315. )
  4316. The name of the above constraint will be rendered as ``"ck_t_x5"``.
  4317. That is, the existing name ``x5`` is used in the naming convention as the
  4318. ``constraint_name`` token.
  4319. In some situations, such as in migration scripts, we may be rendering
  4320. the above :class:`.CheckConstraint` with a name that's already been
  4321. converted. In order to make sure the name isn't double-modified, the
  4322. new name is applied using the :func:`_schema.conv` marker. We can
  4323. use this explicitly as follows::
  4324. m = MetaData(
  4325. naming_convention={"ck": "ck_%(table_name)s_%(constraint_name)s"}
  4326. )
  4327. t = Table(
  4328. "t",
  4329. m,
  4330. Column("x", Integer),
  4331. CheckConstraint("x > 5", name=conv("ck_t_x5")),
  4332. )
  4333. Where above, the :func:`_schema.conv` marker indicates that the constraint
  4334. name here is final, and the name will render as ``"ck_t_x5"`` and not
  4335. ``"ck_t_ck_t_x5"``
  4336. .. seealso::
  4337. :ref:`constraint_naming_conventions`
  4338. """
  4339. __slots__ = ()
  4340. # for backwards compatibility in case
  4341. # someone is re-implementing the
  4342. # _truncated_identifier() sequence in a custom
  4343. # compiler
  4344. _generated_label = _truncated_label
  4345. class _anonymous_label(_truncated_label):
  4346. """A unicode subclass used to identify anonymously
  4347. generated names."""
  4348. __slots__ = ()
  4349. @classmethod
  4350. def safe_construct(
  4351. cls,
  4352. seed: int,
  4353. body: str,
  4354. enclosing_label: Optional[str] = None,
  4355. sanitize_key: bool = False,
  4356. ) -> _anonymous_label:
  4357. # need to escape chars that interfere with format
  4358. # strings in any case, issue #8724
  4359. body = re.sub(r"[%\(\) \$]+", "_", body)
  4360. if sanitize_key:
  4361. # sanitize_key is then an extra step used by BindParameter
  4362. body = body.strip("_")
  4363. label = "%%(%d %s)s" % (seed, body.replace("%", "%%"))
  4364. if enclosing_label:
  4365. label = "%s%s" % (enclosing_label, label)
  4366. return _anonymous_label(label)
  4367. def __add__(self, other):
  4368. if "%" in other and not isinstance(other, _anonymous_label):
  4369. other = str(other).replace("%", "%%")
  4370. else:
  4371. other = str(other)
  4372. return _anonymous_label(
  4373. quoted_name(
  4374. str.__add__(self, other),
  4375. self.quote,
  4376. )
  4377. )
  4378. def __radd__(self, other):
  4379. if "%" in other and not isinstance(other, _anonymous_label):
  4380. other = str(other).replace("%", "%%")
  4381. else:
  4382. other = str(other)
  4383. return _anonymous_label(
  4384. quoted_name(
  4385. str.__add__(other, self),
  4386. self.quote,
  4387. )
  4388. )
  4389. def apply_map(self, map_):
  4390. if self.quote is not None:
  4391. # preserve quoting only if necessary
  4392. return quoted_name(self % map_, self.quote)
  4393. else:
  4394. # else skip the constructor call
  4395. return self % map_