elements.py 178 KB

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