compiler.py 277 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999
  1. # sql/compiler.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: allow-untyped-defs, allow-untyped-calls
  8. """Base SQL and DDL compiler implementations.
  9. Classes provided include:
  10. :class:`.compiler.SQLCompiler` - renders SQL
  11. strings
  12. :class:`.compiler.DDLCompiler` - renders DDL
  13. (data definition language) strings
  14. :class:`.compiler.GenericTypeCompiler` - renders
  15. type specification strings.
  16. To generate user-defined SQL strings, see
  17. :doc:`/ext/compiler`.
  18. """
  19. from __future__ import annotations
  20. import collections
  21. import collections.abc as collections_abc
  22. import contextlib
  23. from enum import IntEnum
  24. import functools
  25. import itertools
  26. import operator
  27. import re
  28. from time import perf_counter
  29. import typing
  30. from typing import Any
  31. from typing import Callable
  32. from typing import cast
  33. from typing import ClassVar
  34. from typing import Dict
  35. from typing import FrozenSet
  36. from typing import Iterable
  37. from typing import Iterator
  38. from typing import List
  39. from typing import Mapping
  40. from typing import MutableMapping
  41. from typing import NamedTuple
  42. from typing import NoReturn
  43. from typing import Optional
  44. from typing import Pattern
  45. from typing import Sequence
  46. from typing import Set
  47. from typing import Tuple
  48. from typing import Type
  49. from typing import TYPE_CHECKING
  50. from typing import Union
  51. from . import base
  52. from . import coercions
  53. from . import crud
  54. from . import elements
  55. from . import functions
  56. from . import operators
  57. from . import roles
  58. from . import schema
  59. from . import selectable
  60. from . import sqltypes
  61. from . import util as sql_util
  62. from ._typing import is_column_element
  63. from ._typing import is_dml
  64. from .base import _de_clone
  65. from .base import _from_objects
  66. from .base import _NONE_NAME
  67. from .base import _SentinelDefaultCharacterization
  68. from .base import NO_ARG
  69. from .elements import quoted_name
  70. from .sqltypes import TupleType
  71. from .visitors import prefix_anon_map
  72. from .. import exc
  73. from .. import util
  74. from ..util import FastIntFlag
  75. from ..util.typing import Literal
  76. from ..util.typing import Protocol
  77. from ..util.typing import Self
  78. from ..util.typing import TypedDict
  79. if typing.TYPE_CHECKING:
  80. from .annotation import _AnnotationDict
  81. from .base import _AmbiguousTableNameMap
  82. from .base import CompileState
  83. from .base import Executable
  84. from .cache_key import CacheKey
  85. from .ddl import ExecutableDDLElement
  86. from .dml import Insert
  87. from .dml import Update
  88. from .dml import UpdateBase
  89. from .dml import UpdateDMLState
  90. from .dml import ValuesBase
  91. from .elements import _truncated_label
  92. from .elements import BinaryExpression
  93. from .elements import BindParameter
  94. from .elements import ClauseElement
  95. from .elements import ColumnClause
  96. from .elements import ColumnElement
  97. from .elements import False_
  98. from .elements import Label
  99. from .elements import Null
  100. from .elements import True_
  101. from .functions import Function
  102. from .schema import Column
  103. from .schema import Constraint
  104. from .schema import ForeignKeyConstraint
  105. from .schema import Index
  106. from .schema import PrimaryKeyConstraint
  107. from .schema import Table
  108. from .schema import UniqueConstraint
  109. from .selectable import _ColumnsClauseElement
  110. from .selectable import AliasedReturnsRows
  111. from .selectable import CompoundSelectState
  112. from .selectable import CTE
  113. from .selectable import FromClause
  114. from .selectable import NamedFromClause
  115. from .selectable import ReturnsRows
  116. from .selectable import Select
  117. from .selectable import SelectState
  118. from .type_api import _BindProcessorType
  119. from .type_api import TypeDecorator
  120. from .type_api import TypeEngine
  121. from .type_api import UserDefinedType
  122. from .visitors import Visitable
  123. from ..engine.cursor import CursorResultMetaData
  124. from ..engine.interfaces import _CoreSingleExecuteParams
  125. from ..engine.interfaces import _DBAPIAnyExecuteParams
  126. from ..engine.interfaces import _DBAPIMultiExecuteParams
  127. from ..engine.interfaces import _DBAPISingleExecuteParams
  128. from ..engine.interfaces import _ExecuteOptions
  129. from ..engine.interfaces import _GenericSetInputSizesType
  130. from ..engine.interfaces import _MutableCoreSingleExecuteParams
  131. from ..engine.interfaces import Dialect
  132. from ..engine.interfaces import SchemaTranslateMapType
  133. _FromHintsType = Dict["FromClause", str]
  134. RESERVED_WORDS = {
  135. "all",
  136. "analyse",
  137. "analyze",
  138. "and",
  139. "any",
  140. "array",
  141. "as",
  142. "asc",
  143. "asymmetric",
  144. "authorization",
  145. "between",
  146. "binary",
  147. "both",
  148. "case",
  149. "cast",
  150. "check",
  151. "collate",
  152. "column",
  153. "constraint",
  154. "create",
  155. "cross",
  156. "current_date",
  157. "current_role",
  158. "current_time",
  159. "current_timestamp",
  160. "current_user",
  161. "default",
  162. "deferrable",
  163. "desc",
  164. "distinct",
  165. "do",
  166. "else",
  167. "end",
  168. "except",
  169. "false",
  170. "for",
  171. "foreign",
  172. "freeze",
  173. "from",
  174. "full",
  175. "grant",
  176. "group",
  177. "having",
  178. "ilike",
  179. "in",
  180. "initially",
  181. "inner",
  182. "intersect",
  183. "into",
  184. "is",
  185. "isnull",
  186. "join",
  187. "leading",
  188. "left",
  189. "like",
  190. "limit",
  191. "localtime",
  192. "localtimestamp",
  193. "natural",
  194. "new",
  195. "not",
  196. "notnull",
  197. "null",
  198. "off",
  199. "offset",
  200. "old",
  201. "on",
  202. "only",
  203. "or",
  204. "order",
  205. "outer",
  206. "overlaps",
  207. "placing",
  208. "primary",
  209. "references",
  210. "right",
  211. "select",
  212. "session_user",
  213. "set",
  214. "similar",
  215. "some",
  216. "symmetric",
  217. "table",
  218. "then",
  219. "to",
  220. "trailing",
  221. "true",
  222. "union",
  223. "unique",
  224. "user",
  225. "using",
  226. "verbose",
  227. "when",
  228. "where",
  229. }
  230. LEGAL_CHARACTERS = re.compile(r"^[A-Z0-9_$]+$", re.I)
  231. LEGAL_CHARACTERS_PLUS_SPACE = re.compile(r"^[A-Z0-9_ $]+$", re.I)
  232. ILLEGAL_INITIAL_CHARACTERS = {str(x) for x in range(0, 10)}.union(["$"])
  233. FK_ON_DELETE = re.compile(
  234. r"^(?:RESTRICT|CASCADE|SET NULL|NO ACTION|SET DEFAULT)$", re.I
  235. )
  236. FK_ON_UPDATE = re.compile(
  237. r"^(?:RESTRICT|CASCADE|SET NULL|NO ACTION|SET DEFAULT)$", re.I
  238. )
  239. FK_INITIALLY = re.compile(r"^(?:DEFERRED|IMMEDIATE)$", re.I)
  240. BIND_PARAMS = re.compile(r"(?<![:\w\$\x5c]):([\w\$]+)(?![:\w\$])", re.UNICODE)
  241. BIND_PARAMS_ESC = re.compile(r"\x5c(:[\w\$]*)(?![:\w\$])", re.UNICODE)
  242. _pyformat_template = "%%(%(name)s)s"
  243. BIND_TEMPLATES = {
  244. "pyformat": _pyformat_template,
  245. "qmark": "?",
  246. "format": "%%s",
  247. "numeric": ":[_POSITION]",
  248. "numeric_dollar": "$[_POSITION]",
  249. "named": ":%(name)s",
  250. }
  251. OPERATORS = {
  252. # binary
  253. operators.and_: " AND ",
  254. operators.or_: " OR ",
  255. operators.add: " + ",
  256. operators.mul: " * ",
  257. operators.sub: " - ",
  258. operators.mod: " % ",
  259. operators.neg: "-",
  260. operators.lt: " < ",
  261. operators.le: " <= ",
  262. operators.ne: " != ",
  263. operators.gt: " > ",
  264. operators.ge: " >= ",
  265. operators.eq: " = ",
  266. operators.is_distinct_from: " IS DISTINCT FROM ",
  267. operators.is_not_distinct_from: " IS NOT DISTINCT FROM ",
  268. operators.concat_op: " || ",
  269. operators.match_op: " MATCH ",
  270. operators.not_match_op: " NOT MATCH ",
  271. operators.in_op: " IN ",
  272. operators.not_in_op: " NOT IN ",
  273. operators.comma_op: ", ",
  274. operators.from_: " FROM ",
  275. operators.as_: " AS ",
  276. operators.is_: " IS ",
  277. operators.is_not: " IS NOT ",
  278. operators.collate: " COLLATE ",
  279. # unary
  280. operators.exists: "EXISTS ",
  281. operators.distinct_op: "DISTINCT ",
  282. operators.inv: "NOT ",
  283. operators.any_op: "ANY ",
  284. operators.all_op: "ALL ",
  285. # modifiers
  286. operators.desc_op: " DESC",
  287. operators.asc_op: " ASC",
  288. operators.nulls_first_op: " NULLS FIRST",
  289. operators.nulls_last_op: " NULLS LAST",
  290. # bitwise
  291. operators.bitwise_xor_op: " ^ ",
  292. operators.bitwise_or_op: " | ",
  293. operators.bitwise_and_op: " & ",
  294. operators.bitwise_not_op: "~",
  295. operators.bitwise_lshift_op: " << ",
  296. operators.bitwise_rshift_op: " >> ",
  297. }
  298. FUNCTIONS: Dict[Type[Function[Any]], str] = {
  299. functions.coalesce: "coalesce",
  300. functions.current_date: "CURRENT_DATE",
  301. functions.current_time: "CURRENT_TIME",
  302. functions.current_timestamp: "CURRENT_TIMESTAMP",
  303. functions.current_user: "CURRENT_USER",
  304. functions.localtime: "LOCALTIME",
  305. functions.localtimestamp: "LOCALTIMESTAMP",
  306. functions.random: "random",
  307. functions.sysdate: "sysdate",
  308. functions.session_user: "SESSION_USER",
  309. functions.user: "USER",
  310. functions.cube: "CUBE",
  311. functions.rollup: "ROLLUP",
  312. functions.grouping_sets: "GROUPING SETS",
  313. }
  314. EXTRACT_MAP = {
  315. "month": "month",
  316. "day": "day",
  317. "year": "year",
  318. "second": "second",
  319. "hour": "hour",
  320. "doy": "doy",
  321. "minute": "minute",
  322. "quarter": "quarter",
  323. "dow": "dow",
  324. "week": "week",
  325. "epoch": "epoch",
  326. "milliseconds": "milliseconds",
  327. "microseconds": "microseconds",
  328. "timezone_hour": "timezone_hour",
  329. "timezone_minute": "timezone_minute",
  330. }
  331. COMPOUND_KEYWORDS = {
  332. selectable._CompoundSelectKeyword.UNION: "UNION",
  333. selectable._CompoundSelectKeyword.UNION_ALL: "UNION ALL",
  334. selectable._CompoundSelectKeyword.EXCEPT: "EXCEPT",
  335. selectable._CompoundSelectKeyword.EXCEPT_ALL: "EXCEPT ALL",
  336. selectable._CompoundSelectKeyword.INTERSECT: "INTERSECT",
  337. selectable._CompoundSelectKeyword.INTERSECT_ALL: "INTERSECT ALL",
  338. }
  339. class ResultColumnsEntry(NamedTuple):
  340. """Tracks a column expression that is expected to be represented
  341. in the result rows for this statement.
  342. This normally refers to the columns clause of a SELECT statement
  343. but may also refer to a RETURNING clause, as well as for dialect-specific
  344. emulations.
  345. """
  346. keyname: str
  347. """string name that's expected in cursor.description"""
  348. name: str
  349. """column name, may be labeled"""
  350. objects: Tuple[Any, ...]
  351. """sequence of objects that should be able to locate this column
  352. in a RowMapping. This is typically string names and aliases
  353. as well as Column objects.
  354. """
  355. type: TypeEngine[Any]
  356. """Datatype to be associated with this column. This is where
  357. the "result processing" logic directly links the compiled statement
  358. to the rows that come back from the cursor.
  359. """
  360. class _ResultMapAppender(Protocol):
  361. def __call__(
  362. self,
  363. keyname: str,
  364. name: str,
  365. objects: Sequence[Any],
  366. type_: TypeEngine[Any],
  367. ) -> None: ...
  368. # integer indexes into ResultColumnsEntry used by cursor.py.
  369. # some profiling showed integer access faster than named tuple
  370. RM_RENDERED_NAME: Literal[0] = 0
  371. RM_NAME: Literal[1] = 1
  372. RM_OBJECTS: Literal[2] = 2
  373. RM_TYPE: Literal[3] = 3
  374. class _BaseCompilerStackEntry(TypedDict):
  375. asfrom_froms: Set[FromClause]
  376. correlate_froms: Set[FromClause]
  377. selectable: ReturnsRows
  378. class _CompilerStackEntry(_BaseCompilerStackEntry, total=False):
  379. compile_state: CompileState
  380. need_result_map_for_nested: bool
  381. need_result_map_for_compound: bool
  382. select_0: ReturnsRows
  383. insert_from_select: Select[Any]
  384. class ExpandedState(NamedTuple):
  385. """represents state to use when producing "expanded" and
  386. "post compile" bound parameters for a statement.
  387. "expanded" parameters are parameters that are generated at
  388. statement execution time to suit a number of parameters passed, the most
  389. prominent example being the individual elements inside of an IN expression.
  390. "post compile" parameters are parameters where the SQL literal value
  391. will be rendered into the SQL statement at execution time, rather than
  392. being passed as separate parameters to the driver.
  393. To create an :class:`.ExpandedState` instance, use the
  394. :meth:`.SQLCompiler.construct_expanded_state` method on any
  395. :class:`.SQLCompiler` instance.
  396. """
  397. statement: str
  398. """String SQL statement with parameters fully expanded"""
  399. parameters: _CoreSingleExecuteParams
  400. """Parameter dictionary with parameters fully expanded.
  401. For a statement that uses named parameters, this dictionary will map
  402. exactly to the names in the statement. For a statement that uses
  403. positional parameters, the :attr:`.ExpandedState.positional_parameters`
  404. will yield a tuple with the positional parameter set.
  405. """
  406. processors: Mapping[str, _BindProcessorType[Any]]
  407. """mapping of bound value processors"""
  408. positiontup: Optional[Sequence[str]]
  409. """Sequence of string names indicating the order of positional
  410. parameters"""
  411. parameter_expansion: Mapping[str, List[str]]
  412. """Mapping representing the intermediary link from original parameter
  413. name to list of "expanded" parameter names, for those parameters that
  414. were expanded."""
  415. @property
  416. def positional_parameters(self) -> Tuple[Any, ...]:
  417. """Tuple of positional parameters, for statements that were compiled
  418. using a positional paramstyle.
  419. """
  420. if self.positiontup is None:
  421. raise exc.InvalidRequestError(
  422. "statement does not use a positional paramstyle"
  423. )
  424. return tuple(self.parameters[key] for key in self.positiontup)
  425. @property
  426. def additional_parameters(self) -> _CoreSingleExecuteParams:
  427. """synonym for :attr:`.ExpandedState.parameters`."""
  428. return self.parameters
  429. class _InsertManyValues(NamedTuple):
  430. """represents state to use for executing an "insertmanyvalues" statement.
  431. The primary consumers of this object are the
  432. :meth:`.SQLCompiler._deliver_insertmanyvalues_batches` and
  433. :meth:`.DefaultDialect._deliver_insertmanyvalues_batches` methods.
  434. .. versionadded:: 2.0
  435. """
  436. is_default_expr: bool
  437. """if True, the statement is of the form
  438. ``INSERT INTO TABLE DEFAULT VALUES``, and can't be rewritten as a "batch"
  439. """
  440. single_values_expr: str
  441. """The rendered "values" clause of the INSERT statement.
  442. This is typically the parenthesized section e.g. "(?, ?, ?)" or similar.
  443. The insertmanyvalues logic uses this string as a search and replace
  444. target.
  445. """
  446. insert_crud_params: List[crud._CrudParamElementStr]
  447. """List of Column / bind names etc. used while rewriting the statement"""
  448. num_positional_params_counted: int
  449. """the number of bound parameters in a single-row statement.
  450. This count may be larger or smaller than the actual number of columns
  451. targeted in the INSERT, as it accommodates for SQL expressions
  452. in the values list that may have zero or more parameters embedded
  453. within them.
  454. This count is part of what's used to organize rewritten parameter lists
  455. when batching.
  456. """
  457. sort_by_parameter_order: bool = False
  458. """if the deterministic_returnined_order parameter were used on the
  459. insert.
  460. All of the attributes following this will only be used if this is True.
  461. """
  462. includes_upsert_behaviors: bool = False
  463. """if True, we have to accommodate for upsert behaviors.
  464. This will in some cases downgrade "insertmanyvalues" that requests
  465. deterministic ordering.
  466. """
  467. sentinel_columns: Optional[Sequence[Column[Any]]] = None
  468. """List of sentinel columns that were located.
  469. This list is only here if the INSERT asked for
  470. sort_by_parameter_order=True,
  471. and dialect-appropriate sentinel columns were located.
  472. .. versionadded:: 2.0.10
  473. """
  474. num_sentinel_columns: int = 0
  475. """how many sentinel columns are in the above list, if any.
  476. This is the same as
  477. ``len(sentinel_columns) if sentinel_columns is not None else 0``
  478. """
  479. sentinel_param_keys: Optional[Sequence[str]] = None
  480. """parameter str keys in each param dictionary / tuple
  481. that would link to the client side "sentinel" values for that row, which
  482. we can use to match up parameter sets to result rows.
  483. This is only present if sentinel_columns is present and the INSERT
  484. statement actually refers to client side values for these sentinel
  485. columns.
  486. .. versionadded:: 2.0.10
  487. .. versionchanged:: 2.0.29 - the sequence is now string dictionary keys
  488. only, used against the "compiled parameteters" collection before
  489. the parameters were converted by bound parameter processors
  490. """
  491. implicit_sentinel: bool = False
  492. """if True, we have exactly one sentinel column and it uses a server side
  493. value, currently has to generate an incrementing integer value.
  494. The dialect in question would have asserted that it supports receiving
  495. these values back and sorting on that value as a means of guaranteeing
  496. correlation with the incoming parameter list.
  497. .. versionadded:: 2.0.10
  498. """
  499. embed_values_counter: bool = False
  500. """Whether to embed an incrementing integer counter in each parameter
  501. set within the VALUES clause as parameters are batched over.
  502. This is only used for a specific INSERT..SELECT..VALUES..RETURNING syntax
  503. where a subquery is used to produce value tuples. Current support
  504. includes PostgreSQL, Microsoft SQL Server.
  505. .. versionadded:: 2.0.10
  506. """
  507. class _InsertManyValuesBatch(NamedTuple):
  508. """represents an individual batch SQL statement for insertmanyvalues.
  509. This is passed through the
  510. :meth:`.SQLCompiler._deliver_insertmanyvalues_batches` and
  511. :meth:`.DefaultDialect._deliver_insertmanyvalues_batches` methods out
  512. to the :class:`.Connection` within the
  513. :meth:`.Connection._exec_insertmany_context` method.
  514. .. versionadded:: 2.0.10
  515. """
  516. replaced_statement: str
  517. replaced_parameters: _DBAPIAnyExecuteParams
  518. processed_setinputsizes: Optional[_GenericSetInputSizesType]
  519. batch: Sequence[_DBAPISingleExecuteParams]
  520. sentinel_values: Sequence[Tuple[Any, ...]]
  521. current_batch_size: int
  522. batchnum: int
  523. total_batches: int
  524. rows_sorted: bool
  525. is_downgraded: bool
  526. class InsertmanyvaluesSentinelOpts(FastIntFlag):
  527. """bitflag enum indicating styles of PK defaults
  528. which can work as implicit sentinel columns
  529. """
  530. NOT_SUPPORTED = 1
  531. AUTOINCREMENT = 2
  532. IDENTITY = 4
  533. SEQUENCE = 8
  534. ANY_AUTOINCREMENT = AUTOINCREMENT | IDENTITY | SEQUENCE
  535. _SUPPORTED_OR_NOT = NOT_SUPPORTED | ANY_AUTOINCREMENT
  536. USE_INSERT_FROM_SELECT = 16
  537. RENDER_SELECT_COL_CASTS = 64
  538. class CompilerState(IntEnum):
  539. COMPILING = 0
  540. """statement is present, compilation phase in progress"""
  541. STRING_APPLIED = 1
  542. """statement is present, string form of the statement has been applied.
  543. Additional processors by subclasses may still be pending.
  544. """
  545. NO_STATEMENT = 2
  546. """compiler does not have a statement to compile, is used
  547. for method access"""
  548. class Linting(IntEnum):
  549. """represent preferences for the 'SQL linting' feature.
  550. this feature currently includes support for flagging cartesian products
  551. in SQL statements.
  552. """
  553. NO_LINTING = 0
  554. "Disable all linting."
  555. COLLECT_CARTESIAN_PRODUCTS = 1
  556. """Collect data on FROMs and cartesian products and gather into
  557. 'self.from_linter'"""
  558. WARN_LINTING = 2
  559. "Emit warnings for linters that find problems"
  560. FROM_LINTING = COLLECT_CARTESIAN_PRODUCTS | WARN_LINTING
  561. """Warn for cartesian products; combines COLLECT_CARTESIAN_PRODUCTS
  562. and WARN_LINTING"""
  563. NO_LINTING, COLLECT_CARTESIAN_PRODUCTS, WARN_LINTING, FROM_LINTING = tuple(
  564. Linting
  565. )
  566. class FromLinter(collections.namedtuple("FromLinter", ["froms", "edges"])):
  567. """represents current state for the "cartesian product" detection
  568. feature."""
  569. def lint(self, start=None):
  570. froms = self.froms
  571. if not froms:
  572. return None, None
  573. edges = set(self.edges)
  574. the_rest = set(froms)
  575. if start is not None:
  576. start_with = start
  577. the_rest.remove(start_with)
  578. else:
  579. start_with = the_rest.pop()
  580. stack = collections.deque([start_with])
  581. while stack and the_rest:
  582. node = stack.popleft()
  583. the_rest.discard(node)
  584. # comparison of nodes in edges here is based on hash equality, as
  585. # there are "annotated" elements that match the non-annotated ones.
  586. # to remove the need for in-python hash() calls, use native
  587. # containment routines (e.g. "node in edge", "edge.index(node)")
  588. to_remove = {edge for edge in edges if node in edge}
  589. # appendleft the node in each edge that is not
  590. # the one that matched.
  591. stack.extendleft(edge[not edge.index(node)] for edge in to_remove)
  592. edges.difference_update(to_remove)
  593. # FROMS left over? boom
  594. if the_rest:
  595. return the_rest, start_with
  596. else:
  597. return None, None
  598. def warn(self, stmt_type="SELECT"):
  599. the_rest, start_with = self.lint()
  600. # FROMS left over? boom
  601. if the_rest:
  602. froms = the_rest
  603. if froms:
  604. template = (
  605. "{stmt_type} statement has a cartesian product between "
  606. "FROM element(s) {froms} and "
  607. 'FROM element "{start}". Apply join condition(s) '
  608. "between each element to resolve."
  609. )
  610. froms_str = ", ".join(
  611. f'"{self.froms[from_]}"' for from_ in froms
  612. )
  613. message = template.format(
  614. stmt_type=stmt_type,
  615. froms=froms_str,
  616. start=self.froms[start_with],
  617. )
  618. util.warn(message)
  619. class Compiled:
  620. """Represent a compiled SQL or DDL expression.
  621. The ``__str__`` method of the ``Compiled`` object should produce
  622. the actual text of the statement. ``Compiled`` objects are
  623. specific to their underlying database dialect, and also may
  624. or may not be specific to the columns referenced within a
  625. particular set of bind parameters. In no case should the
  626. ``Compiled`` object be dependent on the actual values of those
  627. bind parameters, even though it may reference those values as
  628. defaults.
  629. """
  630. statement: Optional[ClauseElement] = None
  631. "The statement to compile."
  632. string: str = ""
  633. "The string representation of the ``statement``"
  634. state: CompilerState
  635. """description of the compiler's state"""
  636. is_sql = False
  637. is_ddl = False
  638. _cached_metadata: Optional[CursorResultMetaData] = None
  639. _result_columns: Optional[List[ResultColumnsEntry]] = None
  640. schema_translate_map: Optional[SchemaTranslateMapType] = None
  641. execution_options: _ExecuteOptions = util.EMPTY_DICT
  642. """
  643. Execution options propagated from the statement. In some cases,
  644. sub-elements of the statement can modify these.
  645. """
  646. preparer: IdentifierPreparer
  647. _annotations: _AnnotationDict = util.EMPTY_DICT
  648. compile_state: Optional[CompileState] = None
  649. """Optional :class:`.CompileState` object that maintains additional
  650. state used by the compiler.
  651. Major executable objects such as :class:`_expression.Insert`,
  652. :class:`_expression.Update`, :class:`_expression.Delete`,
  653. :class:`_expression.Select` will generate this
  654. state when compiled in order to calculate additional information about the
  655. object. For the top level object that is to be executed, the state can be
  656. stored here where it can also have applicability towards result set
  657. processing.
  658. .. versionadded:: 1.4
  659. """
  660. dml_compile_state: Optional[CompileState] = None
  661. """Optional :class:`.CompileState` assigned at the same point that
  662. .isinsert, .isupdate, or .isdelete is assigned.
  663. This will normally be the same object as .compile_state, with the
  664. exception of cases like the :class:`.ORMFromStatementCompileState`
  665. object.
  666. .. versionadded:: 1.4.40
  667. """
  668. cache_key: Optional[CacheKey] = None
  669. """The :class:`.CacheKey` that was generated ahead of creating this
  670. :class:`.Compiled` object.
  671. This is used for routines that need access to the original
  672. :class:`.CacheKey` instance generated when the :class:`.Compiled`
  673. instance was first cached, typically in order to reconcile
  674. the original list of :class:`.BindParameter` objects with a
  675. per-statement list that's generated on each call.
  676. """
  677. _gen_time: float
  678. """Generation time of this :class:`.Compiled`, used for reporting
  679. cache stats."""
  680. def __init__(
  681. self,
  682. dialect: Dialect,
  683. statement: Optional[ClauseElement],
  684. schema_translate_map: Optional[SchemaTranslateMapType] = None,
  685. render_schema_translate: bool = False,
  686. compile_kwargs: Mapping[str, Any] = util.immutabledict(),
  687. ):
  688. """Construct a new :class:`.Compiled` object.
  689. :param dialect: :class:`.Dialect` to compile against.
  690. :param statement: :class:`_expression.ClauseElement` to be compiled.
  691. :param schema_translate_map: dictionary of schema names to be
  692. translated when forming the resultant SQL
  693. .. seealso::
  694. :ref:`schema_translating`
  695. :param compile_kwargs: additional kwargs that will be
  696. passed to the initial call to :meth:`.Compiled.process`.
  697. """
  698. self.dialect = dialect
  699. self.preparer = self.dialect.identifier_preparer
  700. if schema_translate_map:
  701. self.schema_translate_map = schema_translate_map
  702. self.preparer = self.preparer._with_schema_translate(
  703. schema_translate_map
  704. )
  705. if statement is not None:
  706. self.state = CompilerState.COMPILING
  707. self.statement = statement
  708. self.can_execute = statement.supports_execution
  709. self._annotations = statement._annotations
  710. if self.can_execute:
  711. if TYPE_CHECKING:
  712. assert isinstance(statement, Executable)
  713. self.execution_options = statement._execution_options
  714. self.string = self.process(self.statement, **compile_kwargs)
  715. if render_schema_translate:
  716. assert schema_translate_map is not None
  717. self.string = self.preparer._render_schema_translates(
  718. self.string, schema_translate_map
  719. )
  720. self.state = CompilerState.STRING_APPLIED
  721. else:
  722. self.state = CompilerState.NO_STATEMENT
  723. self._gen_time = perf_counter()
  724. def __init_subclass__(cls) -> None:
  725. cls._init_compiler_cls()
  726. return super().__init_subclass__()
  727. @classmethod
  728. def _init_compiler_cls(cls):
  729. pass
  730. def _execute_on_connection(
  731. self, connection, distilled_params, execution_options
  732. ):
  733. if self.can_execute:
  734. return connection._execute_compiled(
  735. self, distilled_params, execution_options
  736. )
  737. else:
  738. raise exc.ObjectNotExecutableError(self.statement)
  739. def visit_unsupported_compilation(self, element, err, **kw):
  740. raise exc.UnsupportedCompilationError(self, type(element)) from err
  741. @property
  742. def sql_compiler(self) -> SQLCompiler:
  743. """Return a Compiled that is capable of processing SQL expressions.
  744. If this compiler is one, it would likely just return 'self'.
  745. """
  746. raise NotImplementedError()
  747. def process(self, obj: Visitable, **kwargs: Any) -> str:
  748. return obj._compiler_dispatch(self, **kwargs)
  749. def __str__(self) -> str:
  750. """Return the string text of the generated SQL or DDL."""
  751. if self.state is CompilerState.STRING_APPLIED:
  752. return self.string
  753. else:
  754. return ""
  755. def construct_params(
  756. self,
  757. params: Optional[_CoreSingleExecuteParams] = None,
  758. extracted_parameters: Optional[Sequence[BindParameter[Any]]] = None,
  759. escape_names: bool = True,
  760. ) -> Optional[_MutableCoreSingleExecuteParams]:
  761. """Return the bind params for this compiled object.
  762. :param params: a dict of string/object pairs whose values will
  763. override bind values compiled in to the
  764. statement.
  765. """
  766. raise NotImplementedError()
  767. @property
  768. def params(self):
  769. """Return the bind params for this compiled object."""
  770. return self.construct_params()
  771. class TypeCompiler(util.EnsureKWArg):
  772. """Produces DDL specification for TypeEngine objects."""
  773. ensure_kwarg = r"visit_\w+"
  774. def __init__(self, dialect: Dialect):
  775. self.dialect = dialect
  776. def process(self, type_: TypeEngine[Any], **kw: Any) -> str:
  777. if (
  778. type_._variant_mapping
  779. and self.dialect.name in type_._variant_mapping
  780. ):
  781. type_ = type_._variant_mapping[self.dialect.name]
  782. return type_._compiler_dispatch(self, **kw)
  783. def visit_unsupported_compilation(
  784. self, element: Any, err: Exception, **kw: Any
  785. ) -> NoReturn:
  786. raise exc.UnsupportedCompilationError(self, element) from err
  787. # this was a Visitable, but to allow accurate detection of
  788. # column elements this is actually a column element
  789. class _CompileLabel(
  790. roles.BinaryElementRole[Any], elements.CompilerColumnElement
  791. ):
  792. """lightweight label object which acts as an expression.Label."""
  793. __visit_name__ = "label"
  794. __slots__ = "element", "name", "_alt_names"
  795. def __init__(self, col, name, alt_names=()):
  796. self.element = col
  797. self.name = name
  798. self._alt_names = (col,) + alt_names
  799. @property
  800. def proxy_set(self):
  801. return self.element.proxy_set
  802. @property
  803. def type(self):
  804. return self.element.type
  805. def self_group(self, **kw):
  806. return self
  807. class ilike_case_insensitive(
  808. roles.BinaryElementRole[Any], elements.CompilerColumnElement
  809. ):
  810. """produce a wrapping element for a case-insensitive portion of
  811. an ILIKE construct.
  812. The construct usually renders the ``lower()`` function, but on
  813. PostgreSQL will pass silently with the assumption that "ILIKE"
  814. is being used.
  815. .. versionadded:: 2.0
  816. """
  817. __visit_name__ = "ilike_case_insensitive_operand"
  818. __slots__ = "element", "comparator"
  819. def __init__(self, element):
  820. self.element = element
  821. self.comparator = element.comparator
  822. @property
  823. def proxy_set(self):
  824. return self.element.proxy_set
  825. @property
  826. def type(self):
  827. return self.element.type
  828. def self_group(self, **kw):
  829. return self
  830. def _with_binary_element_type(self, type_):
  831. return ilike_case_insensitive(
  832. self.element._with_binary_element_type(type_)
  833. )
  834. class SQLCompiler(Compiled):
  835. """Default implementation of :class:`.Compiled`.
  836. Compiles :class:`_expression.ClauseElement` objects into SQL strings.
  837. """
  838. extract_map = EXTRACT_MAP
  839. bindname_escape_characters: ClassVar[Mapping[str, str]] = (
  840. util.immutabledict(
  841. {
  842. "%": "P",
  843. "(": "A",
  844. ")": "Z",
  845. ":": "C",
  846. ".": "_",
  847. "[": "_",
  848. "]": "_",
  849. " ": "_",
  850. }
  851. )
  852. )
  853. """A mapping (e.g. dict or similar) containing a lookup of
  854. characters keyed to replacement characters which will be applied to all
  855. 'bind names' used in SQL statements as a form of 'escaping'; the given
  856. characters are replaced entirely with the 'replacement' character when
  857. rendered in the SQL statement, and a similar translation is performed
  858. on the incoming names used in parameter dictionaries passed to methods
  859. like :meth:`_engine.Connection.execute`.
  860. This allows bound parameter names used in :func:`_sql.bindparam` and
  861. other constructs to have any arbitrary characters present without any
  862. concern for characters that aren't allowed at all on the target database.
  863. Third party dialects can establish their own dictionary here to replace the
  864. default mapping, which will ensure that the particular characters in the
  865. mapping will never appear in a bound parameter name.
  866. The dictionary is evaluated at **class creation time**, so cannot be
  867. modified at runtime; it must be present on the class when the class
  868. is first declared.
  869. Note that for dialects that have additional bound parameter rules such
  870. as additional restrictions on leading characters, the
  871. :meth:`_sql.SQLCompiler.bindparam_string` method may need to be augmented.
  872. See the cx_Oracle compiler for an example of this.
  873. .. versionadded:: 2.0.0rc1
  874. """
  875. _bind_translate_re: ClassVar[Pattern[str]]
  876. _bind_translate_chars: ClassVar[Mapping[str, str]]
  877. is_sql = True
  878. compound_keywords = COMPOUND_KEYWORDS
  879. isdelete: bool = False
  880. isinsert: bool = False
  881. isupdate: bool = False
  882. """class-level defaults which can be set at the instance
  883. level to define if this Compiled instance represents
  884. INSERT/UPDATE/DELETE
  885. """
  886. postfetch: Optional[List[Column[Any]]]
  887. """list of columns that can be post-fetched after INSERT or UPDATE to
  888. receive server-updated values"""
  889. insert_prefetch: Sequence[Column[Any]] = ()
  890. """list of columns for which default values should be evaluated before
  891. an INSERT takes place"""
  892. update_prefetch: Sequence[Column[Any]] = ()
  893. """list of columns for which onupdate default values should be evaluated
  894. before an UPDATE takes place"""
  895. implicit_returning: Optional[Sequence[ColumnElement[Any]]] = None
  896. """list of "implicit" returning columns for a toplevel INSERT or UPDATE
  897. statement, used to receive newly generated values of columns.
  898. .. versionadded:: 2.0 ``implicit_returning`` replaces the previous
  899. ``returning`` collection, which was not a generalized RETURNING
  900. collection and instead was in fact specific to the "implicit returning"
  901. feature.
  902. """
  903. isplaintext: bool = False
  904. binds: Dict[str, BindParameter[Any]]
  905. """a dictionary of bind parameter keys to BindParameter instances."""
  906. bind_names: Dict[BindParameter[Any], str]
  907. """a dictionary of BindParameter instances to "compiled" names
  908. that are actually present in the generated SQL"""
  909. stack: List[_CompilerStackEntry]
  910. """major statements such as SELECT, INSERT, UPDATE, DELETE are
  911. tracked in this stack using an entry format."""
  912. returning_precedes_values: bool = False
  913. """set to True classwide to generate RETURNING
  914. clauses before the VALUES or WHERE clause (i.e. MSSQL)
  915. """
  916. render_table_with_column_in_update_from: bool = False
  917. """set to True classwide to indicate the SET clause
  918. in a multi-table UPDATE statement should qualify
  919. columns with the table name (i.e. MySQL only)
  920. """
  921. ansi_bind_rules: bool = False
  922. """SQL 92 doesn't allow bind parameters to be used
  923. in the columns clause of a SELECT, nor does it allow
  924. ambiguous expressions like "? = ?". A compiler
  925. subclass can set this flag to False if the target
  926. driver/DB enforces this
  927. """
  928. bindtemplate: str
  929. """template to render bound parameters based on paramstyle."""
  930. compilation_bindtemplate: str
  931. """template used by compiler to render parameters before positional
  932. paramstyle application"""
  933. _numeric_binds_identifier_char: str
  934. """Character that's used to as the identifier of a numerical bind param.
  935. For example if this char is set to ``$``, numerical binds will be rendered
  936. in the form ``$1, $2, $3``.
  937. """
  938. _result_columns: List[ResultColumnsEntry]
  939. """relates label names in the final SQL to a tuple of local
  940. column/label name, ColumnElement object (if any) and
  941. TypeEngine. CursorResult uses this for type processing and
  942. column targeting"""
  943. _textual_ordered_columns: bool = False
  944. """tell the result object that the column names as rendered are important,
  945. but they are also "ordered" vs. what is in the compiled object here.
  946. As of 1.4.42 this condition is only present when the statement is a
  947. TextualSelect, e.g. text("....").columns(...), where it is required
  948. that the columns are considered positionally and not by name.
  949. """
  950. _ad_hoc_textual: bool = False
  951. """tell the result that we encountered text() or '*' constructs in the
  952. middle of the result columns, but we also have compiled columns, so
  953. if the number of columns in cursor.description does not match how many
  954. expressions we have, that means we can't rely on positional at all and
  955. should match on name.
  956. """
  957. _ordered_columns: bool = True
  958. """
  959. if False, means we can't be sure the list of entries
  960. in _result_columns is actually the rendered order. Usually
  961. True unless using an unordered TextualSelect.
  962. """
  963. _loose_column_name_matching: bool = False
  964. """tell the result object that the SQL statement is textual, wants to match
  965. up to Column objects, and may be using the ._tq_label in the SELECT rather
  966. than the base name.
  967. """
  968. _numeric_binds: bool = False
  969. """
  970. True if paramstyle is "numeric". This paramstyle is trickier than
  971. all the others.
  972. """
  973. _render_postcompile: bool = False
  974. """
  975. whether to render out POSTCOMPILE params during the compile phase.
  976. This attribute is used only for end-user invocation of stmt.compile();
  977. it's never used for actual statement execution, where instead the
  978. dialect internals access and render the internal postcompile structure
  979. directly.
  980. """
  981. _post_compile_expanded_state: Optional[ExpandedState] = None
  982. """When render_postcompile is used, the ``ExpandedState`` used to create
  983. the "expanded" SQL is assigned here, and then used by the ``.params``
  984. accessor and ``.construct_params()`` methods for their return values.
  985. .. versionadded:: 2.0.0rc1
  986. """
  987. _pre_expanded_string: Optional[str] = None
  988. """Stores the original string SQL before 'post_compile' is applied,
  989. for cases where 'post_compile' were used.
  990. """
  991. _pre_expanded_positiontup: Optional[List[str]] = None
  992. _insertmanyvalues: Optional[_InsertManyValues] = None
  993. _insert_crud_params: Optional[crud._CrudParamSequence] = None
  994. literal_execute_params: FrozenSet[BindParameter[Any]] = frozenset()
  995. """bindparameter objects that are rendered as literal values at statement
  996. execution time.
  997. """
  998. post_compile_params: FrozenSet[BindParameter[Any]] = frozenset()
  999. """bindparameter objects that are rendered as bound parameter placeholders
  1000. at statement execution time.
  1001. """
  1002. escaped_bind_names: util.immutabledict[str, str] = util.EMPTY_DICT
  1003. """Late escaping of bound parameter names that has to be converted
  1004. to the original name when looking in the parameter dictionary.
  1005. """
  1006. has_out_parameters = False
  1007. """if True, there are bindparam() objects that have the isoutparam
  1008. flag set."""
  1009. postfetch_lastrowid = False
  1010. """if True, and this in insert, use cursor.lastrowid to populate
  1011. result.inserted_primary_key. """
  1012. _cache_key_bind_match: Optional[
  1013. Tuple[
  1014. Dict[
  1015. BindParameter[Any],
  1016. List[BindParameter[Any]],
  1017. ],
  1018. Dict[
  1019. str,
  1020. BindParameter[Any],
  1021. ],
  1022. ]
  1023. ] = None
  1024. """a mapping that will relate the BindParameter object we compile
  1025. to those that are part of the extracted collection of parameters
  1026. in the cache key, if we were given a cache key.
  1027. """
  1028. positiontup: Optional[List[str]] = None
  1029. """for a compiled construct that uses a positional paramstyle, will be
  1030. a sequence of strings, indicating the names of bound parameters in order.
  1031. This is used in order to render bound parameters in their correct order,
  1032. and is combined with the :attr:`_sql.Compiled.params` dictionary to
  1033. render parameters.
  1034. This sequence always contains the unescaped name of the parameters.
  1035. .. seealso::
  1036. :ref:`faq_sql_expression_string` - includes a usage example for
  1037. debugging use cases.
  1038. """
  1039. _values_bindparam: Optional[List[str]] = None
  1040. _visited_bindparam: Optional[List[str]] = None
  1041. inline: bool = False
  1042. ctes: Optional[MutableMapping[CTE, str]]
  1043. # Detect same CTE references - Dict[(level, name), cte]
  1044. # Level is required for supporting nesting
  1045. ctes_by_level_name: Dict[Tuple[int, str], CTE]
  1046. # To retrieve key/level in ctes_by_level_name -
  1047. # Dict[cte_reference, (level, cte_name, cte_opts)]
  1048. level_name_by_cte: Dict[CTE, Tuple[int, str, selectable._CTEOpts]]
  1049. ctes_recursive: bool
  1050. _post_compile_pattern = re.compile(r"__\[POSTCOMPILE_(\S+?)(~~.+?~~)?\]")
  1051. _pyformat_pattern = re.compile(r"%\(([^)]+?)\)s")
  1052. _positional_pattern = re.compile(
  1053. f"{_pyformat_pattern.pattern}|{_post_compile_pattern.pattern}"
  1054. )
  1055. @classmethod
  1056. def _init_compiler_cls(cls):
  1057. cls._init_bind_translate()
  1058. @classmethod
  1059. def _init_bind_translate(cls):
  1060. reg = re.escape("".join(cls.bindname_escape_characters))
  1061. cls._bind_translate_re = re.compile(f"[{reg}]")
  1062. cls._bind_translate_chars = cls.bindname_escape_characters
  1063. def __init__(
  1064. self,
  1065. dialect: Dialect,
  1066. statement: Optional[ClauseElement],
  1067. cache_key: Optional[CacheKey] = None,
  1068. column_keys: Optional[Sequence[str]] = None,
  1069. for_executemany: bool = False,
  1070. linting: Linting = NO_LINTING,
  1071. _supporting_against: Optional[SQLCompiler] = None,
  1072. **kwargs: Any,
  1073. ):
  1074. """Construct a new :class:`.SQLCompiler` object.
  1075. :param dialect: :class:`.Dialect` to be used
  1076. :param statement: :class:`_expression.ClauseElement` to be compiled
  1077. :param column_keys: a list of column names to be compiled into an
  1078. INSERT or UPDATE statement.
  1079. :param for_executemany: whether INSERT / UPDATE statements should
  1080. expect that they are to be invoked in an "executemany" style,
  1081. which may impact how the statement will be expected to return the
  1082. values of defaults and autoincrement / sequences and similar.
  1083. Depending on the backend and driver in use, support for retrieving
  1084. these values may be disabled which means SQL expressions may
  1085. be rendered inline, RETURNING may not be rendered, etc.
  1086. :param kwargs: additional keyword arguments to be consumed by the
  1087. superclass.
  1088. """
  1089. self.column_keys = column_keys
  1090. self.cache_key = cache_key
  1091. if cache_key:
  1092. cksm = {b.key: b for b in cache_key[1]}
  1093. ckbm = {b: [b] for b in cache_key[1]}
  1094. self._cache_key_bind_match = (ckbm, cksm)
  1095. # compile INSERT/UPDATE defaults/sequences to expect executemany
  1096. # style execution, which may mean no pre-execute of defaults,
  1097. # or no RETURNING
  1098. self.for_executemany = for_executemany
  1099. self.linting = linting
  1100. # a dictionary of bind parameter keys to BindParameter
  1101. # instances.
  1102. self.binds = {}
  1103. # a dictionary of BindParameter instances to "compiled" names
  1104. # that are actually present in the generated SQL
  1105. self.bind_names = util.column_dict()
  1106. # stack which keeps track of nested SELECT statements
  1107. self.stack = []
  1108. self._result_columns = []
  1109. # true if the paramstyle is positional
  1110. self.positional = dialect.positional
  1111. if self.positional:
  1112. self._numeric_binds = nb = dialect.paramstyle.startswith("numeric")
  1113. if nb:
  1114. self._numeric_binds_identifier_char = (
  1115. "$" if dialect.paramstyle == "numeric_dollar" else ":"
  1116. )
  1117. self.compilation_bindtemplate = _pyformat_template
  1118. else:
  1119. self.compilation_bindtemplate = BIND_TEMPLATES[dialect.paramstyle]
  1120. self.ctes = None
  1121. self.label_length = (
  1122. dialect.label_length or dialect.max_identifier_length
  1123. )
  1124. # a map which tracks "anonymous" identifiers that are created on
  1125. # the fly here
  1126. self.anon_map = prefix_anon_map()
  1127. # a map which tracks "truncated" names based on
  1128. # dialect.label_length or dialect.max_identifier_length
  1129. self.truncated_names: Dict[Tuple[str, str], str] = {}
  1130. self._truncated_counters: Dict[str, int] = {}
  1131. Compiled.__init__(self, dialect, statement, **kwargs)
  1132. if self.isinsert or self.isupdate or self.isdelete:
  1133. if TYPE_CHECKING:
  1134. assert isinstance(statement, UpdateBase)
  1135. if self.isinsert or self.isupdate:
  1136. if TYPE_CHECKING:
  1137. assert isinstance(statement, ValuesBase)
  1138. if statement._inline:
  1139. self.inline = True
  1140. elif self.for_executemany and (
  1141. not self.isinsert
  1142. or (
  1143. self.dialect.insert_executemany_returning
  1144. and statement._return_defaults
  1145. )
  1146. ):
  1147. self.inline = True
  1148. self.bindtemplate = BIND_TEMPLATES[dialect.paramstyle]
  1149. if _supporting_against:
  1150. self.__dict__.update(
  1151. {
  1152. k: v
  1153. for k, v in _supporting_against.__dict__.items()
  1154. if k
  1155. not in {
  1156. "state",
  1157. "dialect",
  1158. "preparer",
  1159. "positional",
  1160. "_numeric_binds",
  1161. "compilation_bindtemplate",
  1162. "bindtemplate",
  1163. }
  1164. }
  1165. )
  1166. if self.state is CompilerState.STRING_APPLIED:
  1167. if self.positional:
  1168. if self._numeric_binds:
  1169. self._process_numeric()
  1170. else:
  1171. self._process_positional()
  1172. if self._render_postcompile:
  1173. parameters = self.construct_params(
  1174. escape_names=False,
  1175. _no_postcompile=True,
  1176. )
  1177. self._process_parameters_for_postcompile(
  1178. parameters, _populate_self=True
  1179. )
  1180. @property
  1181. def insert_single_values_expr(self) -> Optional[str]:
  1182. """When an INSERT is compiled with a single set of parameters inside
  1183. a VALUES expression, the string is assigned here, where it can be
  1184. used for insert batching schemes to rewrite the VALUES expression.
  1185. .. versionadded:: 1.3.8
  1186. .. versionchanged:: 2.0 This collection is no longer used by
  1187. SQLAlchemy's built-in dialects, in favor of the currently
  1188. internal ``_insertmanyvalues`` collection that is used only by
  1189. :class:`.SQLCompiler`.
  1190. """
  1191. if self._insertmanyvalues is None:
  1192. return None
  1193. else:
  1194. return self._insertmanyvalues.single_values_expr
  1195. @util.ro_memoized_property
  1196. def effective_returning(self) -> Optional[Sequence[ColumnElement[Any]]]:
  1197. """The effective "returning" columns for INSERT, UPDATE or DELETE.
  1198. This is either the so-called "implicit returning" columns which are
  1199. calculated by the compiler on the fly, or those present based on what's
  1200. present in ``self.statement._returning`` (expanded into individual
  1201. columns using the ``._all_selected_columns`` attribute) i.e. those set
  1202. explicitly using the :meth:`.UpdateBase.returning` method.
  1203. .. versionadded:: 2.0
  1204. """
  1205. if self.implicit_returning:
  1206. return self.implicit_returning
  1207. elif self.statement is not None and is_dml(self.statement):
  1208. return [
  1209. c
  1210. for c in self.statement._all_selected_columns
  1211. if is_column_element(c)
  1212. ]
  1213. else:
  1214. return None
  1215. @property
  1216. def returning(self):
  1217. """backwards compatibility; returns the
  1218. effective_returning collection.
  1219. """
  1220. return self.effective_returning
  1221. @property
  1222. def current_executable(self):
  1223. """Return the current 'executable' that is being compiled.
  1224. This is currently the :class:`_sql.Select`, :class:`_sql.Insert`,
  1225. :class:`_sql.Update`, :class:`_sql.Delete`,
  1226. :class:`_sql.CompoundSelect` object that is being compiled.
  1227. Specifically it's assigned to the ``self.stack`` list of elements.
  1228. When a statement like the above is being compiled, it normally
  1229. is also assigned to the ``.statement`` attribute of the
  1230. :class:`_sql.Compiler` object. However, all SQL constructs are
  1231. ultimately nestable, and this attribute should never be consulted
  1232. by a ``visit_`` method, as it is not guaranteed to be assigned
  1233. nor guaranteed to correspond to the current statement being compiled.
  1234. .. versionadded:: 1.3.21
  1235. For compatibility with previous versions, use the following
  1236. recipe::
  1237. statement = getattr(self, "current_executable", False)
  1238. if statement is False:
  1239. statement = self.stack[-1]["selectable"]
  1240. For versions 1.4 and above, ensure only .current_executable
  1241. is used; the format of "self.stack" may change.
  1242. """
  1243. try:
  1244. return self.stack[-1]["selectable"]
  1245. except IndexError as ie:
  1246. raise IndexError("Compiler does not have a stack entry") from ie
  1247. @property
  1248. def prefetch(self):
  1249. return list(self.insert_prefetch) + list(self.update_prefetch)
  1250. @util.memoized_property
  1251. def _global_attributes(self) -> Dict[Any, Any]:
  1252. return {}
  1253. @util.memoized_instancemethod
  1254. def _init_cte_state(self) -> MutableMapping[CTE, str]:
  1255. """Initialize collections related to CTEs only if
  1256. a CTE is located, to save on the overhead of
  1257. these collections otherwise.
  1258. """
  1259. # collect CTEs to tack on top of a SELECT
  1260. # To store the query to print - Dict[cte, text_query]
  1261. ctes: MutableMapping[CTE, str] = util.OrderedDict()
  1262. self.ctes = ctes
  1263. # Detect same CTE references - Dict[(level, name), cte]
  1264. # Level is required for supporting nesting
  1265. self.ctes_by_level_name = {}
  1266. # To retrieve key/level in ctes_by_level_name -
  1267. # Dict[cte_reference, (level, cte_name, cte_opts)]
  1268. self.level_name_by_cte = {}
  1269. self.ctes_recursive = False
  1270. return ctes
  1271. @contextlib.contextmanager
  1272. def _nested_result(self):
  1273. """special API to support the use case of 'nested result sets'"""
  1274. result_columns, ordered_columns = (
  1275. self._result_columns,
  1276. self._ordered_columns,
  1277. )
  1278. self._result_columns, self._ordered_columns = [], False
  1279. try:
  1280. if self.stack:
  1281. entry = self.stack[-1]
  1282. entry["need_result_map_for_nested"] = True
  1283. else:
  1284. entry = None
  1285. yield self._result_columns, self._ordered_columns
  1286. finally:
  1287. if entry:
  1288. entry.pop("need_result_map_for_nested")
  1289. self._result_columns, self._ordered_columns = (
  1290. result_columns,
  1291. ordered_columns,
  1292. )
  1293. def _process_positional(self):
  1294. assert not self.positiontup
  1295. assert self.state is CompilerState.STRING_APPLIED
  1296. assert not self._numeric_binds
  1297. if self.dialect.paramstyle == "format":
  1298. placeholder = "%s"
  1299. else:
  1300. assert self.dialect.paramstyle == "qmark"
  1301. placeholder = "?"
  1302. positions = []
  1303. def find_position(m: re.Match[str]) -> str:
  1304. normal_bind = m.group(1)
  1305. if normal_bind:
  1306. positions.append(normal_bind)
  1307. return placeholder
  1308. else:
  1309. # this a post-compile bind
  1310. positions.append(m.group(2))
  1311. return m.group(0)
  1312. self.string = re.sub(
  1313. self._positional_pattern, find_position, self.string
  1314. )
  1315. if self.escaped_bind_names:
  1316. reverse_escape = {v: k for k, v in self.escaped_bind_names.items()}
  1317. assert len(self.escaped_bind_names) == len(reverse_escape)
  1318. self.positiontup = [
  1319. reverse_escape.get(name, name) for name in positions
  1320. ]
  1321. else:
  1322. self.positiontup = positions
  1323. if self._insertmanyvalues:
  1324. positions = []
  1325. single_values_expr = re.sub(
  1326. self._positional_pattern,
  1327. find_position,
  1328. self._insertmanyvalues.single_values_expr,
  1329. )
  1330. insert_crud_params = [
  1331. (
  1332. v[0],
  1333. v[1],
  1334. re.sub(self._positional_pattern, find_position, v[2]),
  1335. v[3],
  1336. )
  1337. for v in self._insertmanyvalues.insert_crud_params
  1338. ]
  1339. self._insertmanyvalues = self._insertmanyvalues._replace(
  1340. single_values_expr=single_values_expr,
  1341. insert_crud_params=insert_crud_params,
  1342. )
  1343. def _process_numeric(self):
  1344. assert self._numeric_binds
  1345. assert self.state is CompilerState.STRING_APPLIED
  1346. num = 1
  1347. param_pos: Dict[str, str] = {}
  1348. order: Iterable[str]
  1349. if self._insertmanyvalues and self._values_bindparam is not None:
  1350. # bindparams that are not in values are always placed first.
  1351. # this avoids the need of changing them when using executemany
  1352. # values () ()
  1353. order = itertools.chain(
  1354. (
  1355. name
  1356. for name in self.bind_names.values()
  1357. if name not in self._values_bindparam
  1358. ),
  1359. self.bind_names.values(),
  1360. )
  1361. else:
  1362. order = self.bind_names.values()
  1363. for bind_name in order:
  1364. if bind_name in param_pos:
  1365. continue
  1366. bind = self.binds[bind_name]
  1367. if (
  1368. bind in self.post_compile_params
  1369. or bind in self.literal_execute_params
  1370. ):
  1371. # set to None to just mark the in positiontup, it will not
  1372. # be replaced below.
  1373. param_pos[bind_name] = None # type: ignore
  1374. else:
  1375. ph = f"{self._numeric_binds_identifier_char}{num}"
  1376. num += 1
  1377. param_pos[bind_name] = ph
  1378. self.next_numeric_pos = num
  1379. self.positiontup = list(param_pos)
  1380. if self.escaped_bind_names:
  1381. len_before = len(param_pos)
  1382. param_pos = {
  1383. self.escaped_bind_names.get(name, name): pos
  1384. for name, pos in param_pos.items()
  1385. }
  1386. assert len(param_pos) == len_before
  1387. # Can't use format here since % chars are not escaped.
  1388. self.string = self._pyformat_pattern.sub(
  1389. lambda m: param_pos[m.group(1)], self.string
  1390. )
  1391. if self._insertmanyvalues:
  1392. single_values_expr = (
  1393. # format is ok here since single_values_expr includes only
  1394. # place-holders
  1395. self._insertmanyvalues.single_values_expr
  1396. % param_pos
  1397. )
  1398. insert_crud_params = [
  1399. (v[0], v[1], "%s", v[3])
  1400. for v in self._insertmanyvalues.insert_crud_params
  1401. ]
  1402. self._insertmanyvalues = self._insertmanyvalues._replace(
  1403. # This has the numbers (:1, :2)
  1404. single_values_expr=single_values_expr,
  1405. # The single binds are instead %s so they can be formatted
  1406. insert_crud_params=insert_crud_params,
  1407. )
  1408. @util.memoized_property
  1409. def _bind_processors(
  1410. self,
  1411. ) -> MutableMapping[
  1412. str, Union[_BindProcessorType[Any], Sequence[_BindProcessorType[Any]]]
  1413. ]:
  1414. # mypy is not able to see the two value types as the above Union,
  1415. # it just sees "object". don't know how to resolve
  1416. return {
  1417. key: value # type: ignore
  1418. for key, value in (
  1419. (
  1420. self.bind_names[bindparam],
  1421. (
  1422. bindparam.type._cached_bind_processor(self.dialect)
  1423. if not bindparam.type._is_tuple_type
  1424. else tuple(
  1425. elem_type._cached_bind_processor(self.dialect)
  1426. for elem_type in cast(
  1427. TupleType, bindparam.type
  1428. ).types
  1429. )
  1430. ),
  1431. )
  1432. for bindparam in self.bind_names
  1433. )
  1434. if value is not None
  1435. }
  1436. def is_subquery(self):
  1437. return len(self.stack) > 1
  1438. @property
  1439. def sql_compiler(self) -> Self:
  1440. return self
  1441. def construct_expanded_state(
  1442. self,
  1443. params: Optional[_CoreSingleExecuteParams] = None,
  1444. escape_names: bool = True,
  1445. ) -> ExpandedState:
  1446. """Return a new :class:`.ExpandedState` for a given parameter set.
  1447. For queries that use "expanding" or other late-rendered parameters,
  1448. this method will provide for both the finalized SQL string as well
  1449. as the parameters that would be used for a particular parameter set.
  1450. .. versionadded:: 2.0.0rc1
  1451. """
  1452. parameters = self.construct_params(
  1453. params,
  1454. escape_names=escape_names,
  1455. _no_postcompile=True,
  1456. )
  1457. return self._process_parameters_for_postcompile(
  1458. parameters,
  1459. )
  1460. def construct_params(
  1461. self,
  1462. params: Optional[_CoreSingleExecuteParams] = None,
  1463. extracted_parameters: Optional[Sequence[BindParameter[Any]]] = None,
  1464. escape_names: bool = True,
  1465. _group_number: Optional[int] = None,
  1466. _check: bool = True,
  1467. _no_postcompile: bool = False,
  1468. ) -> _MutableCoreSingleExecuteParams:
  1469. """return a dictionary of bind parameter keys and values"""
  1470. if self._render_postcompile and not _no_postcompile:
  1471. assert self._post_compile_expanded_state is not None
  1472. if not params:
  1473. return dict(self._post_compile_expanded_state.parameters)
  1474. else:
  1475. raise exc.InvalidRequestError(
  1476. "can't construct new parameters when render_postcompile "
  1477. "is used; the statement is hard-linked to the original "
  1478. "parameters. Use construct_expanded_state to generate a "
  1479. "new statement and parameters."
  1480. )
  1481. has_escaped_names = escape_names and bool(self.escaped_bind_names)
  1482. if extracted_parameters:
  1483. # related the bound parameters collected in the original cache key
  1484. # to those collected in the incoming cache key. They will not have
  1485. # matching names but they will line up positionally in the same
  1486. # way. The parameters present in self.bind_names may be clones of
  1487. # these original cache key params in the case of DML but the .key
  1488. # will be guaranteed to match.
  1489. if self.cache_key is None:
  1490. raise exc.CompileError(
  1491. "This compiled object has no original cache key; "
  1492. "can't pass extracted_parameters to construct_params"
  1493. )
  1494. else:
  1495. orig_extracted = self.cache_key[1]
  1496. ckbm_tuple = self._cache_key_bind_match
  1497. assert ckbm_tuple is not None
  1498. ckbm, _ = ckbm_tuple
  1499. resolved_extracted = {
  1500. bind: extracted
  1501. for b, extracted in zip(orig_extracted, extracted_parameters)
  1502. for bind in ckbm[b]
  1503. }
  1504. else:
  1505. resolved_extracted = None
  1506. if params:
  1507. pd = {}
  1508. for bindparam, name in self.bind_names.items():
  1509. escaped_name = (
  1510. self.escaped_bind_names.get(name, name)
  1511. if has_escaped_names
  1512. else name
  1513. )
  1514. if bindparam.key in params:
  1515. pd[escaped_name] = params[bindparam.key]
  1516. elif name in params:
  1517. pd[escaped_name] = params[name]
  1518. elif _check and bindparam.required:
  1519. if _group_number:
  1520. raise exc.InvalidRequestError(
  1521. "A value is required for bind parameter %r, "
  1522. "in parameter group %d"
  1523. % (bindparam.key, _group_number),
  1524. code="cd3x",
  1525. )
  1526. else:
  1527. raise exc.InvalidRequestError(
  1528. "A value is required for bind parameter %r"
  1529. % bindparam.key,
  1530. code="cd3x",
  1531. )
  1532. else:
  1533. if resolved_extracted:
  1534. value_param = resolved_extracted.get(
  1535. bindparam, bindparam
  1536. )
  1537. else:
  1538. value_param = bindparam
  1539. if bindparam.callable:
  1540. pd[escaped_name] = value_param.effective_value
  1541. else:
  1542. pd[escaped_name] = value_param.value
  1543. return pd
  1544. else:
  1545. pd = {}
  1546. for bindparam, name in self.bind_names.items():
  1547. escaped_name = (
  1548. self.escaped_bind_names.get(name, name)
  1549. if has_escaped_names
  1550. else name
  1551. )
  1552. if _check and bindparam.required:
  1553. if _group_number:
  1554. raise exc.InvalidRequestError(
  1555. "A value is required for bind parameter %r, "
  1556. "in parameter group %d"
  1557. % (bindparam.key, _group_number),
  1558. code="cd3x",
  1559. )
  1560. else:
  1561. raise exc.InvalidRequestError(
  1562. "A value is required for bind parameter %r"
  1563. % bindparam.key,
  1564. code="cd3x",
  1565. )
  1566. if resolved_extracted:
  1567. value_param = resolved_extracted.get(bindparam, bindparam)
  1568. else:
  1569. value_param = bindparam
  1570. if bindparam.callable:
  1571. pd[escaped_name] = value_param.effective_value
  1572. else:
  1573. pd[escaped_name] = value_param.value
  1574. return pd
  1575. @util.memoized_instancemethod
  1576. def _get_set_input_sizes_lookup(self):
  1577. dialect = self.dialect
  1578. include_types = dialect.include_set_input_sizes
  1579. exclude_types = dialect.exclude_set_input_sizes
  1580. dbapi = dialect.dbapi
  1581. def lookup_type(typ):
  1582. dbtype = typ._unwrapped_dialect_impl(dialect).get_dbapi_type(dbapi)
  1583. if (
  1584. dbtype is not None
  1585. and (exclude_types is None or dbtype not in exclude_types)
  1586. and (include_types is None or dbtype in include_types)
  1587. ):
  1588. return dbtype
  1589. else:
  1590. return None
  1591. inputsizes = {}
  1592. literal_execute_params = self.literal_execute_params
  1593. for bindparam in self.bind_names:
  1594. if bindparam in literal_execute_params:
  1595. continue
  1596. if bindparam.type._is_tuple_type:
  1597. inputsizes[bindparam] = [
  1598. lookup_type(typ)
  1599. for typ in cast(TupleType, bindparam.type).types
  1600. ]
  1601. else:
  1602. inputsizes[bindparam] = lookup_type(bindparam.type)
  1603. return inputsizes
  1604. @property
  1605. def params(self):
  1606. """Return the bind param dictionary embedded into this
  1607. compiled object, for those values that are present.
  1608. .. seealso::
  1609. :ref:`faq_sql_expression_string` - includes a usage example for
  1610. debugging use cases.
  1611. """
  1612. return self.construct_params(_check=False)
  1613. def _process_parameters_for_postcompile(
  1614. self,
  1615. parameters: _MutableCoreSingleExecuteParams,
  1616. _populate_self: bool = False,
  1617. ) -> ExpandedState:
  1618. """handle special post compile parameters.
  1619. These include:
  1620. * "expanding" parameters -typically IN tuples that are rendered
  1621. on a per-parameter basis for an otherwise fixed SQL statement string.
  1622. * literal_binds compiled with the literal_execute flag. Used for
  1623. things like SQL Server "TOP N" where the driver does not accommodate
  1624. N as a bound parameter.
  1625. """
  1626. expanded_parameters = {}
  1627. new_positiontup: Optional[List[str]]
  1628. pre_expanded_string = self._pre_expanded_string
  1629. if pre_expanded_string is None:
  1630. pre_expanded_string = self.string
  1631. if self.positional:
  1632. new_positiontup = []
  1633. pre_expanded_positiontup = self._pre_expanded_positiontup
  1634. if pre_expanded_positiontup is None:
  1635. pre_expanded_positiontup = self.positiontup
  1636. else:
  1637. new_positiontup = pre_expanded_positiontup = None
  1638. processors = self._bind_processors
  1639. single_processors = cast(
  1640. "Mapping[str, _BindProcessorType[Any]]", processors
  1641. )
  1642. tuple_processors = cast(
  1643. "Mapping[str, Sequence[_BindProcessorType[Any]]]", processors
  1644. )
  1645. new_processors: Dict[str, _BindProcessorType[Any]] = {}
  1646. replacement_expressions: Dict[str, Any] = {}
  1647. to_update_sets: Dict[str, Any] = {}
  1648. # notes:
  1649. # *unescaped* parameter names in:
  1650. # self.bind_names, self.binds, self._bind_processors, self.positiontup
  1651. #
  1652. # *escaped* parameter names in:
  1653. # construct_params(), replacement_expressions
  1654. numeric_positiontup: Optional[List[str]] = None
  1655. if self.positional and pre_expanded_positiontup is not None:
  1656. names: Iterable[str] = pre_expanded_positiontup
  1657. if self._numeric_binds:
  1658. numeric_positiontup = []
  1659. else:
  1660. names = self.bind_names.values()
  1661. ebn = self.escaped_bind_names
  1662. for name in names:
  1663. escaped_name = ebn.get(name, name) if ebn else name
  1664. parameter = self.binds[name]
  1665. if parameter in self.literal_execute_params:
  1666. if escaped_name not in replacement_expressions:
  1667. replacement_expressions[escaped_name] = (
  1668. self.render_literal_bindparam(
  1669. parameter,
  1670. render_literal_value=parameters.pop(escaped_name),
  1671. )
  1672. )
  1673. continue
  1674. if parameter in self.post_compile_params:
  1675. if escaped_name in replacement_expressions:
  1676. to_update = to_update_sets[escaped_name]
  1677. values = None
  1678. else:
  1679. # we are removing the parameter from parameters
  1680. # because it is a list value, which is not expected by
  1681. # TypeEngine objects that would otherwise be asked to
  1682. # process it. the single name is being replaced with
  1683. # individual numbered parameters for each value in the
  1684. # param.
  1685. #
  1686. # note we are also inserting *escaped* parameter names
  1687. # into the given dictionary. default dialect will
  1688. # use these param names directly as they will not be
  1689. # in the escaped_bind_names dictionary.
  1690. values = parameters.pop(name)
  1691. leep_res = self._literal_execute_expanding_parameter(
  1692. escaped_name, parameter, values
  1693. )
  1694. (to_update, replacement_expr) = leep_res
  1695. to_update_sets[escaped_name] = to_update
  1696. replacement_expressions[escaped_name] = replacement_expr
  1697. if not parameter.literal_execute:
  1698. parameters.update(to_update)
  1699. if parameter.type._is_tuple_type:
  1700. assert values is not None
  1701. new_processors.update(
  1702. (
  1703. "%s_%s_%s" % (name, i, j),
  1704. tuple_processors[name][j - 1],
  1705. )
  1706. for i, tuple_element in enumerate(values, 1)
  1707. for j, _ in enumerate(tuple_element, 1)
  1708. if name in tuple_processors
  1709. and tuple_processors[name][j - 1] is not None
  1710. )
  1711. else:
  1712. new_processors.update(
  1713. (key, single_processors[name])
  1714. for key, _ in to_update
  1715. if name in single_processors
  1716. )
  1717. if numeric_positiontup is not None:
  1718. numeric_positiontup.extend(
  1719. name for name, _ in to_update
  1720. )
  1721. elif new_positiontup is not None:
  1722. # to_update has escaped names, but that's ok since
  1723. # these are new names, that aren't in the
  1724. # escaped_bind_names dict.
  1725. new_positiontup.extend(name for name, _ in to_update)
  1726. expanded_parameters[name] = [
  1727. expand_key for expand_key, _ in to_update
  1728. ]
  1729. elif new_positiontup is not None:
  1730. new_positiontup.append(name)
  1731. def process_expanding(m):
  1732. key = m.group(1)
  1733. expr = replacement_expressions[key]
  1734. # if POSTCOMPILE included a bind_expression, render that
  1735. # around each element
  1736. if m.group(2):
  1737. tok = m.group(2).split("~~")
  1738. be_left, be_right = tok[1], tok[3]
  1739. expr = ", ".join(
  1740. "%s%s%s" % (be_left, exp, be_right)
  1741. for exp in expr.split(", ")
  1742. )
  1743. return expr
  1744. statement = re.sub(
  1745. self._post_compile_pattern, process_expanding, pre_expanded_string
  1746. )
  1747. if numeric_positiontup is not None:
  1748. assert new_positiontup is not None
  1749. param_pos = {
  1750. key: f"{self._numeric_binds_identifier_char}{num}"
  1751. for num, key in enumerate(
  1752. numeric_positiontup, self.next_numeric_pos
  1753. )
  1754. }
  1755. # Can't use format here since % chars are not escaped.
  1756. statement = self._pyformat_pattern.sub(
  1757. lambda m: param_pos[m.group(1)], statement
  1758. )
  1759. new_positiontup.extend(numeric_positiontup)
  1760. expanded_state = ExpandedState(
  1761. statement,
  1762. parameters,
  1763. new_processors,
  1764. new_positiontup,
  1765. expanded_parameters,
  1766. )
  1767. if _populate_self:
  1768. # this is for the "render_postcompile" flag, which is not
  1769. # otherwise used internally and is for end-user debugging and
  1770. # special use cases.
  1771. self._pre_expanded_string = pre_expanded_string
  1772. self._pre_expanded_positiontup = pre_expanded_positiontup
  1773. self.string = expanded_state.statement
  1774. self.positiontup = (
  1775. list(expanded_state.positiontup or ())
  1776. if self.positional
  1777. else None
  1778. )
  1779. self._post_compile_expanded_state = expanded_state
  1780. return expanded_state
  1781. @util.preload_module("sqlalchemy.engine.cursor")
  1782. def _create_result_map(self):
  1783. """utility method used for unit tests only."""
  1784. cursor = util.preloaded.engine_cursor
  1785. return cursor.CursorResultMetaData._create_description_match_map(
  1786. self._result_columns
  1787. )
  1788. # assigned by crud.py for insert/update statements
  1789. _get_bind_name_for_col: _BindNameForColProtocol
  1790. @util.memoized_property
  1791. def _within_exec_param_key_getter(self) -> Callable[[Any], str]:
  1792. getter = self._get_bind_name_for_col
  1793. return getter
  1794. @util.memoized_property
  1795. @util.preload_module("sqlalchemy.engine.result")
  1796. def _inserted_primary_key_from_lastrowid_getter(self):
  1797. result = util.preloaded.engine_result
  1798. param_key_getter = self._within_exec_param_key_getter
  1799. assert self.compile_state is not None
  1800. statement = self.compile_state.statement
  1801. if TYPE_CHECKING:
  1802. assert isinstance(statement, Insert)
  1803. table = statement.table
  1804. getters = [
  1805. (operator.methodcaller("get", param_key_getter(col), None), col)
  1806. for col in table.primary_key
  1807. ]
  1808. autoinc_getter = None
  1809. autoinc_col = table._autoincrement_column
  1810. if autoinc_col is not None:
  1811. # apply type post processors to the lastrowid
  1812. lastrowid_processor = autoinc_col.type._cached_result_processor(
  1813. self.dialect, None
  1814. )
  1815. autoinc_key = param_key_getter(autoinc_col)
  1816. # if a bind value is present for the autoincrement column
  1817. # in the parameters, we need to do the logic dictated by
  1818. # #7998; honor a non-None user-passed parameter over lastrowid.
  1819. # previously in the 1.4 series we weren't fetching lastrowid
  1820. # at all if the key were present in the parameters
  1821. if autoinc_key in self.binds:
  1822. def _autoinc_getter(lastrowid, parameters):
  1823. param_value = parameters.get(autoinc_key, lastrowid)
  1824. if param_value is not None:
  1825. # they supplied non-None parameter, use that.
  1826. # SQLite at least is observed to return the wrong
  1827. # cursor.lastrowid for INSERT..ON CONFLICT so it
  1828. # can't be used in all cases
  1829. return param_value
  1830. else:
  1831. # use lastrowid
  1832. return lastrowid
  1833. # work around mypy https://github.com/python/mypy/issues/14027
  1834. autoinc_getter = _autoinc_getter
  1835. else:
  1836. lastrowid_processor = None
  1837. row_fn = result.result_tuple([col.key for col in table.primary_key])
  1838. def get(lastrowid, parameters):
  1839. """given cursor.lastrowid value and the parameters used for INSERT,
  1840. return a "row" that represents the primary key, either by
  1841. using the "lastrowid" or by extracting values from the parameters
  1842. that were sent along with the INSERT.
  1843. """
  1844. if lastrowid_processor is not None:
  1845. lastrowid = lastrowid_processor(lastrowid)
  1846. if lastrowid is None:
  1847. return row_fn(getter(parameters) for getter, col in getters)
  1848. else:
  1849. return row_fn(
  1850. (
  1851. (
  1852. autoinc_getter(lastrowid, parameters)
  1853. if autoinc_getter is not None
  1854. else lastrowid
  1855. )
  1856. if col is autoinc_col
  1857. else getter(parameters)
  1858. )
  1859. for getter, col in getters
  1860. )
  1861. return get
  1862. @util.memoized_property
  1863. @util.preload_module("sqlalchemy.engine.result")
  1864. def _inserted_primary_key_from_returning_getter(self):
  1865. result = util.preloaded.engine_result
  1866. assert self.compile_state is not None
  1867. statement = self.compile_state.statement
  1868. if TYPE_CHECKING:
  1869. assert isinstance(statement, Insert)
  1870. param_key_getter = self._within_exec_param_key_getter
  1871. table = statement.table
  1872. returning = self.implicit_returning
  1873. assert returning is not None
  1874. ret = {col: idx for idx, col in enumerate(returning)}
  1875. getters = cast(
  1876. "List[Tuple[Callable[[Any], Any], bool]]",
  1877. [
  1878. (
  1879. (operator.itemgetter(ret[col]), True)
  1880. if col in ret
  1881. else (
  1882. operator.methodcaller(
  1883. "get", param_key_getter(col), None
  1884. ),
  1885. False,
  1886. )
  1887. )
  1888. for col in table.primary_key
  1889. ],
  1890. )
  1891. row_fn = result.result_tuple([col.key for col in table.primary_key])
  1892. def get(row, parameters):
  1893. return row_fn(
  1894. getter(row) if use_row else getter(parameters)
  1895. for getter, use_row in getters
  1896. )
  1897. return get
  1898. def default_from(self) -> str:
  1899. """Called when a SELECT statement has no froms, and no FROM clause is
  1900. to be appended.
  1901. Gives Oracle Database a chance to tack on a ``FROM DUAL`` to the string
  1902. output.
  1903. """
  1904. return ""
  1905. def visit_override_binds(self, override_binds, **kw):
  1906. """SQL compile the nested element of an _OverrideBinds with
  1907. bindparams swapped out.
  1908. The _OverrideBinds is not normally expected to be compiled; it
  1909. is meant to be used when an already cached statement is to be used,
  1910. the compilation was already performed, and only the bound params should
  1911. be swapped in at execution time.
  1912. However, there are test cases that exericise this object, and
  1913. additionally the ORM subquery loader is known to feed in expressions
  1914. which include this construct into new queries (discovered in #11173),
  1915. so it has to do the right thing at compile time as well.
  1916. """
  1917. # get SQL text first
  1918. sqltext = override_binds.element._compiler_dispatch(self, **kw)
  1919. # for a test compile that is not for caching, change binds after the
  1920. # fact. note that we don't try to
  1921. # swap the bindparam as we compile, because our element may be
  1922. # elsewhere in the statement already (e.g. a subquery or perhaps a
  1923. # CTE) and was already visited / compiled. See
  1924. # test_relationship_criteria.py ->
  1925. # test_selectinload_local_criteria_subquery
  1926. for k in override_binds.translate:
  1927. if k not in self.binds:
  1928. continue
  1929. bp = self.binds[k]
  1930. # so this would work, just change the value of bp in place.
  1931. # but we dont want to mutate things outside.
  1932. # bp.value = override_binds.translate[bp.key]
  1933. # continue
  1934. # instead, need to replace bp with new_bp or otherwise accommodate
  1935. # in all internal collections
  1936. new_bp = bp._with_value(
  1937. override_binds.translate[bp.key],
  1938. maintain_key=True,
  1939. required=False,
  1940. )
  1941. name = self.bind_names[bp]
  1942. self.binds[k] = self.binds[name] = new_bp
  1943. self.bind_names[new_bp] = name
  1944. self.bind_names.pop(bp, None)
  1945. if bp in self.post_compile_params:
  1946. self.post_compile_params |= {new_bp}
  1947. if bp in self.literal_execute_params:
  1948. self.literal_execute_params |= {new_bp}
  1949. ckbm_tuple = self._cache_key_bind_match
  1950. if ckbm_tuple:
  1951. ckbm, cksm = ckbm_tuple
  1952. for bp in bp._cloned_set:
  1953. if bp.key in cksm:
  1954. cb = cksm[bp.key]
  1955. ckbm[cb].append(new_bp)
  1956. return sqltext
  1957. def visit_grouping(self, grouping, asfrom=False, **kwargs):
  1958. return "(" + grouping.element._compiler_dispatch(self, **kwargs) + ")"
  1959. def visit_select_statement_grouping(self, grouping, **kwargs):
  1960. return "(" + grouping.element._compiler_dispatch(self, **kwargs) + ")"
  1961. def visit_label_reference(
  1962. self, element, within_columns_clause=False, **kwargs
  1963. ):
  1964. if self.stack and self.dialect.supports_simple_order_by_label:
  1965. try:
  1966. compile_state = cast(
  1967. "Union[SelectState, CompoundSelectState]",
  1968. self.stack[-1]["compile_state"],
  1969. )
  1970. except KeyError as ke:
  1971. raise exc.CompileError(
  1972. "Can't resolve label reference for ORDER BY / "
  1973. "GROUP BY / DISTINCT etc."
  1974. ) from ke
  1975. (
  1976. with_cols,
  1977. only_froms,
  1978. only_cols,
  1979. ) = compile_state._label_resolve_dict
  1980. if within_columns_clause:
  1981. resolve_dict = only_froms
  1982. else:
  1983. resolve_dict = only_cols
  1984. # this can be None in the case that a _label_reference()
  1985. # were subject to a replacement operation, in which case
  1986. # the replacement of the Label element may have changed
  1987. # to something else like a ColumnClause expression.
  1988. order_by_elem = element.element._order_by_label_element
  1989. if (
  1990. order_by_elem is not None
  1991. and order_by_elem.name in resolve_dict
  1992. and order_by_elem.shares_lineage(
  1993. resolve_dict[order_by_elem.name]
  1994. )
  1995. ):
  1996. kwargs["render_label_as_label"] = (
  1997. element.element._order_by_label_element
  1998. )
  1999. return self.process(
  2000. element.element,
  2001. within_columns_clause=within_columns_clause,
  2002. **kwargs,
  2003. )
  2004. def visit_textual_label_reference(
  2005. self, element, within_columns_clause=False, **kwargs
  2006. ):
  2007. if not self.stack:
  2008. # compiling the element outside of the context of a SELECT
  2009. return self.process(element._text_clause)
  2010. try:
  2011. compile_state = cast(
  2012. "Union[SelectState, CompoundSelectState]",
  2013. self.stack[-1]["compile_state"],
  2014. )
  2015. except KeyError as ke:
  2016. coercions._no_text_coercion(
  2017. element.element,
  2018. extra=(
  2019. "Can't resolve label reference for ORDER BY / "
  2020. "GROUP BY / DISTINCT etc."
  2021. ),
  2022. exc_cls=exc.CompileError,
  2023. err=ke,
  2024. )
  2025. with_cols, only_froms, only_cols = compile_state._label_resolve_dict
  2026. try:
  2027. if within_columns_clause:
  2028. col = only_froms[element.element]
  2029. else:
  2030. col = with_cols[element.element]
  2031. except KeyError as err:
  2032. coercions._no_text_coercion(
  2033. element.element,
  2034. extra=(
  2035. "Can't resolve label reference for ORDER BY / "
  2036. "GROUP BY / DISTINCT etc."
  2037. ),
  2038. exc_cls=exc.CompileError,
  2039. err=err,
  2040. )
  2041. else:
  2042. kwargs["render_label_as_label"] = col
  2043. return self.process(
  2044. col, within_columns_clause=within_columns_clause, **kwargs
  2045. )
  2046. def visit_label(
  2047. self,
  2048. label,
  2049. add_to_result_map=None,
  2050. within_label_clause=False,
  2051. within_columns_clause=False,
  2052. render_label_as_label=None,
  2053. result_map_targets=(),
  2054. **kw,
  2055. ):
  2056. # only render labels within the columns clause
  2057. # or ORDER BY clause of a select. dialect-specific compilers
  2058. # can modify this behavior.
  2059. render_label_with_as = (
  2060. within_columns_clause and not within_label_clause
  2061. )
  2062. render_label_only = render_label_as_label is label
  2063. if render_label_only or render_label_with_as:
  2064. if isinstance(label.name, elements._truncated_label):
  2065. labelname = self._truncated_identifier("colident", label.name)
  2066. else:
  2067. labelname = label.name
  2068. if render_label_with_as:
  2069. if add_to_result_map is not None:
  2070. add_to_result_map(
  2071. labelname,
  2072. label.name,
  2073. (label, labelname) + label._alt_names + result_map_targets,
  2074. label.type,
  2075. )
  2076. return (
  2077. label.element._compiler_dispatch(
  2078. self,
  2079. within_columns_clause=True,
  2080. within_label_clause=True,
  2081. **kw,
  2082. )
  2083. + OPERATORS[operators.as_]
  2084. + self.preparer.format_label(label, labelname)
  2085. )
  2086. elif render_label_only:
  2087. return self.preparer.format_label(label, labelname)
  2088. else:
  2089. return label.element._compiler_dispatch(
  2090. self, within_columns_clause=False, **kw
  2091. )
  2092. def _fallback_column_name(self, column):
  2093. raise exc.CompileError(
  2094. "Cannot compile Column object until its 'name' is assigned."
  2095. )
  2096. def visit_lambda_element(self, element, **kw):
  2097. sql_element = element._resolved
  2098. return self.process(sql_element, **kw)
  2099. def visit_column(
  2100. self,
  2101. column: ColumnClause[Any],
  2102. add_to_result_map: Optional[_ResultMapAppender] = None,
  2103. include_table: bool = True,
  2104. result_map_targets: Tuple[Any, ...] = (),
  2105. ambiguous_table_name_map: Optional[_AmbiguousTableNameMap] = None,
  2106. **kwargs: Any,
  2107. ) -> str:
  2108. name = orig_name = column.name
  2109. if name is None:
  2110. name = self._fallback_column_name(column)
  2111. is_literal = column.is_literal
  2112. if not is_literal and isinstance(name, elements._truncated_label):
  2113. name = self._truncated_identifier("colident", name)
  2114. if add_to_result_map is not None:
  2115. targets = (column, name, column.key) + result_map_targets
  2116. if column._tq_label:
  2117. targets += (column._tq_label,)
  2118. add_to_result_map(name, orig_name, targets, column.type)
  2119. if is_literal:
  2120. # note we are not currently accommodating for
  2121. # literal_column(quoted_name('ident', True)) here
  2122. name = self.escape_literal_column(name)
  2123. else:
  2124. name = self.preparer.quote(name)
  2125. table = column.table
  2126. if table is None or not include_table or not table.named_with_column:
  2127. return name
  2128. else:
  2129. effective_schema = self.preparer.schema_for_object(table)
  2130. if effective_schema:
  2131. schema_prefix = (
  2132. self.preparer.quote_schema(effective_schema) + "."
  2133. )
  2134. else:
  2135. schema_prefix = ""
  2136. if TYPE_CHECKING:
  2137. assert isinstance(table, NamedFromClause)
  2138. tablename = table.name
  2139. if (
  2140. not effective_schema
  2141. and ambiguous_table_name_map
  2142. and tablename in ambiguous_table_name_map
  2143. ):
  2144. tablename = ambiguous_table_name_map[tablename]
  2145. if isinstance(tablename, elements._truncated_label):
  2146. tablename = self._truncated_identifier("alias", tablename)
  2147. return schema_prefix + self.preparer.quote(tablename) + "." + name
  2148. def visit_collation(self, element, **kw):
  2149. return self.preparer.format_collation(element.collation)
  2150. def visit_fromclause(self, fromclause, **kwargs):
  2151. return fromclause.name
  2152. def visit_index(self, index, **kwargs):
  2153. return index.name
  2154. def visit_typeclause(self, typeclause, **kw):
  2155. kw["type_expression"] = typeclause
  2156. kw["identifier_preparer"] = self.preparer
  2157. return self.dialect.type_compiler_instance.process(
  2158. typeclause.type, **kw
  2159. )
  2160. def post_process_text(self, text):
  2161. if self.preparer._double_percents:
  2162. text = text.replace("%", "%%")
  2163. return text
  2164. def escape_literal_column(self, text):
  2165. if self.preparer._double_percents:
  2166. text = text.replace("%", "%%")
  2167. return text
  2168. def visit_textclause(self, textclause, add_to_result_map=None, **kw):
  2169. def do_bindparam(m):
  2170. name = m.group(1)
  2171. if name in textclause._bindparams:
  2172. return self.process(textclause._bindparams[name], **kw)
  2173. else:
  2174. return self.bindparam_string(name, **kw)
  2175. if not self.stack:
  2176. self.isplaintext = True
  2177. if add_to_result_map:
  2178. # text() object is present in the columns clause of a
  2179. # select(). Add a no-name entry to the result map so that
  2180. # row[text()] produces a result
  2181. add_to_result_map(None, None, (textclause,), sqltypes.NULLTYPE)
  2182. # un-escape any \:params
  2183. return BIND_PARAMS_ESC.sub(
  2184. lambda m: m.group(1),
  2185. BIND_PARAMS.sub(
  2186. do_bindparam, self.post_process_text(textclause.text)
  2187. ),
  2188. )
  2189. def visit_textual_select(
  2190. self, taf, compound_index=None, asfrom=False, **kw
  2191. ):
  2192. toplevel = not self.stack
  2193. entry = self._default_stack_entry if toplevel else self.stack[-1]
  2194. new_entry: _CompilerStackEntry = {
  2195. "correlate_froms": set(),
  2196. "asfrom_froms": set(),
  2197. "selectable": taf,
  2198. }
  2199. self.stack.append(new_entry)
  2200. if taf._independent_ctes:
  2201. self._dispatch_independent_ctes(taf, kw)
  2202. populate_result_map = (
  2203. toplevel
  2204. or (
  2205. compound_index == 0
  2206. and entry.get("need_result_map_for_compound", False)
  2207. )
  2208. or entry.get("need_result_map_for_nested", False)
  2209. )
  2210. if populate_result_map:
  2211. self._ordered_columns = self._textual_ordered_columns = (
  2212. taf.positional
  2213. )
  2214. # enable looser result column matching when the SQL text links to
  2215. # Column objects by name only
  2216. self._loose_column_name_matching = not taf.positional and bool(
  2217. taf.column_args
  2218. )
  2219. for c in taf.column_args:
  2220. self.process(
  2221. c,
  2222. within_columns_clause=True,
  2223. add_to_result_map=self._add_to_result_map,
  2224. )
  2225. text = self.process(taf.element, **kw)
  2226. if self.ctes:
  2227. nesting_level = len(self.stack) if not toplevel else None
  2228. text = self._render_cte_clause(nesting_level=nesting_level) + text
  2229. self.stack.pop(-1)
  2230. return text
  2231. def visit_null(self, expr: Null, **kw: Any) -> str:
  2232. return "NULL"
  2233. def visit_true(self, expr: True_, **kw: Any) -> str:
  2234. if self.dialect.supports_native_boolean:
  2235. return "true"
  2236. else:
  2237. return "1"
  2238. def visit_false(self, expr: False_, **kw: Any) -> str:
  2239. if self.dialect.supports_native_boolean:
  2240. return "false"
  2241. else:
  2242. return "0"
  2243. def _generate_delimited_list(self, elements, separator, **kw):
  2244. return separator.join(
  2245. s
  2246. for s in (c._compiler_dispatch(self, **kw) for c in elements)
  2247. if s
  2248. )
  2249. def _generate_delimited_and_list(self, clauses, **kw):
  2250. lcc, clauses = elements.BooleanClauseList._process_clauses_for_boolean(
  2251. operators.and_,
  2252. elements.True_._singleton,
  2253. elements.False_._singleton,
  2254. clauses,
  2255. )
  2256. if lcc == 1:
  2257. return clauses[0]._compiler_dispatch(self, **kw)
  2258. else:
  2259. separator = OPERATORS[operators.and_]
  2260. return separator.join(
  2261. s
  2262. for s in (c._compiler_dispatch(self, **kw) for c in clauses)
  2263. if s
  2264. )
  2265. def visit_tuple(self, clauselist, **kw):
  2266. return "(%s)" % self.visit_clauselist(clauselist, **kw)
  2267. def visit_clauselist(self, clauselist, **kw):
  2268. sep = clauselist.operator
  2269. if sep is None:
  2270. sep = " "
  2271. else:
  2272. sep = OPERATORS[clauselist.operator]
  2273. return self._generate_delimited_list(clauselist.clauses, sep, **kw)
  2274. def visit_expression_clauselist(self, clauselist, **kw):
  2275. operator_ = clauselist.operator
  2276. disp = self._get_operator_dispatch(
  2277. operator_, "expression_clauselist", None
  2278. )
  2279. if disp:
  2280. return disp(clauselist, operator_, **kw)
  2281. try:
  2282. opstring = OPERATORS[operator_]
  2283. except KeyError as err:
  2284. raise exc.UnsupportedCompilationError(self, operator_) from err
  2285. else:
  2286. kw["_in_operator_expression"] = True
  2287. return self._generate_delimited_list(
  2288. clauselist.clauses, opstring, **kw
  2289. )
  2290. def visit_case(self, clause, **kwargs):
  2291. x = "CASE "
  2292. if clause.value is not None:
  2293. x += clause.value._compiler_dispatch(self, **kwargs) + " "
  2294. for cond, result in clause.whens:
  2295. x += (
  2296. "WHEN "
  2297. + cond._compiler_dispatch(self, **kwargs)
  2298. + " THEN "
  2299. + result._compiler_dispatch(self, **kwargs)
  2300. + " "
  2301. )
  2302. if clause.else_ is not None:
  2303. x += (
  2304. "ELSE " + clause.else_._compiler_dispatch(self, **kwargs) + " "
  2305. )
  2306. x += "END"
  2307. return x
  2308. def visit_type_coerce(self, type_coerce, **kw):
  2309. return type_coerce.typed_expression._compiler_dispatch(self, **kw)
  2310. def visit_cast(self, cast, **kwargs):
  2311. type_clause = cast.typeclause._compiler_dispatch(self, **kwargs)
  2312. match = re.match("(.*)( COLLATE .*)", type_clause)
  2313. return "CAST(%s AS %s)%s" % (
  2314. cast.clause._compiler_dispatch(self, **kwargs),
  2315. match.group(1) if match else type_clause,
  2316. match.group(2) if match else "",
  2317. )
  2318. def _format_frame_clause(self, range_, **kw):
  2319. return "%s AND %s" % (
  2320. (
  2321. "UNBOUNDED PRECEDING"
  2322. if range_[0] is elements.RANGE_UNBOUNDED
  2323. else (
  2324. "CURRENT ROW"
  2325. if range_[0] is elements.RANGE_CURRENT
  2326. else (
  2327. "%s PRECEDING"
  2328. % (
  2329. self.process(
  2330. elements.literal(abs(range_[0])), **kw
  2331. ),
  2332. )
  2333. if range_[0] < 0
  2334. else "%s FOLLOWING"
  2335. % (self.process(elements.literal(range_[0]), **kw),)
  2336. )
  2337. )
  2338. ),
  2339. (
  2340. "UNBOUNDED FOLLOWING"
  2341. if range_[1] is elements.RANGE_UNBOUNDED
  2342. else (
  2343. "CURRENT ROW"
  2344. if range_[1] is elements.RANGE_CURRENT
  2345. else (
  2346. "%s PRECEDING"
  2347. % (
  2348. self.process(
  2349. elements.literal(abs(range_[1])), **kw
  2350. ),
  2351. )
  2352. if range_[1] < 0
  2353. else "%s FOLLOWING"
  2354. % (self.process(elements.literal(range_[1]), **kw),)
  2355. )
  2356. )
  2357. ),
  2358. )
  2359. def visit_over(self, over, **kwargs):
  2360. text = over.element._compiler_dispatch(self, **kwargs)
  2361. if over.range_ is not None:
  2362. range_ = "RANGE BETWEEN %s" % self._format_frame_clause(
  2363. over.range_, **kwargs
  2364. )
  2365. elif over.rows is not None:
  2366. range_ = "ROWS BETWEEN %s" % self._format_frame_clause(
  2367. over.rows, **kwargs
  2368. )
  2369. elif over.groups is not None:
  2370. range_ = "GROUPS BETWEEN %s" % self._format_frame_clause(
  2371. over.groups, **kwargs
  2372. )
  2373. else:
  2374. range_ = None
  2375. return "%s OVER (%s)" % (
  2376. text,
  2377. " ".join(
  2378. [
  2379. "%s BY %s"
  2380. % (word, clause._compiler_dispatch(self, **kwargs))
  2381. for word, clause in (
  2382. ("PARTITION", over.partition_by),
  2383. ("ORDER", over.order_by),
  2384. )
  2385. if clause is not None and len(clause)
  2386. ]
  2387. + ([range_] if range_ else [])
  2388. ),
  2389. )
  2390. def visit_withingroup(self, withingroup, **kwargs):
  2391. return "%s WITHIN GROUP (ORDER BY %s)" % (
  2392. withingroup.element._compiler_dispatch(self, **kwargs),
  2393. withingroup.order_by._compiler_dispatch(self, **kwargs),
  2394. )
  2395. def visit_funcfilter(self, funcfilter, **kwargs):
  2396. return "%s FILTER (WHERE %s)" % (
  2397. funcfilter.func._compiler_dispatch(self, **kwargs),
  2398. funcfilter.criterion._compiler_dispatch(self, **kwargs),
  2399. )
  2400. def visit_extract(self, extract, **kwargs):
  2401. field = self.extract_map.get(extract.field, extract.field)
  2402. return "EXTRACT(%s FROM %s)" % (
  2403. field,
  2404. extract.expr._compiler_dispatch(self, **kwargs),
  2405. )
  2406. def visit_scalar_function_column(self, element, **kw):
  2407. compiled_fn = self.visit_function(element.fn, **kw)
  2408. compiled_col = self.visit_column(element, **kw)
  2409. return "(%s).%s" % (compiled_fn, compiled_col)
  2410. def visit_function(
  2411. self,
  2412. func: Function[Any],
  2413. add_to_result_map: Optional[_ResultMapAppender] = None,
  2414. **kwargs: Any,
  2415. ) -> str:
  2416. if add_to_result_map is not None:
  2417. add_to_result_map(func.name, func.name, (func.name,), func.type)
  2418. disp = getattr(self, "visit_%s_func" % func.name.lower(), None)
  2419. text: str
  2420. if disp:
  2421. text = disp(func, **kwargs)
  2422. else:
  2423. name = FUNCTIONS.get(func._deannotate().__class__, None)
  2424. if name:
  2425. if func._has_args:
  2426. name += "%(expr)s"
  2427. else:
  2428. name = func.name
  2429. name = (
  2430. self.preparer.quote(name)
  2431. if self.preparer._requires_quotes_illegal_chars(name)
  2432. or isinstance(name, elements.quoted_name)
  2433. else name
  2434. )
  2435. name = name + "%(expr)s"
  2436. text = ".".join(
  2437. [
  2438. (
  2439. self.preparer.quote(tok)
  2440. if self.preparer._requires_quotes_illegal_chars(tok)
  2441. or isinstance(name, elements.quoted_name)
  2442. else tok
  2443. )
  2444. for tok in func.packagenames
  2445. ]
  2446. + [name]
  2447. ) % {"expr": self.function_argspec(func, **kwargs)}
  2448. if func._with_ordinality:
  2449. text += " WITH ORDINALITY"
  2450. return text
  2451. def visit_next_value_func(self, next_value, **kw):
  2452. return self.visit_sequence(next_value.sequence)
  2453. def visit_sequence(self, sequence, **kw):
  2454. raise NotImplementedError(
  2455. "Dialect '%s' does not support sequence increments."
  2456. % self.dialect.name
  2457. )
  2458. def function_argspec(self, func: Function[Any], **kwargs: Any) -> str:
  2459. return func.clause_expr._compiler_dispatch(self, **kwargs)
  2460. def visit_compound_select(
  2461. self, cs, asfrom=False, compound_index=None, **kwargs
  2462. ):
  2463. toplevel = not self.stack
  2464. compile_state = cs._compile_state_factory(cs, self, **kwargs)
  2465. if toplevel and not self.compile_state:
  2466. self.compile_state = compile_state
  2467. compound_stmt = compile_state.statement
  2468. entry = self._default_stack_entry if toplevel else self.stack[-1]
  2469. need_result_map = toplevel or (
  2470. not compound_index
  2471. and entry.get("need_result_map_for_compound", False)
  2472. )
  2473. # indicates there is already a CompoundSelect in play
  2474. if compound_index == 0:
  2475. entry["select_0"] = cs
  2476. self.stack.append(
  2477. {
  2478. "correlate_froms": entry["correlate_froms"],
  2479. "asfrom_froms": entry["asfrom_froms"],
  2480. "selectable": cs,
  2481. "compile_state": compile_state,
  2482. "need_result_map_for_compound": need_result_map,
  2483. }
  2484. )
  2485. if compound_stmt._independent_ctes:
  2486. self._dispatch_independent_ctes(compound_stmt, kwargs)
  2487. keyword = self.compound_keywords[cs.keyword]
  2488. text = (" " + keyword + " ").join(
  2489. (
  2490. c._compiler_dispatch(
  2491. self, asfrom=asfrom, compound_index=i, **kwargs
  2492. )
  2493. for i, c in enumerate(cs.selects)
  2494. )
  2495. )
  2496. kwargs["include_table"] = False
  2497. text += self.group_by_clause(cs, **dict(asfrom=asfrom, **kwargs))
  2498. text += self.order_by_clause(cs, **kwargs)
  2499. if cs._has_row_limiting_clause:
  2500. text += self._row_limit_clause(cs, **kwargs)
  2501. if self.ctes:
  2502. nesting_level = len(self.stack) if not toplevel else None
  2503. text = (
  2504. self._render_cte_clause(
  2505. nesting_level=nesting_level,
  2506. include_following_stack=True,
  2507. )
  2508. + text
  2509. )
  2510. self.stack.pop(-1)
  2511. return text
  2512. def _row_limit_clause(self, cs, **kwargs):
  2513. if cs._fetch_clause is not None:
  2514. return self.fetch_clause(cs, **kwargs)
  2515. else:
  2516. return self.limit_clause(cs, **kwargs)
  2517. def _get_operator_dispatch(self, operator_, qualifier1, qualifier2):
  2518. attrname = "visit_%s_%s%s" % (
  2519. operator_.__name__,
  2520. qualifier1,
  2521. "_" + qualifier2 if qualifier2 else "",
  2522. )
  2523. return getattr(self, attrname, None)
  2524. def visit_unary(
  2525. self, unary, add_to_result_map=None, result_map_targets=(), **kw
  2526. ):
  2527. if add_to_result_map is not None:
  2528. result_map_targets += (unary,)
  2529. kw["add_to_result_map"] = add_to_result_map
  2530. kw["result_map_targets"] = result_map_targets
  2531. if unary.operator:
  2532. if unary.modifier:
  2533. raise exc.CompileError(
  2534. "Unary expression does not support operator "
  2535. "and modifier simultaneously"
  2536. )
  2537. disp = self._get_operator_dispatch(
  2538. unary.operator, "unary", "operator"
  2539. )
  2540. if disp:
  2541. return disp(unary, unary.operator, **kw)
  2542. else:
  2543. return self._generate_generic_unary_operator(
  2544. unary, OPERATORS[unary.operator], **kw
  2545. )
  2546. elif unary.modifier:
  2547. disp = self._get_operator_dispatch(
  2548. unary.modifier, "unary", "modifier"
  2549. )
  2550. if disp:
  2551. return disp(unary, unary.modifier, **kw)
  2552. else:
  2553. return self._generate_generic_unary_modifier(
  2554. unary, OPERATORS[unary.modifier], **kw
  2555. )
  2556. else:
  2557. raise exc.CompileError(
  2558. "Unary expression has no operator or modifier"
  2559. )
  2560. def visit_truediv_binary(self, binary, operator, **kw):
  2561. if self.dialect.div_is_floordiv:
  2562. return (
  2563. self.process(binary.left, **kw)
  2564. + " / "
  2565. # TODO: would need a fast cast again here,
  2566. # unless we want to use an implicit cast like "+ 0.0"
  2567. + self.process(
  2568. elements.Cast(
  2569. binary.right,
  2570. (
  2571. binary.right.type
  2572. if binary.right.type._type_affinity
  2573. is sqltypes.Numeric
  2574. else sqltypes.Numeric()
  2575. ),
  2576. ),
  2577. **kw,
  2578. )
  2579. )
  2580. else:
  2581. return (
  2582. self.process(binary.left, **kw)
  2583. + " / "
  2584. + self.process(binary.right, **kw)
  2585. )
  2586. def visit_floordiv_binary(self, binary, operator, **kw):
  2587. if (
  2588. self.dialect.div_is_floordiv
  2589. and binary.right.type._type_affinity is sqltypes.Integer
  2590. ):
  2591. return (
  2592. self.process(binary.left, **kw)
  2593. + " / "
  2594. + self.process(binary.right, **kw)
  2595. )
  2596. else:
  2597. return "FLOOR(%s)" % (
  2598. self.process(binary.left, **kw)
  2599. + " / "
  2600. + self.process(binary.right, **kw)
  2601. )
  2602. def visit_is_true_unary_operator(self, element, operator, **kw):
  2603. if (
  2604. element._is_implicitly_boolean
  2605. or self.dialect.supports_native_boolean
  2606. ):
  2607. return self.process(element.element, **kw)
  2608. else:
  2609. return "%s = 1" % self.process(element.element, **kw)
  2610. def visit_is_false_unary_operator(self, element, operator, **kw):
  2611. if (
  2612. element._is_implicitly_boolean
  2613. or self.dialect.supports_native_boolean
  2614. ):
  2615. return "NOT %s" % self.process(element.element, **kw)
  2616. else:
  2617. return "%s = 0" % self.process(element.element, **kw)
  2618. def visit_not_match_op_binary(self, binary, operator, **kw):
  2619. return "NOT %s" % self.visit_binary(
  2620. binary, override_operator=operators.match_op
  2621. )
  2622. def visit_not_in_op_binary(self, binary, operator, **kw):
  2623. # The brackets are required in the NOT IN operation because the empty
  2624. # case is handled using the form "(col NOT IN (null) OR 1 = 1)".
  2625. # The presence of the OR makes the brackets required.
  2626. return "(%s)" % self._generate_generic_binary(
  2627. binary, OPERATORS[operator], **kw
  2628. )
  2629. def visit_empty_set_op_expr(self, type_, expand_op, **kw):
  2630. if expand_op is operators.not_in_op:
  2631. if len(type_) > 1:
  2632. return "(%s)) OR (1 = 1" % (
  2633. ", ".join("NULL" for element in type_)
  2634. )
  2635. else:
  2636. return "NULL) OR (1 = 1"
  2637. elif expand_op is operators.in_op:
  2638. if len(type_) > 1:
  2639. return "(%s)) AND (1 != 1" % (
  2640. ", ".join("NULL" for element in type_)
  2641. )
  2642. else:
  2643. return "NULL) AND (1 != 1"
  2644. else:
  2645. return self.visit_empty_set_expr(type_)
  2646. def visit_empty_set_expr(self, element_types, **kw):
  2647. raise NotImplementedError(
  2648. "Dialect '%s' does not support empty set expression."
  2649. % self.dialect.name
  2650. )
  2651. def _literal_execute_expanding_parameter_literal_binds(
  2652. self, parameter, values, bind_expression_template=None
  2653. ):
  2654. typ_dialect_impl = parameter.type._unwrapped_dialect_impl(self.dialect)
  2655. if not values:
  2656. # empty IN expression. note we don't need to use
  2657. # bind_expression_template here because there are no
  2658. # expressions to render.
  2659. if typ_dialect_impl._is_tuple_type:
  2660. replacement_expression = (
  2661. "VALUES " if self.dialect.tuple_in_values else ""
  2662. ) + self.visit_empty_set_op_expr(
  2663. parameter.type.types, parameter.expand_op
  2664. )
  2665. else:
  2666. replacement_expression = self.visit_empty_set_op_expr(
  2667. [parameter.type], parameter.expand_op
  2668. )
  2669. elif typ_dialect_impl._is_tuple_type or (
  2670. typ_dialect_impl._isnull
  2671. and isinstance(values[0], collections_abc.Sequence)
  2672. and not isinstance(values[0], (str, bytes))
  2673. ):
  2674. if typ_dialect_impl._has_bind_expression:
  2675. raise NotImplementedError(
  2676. "bind_expression() on TupleType not supported with "
  2677. "literal_binds"
  2678. )
  2679. replacement_expression = (
  2680. "VALUES " if self.dialect.tuple_in_values else ""
  2681. ) + ", ".join(
  2682. "(%s)"
  2683. % (
  2684. ", ".join(
  2685. self.render_literal_value(value, param_type)
  2686. for value, param_type in zip(
  2687. tuple_element, parameter.type.types
  2688. )
  2689. )
  2690. )
  2691. for i, tuple_element in enumerate(values)
  2692. )
  2693. else:
  2694. if bind_expression_template:
  2695. post_compile_pattern = self._post_compile_pattern
  2696. m = post_compile_pattern.search(bind_expression_template)
  2697. assert m and m.group(
  2698. 2
  2699. ), "unexpected format for expanding parameter"
  2700. tok = m.group(2).split("~~")
  2701. be_left, be_right = tok[1], tok[3]
  2702. replacement_expression = ", ".join(
  2703. "%s%s%s"
  2704. % (
  2705. be_left,
  2706. self.render_literal_value(value, parameter.type),
  2707. be_right,
  2708. )
  2709. for value in values
  2710. )
  2711. else:
  2712. replacement_expression = ", ".join(
  2713. self.render_literal_value(value, parameter.type)
  2714. for value in values
  2715. )
  2716. return (), replacement_expression
  2717. def _literal_execute_expanding_parameter(self, name, parameter, values):
  2718. if parameter.literal_execute:
  2719. return self._literal_execute_expanding_parameter_literal_binds(
  2720. parameter, values
  2721. )
  2722. dialect = self.dialect
  2723. typ_dialect_impl = parameter.type._unwrapped_dialect_impl(dialect)
  2724. if self._numeric_binds:
  2725. bind_template = self.compilation_bindtemplate
  2726. else:
  2727. bind_template = self.bindtemplate
  2728. if (
  2729. self.dialect._bind_typing_render_casts
  2730. and typ_dialect_impl.render_bind_cast
  2731. ):
  2732. def _render_bindtemplate(name):
  2733. return self.render_bind_cast(
  2734. parameter.type,
  2735. typ_dialect_impl,
  2736. bind_template % {"name": name},
  2737. )
  2738. else:
  2739. def _render_bindtemplate(name):
  2740. return bind_template % {"name": name}
  2741. if not values:
  2742. to_update = []
  2743. if typ_dialect_impl._is_tuple_type:
  2744. replacement_expression = self.visit_empty_set_op_expr(
  2745. parameter.type.types, parameter.expand_op
  2746. )
  2747. else:
  2748. replacement_expression = self.visit_empty_set_op_expr(
  2749. [parameter.type], parameter.expand_op
  2750. )
  2751. elif typ_dialect_impl._is_tuple_type or (
  2752. typ_dialect_impl._isnull
  2753. and isinstance(values[0], collections_abc.Sequence)
  2754. and not isinstance(values[0], (str, bytes))
  2755. ):
  2756. assert not typ_dialect_impl._is_array
  2757. to_update = [
  2758. ("%s_%s_%s" % (name, i, j), value)
  2759. for i, tuple_element in enumerate(values, 1)
  2760. for j, value in enumerate(tuple_element, 1)
  2761. ]
  2762. replacement_expression = (
  2763. "VALUES " if dialect.tuple_in_values else ""
  2764. ) + ", ".join(
  2765. "(%s)"
  2766. % (
  2767. ", ".join(
  2768. _render_bindtemplate(
  2769. to_update[i * len(tuple_element) + j][0]
  2770. )
  2771. for j, value in enumerate(tuple_element)
  2772. )
  2773. )
  2774. for i, tuple_element in enumerate(values)
  2775. )
  2776. else:
  2777. to_update = [
  2778. ("%s_%s" % (name, i), value)
  2779. for i, value in enumerate(values, 1)
  2780. ]
  2781. replacement_expression = ", ".join(
  2782. _render_bindtemplate(key) for key, value in to_update
  2783. )
  2784. return to_update, replacement_expression
  2785. def visit_binary(
  2786. self,
  2787. binary,
  2788. override_operator=None,
  2789. eager_grouping=False,
  2790. from_linter=None,
  2791. lateral_from_linter=None,
  2792. **kw,
  2793. ):
  2794. if from_linter and operators.is_comparison(binary.operator):
  2795. if lateral_from_linter is not None:
  2796. enclosing_lateral = kw["enclosing_lateral"]
  2797. lateral_from_linter.edges.update(
  2798. itertools.product(
  2799. _de_clone(
  2800. binary.left._from_objects + [enclosing_lateral]
  2801. ),
  2802. _de_clone(
  2803. binary.right._from_objects + [enclosing_lateral]
  2804. ),
  2805. )
  2806. )
  2807. else:
  2808. from_linter.edges.update(
  2809. itertools.product(
  2810. _de_clone(binary.left._from_objects),
  2811. _de_clone(binary.right._from_objects),
  2812. )
  2813. )
  2814. # don't allow "? = ?" to render
  2815. if (
  2816. self.ansi_bind_rules
  2817. and isinstance(binary.left, elements.BindParameter)
  2818. and isinstance(binary.right, elements.BindParameter)
  2819. ):
  2820. kw["literal_execute"] = True
  2821. operator_ = override_operator or binary.operator
  2822. disp = self._get_operator_dispatch(operator_, "binary", None)
  2823. if disp:
  2824. return disp(binary, operator_, **kw)
  2825. else:
  2826. try:
  2827. opstring = OPERATORS[operator_]
  2828. except KeyError as err:
  2829. raise exc.UnsupportedCompilationError(self, operator_) from err
  2830. else:
  2831. return self._generate_generic_binary(
  2832. binary,
  2833. opstring,
  2834. from_linter=from_linter,
  2835. lateral_from_linter=lateral_from_linter,
  2836. **kw,
  2837. )
  2838. def visit_function_as_comparison_op_binary(self, element, operator, **kw):
  2839. return self.process(element.sql_function, **kw)
  2840. def visit_mod_binary(self, binary, operator, **kw):
  2841. if self.preparer._double_percents:
  2842. return (
  2843. self.process(binary.left, **kw)
  2844. + " %% "
  2845. + self.process(binary.right, **kw)
  2846. )
  2847. else:
  2848. return (
  2849. self.process(binary.left, **kw)
  2850. + " % "
  2851. + self.process(binary.right, **kw)
  2852. )
  2853. def visit_custom_op_binary(self, element, operator, **kw):
  2854. kw["eager_grouping"] = operator.eager_grouping
  2855. return self._generate_generic_binary(
  2856. element,
  2857. " " + self.escape_literal_column(operator.opstring) + " ",
  2858. **kw,
  2859. )
  2860. def visit_custom_op_unary_operator(self, element, operator, **kw):
  2861. return self._generate_generic_unary_operator(
  2862. element, self.escape_literal_column(operator.opstring) + " ", **kw
  2863. )
  2864. def visit_custom_op_unary_modifier(self, element, operator, **kw):
  2865. return self._generate_generic_unary_modifier(
  2866. element, " " + self.escape_literal_column(operator.opstring), **kw
  2867. )
  2868. def _generate_generic_binary(
  2869. self,
  2870. binary: BinaryExpression[Any],
  2871. opstring: str,
  2872. eager_grouping: bool = False,
  2873. **kw: Any,
  2874. ) -> str:
  2875. _in_operator_expression = kw.get("_in_operator_expression", False)
  2876. kw["_in_operator_expression"] = True
  2877. kw["_binary_op"] = binary.operator
  2878. text = (
  2879. binary.left._compiler_dispatch(
  2880. self, eager_grouping=eager_grouping, **kw
  2881. )
  2882. + opstring
  2883. + binary.right._compiler_dispatch(
  2884. self, eager_grouping=eager_grouping, **kw
  2885. )
  2886. )
  2887. if _in_operator_expression and eager_grouping:
  2888. text = "(%s)" % text
  2889. return text
  2890. def _generate_generic_unary_operator(self, unary, opstring, **kw):
  2891. return opstring + unary.element._compiler_dispatch(self, **kw)
  2892. def _generate_generic_unary_modifier(self, unary, opstring, **kw):
  2893. return unary.element._compiler_dispatch(self, **kw) + opstring
  2894. @util.memoized_property
  2895. def _like_percent_literal(self):
  2896. return elements.literal_column("'%'", type_=sqltypes.STRINGTYPE)
  2897. def visit_ilike_case_insensitive_operand(self, element, **kw):
  2898. return f"lower({element.element._compiler_dispatch(self, **kw)})"
  2899. def visit_contains_op_binary(self, binary, operator, **kw):
  2900. binary = binary._clone()
  2901. percent = self._like_percent_literal
  2902. binary.right = percent.concat(binary.right).concat(percent)
  2903. return self.visit_like_op_binary(binary, operator, **kw)
  2904. def visit_not_contains_op_binary(self, binary, operator, **kw):
  2905. binary = binary._clone()
  2906. percent = self._like_percent_literal
  2907. binary.right = percent.concat(binary.right).concat(percent)
  2908. return self.visit_not_like_op_binary(binary, operator, **kw)
  2909. def visit_icontains_op_binary(self, binary, operator, **kw):
  2910. binary = binary._clone()
  2911. percent = self._like_percent_literal
  2912. binary.left = ilike_case_insensitive(binary.left)
  2913. binary.right = percent.concat(
  2914. ilike_case_insensitive(binary.right)
  2915. ).concat(percent)
  2916. return self.visit_ilike_op_binary(binary, operator, **kw)
  2917. def visit_not_icontains_op_binary(self, binary, operator, **kw):
  2918. binary = binary._clone()
  2919. percent = self._like_percent_literal
  2920. binary.left = ilike_case_insensitive(binary.left)
  2921. binary.right = percent.concat(
  2922. ilike_case_insensitive(binary.right)
  2923. ).concat(percent)
  2924. return self.visit_not_ilike_op_binary(binary, operator, **kw)
  2925. def visit_startswith_op_binary(self, binary, operator, **kw):
  2926. binary = binary._clone()
  2927. percent = self._like_percent_literal
  2928. binary.right = percent._rconcat(binary.right)
  2929. return self.visit_like_op_binary(binary, operator, **kw)
  2930. def visit_not_startswith_op_binary(self, binary, operator, **kw):
  2931. binary = binary._clone()
  2932. percent = self._like_percent_literal
  2933. binary.right = percent._rconcat(binary.right)
  2934. return self.visit_not_like_op_binary(binary, operator, **kw)
  2935. def visit_istartswith_op_binary(self, binary, operator, **kw):
  2936. binary = binary._clone()
  2937. percent = self._like_percent_literal
  2938. binary.left = ilike_case_insensitive(binary.left)
  2939. binary.right = percent._rconcat(ilike_case_insensitive(binary.right))
  2940. return self.visit_ilike_op_binary(binary, operator, **kw)
  2941. def visit_not_istartswith_op_binary(self, binary, operator, **kw):
  2942. binary = binary._clone()
  2943. percent = self._like_percent_literal
  2944. binary.left = ilike_case_insensitive(binary.left)
  2945. binary.right = percent._rconcat(ilike_case_insensitive(binary.right))
  2946. return self.visit_not_ilike_op_binary(binary, operator, **kw)
  2947. def visit_endswith_op_binary(self, binary, operator, **kw):
  2948. binary = binary._clone()
  2949. percent = self._like_percent_literal
  2950. binary.right = percent.concat(binary.right)
  2951. return self.visit_like_op_binary(binary, operator, **kw)
  2952. def visit_not_endswith_op_binary(self, binary, operator, **kw):
  2953. binary = binary._clone()
  2954. percent = self._like_percent_literal
  2955. binary.right = percent.concat(binary.right)
  2956. return self.visit_not_like_op_binary(binary, operator, **kw)
  2957. def visit_iendswith_op_binary(self, binary, operator, **kw):
  2958. binary = binary._clone()
  2959. percent = self._like_percent_literal
  2960. binary.left = ilike_case_insensitive(binary.left)
  2961. binary.right = percent.concat(ilike_case_insensitive(binary.right))
  2962. return self.visit_ilike_op_binary(binary, operator, **kw)
  2963. def visit_not_iendswith_op_binary(self, binary, operator, **kw):
  2964. binary = binary._clone()
  2965. percent = self._like_percent_literal
  2966. binary.left = ilike_case_insensitive(binary.left)
  2967. binary.right = percent.concat(ilike_case_insensitive(binary.right))
  2968. return self.visit_not_ilike_op_binary(binary, operator, **kw)
  2969. def visit_like_op_binary(self, binary, operator, **kw):
  2970. escape = binary.modifiers.get("escape", None)
  2971. return "%s LIKE %s" % (
  2972. binary.left._compiler_dispatch(self, **kw),
  2973. binary.right._compiler_dispatch(self, **kw),
  2974. ) + (
  2975. " ESCAPE " + self.render_literal_value(escape, sqltypes.STRINGTYPE)
  2976. if escape is not None
  2977. else ""
  2978. )
  2979. def visit_not_like_op_binary(self, binary, operator, **kw):
  2980. escape = binary.modifiers.get("escape", None)
  2981. return "%s NOT LIKE %s" % (
  2982. binary.left._compiler_dispatch(self, **kw),
  2983. binary.right._compiler_dispatch(self, **kw),
  2984. ) + (
  2985. " ESCAPE " + self.render_literal_value(escape, sqltypes.STRINGTYPE)
  2986. if escape is not None
  2987. else ""
  2988. )
  2989. def visit_ilike_op_binary(self, binary, operator, **kw):
  2990. if operator is operators.ilike_op:
  2991. binary = binary._clone()
  2992. binary.left = ilike_case_insensitive(binary.left)
  2993. binary.right = ilike_case_insensitive(binary.right)
  2994. # else we assume ilower() has been applied
  2995. return self.visit_like_op_binary(binary, operator, **kw)
  2996. def visit_not_ilike_op_binary(self, binary, operator, **kw):
  2997. if operator is operators.not_ilike_op:
  2998. binary = binary._clone()
  2999. binary.left = ilike_case_insensitive(binary.left)
  3000. binary.right = ilike_case_insensitive(binary.right)
  3001. # else we assume ilower() has been applied
  3002. return self.visit_not_like_op_binary(binary, operator, **kw)
  3003. def visit_between_op_binary(self, binary, operator, **kw):
  3004. symmetric = binary.modifiers.get("symmetric", False)
  3005. return self._generate_generic_binary(
  3006. binary, " BETWEEN SYMMETRIC " if symmetric else " BETWEEN ", **kw
  3007. )
  3008. def visit_not_between_op_binary(self, binary, operator, **kw):
  3009. symmetric = binary.modifiers.get("symmetric", False)
  3010. return self._generate_generic_binary(
  3011. binary,
  3012. " NOT BETWEEN SYMMETRIC " if symmetric else " NOT BETWEEN ",
  3013. **kw,
  3014. )
  3015. def visit_regexp_match_op_binary(
  3016. self, binary: BinaryExpression[Any], operator: Any, **kw: Any
  3017. ) -> str:
  3018. raise exc.CompileError(
  3019. "%s dialect does not support regular expressions"
  3020. % self.dialect.name
  3021. )
  3022. def visit_not_regexp_match_op_binary(
  3023. self, binary: BinaryExpression[Any], operator: Any, **kw: Any
  3024. ) -> str:
  3025. raise exc.CompileError(
  3026. "%s dialect does not support regular expressions"
  3027. % self.dialect.name
  3028. )
  3029. def visit_regexp_replace_op_binary(
  3030. self, binary: BinaryExpression[Any], operator: Any, **kw: Any
  3031. ) -> str:
  3032. raise exc.CompileError(
  3033. "%s dialect does not support regular expression replacements"
  3034. % self.dialect.name
  3035. )
  3036. def visit_bindparam(
  3037. self,
  3038. bindparam,
  3039. within_columns_clause=False,
  3040. literal_binds=False,
  3041. skip_bind_expression=False,
  3042. literal_execute=False,
  3043. render_postcompile=False,
  3044. **kwargs,
  3045. ):
  3046. if not skip_bind_expression:
  3047. impl = bindparam.type.dialect_impl(self.dialect)
  3048. if impl._has_bind_expression:
  3049. bind_expression = impl.bind_expression(bindparam)
  3050. wrapped = self.process(
  3051. bind_expression,
  3052. skip_bind_expression=True,
  3053. within_columns_clause=within_columns_clause,
  3054. literal_binds=literal_binds and not bindparam.expanding,
  3055. literal_execute=literal_execute,
  3056. render_postcompile=render_postcompile,
  3057. **kwargs,
  3058. )
  3059. if bindparam.expanding:
  3060. # for postcompile w/ expanding, move the "wrapped" part
  3061. # of this into the inside
  3062. m = re.match(
  3063. r"^(.*)\(__\[POSTCOMPILE_(\S+?)\]\)(.*)$", wrapped
  3064. )
  3065. assert m, "unexpected format for expanding parameter"
  3066. wrapped = "(__[POSTCOMPILE_%s~~%s~~REPL~~%s~~])" % (
  3067. m.group(2),
  3068. m.group(1),
  3069. m.group(3),
  3070. )
  3071. if literal_binds:
  3072. ret = self.render_literal_bindparam(
  3073. bindparam,
  3074. within_columns_clause=True,
  3075. bind_expression_template=wrapped,
  3076. **kwargs,
  3077. )
  3078. return "(%s)" % ret
  3079. return wrapped
  3080. if not literal_binds:
  3081. literal_execute = (
  3082. literal_execute
  3083. or bindparam.literal_execute
  3084. or (within_columns_clause and self.ansi_bind_rules)
  3085. )
  3086. post_compile = literal_execute or bindparam.expanding
  3087. else:
  3088. post_compile = False
  3089. if literal_binds:
  3090. ret = self.render_literal_bindparam(
  3091. bindparam, within_columns_clause=True, **kwargs
  3092. )
  3093. if bindparam.expanding:
  3094. ret = "(%s)" % ret
  3095. return ret
  3096. name = self._truncate_bindparam(bindparam)
  3097. if name in self.binds:
  3098. existing = self.binds[name]
  3099. if existing is not bindparam:
  3100. if (
  3101. (existing.unique or bindparam.unique)
  3102. and not existing.proxy_set.intersection(
  3103. bindparam.proxy_set
  3104. )
  3105. and not existing._cloned_set.intersection(
  3106. bindparam._cloned_set
  3107. )
  3108. ):
  3109. raise exc.CompileError(
  3110. "Bind parameter '%s' conflicts with "
  3111. "unique bind parameter of the same name" % name
  3112. )
  3113. elif existing.expanding != bindparam.expanding:
  3114. raise exc.CompileError(
  3115. "Can't reuse bound parameter name '%s' in both "
  3116. "'expanding' (e.g. within an IN expression) and "
  3117. "non-expanding contexts. If this parameter is to "
  3118. "receive a list/array value, set 'expanding=True' on "
  3119. "it for expressions that aren't IN, otherwise use "
  3120. "a different parameter name." % (name,)
  3121. )
  3122. elif existing._is_crud or bindparam._is_crud:
  3123. if existing._is_crud and bindparam._is_crud:
  3124. # TODO: this condition is not well understood.
  3125. # see tests in test/sql/test_update.py
  3126. raise exc.CompileError(
  3127. "Encountered unsupported case when compiling an "
  3128. "INSERT or UPDATE statement. If this is a "
  3129. "multi-table "
  3130. "UPDATE statement, please provide string-named "
  3131. "arguments to the "
  3132. "values() method with distinct names; support for "
  3133. "multi-table UPDATE statements that "
  3134. "target multiple tables for UPDATE is very "
  3135. "limited",
  3136. )
  3137. else:
  3138. raise exc.CompileError(
  3139. f"bindparam() name '{bindparam.key}' is reserved "
  3140. "for automatic usage in the VALUES or SET "
  3141. "clause of this "
  3142. "insert/update statement. Please use a "
  3143. "name other than column name when using "
  3144. "bindparam() "
  3145. "with insert() or update() (for example, "
  3146. f"'b_{bindparam.key}')."
  3147. )
  3148. self.binds[bindparam.key] = self.binds[name] = bindparam
  3149. # if we are given a cache key that we're going to match against,
  3150. # relate the bindparam here to one that is most likely present
  3151. # in the "extracted params" portion of the cache key. this is used
  3152. # to set up a positional mapping that is used to determine the
  3153. # correct parameters for a subsequent use of this compiled with
  3154. # a different set of parameter values. here, we accommodate for
  3155. # parameters that may have been cloned both before and after the cache
  3156. # key was been generated.
  3157. ckbm_tuple = self._cache_key_bind_match
  3158. if ckbm_tuple:
  3159. ckbm, cksm = ckbm_tuple
  3160. for bp in bindparam._cloned_set:
  3161. if bp.key in cksm:
  3162. cb = cksm[bp.key]
  3163. ckbm[cb].append(bindparam)
  3164. if bindparam.isoutparam:
  3165. self.has_out_parameters = True
  3166. if post_compile:
  3167. if render_postcompile:
  3168. self._render_postcompile = True
  3169. if literal_execute:
  3170. self.literal_execute_params |= {bindparam}
  3171. else:
  3172. self.post_compile_params |= {bindparam}
  3173. ret = self.bindparam_string(
  3174. name,
  3175. post_compile=post_compile,
  3176. expanding=bindparam.expanding,
  3177. bindparam_type=bindparam.type,
  3178. **kwargs,
  3179. )
  3180. if bindparam.expanding:
  3181. ret = "(%s)" % ret
  3182. return ret
  3183. def render_bind_cast(self, type_, dbapi_type, sqltext):
  3184. raise NotImplementedError()
  3185. def render_literal_bindparam(
  3186. self,
  3187. bindparam,
  3188. render_literal_value=NO_ARG,
  3189. bind_expression_template=None,
  3190. **kw,
  3191. ):
  3192. if render_literal_value is not NO_ARG:
  3193. value = render_literal_value
  3194. else:
  3195. if bindparam.value is None and bindparam.callable is None:
  3196. op = kw.get("_binary_op", None)
  3197. if op and op not in (operators.is_, operators.is_not):
  3198. util.warn_limited(
  3199. "Bound parameter '%s' rendering literal NULL in a SQL "
  3200. "expression; comparisons to NULL should not use "
  3201. "operators outside of 'is' or 'is not'",
  3202. (bindparam.key,),
  3203. )
  3204. return self.process(sqltypes.NULLTYPE, **kw)
  3205. value = bindparam.effective_value
  3206. if bindparam.expanding:
  3207. leep = self._literal_execute_expanding_parameter_literal_binds
  3208. to_update, replacement_expr = leep(
  3209. bindparam,
  3210. value,
  3211. bind_expression_template=bind_expression_template,
  3212. )
  3213. return replacement_expr
  3214. else:
  3215. return self.render_literal_value(value, bindparam.type)
  3216. def render_literal_value(
  3217. self, value: Any, type_: sqltypes.TypeEngine[Any]
  3218. ) -> str:
  3219. """Render the value of a bind parameter as a quoted literal.
  3220. This is used for statement sections that do not accept bind parameters
  3221. on the target driver/database.
  3222. This should be implemented by subclasses using the quoting services
  3223. of the DBAPI.
  3224. """
  3225. if value is None and not type_.should_evaluate_none:
  3226. # issue #10535 - handle NULL in the compiler without placing
  3227. # this onto each type, except for "evaluate None" types
  3228. # (e.g. JSON)
  3229. return self.process(elements.Null._instance())
  3230. processor = type_._cached_literal_processor(self.dialect)
  3231. if processor:
  3232. try:
  3233. return processor(value)
  3234. except Exception as e:
  3235. raise exc.CompileError(
  3236. f"Could not render literal value "
  3237. f'"{sql_util._repr_single_value(value)}" '
  3238. f"with datatype "
  3239. f"{type_}; see parent stack trace for "
  3240. "more detail."
  3241. ) from e
  3242. else:
  3243. raise exc.CompileError(
  3244. f"No literal value renderer is available for literal value "
  3245. f'"{sql_util._repr_single_value(value)}" '
  3246. f"with datatype {type_}"
  3247. )
  3248. def _truncate_bindparam(self, bindparam):
  3249. if bindparam in self.bind_names:
  3250. return self.bind_names[bindparam]
  3251. bind_name = bindparam.key
  3252. if isinstance(bind_name, elements._truncated_label):
  3253. bind_name = self._truncated_identifier("bindparam", bind_name)
  3254. # add to bind_names for translation
  3255. self.bind_names[bindparam] = bind_name
  3256. return bind_name
  3257. def _truncated_identifier(
  3258. self, ident_class: str, name: _truncated_label
  3259. ) -> str:
  3260. if (ident_class, name) in self.truncated_names:
  3261. return self.truncated_names[(ident_class, name)]
  3262. anonname = name.apply_map(self.anon_map)
  3263. if len(anonname) > self.label_length - 6:
  3264. counter = self._truncated_counters.get(ident_class, 1)
  3265. truncname = (
  3266. anonname[0 : max(self.label_length - 6, 0)]
  3267. + "_"
  3268. + hex(counter)[2:]
  3269. )
  3270. self._truncated_counters[ident_class] = counter + 1
  3271. else:
  3272. truncname = anonname
  3273. self.truncated_names[(ident_class, name)] = truncname
  3274. return truncname
  3275. def _anonymize(self, name: str) -> str:
  3276. return name % self.anon_map
  3277. def bindparam_string(
  3278. self,
  3279. name: str,
  3280. post_compile: bool = False,
  3281. expanding: bool = False,
  3282. escaped_from: Optional[str] = None,
  3283. bindparam_type: Optional[TypeEngine[Any]] = None,
  3284. accumulate_bind_names: Optional[Set[str]] = None,
  3285. visited_bindparam: Optional[List[str]] = None,
  3286. **kw: Any,
  3287. ) -> str:
  3288. # TODO: accumulate_bind_names is passed by crud.py to gather
  3289. # names on a per-value basis, visited_bindparam is passed by
  3290. # visit_insert() to collect all parameters in the statement.
  3291. # see if this gathering can be simplified somehow
  3292. if accumulate_bind_names is not None:
  3293. accumulate_bind_names.add(name)
  3294. if visited_bindparam is not None:
  3295. visited_bindparam.append(name)
  3296. if not escaped_from:
  3297. if self._bind_translate_re.search(name):
  3298. # not quite the translate use case as we want to
  3299. # also get a quick boolean if we even found
  3300. # unusual characters in the name
  3301. new_name = self._bind_translate_re.sub(
  3302. lambda m: self._bind_translate_chars[m.group(0)],
  3303. name,
  3304. )
  3305. escaped_from = name
  3306. name = new_name
  3307. if escaped_from:
  3308. self.escaped_bind_names = self.escaped_bind_names.union(
  3309. {escaped_from: name}
  3310. )
  3311. if post_compile:
  3312. ret = "__[POSTCOMPILE_%s]" % name
  3313. if expanding:
  3314. # for expanding, bound parameters or literal values will be
  3315. # rendered per item
  3316. return ret
  3317. # otherwise, for non-expanding "literal execute", apply
  3318. # bind casts as determined by the datatype
  3319. if bindparam_type is not None:
  3320. type_impl = bindparam_type._unwrapped_dialect_impl(
  3321. self.dialect
  3322. )
  3323. if type_impl.render_literal_cast:
  3324. ret = self.render_bind_cast(bindparam_type, type_impl, ret)
  3325. return ret
  3326. elif self.state is CompilerState.COMPILING:
  3327. ret = self.compilation_bindtemplate % {"name": name}
  3328. else:
  3329. ret = self.bindtemplate % {"name": name}
  3330. if (
  3331. bindparam_type is not None
  3332. and self.dialect._bind_typing_render_casts
  3333. ):
  3334. type_impl = bindparam_type._unwrapped_dialect_impl(self.dialect)
  3335. if type_impl.render_bind_cast:
  3336. ret = self.render_bind_cast(bindparam_type, type_impl, ret)
  3337. return ret
  3338. def _dispatch_independent_ctes(self, stmt, kw):
  3339. local_kw = kw.copy()
  3340. local_kw.pop("cte_opts", None)
  3341. for cte, opt in zip(
  3342. stmt._independent_ctes, stmt._independent_ctes_opts
  3343. ):
  3344. cte._compiler_dispatch(self, cte_opts=opt, **local_kw)
  3345. def visit_cte(
  3346. self,
  3347. cte: CTE,
  3348. asfrom: bool = False,
  3349. ashint: bool = False,
  3350. fromhints: Optional[_FromHintsType] = None,
  3351. visiting_cte: Optional[CTE] = None,
  3352. from_linter: Optional[FromLinter] = None,
  3353. cte_opts: selectable._CTEOpts = selectable._CTEOpts(False),
  3354. **kwargs: Any,
  3355. ) -> Optional[str]:
  3356. self_ctes = self._init_cte_state()
  3357. assert self_ctes is self.ctes
  3358. kwargs["visiting_cte"] = cte
  3359. cte_name = cte.name
  3360. if isinstance(cte_name, elements._truncated_label):
  3361. cte_name = self._truncated_identifier("alias", cte_name)
  3362. is_new_cte = True
  3363. embedded_in_current_named_cte = False
  3364. _reference_cte = cte._get_reference_cte()
  3365. nesting = cte.nesting or cte_opts.nesting
  3366. # check for CTE already encountered
  3367. if _reference_cte in self.level_name_by_cte:
  3368. cte_level, _, existing_cte_opts = self.level_name_by_cte[
  3369. _reference_cte
  3370. ]
  3371. assert _ == cte_name
  3372. cte_level_name = (cte_level, cte_name)
  3373. existing_cte = self.ctes_by_level_name[cte_level_name]
  3374. # check if we are receiving it here with a specific
  3375. # "nest_here" location; if so, move it to this location
  3376. if cte_opts.nesting:
  3377. if existing_cte_opts.nesting:
  3378. raise exc.CompileError(
  3379. "CTE is stated as 'nest_here' in "
  3380. "more than one location"
  3381. )
  3382. old_level_name = (cte_level, cte_name)
  3383. cte_level = len(self.stack) if nesting else 1
  3384. cte_level_name = new_level_name = (cte_level, cte_name)
  3385. del self.ctes_by_level_name[old_level_name]
  3386. self.ctes_by_level_name[new_level_name] = existing_cte
  3387. self.level_name_by_cte[_reference_cte] = new_level_name + (
  3388. cte_opts,
  3389. )
  3390. else:
  3391. cte_level = len(self.stack) if nesting else 1
  3392. cte_level_name = (cte_level, cte_name)
  3393. if cte_level_name in self.ctes_by_level_name:
  3394. existing_cte = self.ctes_by_level_name[cte_level_name]
  3395. else:
  3396. existing_cte = None
  3397. if existing_cte is not None:
  3398. embedded_in_current_named_cte = visiting_cte is existing_cte
  3399. # we've generated a same-named CTE that we are enclosed in,
  3400. # or this is the same CTE. just return the name.
  3401. if cte is existing_cte._restates or cte is existing_cte:
  3402. is_new_cte = False
  3403. elif existing_cte is cte._restates:
  3404. # we've generated a same-named CTE that is
  3405. # enclosed in us - we take precedence, so
  3406. # discard the text for the "inner".
  3407. del self_ctes[existing_cte]
  3408. existing_cte_reference_cte = existing_cte._get_reference_cte()
  3409. assert existing_cte_reference_cte is _reference_cte
  3410. assert existing_cte_reference_cte is existing_cte
  3411. del self.level_name_by_cte[existing_cte_reference_cte]
  3412. else:
  3413. if (
  3414. # if the two CTEs have the same hash, which we expect
  3415. # here means that one/both is an annotated of the other
  3416. (hash(cte) == hash(existing_cte))
  3417. # or...
  3418. or (
  3419. (
  3420. # if they are clones, i.e. they came from the ORM
  3421. # or some other visit method
  3422. cte._is_clone_of is not None
  3423. or existing_cte._is_clone_of is not None
  3424. )
  3425. # and are deep-copy identical
  3426. and cte.compare(existing_cte)
  3427. )
  3428. ):
  3429. # then consider these two CTEs the same
  3430. is_new_cte = False
  3431. else:
  3432. # otherwise these are two CTEs that either will render
  3433. # differently, or were indicated separately by the user,
  3434. # with the same name
  3435. raise exc.CompileError(
  3436. "Multiple, unrelated CTEs found with "
  3437. "the same name: %r" % cte_name
  3438. )
  3439. if not asfrom and not is_new_cte:
  3440. return None
  3441. if cte._cte_alias is not None:
  3442. pre_alias_cte = cte._cte_alias
  3443. cte_pre_alias_name = cte._cte_alias.name
  3444. if isinstance(cte_pre_alias_name, elements._truncated_label):
  3445. cte_pre_alias_name = self._truncated_identifier(
  3446. "alias", cte_pre_alias_name
  3447. )
  3448. else:
  3449. pre_alias_cte = cte
  3450. cte_pre_alias_name = None
  3451. if is_new_cte:
  3452. self.ctes_by_level_name[cte_level_name] = cte
  3453. self.level_name_by_cte[_reference_cte] = cte_level_name + (
  3454. cte_opts,
  3455. )
  3456. if pre_alias_cte not in self.ctes:
  3457. self.visit_cte(pre_alias_cte, **kwargs)
  3458. if not cte_pre_alias_name and cte not in self_ctes:
  3459. if cte.recursive:
  3460. self.ctes_recursive = True
  3461. text = self.preparer.format_alias(cte, cte_name)
  3462. if cte.recursive or cte.element.name_cte_columns:
  3463. col_source = cte.element
  3464. # TODO: can we get at the .columns_plus_names collection
  3465. # that is already (or will be?) generated for the SELECT
  3466. # rather than calling twice?
  3467. recur_cols = [
  3468. # TODO: proxy_name is not technically safe,
  3469. # see test_cte->
  3470. # test_with_recursive_no_name_currently_buggy. not
  3471. # clear what should be done with such a case
  3472. fallback_label_name or proxy_name
  3473. for (
  3474. _,
  3475. proxy_name,
  3476. fallback_label_name,
  3477. c,
  3478. repeated,
  3479. ) in (col_source._generate_columns_plus_names(True))
  3480. if not repeated
  3481. ]
  3482. text += "(%s)" % (
  3483. ", ".join(
  3484. self.preparer.format_label_name(
  3485. ident, anon_map=self.anon_map
  3486. )
  3487. for ident in recur_cols
  3488. )
  3489. )
  3490. assert kwargs.get("subquery", False) is False
  3491. if not self.stack:
  3492. # toplevel, this is a stringify of the
  3493. # cte directly. just compile the inner
  3494. # the way alias() does.
  3495. return cte.element._compiler_dispatch(
  3496. self, asfrom=asfrom, **kwargs
  3497. )
  3498. else:
  3499. prefixes = self._generate_prefixes(
  3500. cte, cte._prefixes, **kwargs
  3501. )
  3502. inner = cte.element._compiler_dispatch(
  3503. self, asfrom=True, **kwargs
  3504. )
  3505. text += " AS %s\n(%s)" % (prefixes, inner)
  3506. if cte._suffixes:
  3507. text += " " + self._generate_prefixes(
  3508. cte, cte._suffixes, **kwargs
  3509. )
  3510. self_ctes[cte] = text
  3511. if asfrom:
  3512. if from_linter:
  3513. from_linter.froms[cte._de_clone()] = cte_name
  3514. if not is_new_cte and embedded_in_current_named_cte:
  3515. return self.preparer.format_alias(cte, cte_name)
  3516. if cte_pre_alias_name:
  3517. text = self.preparer.format_alias(cte, cte_pre_alias_name)
  3518. if self.preparer._requires_quotes(cte_name):
  3519. cte_name = self.preparer.quote(cte_name)
  3520. text += self.get_render_as_alias_suffix(cte_name)
  3521. return text # type: ignore[no-any-return]
  3522. else:
  3523. return self.preparer.format_alias(cte, cte_name)
  3524. return None
  3525. def visit_table_valued_alias(self, element, **kw):
  3526. if element.joins_implicitly:
  3527. kw["from_linter"] = None
  3528. if element._is_lateral:
  3529. return self.visit_lateral(element, **kw)
  3530. else:
  3531. return self.visit_alias(element, **kw)
  3532. def visit_table_valued_column(self, element, **kw):
  3533. return self.visit_column(element, **kw)
  3534. def visit_alias(
  3535. self,
  3536. alias,
  3537. asfrom=False,
  3538. ashint=False,
  3539. iscrud=False,
  3540. fromhints=None,
  3541. subquery=False,
  3542. lateral=False,
  3543. enclosing_alias=None,
  3544. from_linter=None,
  3545. **kwargs,
  3546. ):
  3547. if lateral:
  3548. if "enclosing_lateral" not in kwargs:
  3549. # if lateral is set and enclosing_lateral is not
  3550. # present, we assume we are being called directly
  3551. # from visit_lateral() and we need to set enclosing_lateral.
  3552. assert alias._is_lateral
  3553. kwargs["enclosing_lateral"] = alias
  3554. # for lateral objects, we track a second from_linter that is...
  3555. # lateral! to the level above us.
  3556. if (
  3557. from_linter
  3558. and "lateral_from_linter" not in kwargs
  3559. and "enclosing_lateral" in kwargs
  3560. ):
  3561. kwargs["lateral_from_linter"] = from_linter
  3562. if enclosing_alias is not None and enclosing_alias.element is alias:
  3563. inner = alias.element._compiler_dispatch(
  3564. self,
  3565. asfrom=asfrom,
  3566. ashint=ashint,
  3567. iscrud=iscrud,
  3568. fromhints=fromhints,
  3569. lateral=lateral,
  3570. enclosing_alias=alias,
  3571. **kwargs,
  3572. )
  3573. if subquery and (asfrom or lateral):
  3574. inner = "(%s)" % (inner,)
  3575. return inner
  3576. else:
  3577. kwargs["enclosing_alias"] = alias
  3578. if asfrom or ashint:
  3579. if isinstance(alias.name, elements._truncated_label):
  3580. alias_name = self._truncated_identifier("alias", alias.name)
  3581. else:
  3582. alias_name = alias.name
  3583. if ashint:
  3584. return self.preparer.format_alias(alias, alias_name)
  3585. elif asfrom:
  3586. if from_linter:
  3587. from_linter.froms[alias._de_clone()] = alias_name
  3588. inner = alias.element._compiler_dispatch(
  3589. self, asfrom=True, lateral=lateral, **kwargs
  3590. )
  3591. if subquery:
  3592. inner = "(%s)" % (inner,)
  3593. ret = inner + self.get_render_as_alias_suffix(
  3594. self.preparer.format_alias(alias, alias_name)
  3595. )
  3596. if alias._supports_derived_columns and alias._render_derived:
  3597. ret += "(%s)" % (
  3598. ", ".join(
  3599. "%s%s"
  3600. % (
  3601. self.preparer.quote(col.name),
  3602. (
  3603. " %s"
  3604. % self.dialect.type_compiler_instance.process(
  3605. col.type, **kwargs
  3606. )
  3607. if alias._render_derived_w_types
  3608. else ""
  3609. ),
  3610. )
  3611. for col in alias.c
  3612. )
  3613. )
  3614. if fromhints and alias in fromhints:
  3615. ret = self.format_from_hint_text(
  3616. ret, alias, fromhints[alias], iscrud
  3617. )
  3618. return ret
  3619. else:
  3620. # note we cancel the "subquery" flag here as well
  3621. return alias.element._compiler_dispatch(
  3622. self, lateral=lateral, **kwargs
  3623. )
  3624. def visit_subquery(self, subquery, **kw):
  3625. kw["subquery"] = True
  3626. return self.visit_alias(subquery, **kw)
  3627. def visit_lateral(self, lateral_, **kw):
  3628. kw["lateral"] = True
  3629. return "LATERAL %s" % self.visit_alias(lateral_, **kw)
  3630. def visit_tablesample(self, tablesample, asfrom=False, **kw):
  3631. text = "%s TABLESAMPLE %s" % (
  3632. self.visit_alias(tablesample, asfrom=True, **kw),
  3633. tablesample._get_method()._compiler_dispatch(self, **kw),
  3634. )
  3635. if tablesample.seed is not None:
  3636. text += " REPEATABLE (%s)" % (
  3637. tablesample.seed._compiler_dispatch(self, **kw)
  3638. )
  3639. return text
  3640. def _render_values(self, element, **kw):
  3641. kw.setdefault("literal_binds", element.literal_binds)
  3642. tuples = ", ".join(
  3643. self.process(
  3644. elements.Tuple(
  3645. types=element._column_types, *elem
  3646. ).self_group(),
  3647. **kw,
  3648. )
  3649. for chunk in element._data
  3650. for elem in chunk
  3651. )
  3652. return f"VALUES {tuples}"
  3653. def visit_values(
  3654. self, element, asfrom=False, from_linter=None, visiting_cte=None, **kw
  3655. ):
  3656. if element._independent_ctes:
  3657. self._dispatch_independent_ctes(element, kw)
  3658. v = self._render_values(element, **kw)
  3659. if element._unnamed:
  3660. name = None
  3661. elif isinstance(element.name, elements._truncated_label):
  3662. name = self._truncated_identifier("values", element.name)
  3663. else:
  3664. name = element.name
  3665. if element._is_lateral:
  3666. lateral = "LATERAL "
  3667. else:
  3668. lateral = ""
  3669. if asfrom:
  3670. if from_linter:
  3671. from_linter.froms[element._de_clone()] = (
  3672. name if name is not None else "(unnamed VALUES element)"
  3673. )
  3674. if visiting_cte is not None and visiting_cte.element is element:
  3675. if element._is_lateral:
  3676. raise exc.CompileError(
  3677. "Can't use a LATERAL VALUES expression inside of a CTE"
  3678. )
  3679. elif name:
  3680. kw["include_table"] = False
  3681. v = "%s(%s)%s (%s)" % (
  3682. lateral,
  3683. v,
  3684. self.get_render_as_alias_suffix(self.preparer.quote(name)),
  3685. (
  3686. ", ".join(
  3687. c._compiler_dispatch(self, **kw)
  3688. for c in element.columns
  3689. )
  3690. ),
  3691. )
  3692. else:
  3693. v = "%s(%s)" % (lateral, v)
  3694. return v
  3695. def visit_scalar_values(self, element, **kw):
  3696. return f"({self._render_values(element, **kw)})"
  3697. def get_render_as_alias_suffix(self, alias_name_text):
  3698. return " AS " + alias_name_text
  3699. def _add_to_result_map(
  3700. self,
  3701. keyname: str,
  3702. name: str,
  3703. objects: Tuple[Any, ...],
  3704. type_: TypeEngine[Any],
  3705. ) -> None:
  3706. # note objects must be non-empty for cursor.py to handle the
  3707. # collection properly
  3708. assert objects
  3709. if keyname is None or keyname == "*":
  3710. self._ordered_columns = False
  3711. self._ad_hoc_textual = True
  3712. if type_._is_tuple_type:
  3713. raise exc.CompileError(
  3714. "Most backends don't support SELECTing "
  3715. "from a tuple() object. If this is an ORM query, "
  3716. "consider using the Bundle object."
  3717. )
  3718. self._result_columns.append(
  3719. ResultColumnsEntry(keyname, name, objects, type_)
  3720. )
  3721. def _label_returning_column(
  3722. self, stmt, column, populate_result_map, column_clause_args=None, **kw
  3723. ):
  3724. """Render a column with necessary labels inside of a RETURNING clause.
  3725. This method is provided for individual dialects in place of calling
  3726. the _label_select_column method directly, so that the two use cases
  3727. of RETURNING vs. SELECT can be disambiguated going forward.
  3728. .. versionadded:: 1.4.21
  3729. """
  3730. return self._label_select_column(
  3731. None,
  3732. column,
  3733. populate_result_map,
  3734. False,
  3735. {} if column_clause_args is None else column_clause_args,
  3736. **kw,
  3737. )
  3738. def _label_select_column(
  3739. self,
  3740. select,
  3741. column,
  3742. populate_result_map,
  3743. asfrom,
  3744. column_clause_args,
  3745. name=None,
  3746. proxy_name=None,
  3747. fallback_label_name=None,
  3748. within_columns_clause=True,
  3749. column_is_repeated=False,
  3750. need_column_expressions=False,
  3751. include_table=True,
  3752. ):
  3753. """produce labeled columns present in a select()."""
  3754. impl = column.type.dialect_impl(self.dialect)
  3755. if impl._has_column_expression and (
  3756. need_column_expressions or populate_result_map
  3757. ):
  3758. col_expr = impl.column_expression(column)
  3759. else:
  3760. col_expr = column
  3761. if populate_result_map:
  3762. # pass an "add_to_result_map" callable into the compilation
  3763. # of embedded columns. this collects information about the
  3764. # column as it will be fetched in the result and is coordinated
  3765. # with cursor.description when the query is executed.
  3766. add_to_result_map = self._add_to_result_map
  3767. # if the SELECT statement told us this column is a repeat,
  3768. # wrap the callable with one that prevents the addition of the
  3769. # targets
  3770. if column_is_repeated:
  3771. _add_to_result_map = add_to_result_map
  3772. def add_to_result_map(keyname, name, objects, type_):
  3773. _add_to_result_map(keyname, name, (keyname,), type_)
  3774. # if we redefined col_expr for type expressions, wrap the
  3775. # callable with one that adds the original column to the targets
  3776. elif col_expr is not column:
  3777. _add_to_result_map = add_to_result_map
  3778. def add_to_result_map(keyname, name, objects, type_):
  3779. _add_to_result_map(
  3780. keyname, name, (column,) + objects, type_
  3781. )
  3782. else:
  3783. add_to_result_map = None
  3784. # this method is used by some of the dialects for RETURNING,
  3785. # which has different inputs. _label_returning_column was added
  3786. # as the better target for this now however for 1.4 we will keep
  3787. # _label_select_column directly compatible with this use case.
  3788. # these assertions right now set up the current expected inputs
  3789. assert within_columns_clause, (
  3790. "_label_select_column is only relevant within "
  3791. "the columns clause of a SELECT or RETURNING"
  3792. )
  3793. if isinstance(column, elements.Label):
  3794. if col_expr is not column:
  3795. result_expr = _CompileLabel(
  3796. col_expr, column.name, alt_names=(column.element,)
  3797. )
  3798. else:
  3799. result_expr = col_expr
  3800. elif name:
  3801. # here, _columns_plus_names has determined there's an explicit
  3802. # label name we need to use. this is the default for
  3803. # tablenames_plus_columnnames as well as when columns are being
  3804. # deduplicated on name
  3805. assert (
  3806. proxy_name is not None
  3807. ), "proxy_name is required if 'name' is passed"
  3808. result_expr = _CompileLabel(
  3809. col_expr,
  3810. name,
  3811. alt_names=(
  3812. proxy_name,
  3813. # this is a hack to allow legacy result column lookups
  3814. # to work as they did before; this goes away in 2.0.
  3815. # TODO: this only seems to be tested indirectly
  3816. # via test/orm/test_deprecations.py. should be a
  3817. # resultset test for this
  3818. column._tq_label,
  3819. ),
  3820. )
  3821. else:
  3822. # determine here whether this column should be rendered in
  3823. # a labelled context or not, as we were given no required label
  3824. # name from the caller. Here we apply heuristics based on the kind
  3825. # of SQL expression involved.
  3826. if col_expr is not column:
  3827. # type-specific expression wrapping the given column,
  3828. # so we render a label
  3829. render_with_label = True
  3830. elif isinstance(column, elements.ColumnClause):
  3831. # table-bound column, we render its name as a label if we are
  3832. # inside of a subquery only
  3833. render_with_label = (
  3834. asfrom
  3835. and not column.is_literal
  3836. and column.table is not None
  3837. )
  3838. elif isinstance(column, elements.TextClause):
  3839. render_with_label = False
  3840. elif isinstance(column, elements.UnaryExpression):
  3841. # unary expression. notes added as of #12681
  3842. #
  3843. # By convention, the visit_unary() method
  3844. # itself does not add an entry to the result map, and relies
  3845. # upon either the inner expression creating a result map
  3846. # entry, or if not, by creating a label here that produces
  3847. # the result map entry. Where that happens is based on whether
  3848. # or not the element immediately inside the unary is a
  3849. # NamedColumn subclass or not.
  3850. #
  3851. # Now, this also impacts how the SELECT is written; if
  3852. # we decide to generate a label here, we get the usual
  3853. # "~(x+y) AS anon_1" thing in the columns clause. If we
  3854. # don't, we don't get an AS at all, we get like
  3855. # "~table.column".
  3856. #
  3857. # But here is the important thing as of modernish (like 1.4)
  3858. # versions of SQLAlchemy - **whether or not the AS <label>
  3859. # is present in the statement is not actually important**.
  3860. # We target result columns **positionally** for a fully
  3861. # compiled ``Select()`` object; before 1.4 we needed those
  3862. # labels to match in cursor.description etc etc but now it
  3863. # really doesn't matter.
  3864. # So really, we could set render_with_label True in all cases.
  3865. # Or we could just have visit_unary() populate the result map
  3866. # in all cases.
  3867. #
  3868. # What we're doing here is strictly trying to not rock the
  3869. # boat too much with when we do/don't render "AS label";
  3870. # labels being present helps in the edge cases that we
  3871. # "fall back" to named cursor.description matching, labels
  3872. # not being present for columns keeps us from having awkward
  3873. # phrases like "SELECT DISTINCT table.x AS x".
  3874. render_with_label = (
  3875. (
  3876. # exception case to detect if we render "not boolean"
  3877. # as "not <col>" for native boolean or "<col> = 1"
  3878. # for non-native boolean. this is controlled by
  3879. # visit_is_<true|false>_unary_operator
  3880. column.operator
  3881. in (operators.is_false, operators.is_true)
  3882. and not self.dialect.supports_native_boolean
  3883. )
  3884. or column._wraps_unnamed_column()
  3885. or asfrom
  3886. )
  3887. elif (
  3888. # general class of expressions that don't have a SQL-column
  3889. # addressible name. includes scalar selects, bind parameters,
  3890. # SQL functions, others
  3891. not isinstance(column, elements.NamedColumn)
  3892. # deeper check that indicates there's no natural "name" to
  3893. # this element, which accommodates for custom SQL constructs
  3894. # that might have a ".name" attribute (but aren't SQL
  3895. # functions) but are not implementing this more recently added
  3896. # base class. in theory the "NamedColumn" check should be
  3897. # enough, however here we seek to maintain legacy behaviors
  3898. # as well.
  3899. and column._non_anon_label is None
  3900. ):
  3901. render_with_label = True
  3902. else:
  3903. render_with_label = False
  3904. if render_with_label:
  3905. if not fallback_label_name:
  3906. # used by the RETURNING case right now. we generate it
  3907. # here as 3rd party dialects may be referring to
  3908. # _label_select_column method directly instead of the
  3909. # just-added _label_returning_column method
  3910. assert not column_is_repeated
  3911. fallback_label_name = column._anon_name_label
  3912. fallback_label_name = (
  3913. elements._truncated_label(fallback_label_name)
  3914. if not isinstance(
  3915. fallback_label_name, elements._truncated_label
  3916. )
  3917. else fallback_label_name
  3918. )
  3919. result_expr = _CompileLabel(
  3920. col_expr, fallback_label_name, alt_names=(proxy_name,)
  3921. )
  3922. else:
  3923. result_expr = col_expr
  3924. column_clause_args.update(
  3925. within_columns_clause=within_columns_clause,
  3926. add_to_result_map=add_to_result_map,
  3927. include_table=include_table,
  3928. )
  3929. return result_expr._compiler_dispatch(self, **column_clause_args)
  3930. def format_from_hint_text(self, sqltext, table, hint, iscrud):
  3931. hinttext = self.get_from_hint_text(table, hint)
  3932. if hinttext:
  3933. sqltext += " " + hinttext
  3934. return sqltext
  3935. def get_select_hint_text(self, byfroms):
  3936. return None
  3937. def get_from_hint_text(
  3938. self, table: FromClause, text: Optional[str]
  3939. ) -> Optional[str]:
  3940. return None
  3941. def get_crud_hint_text(self, table, text):
  3942. return None
  3943. def get_statement_hint_text(self, hint_texts):
  3944. return " ".join(hint_texts)
  3945. _default_stack_entry: _CompilerStackEntry
  3946. if not typing.TYPE_CHECKING:
  3947. _default_stack_entry = util.immutabledict(
  3948. [("correlate_froms", frozenset()), ("asfrom_froms", frozenset())]
  3949. )
  3950. def _display_froms_for_select(
  3951. self, select_stmt, asfrom, lateral=False, **kw
  3952. ):
  3953. # utility method to help external dialects
  3954. # get the correct from list for a select.
  3955. # specifically the oracle dialect needs this feature
  3956. # right now.
  3957. toplevel = not self.stack
  3958. entry = self._default_stack_entry if toplevel else self.stack[-1]
  3959. compile_state = select_stmt._compile_state_factory(select_stmt, self)
  3960. correlate_froms = entry["correlate_froms"]
  3961. asfrom_froms = entry["asfrom_froms"]
  3962. if asfrom and not lateral:
  3963. froms = compile_state._get_display_froms(
  3964. explicit_correlate_froms=correlate_froms.difference(
  3965. asfrom_froms
  3966. ),
  3967. implicit_correlate_froms=(),
  3968. )
  3969. else:
  3970. froms = compile_state._get_display_froms(
  3971. explicit_correlate_froms=correlate_froms,
  3972. implicit_correlate_froms=asfrom_froms,
  3973. )
  3974. return froms
  3975. translate_select_structure: Any = None
  3976. """if not ``None``, should be a callable which accepts ``(select_stmt,
  3977. **kw)`` and returns a select object. this is used for structural changes
  3978. mostly to accommodate for LIMIT/OFFSET schemes
  3979. """
  3980. def visit_select(
  3981. self,
  3982. select_stmt,
  3983. asfrom=False,
  3984. insert_into=False,
  3985. fromhints=None,
  3986. compound_index=None,
  3987. select_wraps_for=None,
  3988. lateral=False,
  3989. from_linter=None,
  3990. **kwargs,
  3991. ):
  3992. assert select_wraps_for is None, (
  3993. "SQLAlchemy 1.4 requires use of "
  3994. "the translate_select_structure hook for structural "
  3995. "translations of SELECT objects"
  3996. )
  3997. # initial setup of SELECT. the compile_state_factory may now
  3998. # be creating a totally different SELECT from the one that was
  3999. # passed in. for ORM use this will convert from an ORM-state
  4000. # SELECT to a regular "Core" SELECT. other composed operations
  4001. # such as computation of joins will be performed.
  4002. kwargs["within_columns_clause"] = False
  4003. compile_state = select_stmt._compile_state_factory(
  4004. select_stmt, self, **kwargs
  4005. )
  4006. kwargs["ambiguous_table_name_map"] = (
  4007. compile_state._ambiguous_table_name_map
  4008. )
  4009. select_stmt = compile_state.statement
  4010. toplevel = not self.stack
  4011. if toplevel and not self.compile_state:
  4012. self.compile_state = compile_state
  4013. is_embedded_select = compound_index is not None or insert_into
  4014. # translate step for Oracle, SQL Server which often need to
  4015. # restructure the SELECT to allow for LIMIT/OFFSET and possibly
  4016. # other conditions
  4017. if self.translate_select_structure:
  4018. new_select_stmt = self.translate_select_structure(
  4019. select_stmt, asfrom=asfrom, **kwargs
  4020. )
  4021. # if SELECT was restructured, maintain a link to the originals
  4022. # and assemble a new compile state
  4023. if new_select_stmt is not select_stmt:
  4024. compile_state_wraps_for = compile_state
  4025. select_wraps_for = select_stmt
  4026. select_stmt = new_select_stmt
  4027. compile_state = select_stmt._compile_state_factory(
  4028. select_stmt, self, **kwargs
  4029. )
  4030. select_stmt = compile_state.statement
  4031. entry = self._default_stack_entry if toplevel else self.stack[-1]
  4032. populate_result_map = need_column_expressions = (
  4033. toplevel
  4034. or entry.get("need_result_map_for_compound", False)
  4035. or entry.get("need_result_map_for_nested", False)
  4036. )
  4037. # indicates there is a CompoundSelect in play and we are not the
  4038. # first select
  4039. if compound_index:
  4040. populate_result_map = False
  4041. # this was first proposed as part of #3372; however, it is not
  4042. # reached in current tests and could possibly be an assertion
  4043. # instead.
  4044. if not populate_result_map and "add_to_result_map" in kwargs:
  4045. del kwargs["add_to_result_map"]
  4046. froms = self._setup_select_stack(
  4047. select_stmt, compile_state, entry, asfrom, lateral, compound_index
  4048. )
  4049. column_clause_args = kwargs.copy()
  4050. column_clause_args.update(
  4051. {"within_label_clause": False, "within_columns_clause": False}
  4052. )
  4053. text = "SELECT " # we're off to a good start !
  4054. if select_stmt._hints:
  4055. hint_text, byfrom = self._setup_select_hints(select_stmt)
  4056. if hint_text:
  4057. text += hint_text + " "
  4058. else:
  4059. byfrom = None
  4060. if select_stmt._independent_ctes:
  4061. self._dispatch_independent_ctes(select_stmt, kwargs)
  4062. if select_stmt._prefixes:
  4063. text += self._generate_prefixes(
  4064. select_stmt, select_stmt._prefixes, **kwargs
  4065. )
  4066. text += self.get_select_precolumns(select_stmt, **kwargs)
  4067. # the actual list of columns to print in the SELECT column list.
  4068. inner_columns = [
  4069. c
  4070. for c in [
  4071. self._label_select_column(
  4072. select_stmt,
  4073. column,
  4074. populate_result_map,
  4075. asfrom,
  4076. column_clause_args,
  4077. name=name,
  4078. proxy_name=proxy_name,
  4079. fallback_label_name=fallback_label_name,
  4080. column_is_repeated=repeated,
  4081. need_column_expressions=need_column_expressions,
  4082. )
  4083. for (
  4084. name,
  4085. proxy_name,
  4086. fallback_label_name,
  4087. column,
  4088. repeated,
  4089. ) in compile_state.columns_plus_names
  4090. ]
  4091. if c is not None
  4092. ]
  4093. if populate_result_map and select_wraps_for is not None:
  4094. # if this select was generated from translate_select,
  4095. # rewrite the targeted columns in the result map
  4096. translate = dict(
  4097. zip(
  4098. [
  4099. name
  4100. for (
  4101. key,
  4102. proxy_name,
  4103. fallback_label_name,
  4104. name,
  4105. repeated,
  4106. ) in compile_state.columns_plus_names
  4107. ],
  4108. [
  4109. name
  4110. for (
  4111. key,
  4112. proxy_name,
  4113. fallback_label_name,
  4114. name,
  4115. repeated,
  4116. ) in compile_state_wraps_for.columns_plus_names
  4117. ],
  4118. )
  4119. )
  4120. self._result_columns = [
  4121. ResultColumnsEntry(
  4122. key, name, tuple(translate.get(o, o) for o in obj), type_
  4123. )
  4124. for key, name, obj, type_ in self._result_columns
  4125. ]
  4126. text = self._compose_select_body(
  4127. text,
  4128. select_stmt,
  4129. compile_state,
  4130. inner_columns,
  4131. froms,
  4132. byfrom,
  4133. toplevel,
  4134. kwargs,
  4135. )
  4136. if select_stmt._statement_hints:
  4137. per_dialect = [
  4138. ht
  4139. for (dialect_name, ht) in select_stmt._statement_hints
  4140. if dialect_name in ("*", self.dialect.name)
  4141. ]
  4142. if per_dialect:
  4143. text += " " + self.get_statement_hint_text(per_dialect)
  4144. # In compound query, CTEs are shared at the compound level
  4145. if self.ctes and (not is_embedded_select or toplevel):
  4146. nesting_level = len(self.stack) if not toplevel else None
  4147. text = self._render_cte_clause(nesting_level=nesting_level) + text
  4148. if select_stmt._suffixes:
  4149. text += " " + self._generate_prefixes(
  4150. select_stmt, select_stmt._suffixes, **kwargs
  4151. )
  4152. self.stack.pop(-1)
  4153. return text
  4154. def _setup_select_hints(
  4155. self, select: Select[Any]
  4156. ) -> Tuple[str, _FromHintsType]:
  4157. byfrom = {
  4158. from_: hinttext
  4159. % {"name": from_._compiler_dispatch(self, ashint=True)}
  4160. for (from_, dialect), hinttext in select._hints.items()
  4161. if dialect in ("*", self.dialect.name)
  4162. }
  4163. hint_text = self.get_select_hint_text(byfrom)
  4164. return hint_text, byfrom
  4165. def _setup_select_stack(
  4166. self, select, compile_state, entry, asfrom, lateral, compound_index
  4167. ):
  4168. correlate_froms = entry["correlate_froms"]
  4169. asfrom_froms = entry["asfrom_froms"]
  4170. if compound_index == 0:
  4171. entry["select_0"] = select
  4172. elif compound_index:
  4173. select_0 = entry["select_0"]
  4174. numcols = len(select_0._all_selected_columns)
  4175. if len(compile_state.columns_plus_names) != numcols:
  4176. raise exc.CompileError(
  4177. "All selectables passed to "
  4178. "CompoundSelect must have identical numbers of "
  4179. "columns; select #%d has %d columns, select "
  4180. "#%d has %d"
  4181. % (
  4182. 1,
  4183. numcols,
  4184. compound_index + 1,
  4185. len(select._all_selected_columns),
  4186. )
  4187. )
  4188. if asfrom and not lateral:
  4189. froms = compile_state._get_display_froms(
  4190. explicit_correlate_froms=correlate_froms.difference(
  4191. asfrom_froms
  4192. ),
  4193. implicit_correlate_froms=(),
  4194. )
  4195. else:
  4196. froms = compile_state._get_display_froms(
  4197. explicit_correlate_froms=correlate_froms,
  4198. implicit_correlate_froms=asfrom_froms,
  4199. )
  4200. new_correlate_froms = set(_from_objects(*froms))
  4201. all_correlate_froms = new_correlate_froms.union(correlate_froms)
  4202. new_entry: _CompilerStackEntry = {
  4203. "asfrom_froms": new_correlate_froms,
  4204. "correlate_froms": all_correlate_froms,
  4205. "selectable": select,
  4206. "compile_state": compile_state,
  4207. }
  4208. self.stack.append(new_entry)
  4209. return froms
  4210. def _compose_select_body(
  4211. self,
  4212. text,
  4213. select,
  4214. compile_state,
  4215. inner_columns,
  4216. froms,
  4217. byfrom,
  4218. toplevel,
  4219. kwargs,
  4220. ):
  4221. text += ", ".join(inner_columns)
  4222. if self.linting & COLLECT_CARTESIAN_PRODUCTS:
  4223. from_linter = FromLinter({}, set())
  4224. warn_linting = self.linting & WARN_LINTING
  4225. if toplevel:
  4226. self.from_linter = from_linter
  4227. else:
  4228. from_linter = None
  4229. warn_linting = False
  4230. # adjust the whitespace for no inner columns, part of #9440,
  4231. # so that a no-col SELECT comes out as "SELECT WHERE..." or
  4232. # "SELECT FROM ...".
  4233. # while it would be better to have built the SELECT starting string
  4234. # without trailing whitespace first, then add whitespace only if inner
  4235. # cols were present, this breaks compatibility with various custom
  4236. # compilation schemes that are currently being tested.
  4237. if not inner_columns:
  4238. text = text.rstrip()
  4239. if froms:
  4240. text += " \nFROM "
  4241. if select._hints:
  4242. text += ", ".join(
  4243. [
  4244. f._compiler_dispatch(
  4245. self,
  4246. asfrom=True,
  4247. fromhints=byfrom,
  4248. from_linter=from_linter,
  4249. **kwargs,
  4250. )
  4251. for f in froms
  4252. ]
  4253. )
  4254. else:
  4255. text += ", ".join(
  4256. [
  4257. f._compiler_dispatch(
  4258. self,
  4259. asfrom=True,
  4260. from_linter=from_linter,
  4261. **kwargs,
  4262. )
  4263. for f in froms
  4264. ]
  4265. )
  4266. else:
  4267. text += self.default_from()
  4268. if select._where_criteria:
  4269. t = self._generate_delimited_and_list(
  4270. select._where_criteria, from_linter=from_linter, **kwargs
  4271. )
  4272. if t:
  4273. text += " \nWHERE " + t
  4274. if warn_linting:
  4275. assert from_linter is not None
  4276. from_linter.warn()
  4277. if select._group_by_clauses:
  4278. text += self.group_by_clause(select, **kwargs)
  4279. if select._having_criteria:
  4280. t = self._generate_delimited_and_list(
  4281. select._having_criteria, **kwargs
  4282. )
  4283. if t:
  4284. text += " \nHAVING " + t
  4285. if select._order_by_clauses:
  4286. text += self.order_by_clause(select, **kwargs)
  4287. if select._has_row_limiting_clause:
  4288. text += self._row_limit_clause(select, **kwargs)
  4289. if select._for_update_arg is not None:
  4290. text += self.for_update_clause(select, **kwargs)
  4291. return text
  4292. def _generate_prefixes(self, stmt, prefixes, **kw):
  4293. clause = " ".join(
  4294. prefix._compiler_dispatch(self, **kw)
  4295. for prefix, dialect_name in prefixes
  4296. if dialect_name in (None, "*") or dialect_name == self.dialect.name
  4297. )
  4298. if clause:
  4299. clause += " "
  4300. return clause
  4301. def _render_cte_clause(
  4302. self,
  4303. nesting_level=None,
  4304. include_following_stack=False,
  4305. ):
  4306. """
  4307. include_following_stack
  4308. Also render the nesting CTEs on the next stack. Useful for
  4309. SQL structures like UNION or INSERT that can wrap SELECT
  4310. statements containing nesting CTEs.
  4311. """
  4312. if not self.ctes:
  4313. return ""
  4314. ctes: MutableMapping[CTE, str]
  4315. if nesting_level and nesting_level > 1:
  4316. ctes = util.OrderedDict()
  4317. for cte in list(self.ctes.keys()):
  4318. cte_level, cte_name, cte_opts = self.level_name_by_cte[
  4319. cte._get_reference_cte()
  4320. ]
  4321. nesting = cte.nesting or cte_opts.nesting
  4322. is_rendered_level = cte_level == nesting_level or (
  4323. include_following_stack and cte_level == nesting_level + 1
  4324. )
  4325. if not (nesting and is_rendered_level):
  4326. continue
  4327. ctes[cte] = self.ctes[cte]
  4328. else:
  4329. ctes = self.ctes
  4330. if not ctes:
  4331. return ""
  4332. ctes_recursive = any([cte.recursive for cte in ctes])
  4333. cte_text = self.get_cte_preamble(ctes_recursive) + " "
  4334. cte_text += ", \n".join([txt for txt in ctes.values()])
  4335. cte_text += "\n "
  4336. if nesting_level and nesting_level > 1:
  4337. for cte in list(ctes.keys()):
  4338. cte_level, cte_name, cte_opts = self.level_name_by_cte[
  4339. cte._get_reference_cte()
  4340. ]
  4341. del self.ctes[cte]
  4342. del self.ctes_by_level_name[(cte_level, cte_name)]
  4343. del self.level_name_by_cte[cte._get_reference_cte()]
  4344. return cte_text
  4345. def get_cte_preamble(self, recursive):
  4346. if recursive:
  4347. return "WITH RECURSIVE"
  4348. else:
  4349. return "WITH"
  4350. def get_select_precolumns(self, select: Select[Any], **kw: Any) -> str:
  4351. """Called when building a ``SELECT`` statement, position is just
  4352. before column list.
  4353. """
  4354. if select._distinct_on:
  4355. util.warn_deprecated(
  4356. "DISTINCT ON is currently supported only by the PostgreSQL "
  4357. "dialect. Use of DISTINCT ON for other backends is currently "
  4358. "silently ignored, however this usage is deprecated, and will "
  4359. "raise CompileError in a future release for all backends "
  4360. "that do not support this syntax.",
  4361. version="1.4",
  4362. )
  4363. return "DISTINCT " if select._distinct else ""
  4364. def group_by_clause(self, select, **kw):
  4365. """allow dialects to customize how GROUP BY is rendered."""
  4366. group_by = self._generate_delimited_list(
  4367. select._group_by_clauses, OPERATORS[operators.comma_op], **kw
  4368. )
  4369. if group_by:
  4370. return " GROUP BY " + group_by
  4371. else:
  4372. return ""
  4373. def order_by_clause(self, select, **kw):
  4374. """allow dialects to customize how ORDER BY is rendered."""
  4375. order_by = self._generate_delimited_list(
  4376. select._order_by_clauses, OPERATORS[operators.comma_op], **kw
  4377. )
  4378. if order_by:
  4379. return " ORDER BY " + order_by
  4380. else:
  4381. return ""
  4382. def for_update_clause(self, select, **kw):
  4383. return " FOR UPDATE"
  4384. def returning_clause(
  4385. self,
  4386. stmt: UpdateBase,
  4387. returning_cols: Sequence[_ColumnsClauseElement],
  4388. *,
  4389. populate_result_map: bool,
  4390. **kw: Any,
  4391. ) -> str:
  4392. columns = [
  4393. self._label_returning_column(
  4394. stmt,
  4395. column,
  4396. populate_result_map,
  4397. fallback_label_name=fallback_label_name,
  4398. column_is_repeated=repeated,
  4399. name=name,
  4400. proxy_name=proxy_name,
  4401. **kw,
  4402. )
  4403. for (
  4404. name,
  4405. proxy_name,
  4406. fallback_label_name,
  4407. column,
  4408. repeated,
  4409. ) in stmt._generate_columns_plus_names(
  4410. True, cols=base._select_iterables(returning_cols)
  4411. )
  4412. ]
  4413. return "RETURNING " + ", ".join(columns)
  4414. def limit_clause(self, select, **kw):
  4415. text = ""
  4416. if select._limit_clause is not None:
  4417. text += "\n LIMIT " + self.process(select._limit_clause, **kw)
  4418. if select._offset_clause is not None:
  4419. if select._limit_clause is None:
  4420. text += "\n LIMIT -1"
  4421. text += " OFFSET " + self.process(select._offset_clause, **kw)
  4422. return text
  4423. def fetch_clause(
  4424. self,
  4425. select,
  4426. fetch_clause=None,
  4427. require_offset=False,
  4428. use_literal_execute_for_simple_int=False,
  4429. **kw,
  4430. ):
  4431. if fetch_clause is None:
  4432. fetch_clause = select._fetch_clause
  4433. fetch_clause_options = select._fetch_clause_options
  4434. else:
  4435. fetch_clause_options = {"percent": False, "with_ties": False}
  4436. text = ""
  4437. if select._offset_clause is not None:
  4438. offset_clause = select._offset_clause
  4439. if (
  4440. use_literal_execute_for_simple_int
  4441. and select._simple_int_clause(offset_clause)
  4442. ):
  4443. offset_clause = offset_clause.render_literal_execute()
  4444. offset_str = self.process(offset_clause, **kw)
  4445. text += "\n OFFSET %s ROWS" % offset_str
  4446. elif require_offset:
  4447. text += "\n OFFSET 0 ROWS"
  4448. if fetch_clause is not None:
  4449. if (
  4450. use_literal_execute_for_simple_int
  4451. and select._simple_int_clause(fetch_clause)
  4452. ):
  4453. fetch_clause = fetch_clause.render_literal_execute()
  4454. text += "\n FETCH FIRST %s%s ROWS %s" % (
  4455. self.process(fetch_clause, **kw),
  4456. " PERCENT" if fetch_clause_options["percent"] else "",
  4457. "WITH TIES" if fetch_clause_options["with_ties"] else "ONLY",
  4458. )
  4459. return text
  4460. def visit_table(
  4461. self,
  4462. table,
  4463. asfrom=False,
  4464. iscrud=False,
  4465. ashint=False,
  4466. fromhints=None,
  4467. use_schema=True,
  4468. from_linter=None,
  4469. ambiguous_table_name_map=None,
  4470. enclosing_alias=None,
  4471. **kwargs,
  4472. ):
  4473. if from_linter:
  4474. from_linter.froms[table] = table.fullname
  4475. if asfrom or ashint:
  4476. effective_schema = self.preparer.schema_for_object(table)
  4477. if use_schema and effective_schema:
  4478. ret = (
  4479. self.preparer.quote_schema(effective_schema)
  4480. + "."
  4481. + self.preparer.quote(table.name)
  4482. )
  4483. else:
  4484. ret = self.preparer.quote(table.name)
  4485. if (
  4486. (
  4487. enclosing_alias is None
  4488. or enclosing_alias.element is not table
  4489. )
  4490. and not effective_schema
  4491. and ambiguous_table_name_map
  4492. and table.name in ambiguous_table_name_map
  4493. ):
  4494. anon_name = self._truncated_identifier(
  4495. "alias", ambiguous_table_name_map[table.name]
  4496. )
  4497. ret = ret + self.get_render_as_alias_suffix(
  4498. self.preparer.format_alias(None, anon_name)
  4499. )
  4500. if fromhints and table in fromhints:
  4501. ret = self.format_from_hint_text(
  4502. ret, table, fromhints[table], iscrud
  4503. )
  4504. return ret
  4505. else:
  4506. return ""
  4507. def visit_join(self, join, asfrom=False, from_linter=None, **kwargs):
  4508. if from_linter:
  4509. from_linter.edges.update(
  4510. itertools.product(
  4511. _de_clone(join.left._from_objects),
  4512. _de_clone(join.right._from_objects),
  4513. )
  4514. )
  4515. if join.full:
  4516. join_type = " FULL OUTER JOIN "
  4517. elif join.isouter:
  4518. join_type = " LEFT OUTER JOIN "
  4519. else:
  4520. join_type = " JOIN "
  4521. return (
  4522. join.left._compiler_dispatch(
  4523. self, asfrom=True, from_linter=from_linter, **kwargs
  4524. )
  4525. + join_type
  4526. + join.right._compiler_dispatch(
  4527. self, asfrom=True, from_linter=from_linter, **kwargs
  4528. )
  4529. + " ON "
  4530. # TODO: likely need asfrom=True here?
  4531. + join.onclause._compiler_dispatch(
  4532. self, from_linter=from_linter, **kwargs
  4533. )
  4534. )
  4535. def _setup_crud_hints(self, stmt, table_text):
  4536. dialect_hints = {
  4537. table: hint_text
  4538. for (table, dialect), hint_text in stmt._hints.items()
  4539. if dialect in ("*", self.dialect.name)
  4540. }
  4541. if stmt.table in dialect_hints:
  4542. table_text = self.format_from_hint_text(
  4543. table_text, stmt.table, dialect_hints[stmt.table], True
  4544. )
  4545. return dialect_hints, table_text
  4546. # within the realm of "insertmanyvalues sentinel columns",
  4547. # these lookups match different kinds of Column() configurations
  4548. # to specific backend capabilities. they are broken into two
  4549. # lookups, one for autoincrement columns and the other for non
  4550. # autoincrement columns
  4551. _sentinel_col_non_autoinc_lookup = util.immutabledict(
  4552. {
  4553. _SentinelDefaultCharacterization.CLIENTSIDE: (
  4554. InsertmanyvaluesSentinelOpts._SUPPORTED_OR_NOT
  4555. ),
  4556. _SentinelDefaultCharacterization.SENTINEL_DEFAULT: (
  4557. InsertmanyvaluesSentinelOpts._SUPPORTED_OR_NOT
  4558. ),
  4559. _SentinelDefaultCharacterization.NONE: (
  4560. InsertmanyvaluesSentinelOpts._SUPPORTED_OR_NOT
  4561. ),
  4562. _SentinelDefaultCharacterization.IDENTITY: (
  4563. InsertmanyvaluesSentinelOpts.IDENTITY
  4564. ),
  4565. _SentinelDefaultCharacterization.SEQUENCE: (
  4566. InsertmanyvaluesSentinelOpts.SEQUENCE
  4567. ),
  4568. }
  4569. )
  4570. _sentinel_col_autoinc_lookup = _sentinel_col_non_autoinc_lookup.union(
  4571. {
  4572. _SentinelDefaultCharacterization.NONE: (
  4573. InsertmanyvaluesSentinelOpts.AUTOINCREMENT
  4574. ),
  4575. }
  4576. )
  4577. def _get_sentinel_column_for_table(
  4578. self, table: Table
  4579. ) -> Optional[Sequence[Column[Any]]]:
  4580. """given a :class:`.Table`, return a usable sentinel column or
  4581. columns for this dialect if any.
  4582. Return None if no sentinel columns could be identified, or raise an
  4583. error if a column was marked as a sentinel explicitly but isn't
  4584. compatible with this dialect.
  4585. """
  4586. sentinel_opts = self.dialect.insertmanyvalues_implicit_sentinel
  4587. sentinel_characteristics = table._sentinel_column_characteristics
  4588. sent_cols = sentinel_characteristics.columns
  4589. if sent_cols is None:
  4590. return None
  4591. if sentinel_characteristics.is_autoinc:
  4592. bitmask = self._sentinel_col_autoinc_lookup.get(
  4593. sentinel_characteristics.default_characterization, 0
  4594. )
  4595. else:
  4596. bitmask = self._sentinel_col_non_autoinc_lookup.get(
  4597. sentinel_characteristics.default_characterization, 0
  4598. )
  4599. if sentinel_opts & bitmask:
  4600. return sent_cols
  4601. if sentinel_characteristics.is_explicit:
  4602. # a column was explicitly marked as insert_sentinel=True,
  4603. # however it is not compatible with this dialect. they should
  4604. # not indicate this column as a sentinel if they need to include
  4605. # this dialect.
  4606. # TODO: do we want non-primary key explicit sentinel cols
  4607. # that can gracefully degrade for some backends?
  4608. # insert_sentinel="degrade" perhaps. not for the initial release.
  4609. # I am hoping people are generally not dealing with this sentinel
  4610. # business at all.
  4611. # if is_explicit is True, there will be only one sentinel column.
  4612. raise exc.InvalidRequestError(
  4613. f"Column {sent_cols[0]} can't be explicitly "
  4614. "marked as a sentinel column when using the "
  4615. f"{self.dialect.name} dialect, as the "
  4616. "particular type of default generation on this column is "
  4617. "not currently compatible with this dialect's specific "
  4618. f"INSERT..RETURNING syntax which can receive the "
  4619. "server-generated value in "
  4620. "a deterministic way. To remove this error, remove "
  4621. "insert_sentinel=True from primary key autoincrement "
  4622. "columns; these columns are automatically used as "
  4623. "sentinels for supported dialects in any case."
  4624. )
  4625. return None
  4626. def _deliver_insertmanyvalues_batches(
  4627. self,
  4628. statement: str,
  4629. parameters: _DBAPIMultiExecuteParams,
  4630. compiled_parameters: List[_MutableCoreSingleExecuteParams],
  4631. generic_setinputsizes: Optional[_GenericSetInputSizesType],
  4632. batch_size: int,
  4633. sort_by_parameter_order: bool,
  4634. schema_translate_map: Optional[SchemaTranslateMapType],
  4635. ) -> Iterator[_InsertManyValuesBatch]:
  4636. imv = self._insertmanyvalues
  4637. assert imv is not None
  4638. if not imv.sentinel_param_keys:
  4639. _sentinel_from_params = None
  4640. else:
  4641. _sentinel_from_params = operator.itemgetter(
  4642. *imv.sentinel_param_keys
  4643. )
  4644. lenparams = len(parameters)
  4645. if imv.is_default_expr and not self.dialect.supports_default_metavalue:
  4646. # backend doesn't support
  4647. # INSERT INTO table (pk_col) VALUES (DEFAULT), (DEFAULT), ...
  4648. # at the moment this is basically SQL Server due to
  4649. # not being able to use DEFAULT for identity column
  4650. # just yield out that many single statements! still
  4651. # faster than a whole connection.execute() call ;)
  4652. #
  4653. # note we still are taking advantage of the fact that we know
  4654. # we are using RETURNING. The generalized approach of fetching
  4655. # cursor.lastrowid etc. still goes through the more heavyweight
  4656. # "ExecutionContext per statement" system as it isn't usable
  4657. # as a generic "RETURNING" approach
  4658. use_row_at_a_time = True
  4659. downgraded = False
  4660. elif not self.dialect.supports_multivalues_insert or (
  4661. sort_by_parameter_order
  4662. and self._result_columns
  4663. and (imv.sentinel_columns is None or imv.includes_upsert_behaviors)
  4664. ):
  4665. # deterministic order was requested and the compiler could
  4666. # not organize sentinel columns for this dialect/statement.
  4667. # use row at a time
  4668. use_row_at_a_time = True
  4669. downgraded = True
  4670. else:
  4671. use_row_at_a_time = False
  4672. downgraded = False
  4673. if use_row_at_a_time:
  4674. for batchnum, (param, compiled_param) in enumerate(
  4675. cast(
  4676. "Sequence[Tuple[_DBAPISingleExecuteParams, _MutableCoreSingleExecuteParams]]", # noqa: E501
  4677. zip(parameters, compiled_parameters),
  4678. ),
  4679. 1,
  4680. ):
  4681. yield _InsertManyValuesBatch(
  4682. statement,
  4683. param,
  4684. generic_setinputsizes,
  4685. [param],
  4686. (
  4687. [_sentinel_from_params(compiled_param)]
  4688. if _sentinel_from_params
  4689. else []
  4690. ),
  4691. 1,
  4692. batchnum,
  4693. lenparams,
  4694. sort_by_parameter_order,
  4695. downgraded,
  4696. )
  4697. return
  4698. if schema_translate_map:
  4699. rst = functools.partial(
  4700. self.preparer._render_schema_translates,
  4701. schema_translate_map=schema_translate_map,
  4702. )
  4703. else:
  4704. rst = None
  4705. imv_single_values_expr = imv.single_values_expr
  4706. if rst:
  4707. imv_single_values_expr = rst(imv_single_values_expr)
  4708. executemany_values = f"({imv_single_values_expr})"
  4709. statement = statement.replace(executemany_values, "__EXECMANY_TOKEN__")
  4710. # Use optional insertmanyvalues_max_parameters
  4711. # to further shrink the batch size so that there are no more than
  4712. # insertmanyvalues_max_parameters params.
  4713. # Currently used by SQL Server, which limits statements to 2100 bound
  4714. # parameters (actually 2099).
  4715. max_params = self.dialect.insertmanyvalues_max_parameters
  4716. if max_params:
  4717. total_num_of_params = len(self.bind_names)
  4718. num_params_per_batch = len(imv.insert_crud_params)
  4719. num_params_outside_of_batch = (
  4720. total_num_of_params - num_params_per_batch
  4721. )
  4722. batch_size = min(
  4723. batch_size,
  4724. (
  4725. (max_params - num_params_outside_of_batch)
  4726. // num_params_per_batch
  4727. ),
  4728. )
  4729. batches = cast("List[Sequence[Any]]", list(parameters))
  4730. compiled_batches = cast(
  4731. "List[Sequence[Any]]", list(compiled_parameters)
  4732. )
  4733. processed_setinputsizes: Optional[_GenericSetInputSizesType] = None
  4734. batchnum = 1
  4735. total_batches = lenparams // batch_size + (
  4736. 1 if lenparams % batch_size else 0
  4737. )
  4738. insert_crud_params = imv.insert_crud_params
  4739. assert insert_crud_params is not None
  4740. if rst:
  4741. insert_crud_params = [
  4742. (col, key, rst(expr), st)
  4743. for col, key, expr, st in insert_crud_params
  4744. ]
  4745. escaped_bind_names: Mapping[str, str]
  4746. expand_pos_lower_index = expand_pos_upper_index = 0
  4747. if not self.positional:
  4748. if self.escaped_bind_names:
  4749. escaped_bind_names = self.escaped_bind_names
  4750. else:
  4751. escaped_bind_names = {}
  4752. all_keys = set(parameters[0])
  4753. def apply_placeholders(keys, formatted):
  4754. for key in keys:
  4755. key = escaped_bind_names.get(key, key)
  4756. formatted = formatted.replace(
  4757. self.bindtemplate % {"name": key},
  4758. self.bindtemplate
  4759. % {"name": f"{key}__EXECMANY_INDEX__"},
  4760. )
  4761. return formatted
  4762. if imv.embed_values_counter:
  4763. imv_values_counter = ", _IMV_VALUES_COUNTER"
  4764. else:
  4765. imv_values_counter = ""
  4766. formatted_values_clause = f"""({', '.join(
  4767. apply_placeholders(bind_keys, formatted)
  4768. for _, _, formatted, bind_keys in insert_crud_params
  4769. )}{imv_values_counter})"""
  4770. keys_to_replace = all_keys.intersection(
  4771. escaped_bind_names.get(key, key)
  4772. for _, _, _, bind_keys in insert_crud_params
  4773. for key in bind_keys
  4774. )
  4775. base_parameters = {
  4776. key: parameters[0][key]
  4777. for key in all_keys.difference(keys_to_replace)
  4778. }
  4779. executemany_values_w_comma = ""
  4780. else:
  4781. formatted_values_clause = ""
  4782. keys_to_replace = set()
  4783. base_parameters = {}
  4784. if imv.embed_values_counter:
  4785. executemany_values_w_comma = (
  4786. f"({imv_single_values_expr}, _IMV_VALUES_COUNTER), "
  4787. )
  4788. else:
  4789. executemany_values_w_comma = f"({imv_single_values_expr}), "
  4790. all_names_we_will_expand: Set[str] = set()
  4791. for elem in imv.insert_crud_params:
  4792. all_names_we_will_expand.update(elem[3])
  4793. # get the start and end position in a particular list
  4794. # of parameters where we will be doing the "expanding".
  4795. # statements can have params on either side or both sides,
  4796. # given RETURNING and CTEs
  4797. if all_names_we_will_expand:
  4798. positiontup = self.positiontup
  4799. assert positiontup is not None
  4800. all_expand_positions = {
  4801. idx
  4802. for idx, name in enumerate(positiontup)
  4803. if name in all_names_we_will_expand
  4804. }
  4805. expand_pos_lower_index = min(all_expand_positions)
  4806. expand_pos_upper_index = max(all_expand_positions) + 1
  4807. assert (
  4808. len(all_expand_positions)
  4809. == expand_pos_upper_index - expand_pos_lower_index
  4810. )
  4811. if self._numeric_binds:
  4812. escaped = re.escape(self._numeric_binds_identifier_char)
  4813. executemany_values_w_comma = re.sub(
  4814. rf"{escaped}\d+", "%s", executemany_values_w_comma
  4815. )
  4816. while batches:
  4817. batch = batches[0:batch_size]
  4818. compiled_batch = compiled_batches[0:batch_size]
  4819. batches[0:batch_size] = []
  4820. compiled_batches[0:batch_size] = []
  4821. if batches:
  4822. current_batch_size = batch_size
  4823. else:
  4824. current_batch_size = len(batch)
  4825. if generic_setinputsizes:
  4826. # if setinputsizes is present, expand this collection to
  4827. # suit the batch length as well
  4828. # currently this will be mssql+pyodbc for internal dialects
  4829. processed_setinputsizes = [
  4830. (new_key, len_, typ)
  4831. for new_key, len_, typ in (
  4832. (f"{key}_{index}", len_, typ)
  4833. for index in range(current_batch_size)
  4834. for key, len_, typ in generic_setinputsizes
  4835. )
  4836. ]
  4837. replaced_parameters: Any
  4838. if self.positional:
  4839. num_ins_params = imv.num_positional_params_counted
  4840. batch_iterator: Iterable[Sequence[Any]]
  4841. extra_params_left: Sequence[Any]
  4842. extra_params_right: Sequence[Any]
  4843. if num_ins_params == len(batch[0]):
  4844. extra_params_left = extra_params_right = ()
  4845. batch_iterator = batch
  4846. else:
  4847. extra_params_left = batch[0][:expand_pos_lower_index]
  4848. extra_params_right = batch[0][expand_pos_upper_index:]
  4849. batch_iterator = (
  4850. b[expand_pos_lower_index:expand_pos_upper_index]
  4851. for b in batch
  4852. )
  4853. if imv.embed_values_counter:
  4854. expanded_values_string = (
  4855. "".join(
  4856. executemany_values_w_comma.replace(
  4857. "_IMV_VALUES_COUNTER", str(i)
  4858. )
  4859. for i, _ in enumerate(batch)
  4860. )
  4861. )[:-2]
  4862. else:
  4863. expanded_values_string = (
  4864. (executemany_values_w_comma * current_batch_size)
  4865. )[:-2]
  4866. if self._numeric_binds and num_ins_params > 0:
  4867. # numeric will always number the parameters inside of
  4868. # VALUES (and thus order self.positiontup) to be higher
  4869. # than non-VALUES parameters, no matter where in the
  4870. # statement those non-VALUES parameters appear (this is
  4871. # ensured in _process_numeric by numbering first all
  4872. # params that are not in _values_bindparam)
  4873. # therefore all extra params are always
  4874. # on the left side and numbered lower than the VALUES
  4875. # parameters
  4876. assert not extra_params_right
  4877. start = expand_pos_lower_index + 1
  4878. end = num_ins_params * (current_batch_size) + start
  4879. # need to format here, since statement may contain
  4880. # unescaped %, while values_string contains just (%s, %s)
  4881. positions = tuple(
  4882. f"{self._numeric_binds_identifier_char}{i}"
  4883. for i in range(start, end)
  4884. )
  4885. expanded_values_string = expanded_values_string % positions
  4886. replaced_statement = statement.replace(
  4887. "__EXECMANY_TOKEN__", expanded_values_string
  4888. )
  4889. replaced_parameters = tuple(
  4890. itertools.chain.from_iterable(batch_iterator)
  4891. )
  4892. replaced_parameters = (
  4893. extra_params_left
  4894. + replaced_parameters
  4895. + extra_params_right
  4896. )
  4897. else:
  4898. replaced_values_clauses = []
  4899. replaced_parameters = base_parameters.copy()
  4900. for i, param in enumerate(batch):
  4901. fmv = formatted_values_clause.replace(
  4902. "EXECMANY_INDEX__", str(i)
  4903. )
  4904. if imv.embed_values_counter:
  4905. fmv = fmv.replace("_IMV_VALUES_COUNTER", str(i))
  4906. replaced_values_clauses.append(fmv)
  4907. replaced_parameters.update(
  4908. {f"{key}__{i}": param[key] for key in keys_to_replace}
  4909. )
  4910. replaced_statement = statement.replace(
  4911. "__EXECMANY_TOKEN__",
  4912. ", ".join(replaced_values_clauses),
  4913. )
  4914. yield _InsertManyValuesBatch(
  4915. replaced_statement,
  4916. replaced_parameters,
  4917. processed_setinputsizes,
  4918. batch,
  4919. (
  4920. [_sentinel_from_params(cb) for cb in compiled_batch]
  4921. if _sentinel_from_params
  4922. else []
  4923. ),
  4924. current_batch_size,
  4925. batchnum,
  4926. total_batches,
  4927. sort_by_parameter_order,
  4928. False,
  4929. )
  4930. batchnum += 1
  4931. def visit_insert(
  4932. self, insert_stmt, visited_bindparam=None, visiting_cte=None, **kw
  4933. ):
  4934. compile_state = insert_stmt._compile_state_factory(
  4935. insert_stmt, self, **kw
  4936. )
  4937. insert_stmt = compile_state.statement
  4938. if visiting_cte is not None:
  4939. kw["visiting_cte"] = visiting_cte
  4940. toplevel = False
  4941. else:
  4942. toplevel = not self.stack
  4943. if toplevel:
  4944. self.isinsert = True
  4945. if not self.dml_compile_state:
  4946. self.dml_compile_state = compile_state
  4947. if not self.compile_state:
  4948. self.compile_state = compile_state
  4949. self.stack.append(
  4950. {
  4951. "correlate_froms": set(),
  4952. "asfrom_froms": set(),
  4953. "selectable": insert_stmt,
  4954. }
  4955. )
  4956. counted_bindparam = 0
  4957. # reset any incoming "visited_bindparam" collection
  4958. visited_bindparam = None
  4959. # for positional, insertmanyvalues needs to know how many
  4960. # bound parameters are in the VALUES sequence; there's no simple
  4961. # rule because default expressions etc. can have zero or more
  4962. # params inside them. After multiple attempts to figure this out,
  4963. # this very simplistic "count after" works and is
  4964. # likely the least amount of callcounts, though looks clumsy
  4965. if self.positional and visiting_cte is None:
  4966. # if we are inside a CTE, don't count parameters
  4967. # here since they wont be for insertmanyvalues. keep
  4968. # visited_bindparam at None so no counting happens.
  4969. # see #9173
  4970. visited_bindparam = []
  4971. crud_params_struct = crud._get_crud_params(
  4972. self,
  4973. insert_stmt,
  4974. compile_state,
  4975. toplevel,
  4976. visited_bindparam=visited_bindparam,
  4977. **kw,
  4978. )
  4979. if self.positional and visited_bindparam is not None:
  4980. counted_bindparam = len(visited_bindparam)
  4981. if self._numeric_binds:
  4982. if self._values_bindparam is not None:
  4983. self._values_bindparam += visited_bindparam
  4984. else:
  4985. self._values_bindparam = visited_bindparam
  4986. crud_params_single = crud_params_struct.single_params
  4987. if (
  4988. not crud_params_single
  4989. and not self.dialect.supports_default_values
  4990. and not self.dialect.supports_default_metavalue
  4991. and not self.dialect.supports_empty_insert
  4992. ):
  4993. raise exc.CompileError(
  4994. "The '%s' dialect with current database "
  4995. "version settings does not support empty "
  4996. "inserts." % self.dialect.name
  4997. )
  4998. if compile_state._has_multi_parameters:
  4999. if not self.dialect.supports_multivalues_insert:
  5000. raise exc.CompileError(
  5001. "The '%s' dialect with current database "
  5002. "version settings does not support "
  5003. "in-place multirow inserts." % self.dialect.name
  5004. )
  5005. elif (
  5006. self.implicit_returning or insert_stmt._returning
  5007. ) and insert_stmt._sort_by_parameter_order:
  5008. raise exc.CompileError(
  5009. "RETURNING cannot be determinstically sorted when "
  5010. "using an INSERT which includes multi-row values()."
  5011. )
  5012. crud_params_single = crud_params_struct.single_params
  5013. else:
  5014. crud_params_single = crud_params_struct.single_params
  5015. preparer = self.preparer
  5016. supports_default_values = self.dialect.supports_default_values
  5017. text = "INSERT "
  5018. if insert_stmt._prefixes:
  5019. text += self._generate_prefixes(
  5020. insert_stmt, insert_stmt._prefixes, **kw
  5021. )
  5022. text += "INTO "
  5023. table_text = preparer.format_table(insert_stmt.table)
  5024. if insert_stmt._hints:
  5025. _, table_text = self._setup_crud_hints(insert_stmt, table_text)
  5026. if insert_stmt._independent_ctes:
  5027. self._dispatch_independent_ctes(insert_stmt, kw)
  5028. text += table_text
  5029. if crud_params_single or not supports_default_values:
  5030. text += " (%s)" % ", ".join(
  5031. [expr for _, expr, _, _ in crud_params_single]
  5032. )
  5033. # look for insertmanyvalues attributes that would have been configured
  5034. # by crud.py as it scanned through the columns to be part of the
  5035. # INSERT
  5036. use_insertmanyvalues = crud_params_struct.use_insertmanyvalues
  5037. named_sentinel_params: Optional[Sequence[str]] = None
  5038. add_sentinel_cols = None
  5039. implicit_sentinel = False
  5040. returning_cols = self.implicit_returning or insert_stmt._returning
  5041. if returning_cols:
  5042. add_sentinel_cols = crud_params_struct.use_sentinel_columns
  5043. if add_sentinel_cols is not None:
  5044. assert use_insertmanyvalues
  5045. # search for the sentinel column explicitly present
  5046. # in the INSERT columns list, and additionally check that
  5047. # this column has a bound parameter name set up that's in the
  5048. # parameter list. If both of these cases are present, it means
  5049. # we will have a client side value for the sentinel in each
  5050. # parameter set.
  5051. _params_by_col = {
  5052. col: param_names
  5053. for col, _, _, param_names in crud_params_single
  5054. }
  5055. named_sentinel_params = []
  5056. for _add_sentinel_col in add_sentinel_cols:
  5057. if _add_sentinel_col not in _params_by_col:
  5058. named_sentinel_params = None
  5059. break
  5060. param_name = self._within_exec_param_key_getter(
  5061. _add_sentinel_col
  5062. )
  5063. if param_name not in _params_by_col[_add_sentinel_col]:
  5064. named_sentinel_params = None
  5065. break
  5066. named_sentinel_params.append(param_name)
  5067. if named_sentinel_params is None:
  5068. # if we are not going to have a client side value for
  5069. # the sentinel in the parameter set, that means it's
  5070. # an autoincrement, an IDENTITY, or a server-side SQL
  5071. # expression like nextval('seqname'). So this is
  5072. # an "implicit" sentinel; we will look for it in
  5073. # RETURNING
  5074. # only, and then sort on it. For this case on PG,
  5075. # SQL Server we have to use a special INSERT form
  5076. # that guarantees the server side function lines up with
  5077. # the entries in the VALUES.
  5078. if (
  5079. self.dialect.insertmanyvalues_implicit_sentinel
  5080. & InsertmanyvaluesSentinelOpts.ANY_AUTOINCREMENT
  5081. ):
  5082. implicit_sentinel = True
  5083. else:
  5084. # here, we are not using a sentinel at all
  5085. # and we are likely the SQLite dialect.
  5086. # The first add_sentinel_col that we have should not
  5087. # be marked as "insert_sentinel=True". if it was,
  5088. # an error should have been raised in
  5089. # _get_sentinel_column_for_table.
  5090. assert not add_sentinel_cols[0]._insert_sentinel, (
  5091. "sentinel selection rules should have prevented "
  5092. "us from getting here for this dialect"
  5093. )
  5094. # always put the sentinel columns last. even if they are
  5095. # in the returning list already, they will be there twice
  5096. # then.
  5097. returning_cols = list(returning_cols) + list(add_sentinel_cols)
  5098. returning_clause = self.returning_clause(
  5099. insert_stmt,
  5100. returning_cols,
  5101. populate_result_map=toplevel,
  5102. )
  5103. if self.returning_precedes_values:
  5104. text += " " + returning_clause
  5105. else:
  5106. returning_clause = None
  5107. if insert_stmt.select is not None:
  5108. # placed here by crud.py
  5109. select_text = self.process(
  5110. self.stack[-1]["insert_from_select"], insert_into=True, **kw
  5111. )
  5112. if self.ctes and self.dialect.cte_follows_insert:
  5113. nesting_level = len(self.stack) if not toplevel else None
  5114. text += " %s%s" % (
  5115. self._render_cte_clause(
  5116. nesting_level=nesting_level,
  5117. include_following_stack=True,
  5118. ),
  5119. select_text,
  5120. )
  5121. else:
  5122. text += " %s" % select_text
  5123. elif not crud_params_single and supports_default_values:
  5124. text += " DEFAULT VALUES"
  5125. if use_insertmanyvalues:
  5126. self._insertmanyvalues = _InsertManyValues(
  5127. True,
  5128. self.dialect.default_metavalue_token,
  5129. cast(
  5130. "List[crud._CrudParamElementStr]", crud_params_single
  5131. ),
  5132. counted_bindparam,
  5133. sort_by_parameter_order=(
  5134. insert_stmt._sort_by_parameter_order
  5135. ),
  5136. includes_upsert_behaviors=(
  5137. insert_stmt._post_values_clause is not None
  5138. ),
  5139. sentinel_columns=add_sentinel_cols,
  5140. num_sentinel_columns=(
  5141. len(add_sentinel_cols) if add_sentinel_cols else 0
  5142. ),
  5143. implicit_sentinel=implicit_sentinel,
  5144. )
  5145. elif compile_state._has_multi_parameters:
  5146. text += " VALUES %s" % (
  5147. ", ".join(
  5148. "(%s)"
  5149. % (", ".join(value for _, _, value, _ in crud_param_set))
  5150. for crud_param_set in crud_params_struct.all_multi_params
  5151. ),
  5152. )
  5153. else:
  5154. insert_single_values_expr = ", ".join(
  5155. [
  5156. value
  5157. for _, _, value, _ in cast(
  5158. "List[crud._CrudParamElementStr]",
  5159. crud_params_single,
  5160. )
  5161. ]
  5162. )
  5163. if use_insertmanyvalues:
  5164. if (
  5165. implicit_sentinel
  5166. and (
  5167. self.dialect.insertmanyvalues_implicit_sentinel
  5168. & InsertmanyvaluesSentinelOpts.USE_INSERT_FROM_SELECT
  5169. )
  5170. # this is checking if we have
  5171. # INSERT INTO table (id) VALUES (DEFAULT).
  5172. and not (crud_params_struct.is_default_metavalue_only)
  5173. ):
  5174. # if we have a sentinel column that is server generated,
  5175. # then for selected backends render the VALUES list as a
  5176. # subquery. This is the orderable form supported by
  5177. # PostgreSQL and SQL Server.
  5178. embed_sentinel_value = True
  5179. render_bind_casts = (
  5180. self.dialect.insertmanyvalues_implicit_sentinel
  5181. & InsertmanyvaluesSentinelOpts.RENDER_SELECT_COL_CASTS
  5182. )
  5183. colnames = ", ".join(
  5184. f"p{i}" for i, _ in enumerate(crud_params_single)
  5185. )
  5186. if render_bind_casts:
  5187. # render casts for the SELECT list. For PG, we are
  5188. # already rendering bind casts in the parameter list,
  5189. # selectively for the more "tricky" types like ARRAY.
  5190. # however, even for the "easy" types, if the parameter
  5191. # is NULL for every entry, PG gives up and says
  5192. # "it must be TEXT", which fails for other easy types
  5193. # like ints. So we cast on this side too.
  5194. colnames_w_cast = ", ".join(
  5195. self.render_bind_cast(
  5196. col.type,
  5197. col.type._unwrapped_dialect_impl(self.dialect),
  5198. f"p{i}",
  5199. )
  5200. for i, (col, *_) in enumerate(crud_params_single)
  5201. )
  5202. else:
  5203. colnames_w_cast = colnames
  5204. text += (
  5205. f" SELECT {colnames_w_cast} FROM "
  5206. f"(VALUES ({insert_single_values_expr})) "
  5207. f"AS imp_sen({colnames}, sen_counter) "
  5208. "ORDER BY sen_counter"
  5209. )
  5210. else:
  5211. # otherwise, if no sentinel or backend doesn't support
  5212. # orderable subquery form, use a plain VALUES list
  5213. embed_sentinel_value = False
  5214. text += f" VALUES ({insert_single_values_expr})"
  5215. self._insertmanyvalues = _InsertManyValues(
  5216. is_default_expr=False,
  5217. single_values_expr=insert_single_values_expr,
  5218. insert_crud_params=cast(
  5219. "List[crud._CrudParamElementStr]",
  5220. crud_params_single,
  5221. ),
  5222. num_positional_params_counted=counted_bindparam,
  5223. sort_by_parameter_order=(
  5224. insert_stmt._sort_by_parameter_order
  5225. ),
  5226. includes_upsert_behaviors=(
  5227. insert_stmt._post_values_clause is not None
  5228. ),
  5229. sentinel_columns=add_sentinel_cols,
  5230. num_sentinel_columns=(
  5231. len(add_sentinel_cols) if add_sentinel_cols else 0
  5232. ),
  5233. sentinel_param_keys=named_sentinel_params,
  5234. implicit_sentinel=implicit_sentinel,
  5235. embed_values_counter=embed_sentinel_value,
  5236. )
  5237. else:
  5238. text += f" VALUES ({insert_single_values_expr})"
  5239. if insert_stmt._post_values_clause is not None:
  5240. post_values_clause = self.process(
  5241. insert_stmt._post_values_clause, **kw
  5242. )
  5243. if post_values_clause:
  5244. text += " " + post_values_clause
  5245. if returning_clause and not self.returning_precedes_values:
  5246. text += " " + returning_clause
  5247. if self.ctes and not self.dialect.cte_follows_insert:
  5248. nesting_level = len(self.stack) if not toplevel else None
  5249. text = (
  5250. self._render_cte_clause(
  5251. nesting_level=nesting_level,
  5252. include_following_stack=True,
  5253. )
  5254. + text
  5255. )
  5256. self.stack.pop(-1)
  5257. return text
  5258. def update_limit_clause(self, update_stmt):
  5259. """Provide a hook for MySQL to add LIMIT to the UPDATE"""
  5260. return None
  5261. def delete_limit_clause(self, delete_stmt):
  5262. """Provide a hook for MySQL to add LIMIT to the DELETE"""
  5263. return None
  5264. def update_tables_clause(self, update_stmt, from_table, extra_froms, **kw):
  5265. """Provide a hook to override the initial table clause
  5266. in an UPDATE statement.
  5267. MySQL overrides this.
  5268. """
  5269. kw["asfrom"] = True
  5270. return from_table._compiler_dispatch(self, iscrud=True, **kw)
  5271. def update_from_clause(
  5272. self, update_stmt, from_table, extra_froms, from_hints, **kw
  5273. ):
  5274. """Provide a hook to override the generation of an
  5275. UPDATE..FROM clause.
  5276. MySQL and MSSQL override this.
  5277. """
  5278. raise NotImplementedError(
  5279. "This backend does not support multiple-table "
  5280. "criteria within UPDATE"
  5281. )
  5282. def visit_update(
  5283. self,
  5284. update_stmt: Update,
  5285. visiting_cte: Optional[CTE] = None,
  5286. **kw: Any,
  5287. ) -> str:
  5288. compile_state = update_stmt._compile_state_factory(
  5289. update_stmt, self, **kw
  5290. )
  5291. if TYPE_CHECKING:
  5292. assert isinstance(compile_state, UpdateDMLState)
  5293. update_stmt = compile_state.statement # type: ignore[assignment]
  5294. if visiting_cte is not None:
  5295. kw["visiting_cte"] = visiting_cte
  5296. toplevel = False
  5297. else:
  5298. toplevel = not self.stack
  5299. if toplevel:
  5300. self.isupdate = True
  5301. if not self.dml_compile_state:
  5302. self.dml_compile_state = compile_state
  5303. if not self.compile_state:
  5304. self.compile_state = compile_state
  5305. if self.linting & COLLECT_CARTESIAN_PRODUCTS:
  5306. from_linter = FromLinter({}, set())
  5307. warn_linting = self.linting & WARN_LINTING
  5308. if toplevel:
  5309. self.from_linter = from_linter
  5310. else:
  5311. from_linter = None
  5312. warn_linting = False
  5313. extra_froms = compile_state._extra_froms
  5314. is_multitable = bool(extra_froms)
  5315. if is_multitable:
  5316. # main table might be a JOIN
  5317. main_froms = set(_from_objects(update_stmt.table))
  5318. render_extra_froms = [
  5319. f for f in extra_froms if f not in main_froms
  5320. ]
  5321. correlate_froms = main_froms.union(extra_froms)
  5322. else:
  5323. render_extra_froms = []
  5324. correlate_froms = {update_stmt.table}
  5325. self.stack.append(
  5326. {
  5327. "correlate_froms": correlate_froms,
  5328. "asfrom_froms": correlate_froms,
  5329. "selectable": update_stmt,
  5330. }
  5331. )
  5332. text = "UPDATE "
  5333. if update_stmt._prefixes:
  5334. text += self._generate_prefixes(
  5335. update_stmt, update_stmt._prefixes, **kw
  5336. )
  5337. table_text = self.update_tables_clause(
  5338. update_stmt,
  5339. update_stmt.table,
  5340. render_extra_froms,
  5341. from_linter=from_linter,
  5342. **kw,
  5343. )
  5344. crud_params_struct = crud._get_crud_params(
  5345. self, update_stmt, compile_state, toplevel, **kw
  5346. )
  5347. crud_params = crud_params_struct.single_params
  5348. if update_stmt._hints:
  5349. dialect_hints, table_text = self._setup_crud_hints(
  5350. update_stmt, table_text
  5351. )
  5352. else:
  5353. dialect_hints = None
  5354. if update_stmt._independent_ctes:
  5355. self._dispatch_independent_ctes(update_stmt, kw)
  5356. text += table_text
  5357. text += " SET "
  5358. text += ", ".join(
  5359. expr + "=" + value
  5360. for _, expr, value, _ in cast(
  5361. "List[Tuple[Any, str, str, Any]]", crud_params
  5362. )
  5363. )
  5364. if self.implicit_returning or update_stmt._returning:
  5365. if self.returning_precedes_values:
  5366. text += " " + self.returning_clause(
  5367. update_stmt,
  5368. self.implicit_returning or update_stmt._returning,
  5369. populate_result_map=toplevel,
  5370. )
  5371. if extra_froms:
  5372. extra_from_text = self.update_from_clause(
  5373. update_stmt,
  5374. update_stmt.table,
  5375. render_extra_froms,
  5376. dialect_hints,
  5377. from_linter=from_linter,
  5378. **kw,
  5379. )
  5380. if extra_from_text:
  5381. text += " " + extra_from_text
  5382. if update_stmt._where_criteria:
  5383. t = self._generate_delimited_and_list(
  5384. update_stmt._where_criteria, from_linter=from_linter, **kw
  5385. )
  5386. if t:
  5387. text += " WHERE " + t
  5388. limit_clause = self.update_limit_clause(update_stmt)
  5389. if limit_clause:
  5390. text += " " + limit_clause
  5391. if (
  5392. self.implicit_returning or update_stmt._returning
  5393. ) and not self.returning_precedes_values:
  5394. text += " " + self.returning_clause(
  5395. update_stmt,
  5396. self.implicit_returning or update_stmt._returning,
  5397. populate_result_map=toplevel,
  5398. )
  5399. if self.ctes:
  5400. nesting_level = len(self.stack) if not toplevel else None
  5401. text = self._render_cte_clause(nesting_level=nesting_level) + text
  5402. if warn_linting:
  5403. assert from_linter is not None
  5404. from_linter.warn(stmt_type="UPDATE")
  5405. self.stack.pop(-1)
  5406. return text # type: ignore[no-any-return]
  5407. def delete_extra_from_clause(
  5408. self, delete_stmt, from_table, extra_froms, from_hints, **kw
  5409. ):
  5410. """Provide a hook to override the generation of an
  5411. DELETE..FROM clause.
  5412. This can be used to implement DELETE..USING for example.
  5413. MySQL and MSSQL override this.
  5414. """
  5415. raise NotImplementedError(
  5416. "This backend does not support multiple-table "
  5417. "criteria within DELETE"
  5418. )
  5419. def delete_table_clause(self, delete_stmt, from_table, extra_froms, **kw):
  5420. return from_table._compiler_dispatch(
  5421. self, asfrom=True, iscrud=True, **kw
  5422. )
  5423. def visit_delete(self, delete_stmt, visiting_cte=None, **kw):
  5424. compile_state = delete_stmt._compile_state_factory(
  5425. delete_stmt, self, **kw
  5426. )
  5427. delete_stmt = compile_state.statement
  5428. if visiting_cte is not None:
  5429. kw["visiting_cte"] = visiting_cte
  5430. toplevel = False
  5431. else:
  5432. toplevel = not self.stack
  5433. if toplevel:
  5434. self.isdelete = True
  5435. if not self.dml_compile_state:
  5436. self.dml_compile_state = compile_state
  5437. if not self.compile_state:
  5438. self.compile_state = compile_state
  5439. if self.linting & COLLECT_CARTESIAN_PRODUCTS:
  5440. from_linter = FromLinter({}, set())
  5441. warn_linting = self.linting & WARN_LINTING
  5442. if toplevel:
  5443. self.from_linter = from_linter
  5444. else:
  5445. from_linter = None
  5446. warn_linting = False
  5447. extra_froms = compile_state._extra_froms
  5448. correlate_froms = {delete_stmt.table}.union(extra_froms)
  5449. self.stack.append(
  5450. {
  5451. "correlate_froms": correlate_froms,
  5452. "asfrom_froms": correlate_froms,
  5453. "selectable": delete_stmt,
  5454. }
  5455. )
  5456. text = "DELETE "
  5457. if delete_stmt._prefixes:
  5458. text += self._generate_prefixes(
  5459. delete_stmt, delete_stmt._prefixes, **kw
  5460. )
  5461. text += "FROM "
  5462. try:
  5463. table_text = self.delete_table_clause(
  5464. delete_stmt,
  5465. delete_stmt.table,
  5466. extra_froms,
  5467. from_linter=from_linter,
  5468. )
  5469. except TypeError:
  5470. # anticipate 3rd party dialects that don't include **kw
  5471. # TODO: remove in 2.1
  5472. table_text = self.delete_table_clause(
  5473. delete_stmt, delete_stmt.table, extra_froms
  5474. )
  5475. if from_linter:
  5476. _ = self.process(delete_stmt.table, from_linter=from_linter)
  5477. crud._get_crud_params(self, delete_stmt, compile_state, toplevel, **kw)
  5478. if delete_stmt._hints:
  5479. dialect_hints, table_text = self._setup_crud_hints(
  5480. delete_stmt, table_text
  5481. )
  5482. else:
  5483. dialect_hints = None
  5484. if delete_stmt._independent_ctes:
  5485. self._dispatch_independent_ctes(delete_stmt, kw)
  5486. text += table_text
  5487. if (
  5488. self.implicit_returning or delete_stmt._returning
  5489. ) and self.returning_precedes_values:
  5490. text += " " + self.returning_clause(
  5491. delete_stmt,
  5492. self.implicit_returning or delete_stmt._returning,
  5493. populate_result_map=toplevel,
  5494. )
  5495. if extra_froms:
  5496. extra_from_text = self.delete_extra_from_clause(
  5497. delete_stmt,
  5498. delete_stmt.table,
  5499. extra_froms,
  5500. dialect_hints,
  5501. from_linter=from_linter,
  5502. **kw,
  5503. )
  5504. if extra_from_text:
  5505. text += " " + extra_from_text
  5506. if delete_stmt._where_criteria:
  5507. t = self._generate_delimited_and_list(
  5508. delete_stmt._where_criteria, from_linter=from_linter, **kw
  5509. )
  5510. if t:
  5511. text += " WHERE " + t
  5512. limit_clause = self.delete_limit_clause(delete_stmt)
  5513. if limit_clause:
  5514. text += " " + limit_clause
  5515. if (
  5516. self.implicit_returning or delete_stmt._returning
  5517. ) and not self.returning_precedes_values:
  5518. text += " " + self.returning_clause(
  5519. delete_stmt,
  5520. self.implicit_returning or delete_stmt._returning,
  5521. populate_result_map=toplevel,
  5522. )
  5523. if self.ctes:
  5524. nesting_level = len(self.stack) if not toplevel else None
  5525. text = self._render_cte_clause(nesting_level=nesting_level) + text
  5526. if warn_linting:
  5527. assert from_linter is not None
  5528. from_linter.warn(stmt_type="DELETE")
  5529. self.stack.pop(-1)
  5530. return text
  5531. def visit_savepoint(self, savepoint_stmt, **kw):
  5532. return "SAVEPOINT %s" % self.preparer.format_savepoint(savepoint_stmt)
  5533. def visit_rollback_to_savepoint(self, savepoint_stmt, **kw):
  5534. return "ROLLBACK TO SAVEPOINT %s" % self.preparer.format_savepoint(
  5535. savepoint_stmt
  5536. )
  5537. def visit_release_savepoint(self, savepoint_stmt, **kw):
  5538. return "RELEASE SAVEPOINT %s" % self.preparer.format_savepoint(
  5539. savepoint_stmt
  5540. )
  5541. class StrSQLCompiler(SQLCompiler):
  5542. """A :class:`.SQLCompiler` subclass which allows a small selection
  5543. of non-standard SQL features to render into a string value.
  5544. The :class:`.StrSQLCompiler` is invoked whenever a Core expression
  5545. element is directly stringified without calling upon the
  5546. :meth:`_expression.ClauseElement.compile` method.
  5547. It can render a limited set
  5548. of non-standard SQL constructs to assist in basic stringification,
  5549. however for more substantial custom or dialect-specific SQL constructs,
  5550. it will be necessary to make use of
  5551. :meth:`_expression.ClauseElement.compile`
  5552. directly.
  5553. .. seealso::
  5554. :ref:`faq_sql_expression_string`
  5555. """
  5556. def _fallback_column_name(self, column):
  5557. return "<name unknown>"
  5558. @util.preload_module("sqlalchemy.engine.url")
  5559. def visit_unsupported_compilation(self, element, err, **kw):
  5560. if element.stringify_dialect != "default":
  5561. url = util.preloaded.engine_url
  5562. dialect = url.URL.create(element.stringify_dialect).get_dialect()()
  5563. compiler = dialect.statement_compiler(
  5564. dialect, None, _supporting_against=self
  5565. )
  5566. if not isinstance(compiler, StrSQLCompiler):
  5567. return compiler.process(element, **kw)
  5568. return super().visit_unsupported_compilation(element, err)
  5569. def visit_getitem_binary(self, binary, operator, **kw):
  5570. return "%s[%s]" % (
  5571. self.process(binary.left, **kw),
  5572. self.process(binary.right, **kw),
  5573. )
  5574. def visit_json_getitem_op_binary(self, binary, operator, **kw):
  5575. return self.visit_getitem_binary(binary, operator, **kw)
  5576. def visit_json_path_getitem_op_binary(self, binary, operator, **kw):
  5577. return self.visit_getitem_binary(binary, operator, **kw)
  5578. def visit_sequence(self, sequence, **kw):
  5579. return (
  5580. f"<next sequence value: {self.preparer.format_sequence(sequence)}>"
  5581. )
  5582. def returning_clause(
  5583. self,
  5584. stmt: UpdateBase,
  5585. returning_cols: Sequence[_ColumnsClauseElement],
  5586. *,
  5587. populate_result_map: bool,
  5588. **kw: Any,
  5589. ) -> str:
  5590. columns = [
  5591. self._label_select_column(None, c, True, False, {})
  5592. for c in base._select_iterables(returning_cols)
  5593. ]
  5594. return "RETURNING " + ", ".join(columns)
  5595. def update_from_clause(
  5596. self, update_stmt, from_table, extra_froms, from_hints, **kw
  5597. ):
  5598. kw["asfrom"] = True
  5599. return "FROM " + ", ".join(
  5600. t._compiler_dispatch(self, fromhints=from_hints, **kw)
  5601. for t in extra_froms
  5602. )
  5603. def delete_extra_from_clause(
  5604. self, delete_stmt, from_table, extra_froms, from_hints, **kw
  5605. ):
  5606. kw["asfrom"] = True
  5607. return ", " + ", ".join(
  5608. t._compiler_dispatch(self, fromhints=from_hints, **kw)
  5609. for t in extra_froms
  5610. )
  5611. def visit_empty_set_expr(self, element_types, **kw):
  5612. return "SELECT 1 WHERE 1!=1"
  5613. def get_from_hint_text(self, table, text):
  5614. return "[%s]" % text
  5615. def visit_regexp_match_op_binary(self, binary, operator, **kw):
  5616. return self._generate_generic_binary(binary, " <regexp> ", **kw)
  5617. def visit_not_regexp_match_op_binary(self, binary, operator, **kw):
  5618. return self._generate_generic_binary(binary, " <not regexp> ", **kw)
  5619. def visit_regexp_replace_op_binary(self, binary, operator, **kw):
  5620. return "<regexp replace>(%s, %s)" % (
  5621. binary.left._compiler_dispatch(self, **kw),
  5622. binary.right._compiler_dispatch(self, **kw),
  5623. )
  5624. def visit_try_cast(self, cast, **kwargs):
  5625. return "TRY_CAST(%s AS %s)" % (
  5626. cast.clause._compiler_dispatch(self, **kwargs),
  5627. cast.typeclause._compiler_dispatch(self, **kwargs),
  5628. )
  5629. class DDLCompiler(Compiled):
  5630. is_ddl = True
  5631. if TYPE_CHECKING:
  5632. def __init__(
  5633. self,
  5634. dialect: Dialect,
  5635. statement: ExecutableDDLElement,
  5636. schema_translate_map: Optional[SchemaTranslateMapType] = ...,
  5637. render_schema_translate: bool = ...,
  5638. compile_kwargs: Mapping[str, Any] = ...,
  5639. ): ...
  5640. @util.ro_memoized_property
  5641. def sql_compiler(self) -> SQLCompiler:
  5642. return self.dialect.statement_compiler(
  5643. self.dialect, None, schema_translate_map=self.schema_translate_map
  5644. )
  5645. @util.memoized_property
  5646. def type_compiler(self):
  5647. return self.dialect.type_compiler_instance
  5648. def construct_params(
  5649. self,
  5650. params: Optional[_CoreSingleExecuteParams] = None,
  5651. extracted_parameters: Optional[Sequence[BindParameter[Any]]] = None,
  5652. escape_names: bool = True,
  5653. ) -> Optional[_MutableCoreSingleExecuteParams]:
  5654. return None
  5655. def visit_ddl(self, ddl, **kwargs):
  5656. # table events can substitute table and schema name
  5657. context = ddl.context
  5658. if isinstance(ddl.target, schema.Table):
  5659. context = context.copy()
  5660. preparer = self.preparer
  5661. path = preparer.format_table_seq(ddl.target)
  5662. if len(path) == 1:
  5663. table, sch = path[0], ""
  5664. else:
  5665. table, sch = path[-1], path[0]
  5666. context.setdefault("table", table)
  5667. context.setdefault("schema", sch)
  5668. context.setdefault("fullname", preparer.format_table(ddl.target))
  5669. return self.sql_compiler.post_process_text(ddl.statement % context)
  5670. def visit_create_schema(self, create, **kw):
  5671. text = "CREATE SCHEMA "
  5672. if create.if_not_exists:
  5673. text += "IF NOT EXISTS "
  5674. return text + self.preparer.format_schema(create.element)
  5675. def visit_drop_schema(self, drop, **kw):
  5676. text = "DROP SCHEMA "
  5677. if drop.if_exists:
  5678. text += "IF EXISTS "
  5679. text += self.preparer.format_schema(drop.element)
  5680. if drop.cascade:
  5681. text += " CASCADE"
  5682. return text
  5683. def visit_create_table(self, create, **kw):
  5684. table = create.element
  5685. preparer = self.preparer
  5686. text = "\nCREATE "
  5687. if table._prefixes:
  5688. text += " ".join(table._prefixes) + " "
  5689. text += "TABLE "
  5690. if create.if_not_exists:
  5691. text += "IF NOT EXISTS "
  5692. text += preparer.format_table(table) + " "
  5693. create_table_suffix = self.create_table_suffix(table)
  5694. if create_table_suffix:
  5695. text += create_table_suffix + " "
  5696. text += "("
  5697. separator = "\n"
  5698. # if only one primary key, specify it along with the column
  5699. first_pk = False
  5700. for create_column in create.columns:
  5701. column = create_column.element
  5702. try:
  5703. processed = self.process(
  5704. create_column, first_pk=column.primary_key and not first_pk
  5705. )
  5706. if processed is not None:
  5707. text += separator
  5708. separator = ", \n"
  5709. text += "\t" + processed
  5710. if column.primary_key:
  5711. first_pk = True
  5712. except exc.CompileError as ce:
  5713. raise exc.CompileError(
  5714. "(in table '%s', column '%s'): %s"
  5715. % (table.description, column.name, ce.args[0])
  5716. ) from ce
  5717. const = self.create_table_constraints(
  5718. table,
  5719. _include_foreign_key_constraints=create.include_foreign_key_constraints, # noqa
  5720. )
  5721. if const:
  5722. text += separator + "\t" + const
  5723. text += "\n)%s\n\n" % self.post_create_table(table)
  5724. return text
  5725. def visit_create_column(self, create, first_pk=False, **kw):
  5726. column = create.element
  5727. if column.system:
  5728. return None
  5729. text = self.get_column_specification(column, first_pk=first_pk)
  5730. const = " ".join(
  5731. self.process(constraint) for constraint in column.constraints
  5732. )
  5733. if const:
  5734. text += " " + const
  5735. return text
  5736. def create_table_constraints(
  5737. self, table, _include_foreign_key_constraints=None, **kw
  5738. ):
  5739. # On some DB order is significant: visit PK first, then the
  5740. # other constraints (engine.ReflectionTest.testbasic failed on FB2)
  5741. constraints = []
  5742. if table.primary_key:
  5743. constraints.append(table.primary_key)
  5744. all_fkcs = table.foreign_key_constraints
  5745. if _include_foreign_key_constraints is not None:
  5746. omit_fkcs = all_fkcs.difference(_include_foreign_key_constraints)
  5747. else:
  5748. omit_fkcs = set()
  5749. constraints.extend(
  5750. [
  5751. c
  5752. for c in table._sorted_constraints
  5753. if c is not table.primary_key and c not in omit_fkcs
  5754. ]
  5755. )
  5756. return ", \n\t".join(
  5757. p
  5758. for p in (
  5759. self.process(constraint)
  5760. for constraint in constraints
  5761. if (constraint._should_create_for_compiler(self))
  5762. and (
  5763. not self.dialect.supports_alter
  5764. or not getattr(constraint, "use_alter", False)
  5765. )
  5766. )
  5767. if p is not None
  5768. )
  5769. def visit_drop_table(self, drop, **kw):
  5770. text = "\nDROP TABLE "
  5771. if drop.if_exists:
  5772. text += "IF EXISTS "
  5773. return text + self.preparer.format_table(drop.element)
  5774. def visit_drop_view(self, drop, **kw):
  5775. return "\nDROP VIEW " + self.preparer.format_table(drop.element)
  5776. def _verify_index_table(self, index: Index) -> None:
  5777. if index.table is None:
  5778. raise exc.CompileError(
  5779. "Index '%s' is not associated with any table." % index.name
  5780. )
  5781. def visit_create_index(
  5782. self, create, include_schema=False, include_table_schema=True, **kw
  5783. ):
  5784. index = create.element
  5785. self._verify_index_table(index)
  5786. preparer = self.preparer
  5787. text = "CREATE "
  5788. if index.unique:
  5789. text += "UNIQUE "
  5790. if index.name is None:
  5791. raise exc.CompileError(
  5792. "CREATE INDEX requires that the index have a name"
  5793. )
  5794. text += "INDEX "
  5795. if create.if_not_exists:
  5796. text += "IF NOT EXISTS "
  5797. text += "%s ON %s (%s)" % (
  5798. self._prepared_index_name(index, include_schema=include_schema),
  5799. preparer.format_table(
  5800. index.table, use_schema=include_table_schema
  5801. ),
  5802. ", ".join(
  5803. self.sql_compiler.process(
  5804. expr, include_table=False, literal_binds=True
  5805. )
  5806. for expr in index.expressions
  5807. ),
  5808. )
  5809. return text
  5810. def visit_drop_index(self, drop, **kw):
  5811. index = drop.element
  5812. if index.name is None:
  5813. raise exc.CompileError(
  5814. "DROP INDEX requires that the index have a name"
  5815. )
  5816. text = "\nDROP INDEX "
  5817. if drop.if_exists:
  5818. text += "IF EXISTS "
  5819. return text + self._prepared_index_name(index, include_schema=True)
  5820. def _prepared_index_name(
  5821. self, index: Index, include_schema: bool = False
  5822. ) -> str:
  5823. if index.table is not None:
  5824. effective_schema = self.preparer.schema_for_object(index.table)
  5825. else:
  5826. effective_schema = None
  5827. if include_schema and effective_schema:
  5828. schema_name = self.preparer.quote_schema(effective_schema)
  5829. else:
  5830. schema_name = None
  5831. index_name: str = self.preparer.format_index(index)
  5832. if schema_name:
  5833. index_name = schema_name + "." + index_name
  5834. return index_name
  5835. def visit_add_constraint(self, create, **kw):
  5836. return "ALTER TABLE %s ADD %s" % (
  5837. self.preparer.format_table(create.element.table),
  5838. self.process(create.element),
  5839. )
  5840. def visit_set_table_comment(self, create, **kw):
  5841. return "COMMENT ON TABLE %s IS %s" % (
  5842. self.preparer.format_table(create.element),
  5843. self.sql_compiler.render_literal_value(
  5844. create.element.comment, sqltypes.String()
  5845. ),
  5846. )
  5847. def visit_drop_table_comment(self, drop, **kw):
  5848. return "COMMENT ON TABLE %s IS NULL" % self.preparer.format_table(
  5849. drop.element
  5850. )
  5851. def visit_set_column_comment(self, create, **kw):
  5852. return "COMMENT ON COLUMN %s IS %s" % (
  5853. self.preparer.format_column(
  5854. create.element, use_table=True, use_schema=True
  5855. ),
  5856. self.sql_compiler.render_literal_value(
  5857. create.element.comment, sqltypes.String()
  5858. ),
  5859. )
  5860. def visit_drop_column_comment(self, drop, **kw):
  5861. return "COMMENT ON COLUMN %s IS NULL" % self.preparer.format_column(
  5862. drop.element, use_table=True
  5863. )
  5864. def visit_set_constraint_comment(self, create, **kw):
  5865. raise exc.UnsupportedCompilationError(self, type(create))
  5866. def visit_drop_constraint_comment(self, drop, **kw):
  5867. raise exc.UnsupportedCompilationError(self, type(drop))
  5868. def get_identity_options(self, identity_options):
  5869. text = []
  5870. if identity_options.increment is not None:
  5871. text.append("INCREMENT BY %d" % identity_options.increment)
  5872. if identity_options.start is not None:
  5873. text.append("START WITH %d" % identity_options.start)
  5874. if identity_options.minvalue is not None:
  5875. text.append("MINVALUE %d" % identity_options.minvalue)
  5876. if identity_options.maxvalue is not None:
  5877. text.append("MAXVALUE %d" % identity_options.maxvalue)
  5878. if identity_options.nominvalue is not None:
  5879. text.append("NO MINVALUE")
  5880. if identity_options.nomaxvalue is not None:
  5881. text.append("NO MAXVALUE")
  5882. if identity_options.cache is not None:
  5883. text.append("CACHE %d" % identity_options.cache)
  5884. if identity_options.cycle is not None:
  5885. text.append("CYCLE" if identity_options.cycle else "NO CYCLE")
  5886. return " ".join(text)
  5887. def visit_create_sequence(self, create, prefix=None, **kw):
  5888. text = "CREATE SEQUENCE "
  5889. if create.if_not_exists:
  5890. text += "IF NOT EXISTS "
  5891. text += self.preparer.format_sequence(create.element)
  5892. if prefix:
  5893. text += prefix
  5894. options = self.get_identity_options(create.element)
  5895. if options:
  5896. text += " " + options
  5897. return text
  5898. def visit_drop_sequence(self, drop, **kw):
  5899. text = "DROP SEQUENCE "
  5900. if drop.if_exists:
  5901. text += "IF EXISTS "
  5902. return text + self.preparer.format_sequence(drop.element)
  5903. def visit_drop_constraint(self, drop, **kw):
  5904. constraint = drop.element
  5905. if constraint.name is not None:
  5906. formatted_name = self.preparer.format_constraint(constraint)
  5907. else:
  5908. formatted_name = None
  5909. if formatted_name is None:
  5910. raise exc.CompileError(
  5911. "Can't emit DROP CONSTRAINT for constraint %r; "
  5912. "it has no name" % drop.element
  5913. )
  5914. return "ALTER TABLE %s DROP CONSTRAINT %s%s%s" % (
  5915. self.preparer.format_table(drop.element.table),
  5916. "IF EXISTS " if drop.if_exists else "",
  5917. formatted_name,
  5918. " CASCADE" if drop.cascade else "",
  5919. )
  5920. def get_column_specification(self, column, **kwargs):
  5921. colspec = (
  5922. self.preparer.format_column(column)
  5923. + " "
  5924. + self.dialect.type_compiler_instance.process(
  5925. column.type, type_expression=column
  5926. )
  5927. )
  5928. default = self.get_column_default_string(column)
  5929. if default is not None:
  5930. colspec += " DEFAULT " + default
  5931. if column.computed is not None:
  5932. colspec += " " + self.process(column.computed)
  5933. if (
  5934. column.identity is not None
  5935. and self.dialect.supports_identity_columns
  5936. ):
  5937. colspec += " " + self.process(column.identity)
  5938. if not column.nullable and (
  5939. not column.identity or not self.dialect.supports_identity_columns
  5940. ):
  5941. colspec += " NOT NULL"
  5942. return colspec
  5943. def create_table_suffix(self, table):
  5944. return ""
  5945. def post_create_table(self, table):
  5946. return ""
  5947. def get_column_default_string(self, column: Column[Any]) -> Optional[str]:
  5948. if isinstance(column.server_default, schema.DefaultClause):
  5949. return self.render_default_string(column.server_default.arg)
  5950. else:
  5951. return None
  5952. def render_default_string(self, default: Union[Visitable, str]) -> str:
  5953. if isinstance(default, str):
  5954. return self.sql_compiler.render_literal_value(
  5955. default, sqltypes.STRINGTYPE
  5956. )
  5957. else:
  5958. return self.sql_compiler.process(default, literal_binds=True)
  5959. def visit_table_or_column_check_constraint(self, constraint, **kw):
  5960. if constraint.is_column_level:
  5961. return self.visit_column_check_constraint(constraint)
  5962. else:
  5963. return self.visit_check_constraint(constraint)
  5964. def visit_check_constraint(self, constraint, **kw):
  5965. text = ""
  5966. if constraint.name is not None:
  5967. formatted_name = self.preparer.format_constraint(constraint)
  5968. if formatted_name is not None:
  5969. text += "CONSTRAINT %s " % formatted_name
  5970. text += "CHECK (%s)" % self.sql_compiler.process(
  5971. constraint.sqltext, include_table=False, literal_binds=True
  5972. )
  5973. text += self.define_constraint_deferrability(constraint)
  5974. return text
  5975. def visit_column_check_constraint(self, constraint, **kw):
  5976. text = ""
  5977. if constraint.name is not None:
  5978. formatted_name = self.preparer.format_constraint(constraint)
  5979. if formatted_name is not None:
  5980. text += "CONSTRAINT %s " % formatted_name
  5981. text += "CHECK (%s)" % self.sql_compiler.process(
  5982. constraint.sqltext, include_table=False, literal_binds=True
  5983. )
  5984. text += self.define_constraint_deferrability(constraint)
  5985. return text
  5986. def visit_primary_key_constraint(
  5987. self, constraint: PrimaryKeyConstraint, **kw: Any
  5988. ) -> str:
  5989. if len(constraint) == 0:
  5990. return ""
  5991. text = ""
  5992. if constraint.name is not None:
  5993. formatted_name = self.preparer.format_constraint(constraint)
  5994. if formatted_name is not None:
  5995. text += "CONSTRAINT %s " % formatted_name
  5996. text += "PRIMARY KEY "
  5997. text += "(%s)" % ", ".join(
  5998. self.preparer.quote(c.name)
  5999. for c in (
  6000. constraint.columns_autoinc_first
  6001. if constraint._implicit_generated
  6002. else constraint.columns
  6003. )
  6004. )
  6005. text += self.define_constraint_deferrability(constraint)
  6006. return text
  6007. def visit_foreign_key_constraint(self, constraint, **kw):
  6008. preparer = self.preparer
  6009. text = ""
  6010. if constraint.name is not None:
  6011. formatted_name = self.preparer.format_constraint(constraint)
  6012. if formatted_name is not None:
  6013. text += "CONSTRAINT %s " % formatted_name
  6014. remote_table = list(constraint.elements)[0].column.table
  6015. text += "FOREIGN KEY(%s) REFERENCES %s (%s)" % (
  6016. ", ".join(
  6017. preparer.quote(f.parent.name) for f in constraint.elements
  6018. ),
  6019. self.define_constraint_remote_table(
  6020. constraint, remote_table, preparer
  6021. ),
  6022. ", ".join(
  6023. preparer.quote(f.column.name) for f in constraint.elements
  6024. ),
  6025. )
  6026. text += self.define_constraint_match(constraint)
  6027. text += self.define_constraint_cascades(constraint)
  6028. text += self.define_constraint_deferrability(constraint)
  6029. return text
  6030. def define_constraint_remote_table(self, constraint, table, preparer):
  6031. """Format the remote table clause of a CREATE CONSTRAINT clause."""
  6032. return preparer.format_table(table)
  6033. def visit_unique_constraint(
  6034. self, constraint: UniqueConstraint, **kw: Any
  6035. ) -> str:
  6036. if len(constraint) == 0:
  6037. return ""
  6038. text = ""
  6039. if constraint.name is not None:
  6040. formatted_name = self.preparer.format_constraint(constraint)
  6041. if formatted_name is not None:
  6042. text += "CONSTRAINT %s " % formatted_name
  6043. text += "UNIQUE %s(%s)" % (
  6044. self.define_unique_constraint_distinct(constraint, **kw),
  6045. ", ".join(self.preparer.quote(c.name) for c in constraint),
  6046. )
  6047. text += self.define_constraint_deferrability(constraint)
  6048. return text
  6049. def define_unique_constraint_distinct(
  6050. self, constraint: UniqueConstraint, **kw: Any
  6051. ) -> str:
  6052. return ""
  6053. def define_constraint_cascades(
  6054. self, constraint: ForeignKeyConstraint
  6055. ) -> str:
  6056. text = ""
  6057. if constraint.ondelete is not None:
  6058. text += self.define_constraint_ondelete_cascade(constraint)
  6059. if constraint.onupdate is not None:
  6060. text += self.define_constraint_onupdate_cascade(constraint)
  6061. return text
  6062. def define_constraint_ondelete_cascade(
  6063. self, constraint: ForeignKeyConstraint
  6064. ) -> str:
  6065. return " ON DELETE %s" % self.preparer.validate_sql_phrase(
  6066. constraint.ondelete, FK_ON_DELETE
  6067. )
  6068. def define_constraint_onupdate_cascade(
  6069. self, constraint: ForeignKeyConstraint
  6070. ) -> str:
  6071. return " ON UPDATE %s" % self.preparer.validate_sql_phrase(
  6072. constraint.onupdate, FK_ON_UPDATE
  6073. )
  6074. def define_constraint_deferrability(self, constraint: Constraint) -> str:
  6075. text = ""
  6076. if constraint.deferrable is not None:
  6077. if constraint.deferrable:
  6078. text += " DEFERRABLE"
  6079. else:
  6080. text += " NOT DEFERRABLE"
  6081. if constraint.initially is not None:
  6082. text += " INITIALLY %s" % self.preparer.validate_sql_phrase(
  6083. constraint.initially, FK_INITIALLY
  6084. )
  6085. return text
  6086. def define_constraint_match(self, constraint):
  6087. text = ""
  6088. if constraint.match is not None:
  6089. text += " MATCH %s" % constraint.match
  6090. return text
  6091. def visit_computed_column(self, generated, **kw):
  6092. text = "GENERATED ALWAYS AS (%s)" % self.sql_compiler.process(
  6093. generated.sqltext, include_table=False, literal_binds=True
  6094. )
  6095. if generated.persisted is True:
  6096. text += " STORED"
  6097. elif generated.persisted is False:
  6098. text += " VIRTUAL"
  6099. return text
  6100. def visit_identity_column(self, identity, **kw):
  6101. text = "GENERATED %s AS IDENTITY" % (
  6102. "ALWAYS" if identity.always else "BY DEFAULT",
  6103. )
  6104. options = self.get_identity_options(identity)
  6105. if options:
  6106. text += " (%s)" % options
  6107. return text
  6108. class GenericTypeCompiler(TypeCompiler):
  6109. def visit_FLOAT(self, type_: sqltypes.Float[Any], **kw: Any) -> str:
  6110. return "FLOAT"
  6111. def visit_DOUBLE(self, type_: sqltypes.Double[Any], **kw: Any) -> str:
  6112. return "DOUBLE"
  6113. def visit_DOUBLE_PRECISION(
  6114. self, type_: sqltypes.DOUBLE_PRECISION[Any], **kw: Any
  6115. ) -> str:
  6116. return "DOUBLE PRECISION"
  6117. def visit_REAL(self, type_: sqltypes.REAL[Any], **kw: Any) -> str:
  6118. return "REAL"
  6119. def visit_NUMERIC(self, type_: sqltypes.Numeric[Any], **kw: Any) -> str:
  6120. if type_.precision is None:
  6121. return "NUMERIC"
  6122. elif type_.scale is None:
  6123. return "NUMERIC(%(precision)s)" % {"precision": type_.precision}
  6124. else:
  6125. return "NUMERIC(%(precision)s, %(scale)s)" % {
  6126. "precision": type_.precision,
  6127. "scale": type_.scale,
  6128. }
  6129. def visit_DECIMAL(self, type_: sqltypes.DECIMAL[Any], **kw: Any) -> str:
  6130. if type_.precision is None:
  6131. return "DECIMAL"
  6132. elif type_.scale is None:
  6133. return "DECIMAL(%(precision)s)" % {"precision": type_.precision}
  6134. else:
  6135. return "DECIMAL(%(precision)s, %(scale)s)" % {
  6136. "precision": type_.precision,
  6137. "scale": type_.scale,
  6138. }
  6139. def visit_INTEGER(self, type_: sqltypes.Integer, **kw: Any) -> str:
  6140. return "INTEGER"
  6141. def visit_SMALLINT(self, type_: sqltypes.SmallInteger, **kw: Any) -> str:
  6142. return "SMALLINT"
  6143. def visit_BIGINT(self, type_: sqltypes.BigInteger, **kw: Any) -> str:
  6144. return "BIGINT"
  6145. def visit_TIMESTAMP(self, type_: sqltypes.TIMESTAMP, **kw: Any) -> str:
  6146. return "TIMESTAMP"
  6147. def visit_DATETIME(self, type_: sqltypes.DateTime, **kw: Any) -> str:
  6148. return "DATETIME"
  6149. def visit_DATE(self, type_: sqltypes.Date, **kw: Any) -> str:
  6150. return "DATE"
  6151. def visit_TIME(self, type_: sqltypes.Time, **kw: Any) -> str:
  6152. return "TIME"
  6153. def visit_CLOB(self, type_: sqltypes.CLOB, **kw: Any) -> str:
  6154. return "CLOB"
  6155. def visit_NCLOB(self, type_: sqltypes.Text, **kw: Any) -> str:
  6156. return "NCLOB"
  6157. def _render_string_type(
  6158. self, name: str, length: Optional[int], collation: Optional[str]
  6159. ) -> str:
  6160. text = name
  6161. if length:
  6162. text += f"({length})"
  6163. if collation:
  6164. text += f' COLLATE "{collation}"'
  6165. return text
  6166. def visit_CHAR(self, type_: sqltypes.CHAR, **kw: Any) -> str:
  6167. return self._render_string_type("CHAR", type_.length, type_.collation)
  6168. def visit_NCHAR(self, type_: sqltypes.NCHAR, **kw: Any) -> str:
  6169. return self._render_string_type("NCHAR", type_.length, type_.collation)
  6170. def visit_VARCHAR(self, type_: sqltypes.String, **kw: Any) -> str:
  6171. return self._render_string_type(
  6172. "VARCHAR", type_.length, type_.collation
  6173. )
  6174. def visit_NVARCHAR(self, type_: sqltypes.NVARCHAR, **kw: Any) -> str:
  6175. return self._render_string_type(
  6176. "NVARCHAR", type_.length, type_.collation
  6177. )
  6178. def visit_TEXT(self, type_: sqltypes.Text, **kw: Any) -> str:
  6179. return self._render_string_type("TEXT", type_.length, type_.collation)
  6180. def visit_UUID(self, type_: sqltypes.Uuid[Any], **kw: Any) -> str:
  6181. return "UUID"
  6182. def visit_BLOB(self, type_: sqltypes.LargeBinary, **kw: Any) -> str:
  6183. return "BLOB"
  6184. def visit_BINARY(self, type_: sqltypes.BINARY, **kw: Any) -> str:
  6185. return "BINARY" + (type_.length and "(%d)" % type_.length or "")
  6186. def visit_VARBINARY(self, type_: sqltypes.VARBINARY, **kw: Any) -> str:
  6187. return "VARBINARY" + (type_.length and "(%d)" % type_.length or "")
  6188. def visit_BOOLEAN(self, type_: sqltypes.Boolean, **kw: Any) -> str:
  6189. return "BOOLEAN"
  6190. def visit_uuid(self, type_: sqltypes.Uuid[Any], **kw: Any) -> str:
  6191. if not type_.native_uuid or not self.dialect.supports_native_uuid:
  6192. return self._render_string_type("CHAR", length=32, collation=None)
  6193. else:
  6194. return self.visit_UUID(type_, **kw)
  6195. def visit_large_binary(
  6196. self, type_: sqltypes.LargeBinary, **kw: Any
  6197. ) -> str:
  6198. return self.visit_BLOB(type_, **kw)
  6199. def visit_boolean(self, type_: sqltypes.Boolean, **kw: Any) -> str:
  6200. return self.visit_BOOLEAN(type_, **kw)
  6201. def visit_time(self, type_: sqltypes.Time, **kw: Any) -> str:
  6202. return self.visit_TIME(type_, **kw)
  6203. def visit_datetime(self, type_: sqltypes.DateTime, **kw: Any) -> str:
  6204. return self.visit_DATETIME(type_, **kw)
  6205. def visit_date(self, type_: sqltypes.Date, **kw: Any) -> str:
  6206. return self.visit_DATE(type_, **kw)
  6207. def visit_big_integer(self, type_: sqltypes.BigInteger, **kw: Any) -> str:
  6208. return self.visit_BIGINT(type_, **kw)
  6209. def visit_small_integer(
  6210. self, type_: sqltypes.SmallInteger, **kw: Any
  6211. ) -> str:
  6212. return self.visit_SMALLINT(type_, **kw)
  6213. def visit_integer(self, type_: sqltypes.Integer, **kw: Any) -> str:
  6214. return self.visit_INTEGER(type_, **kw)
  6215. def visit_real(self, type_: sqltypes.REAL[Any], **kw: Any) -> str:
  6216. return self.visit_REAL(type_, **kw)
  6217. def visit_float(self, type_: sqltypes.Float[Any], **kw: Any) -> str:
  6218. return self.visit_FLOAT(type_, **kw)
  6219. def visit_double(self, type_: sqltypes.Double[Any], **kw: Any) -> str:
  6220. return self.visit_DOUBLE(type_, **kw)
  6221. def visit_numeric(self, type_: sqltypes.Numeric[Any], **kw: Any) -> str:
  6222. return self.visit_NUMERIC(type_, **kw)
  6223. def visit_string(self, type_: sqltypes.String, **kw: Any) -> str:
  6224. return self.visit_VARCHAR(type_, **kw)
  6225. def visit_unicode(self, type_: sqltypes.Unicode, **kw: Any) -> str:
  6226. return self.visit_VARCHAR(type_, **kw)
  6227. def visit_text(self, type_: sqltypes.Text, **kw: Any) -> str:
  6228. return self.visit_TEXT(type_, **kw)
  6229. def visit_unicode_text(
  6230. self, type_: sqltypes.UnicodeText, **kw: Any
  6231. ) -> str:
  6232. return self.visit_TEXT(type_, **kw)
  6233. def visit_enum(self, type_: sqltypes.Enum, **kw: Any) -> str:
  6234. return self.visit_VARCHAR(type_, **kw)
  6235. def visit_null(self, type_, **kw):
  6236. raise exc.CompileError(
  6237. "Can't generate DDL for %r; "
  6238. "did you forget to specify a "
  6239. "type on this Column?" % type_
  6240. )
  6241. def visit_type_decorator(
  6242. self, type_: TypeDecorator[Any], **kw: Any
  6243. ) -> str:
  6244. return self.process(type_.type_engine(self.dialect), **kw)
  6245. def visit_user_defined(
  6246. self, type_: UserDefinedType[Any], **kw: Any
  6247. ) -> str:
  6248. return type_.get_col_spec(**kw)
  6249. class StrSQLTypeCompiler(GenericTypeCompiler):
  6250. def process(self, type_, **kw):
  6251. try:
  6252. _compiler_dispatch = type_._compiler_dispatch
  6253. except AttributeError:
  6254. return self._visit_unknown(type_, **kw)
  6255. else:
  6256. return _compiler_dispatch(self, **kw)
  6257. def __getattr__(self, key):
  6258. if key.startswith("visit_"):
  6259. return self._visit_unknown
  6260. else:
  6261. raise AttributeError(key)
  6262. def _visit_unknown(self, type_, **kw):
  6263. if type_.__class__.__name__ == type_.__class__.__name__.upper():
  6264. return type_.__class__.__name__
  6265. else:
  6266. return repr(type_)
  6267. def visit_null(self, type_, **kw):
  6268. return "NULL"
  6269. def visit_user_defined(self, type_, **kw):
  6270. try:
  6271. get_col_spec = type_.get_col_spec
  6272. except AttributeError:
  6273. return repr(type_)
  6274. else:
  6275. return get_col_spec(**kw)
  6276. class _SchemaForObjectCallable(Protocol):
  6277. def __call__(self, __obj: Any) -> str: ...
  6278. class _BindNameForColProtocol(Protocol):
  6279. def __call__(self, col: ColumnClause[Any]) -> str: ...
  6280. class IdentifierPreparer:
  6281. """Handle quoting and case-folding of identifiers based on options."""
  6282. reserved_words = RESERVED_WORDS
  6283. legal_characters = LEGAL_CHARACTERS
  6284. illegal_initial_characters = ILLEGAL_INITIAL_CHARACTERS
  6285. initial_quote: str
  6286. final_quote: str
  6287. _strings: MutableMapping[str, str]
  6288. schema_for_object: _SchemaForObjectCallable = operator.attrgetter("schema")
  6289. """Return the .schema attribute for an object.
  6290. For the default IdentifierPreparer, the schema for an object is always
  6291. the value of the ".schema" attribute. if the preparer is replaced
  6292. with one that has a non-empty schema_translate_map, the value of the
  6293. ".schema" attribute is rendered a symbol that will be converted to a
  6294. real schema name from the mapping post-compile.
  6295. """
  6296. _includes_none_schema_translate: bool = False
  6297. def __init__(
  6298. self,
  6299. dialect: Dialect,
  6300. initial_quote: str = '"',
  6301. final_quote: Optional[str] = None,
  6302. escape_quote: str = '"',
  6303. quote_case_sensitive_collations: bool = True,
  6304. omit_schema: bool = False,
  6305. ):
  6306. """Construct a new ``IdentifierPreparer`` object.
  6307. initial_quote
  6308. Character that begins a delimited identifier.
  6309. final_quote
  6310. Character that ends a delimited identifier. Defaults to
  6311. `initial_quote`.
  6312. omit_schema
  6313. Prevent prepending schema name. Useful for databases that do
  6314. not support schemae.
  6315. """
  6316. self.dialect = dialect
  6317. self.initial_quote = initial_quote
  6318. self.final_quote = final_quote or self.initial_quote
  6319. self.escape_quote = escape_quote
  6320. self.escape_to_quote = self.escape_quote * 2
  6321. self.omit_schema = omit_schema
  6322. self.quote_case_sensitive_collations = quote_case_sensitive_collations
  6323. self._strings = {}
  6324. self._double_percents = self.dialect.paramstyle in (
  6325. "format",
  6326. "pyformat",
  6327. )
  6328. def _with_schema_translate(self, schema_translate_map):
  6329. prep = self.__class__.__new__(self.__class__)
  6330. prep.__dict__.update(self.__dict__)
  6331. includes_none = None in schema_translate_map
  6332. def symbol_getter(obj):
  6333. name = obj.schema
  6334. if obj._use_schema_map and (name is not None or includes_none):
  6335. if name is not None and ("[" in name or "]" in name):
  6336. raise exc.CompileError(
  6337. "Square bracket characters ([]) not supported "
  6338. "in schema translate name '%s'" % name
  6339. )
  6340. return quoted_name(
  6341. "__[SCHEMA_%s]" % (name or "_none"), quote=False
  6342. )
  6343. else:
  6344. return obj.schema
  6345. prep.schema_for_object = symbol_getter
  6346. prep._includes_none_schema_translate = includes_none
  6347. return prep
  6348. def _render_schema_translates(
  6349. self, statement: str, schema_translate_map: SchemaTranslateMapType
  6350. ) -> str:
  6351. d = schema_translate_map
  6352. if None in d:
  6353. if not self._includes_none_schema_translate:
  6354. raise exc.InvalidRequestError(
  6355. "schema translate map which previously did not have "
  6356. "`None` present as a key now has `None` present; compiled "
  6357. "statement may lack adequate placeholders. Please use "
  6358. "consistent keys in successive "
  6359. "schema_translate_map dictionaries."
  6360. )
  6361. d["_none"] = d[None] # type: ignore[index]
  6362. def replace(m):
  6363. name = m.group(2)
  6364. if name in d:
  6365. effective_schema = d[name]
  6366. else:
  6367. if name in (None, "_none"):
  6368. raise exc.InvalidRequestError(
  6369. "schema translate map which previously had `None` "
  6370. "present as a key now no longer has it present; don't "
  6371. "know how to apply schema for compiled statement. "
  6372. "Please use consistent keys in successive "
  6373. "schema_translate_map dictionaries."
  6374. )
  6375. effective_schema = name
  6376. if not effective_schema:
  6377. effective_schema = self.dialect.default_schema_name
  6378. if not effective_schema:
  6379. # TODO: no coverage here
  6380. raise exc.CompileError(
  6381. "Dialect has no default schema name; can't "
  6382. "use None as dynamic schema target."
  6383. )
  6384. return self.quote_schema(effective_schema)
  6385. return re.sub(r"(__\[SCHEMA_([^\]]+)\])", replace, statement)
  6386. def _escape_identifier(self, value: str) -> str:
  6387. """Escape an identifier.
  6388. Subclasses should override this to provide database-dependent
  6389. escaping behavior.
  6390. """
  6391. value = value.replace(self.escape_quote, self.escape_to_quote)
  6392. if self._double_percents:
  6393. value = value.replace("%", "%%")
  6394. return value
  6395. def _unescape_identifier(self, value: str) -> str:
  6396. """Canonicalize an escaped identifier.
  6397. Subclasses should override this to provide database-dependent
  6398. unescaping behavior that reverses _escape_identifier.
  6399. """
  6400. return value.replace(self.escape_to_quote, self.escape_quote)
  6401. def validate_sql_phrase(self, element, reg):
  6402. """keyword sequence filter.
  6403. a filter for elements that are intended to represent keyword sequences,
  6404. such as "INITIALLY", "INITIALLY DEFERRED", etc. no special characters
  6405. should be present.
  6406. .. versionadded:: 1.3
  6407. """
  6408. if element is not None and not reg.match(element):
  6409. raise exc.CompileError(
  6410. "Unexpected SQL phrase: %r (matching against %r)"
  6411. % (element, reg.pattern)
  6412. )
  6413. return element
  6414. def quote_identifier(self, value: str) -> str:
  6415. """Quote an identifier.
  6416. Subclasses should override this to provide database-dependent
  6417. quoting behavior.
  6418. """
  6419. return (
  6420. self.initial_quote
  6421. + self._escape_identifier(value)
  6422. + self.final_quote
  6423. )
  6424. def _requires_quotes(self, value: str) -> bool:
  6425. """Return True if the given identifier requires quoting."""
  6426. lc_value = value.lower()
  6427. return (
  6428. lc_value in self.reserved_words
  6429. or value[0] in self.illegal_initial_characters
  6430. or not self.legal_characters.match(str(value))
  6431. or (lc_value != value)
  6432. )
  6433. def _requires_quotes_illegal_chars(self, value):
  6434. """Return True if the given identifier requires quoting, but
  6435. not taking case convention into account."""
  6436. return not self.legal_characters.match(str(value))
  6437. def quote_schema(self, schema: str, force: Any = None) -> str:
  6438. """Conditionally quote a schema name.
  6439. The name is quoted if it is a reserved word, contains quote-necessary
  6440. characters, or is an instance of :class:`.quoted_name` which includes
  6441. ``quote`` set to ``True``.
  6442. Subclasses can override this to provide database-dependent
  6443. quoting behavior for schema names.
  6444. :param schema: string schema name
  6445. :param force: unused
  6446. .. deprecated:: 0.9
  6447. The :paramref:`.IdentifierPreparer.quote_schema.force`
  6448. parameter is deprecated and will be removed in a future
  6449. release. This flag has no effect on the behavior of the
  6450. :meth:`.IdentifierPreparer.quote` method; please refer to
  6451. :class:`.quoted_name`.
  6452. """
  6453. if force is not None:
  6454. # not using the util.deprecated_params() decorator in this
  6455. # case because of the additional function call overhead on this
  6456. # very performance-critical spot.
  6457. util.warn_deprecated(
  6458. "The IdentifierPreparer.quote_schema.force parameter is "
  6459. "deprecated and will be removed in a future release. This "
  6460. "flag has no effect on the behavior of the "
  6461. "IdentifierPreparer.quote method; please refer to "
  6462. "quoted_name().",
  6463. # deprecated 0.9. warning from 1.3
  6464. version="0.9",
  6465. )
  6466. return self.quote(schema)
  6467. def quote(self, ident: str, force: Any = None) -> str:
  6468. """Conditionally quote an identifier.
  6469. The identifier is quoted if it is a reserved word, contains
  6470. quote-necessary characters, or is an instance of
  6471. :class:`.quoted_name` which includes ``quote`` set to ``True``.
  6472. Subclasses can override this to provide database-dependent
  6473. quoting behavior for identifier names.
  6474. :param ident: string identifier
  6475. :param force: unused
  6476. .. deprecated:: 0.9
  6477. The :paramref:`.IdentifierPreparer.quote.force`
  6478. parameter is deprecated and will be removed in a future
  6479. release. This flag has no effect on the behavior of the
  6480. :meth:`.IdentifierPreparer.quote` method; please refer to
  6481. :class:`.quoted_name`.
  6482. """
  6483. if force is not None:
  6484. # not using the util.deprecated_params() decorator in this
  6485. # case because of the additional function call overhead on this
  6486. # very performance-critical spot.
  6487. util.warn_deprecated(
  6488. "The IdentifierPreparer.quote.force parameter is "
  6489. "deprecated and will be removed in a future release. This "
  6490. "flag has no effect on the behavior of the "
  6491. "IdentifierPreparer.quote method; please refer to "
  6492. "quoted_name().",
  6493. # deprecated 0.9. warning from 1.3
  6494. version="0.9",
  6495. )
  6496. force = getattr(ident, "quote", None)
  6497. if force is None:
  6498. if ident in self._strings:
  6499. return self._strings[ident]
  6500. else:
  6501. if self._requires_quotes(ident):
  6502. self._strings[ident] = self.quote_identifier(ident)
  6503. else:
  6504. self._strings[ident] = ident
  6505. return self._strings[ident]
  6506. elif force:
  6507. return self.quote_identifier(ident)
  6508. else:
  6509. return ident
  6510. def format_collation(self, collation_name):
  6511. if self.quote_case_sensitive_collations:
  6512. return self.quote(collation_name)
  6513. else:
  6514. return collation_name
  6515. def format_sequence(
  6516. self, sequence: schema.Sequence, use_schema: bool = True
  6517. ) -> str:
  6518. name = self.quote(sequence.name)
  6519. effective_schema = self.schema_for_object(sequence)
  6520. if (
  6521. not self.omit_schema
  6522. and use_schema
  6523. and effective_schema is not None
  6524. ):
  6525. name = self.quote_schema(effective_schema) + "." + name
  6526. return name
  6527. def format_label(
  6528. self, label: Label[Any], name: Optional[str] = None
  6529. ) -> str:
  6530. return self.quote(name or label.name)
  6531. def format_alias(
  6532. self, alias: Optional[AliasedReturnsRows], name: Optional[str] = None
  6533. ) -> str:
  6534. if name is None:
  6535. assert alias is not None
  6536. return self.quote(alias.name)
  6537. else:
  6538. return self.quote(name)
  6539. def format_savepoint(self, savepoint, name=None):
  6540. # Running the savepoint name through quoting is unnecessary
  6541. # for all known dialects. This is here to support potential
  6542. # third party use cases
  6543. ident = name or savepoint.ident
  6544. if self._requires_quotes(ident):
  6545. ident = self.quote_identifier(ident)
  6546. return ident
  6547. @util.preload_module("sqlalchemy.sql.naming")
  6548. def format_constraint(
  6549. self, constraint: Union[Constraint, Index], _alembic_quote: bool = True
  6550. ) -> Optional[str]:
  6551. naming = util.preloaded.sql_naming
  6552. if constraint.name is _NONE_NAME:
  6553. name = naming._constraint_name_for_table(
  6554. constraint, constraint.table
  6555. )
  6556. if name is None:
  6557. return None
  6558. else:
  6559. name = constraint.name
  6560. assert name is not None
  6561. if constraint.__visit_name__ == "index":
  6562. return self.truncate_and_render_index_name(
  6563. name, _alembic_quote=_alembic_quote
  6564. )
  6565. else:
  6566. return self.truncate_and_render_constraint_name(
  6567. name, _alembic_quote=_alembic_quote
  6568. )
  6569. def truncate_and_render_index_name(
  6570. self, name: str, _alembic_quote: bool = True
  6571. ) -> str:
  6572. # calculate these at format time so that ad-hoc changes
  6573. # to dialect.max_identifier_length etc. can be reflected
  6574. # as IdentifierPreparer is long lived
  6575. max_ = (
  6576. self.dialect.max_index_name_length
  6577. or self.dialect.max_identifier_length
  6578. )
  6579. return self._truncate_and_render_maxlen_name(
  6580. name, max_, _alembic_quote
  6581. )
  6582. def truncate_and_render_constraint_name(
  6583. self, name: str, _alembic_quote: bool = True
  6584. ) -> str:
  6585. # calculate these at format time so that ad-hoc changes
  6586. # to dialect.max_identifier_length etc. can be reflected
  6587. # as IdentifierPreparer is long lived
  6588. max_ = (
  6589. self.dialect.max_constraint_name_length
  6590. or self.dialect.max_identifier_length
  6591. )
  6592. return self._truncate_and_render_maxlen_name(
  6593. name, max_, _alembic_quote
  6594. )
  6595. def _truncate_and_render_maxlen_name(
  6596. self, name: str, max_: int, _alembic_quote: bool
  6597. ) -> str:
  6598. if isinstance(name, elements._truncated_label):
  6599. if len(name) > max_:
  6600. name = name[0 : max_ - 8] + "_" + util.md5_hex(name)[-4:]
  6601. else:
  6602. self.dialect.validate_identifier(name)
  6603. if not _alembic_quote:
  6604. return name
  6605. else:
  6606. return self.quote(name)
  6607. def format_index(self, index: Index) -> str:
  6608. name = self.format_constraint(index)
  6609. assert name is not None
  6610. return name
  6611. def format_table(
  6612. self,
  6613. table: FromClause,
  6614. use_schema: bool = True,
  6615. name: Optional[str] = None,
  6616. ) -> str:
  6617. """Prepare a quoted table and schema name."""
  6618. if name is None:
  6619. if TYPE_CHECKING:
  6620. assert isinstance(table, NamedFromClause)
  6621. name = table.name
  6622. result = self.quote(name)
  6623. effective_schema = self.schema_for_object(table)
  6624. if not self.omit_schema and use_schema and effective_schema:
  6625. result = self.quote_schema(effective_schema) + "." + result
  6626. return result
  6627. def format_schema(self, name):
  6628. """Prepare a quoted schema name."""
  6629. return self.quote(name)
  6630. def format_label_name(
  6631. self,
  6632. name,
  6633. anon_map=None,
  6634. ):
  6635. """Prepare a quoted column name."""
  6636. if anon_map is not None and isinstance(
  6637. name, elements._truncated_label
  6638. ):
  6639. name = name.apply_map(anon_map)
  6640. return self.quote(name)
  6641. def format_column(
  6642. self,
  6643. column: ColumnElement[Any],
  6644. use_table: bool = False,
  6645. name: Optional[str] = None,
  6646. table_name: Optional[str] = None,
  6647. use_schema: bool = False,
  6648. anon_map: Optional[Mapping[str, Any]] = None,
  6649. ) -> str:
  6650. """Prepare a quoted column name."""
  6651. if name is None:
  6652. name = column.name
  6653. assert name is not None
  6654. if anon_map is not None and isinstance(
  6655. name, elements._truncated_label
  6656. ):
  6657. name = name.apply_map(anon_map)
  6658. if not getattr(column, "is_literal", False):
  6659. if use_table:
  6660. return (
  6661. self.format_table(
  6662. column.table, use_schema=use_schema, name=table_name
  6663. )
  6664. + "."
  6665. + self.quote(name)
  6666. )
  6667. else:
  6668. return self.quote(name)
  6669. else:
  6670. # literal textual elements get stuck into ColumnClause a lot,
  6671. # which shouldn't get quoted
  6672. if use_table:
  6673. return (
  6674. self.format_table(
  6675. column.table, use_schema=use_schema, name=table_name
  6676. )
  6677. + "."
  6678. + name
  6679. )
  6680. else:
  6681. return name
  6682. def format_table_seq(self, table, use_schema=True):
  6683. """Format table name and schema as a tuple."""
  6684. # Dialects with more levels in their fully qualified references
  6685. # ('database', 'owner', etc.) could override this and return
  6686. # a longer sequence.
  6687. effective_schema = self.schema_for_object(table)
  6688. if not self.omit_schema and use_schema and effective_schema:
  6689. return (
  6690. self.quote_schema(effective_schema),
  6691. self.format_table(table, use_schema=False),
  6692. )
  6693. else:
  6694. return (self.format_table(table, use_schema=False),)
  6695. @util.memoized_property
  6696. def _r_identifiers(self):
  6697. initial, final, escaped_final = (
  6698. re.escape(s)
  6699. for s in (
  6700. self.initial_quote,
  6701. self.final_quote,
  6702. self._escape_identifier(self.final_quote),
  6703. )
  6704. )
  6705. r = re.compile(
  6706. r"(?:"
  6707. r"(?:%(initial)s((?:%(escaped)s|[^%(final)s])+)%(final)s"
  6708. r"|([^\.]+))(?=\.|$))+"
  6709. % {"initial": initial, "final": final, "escaped": escaped_final}
  6710. )
  6711. return r
  6712. def unformat_identifiers(self, identifiers: str) -> Sequence[str]:
  6713. """Unpack 'schema.table.column'-like strings into components."""
  6714. r = self._r_identifiers
  6715. return [
  6716. self._unescape_identifier(i)
  6717. for i in [a or b for a, b in r.findall(identifiers)]
  6718. ]