elements.py 175 KB

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