elements.py 150 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403
  1. # sql/elements.py
  2. # Copyright (C) 2005-2017 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: http://www.opensource.org/licenses/mit-license.php
  7. """Core SQL expression elements, including :class:`.ClauseElement`,
  8. :class:`.ColumnElement`, and derived classes.
  9. """
  10. from __future__ import unicode_literals
  11. from .. import util, exc, inspection
  12. from . import type_api
  13. from . import operators
  14. from .visitors import Visitable, cloned_traverse, traverse
  15. from .annotation import Annotated
  16. import itertools
  17. from .base import Executable, PARSE_AUTOCOMMIT, Immutable, NO_ARG
  18. from .base import _generative
  19. import numbers
  20. import re
  21. import operator
  22. def _clone(element, **kw):
  23. return element._clone()
  24. def collate(expression, collation):
  25. """Return the clause ``expression COLLATE collation``.
  26. e.g.::
  27. collate(mycolumn, 'utf8_bin')
  28. produces::
  29. mycolumn COLLATE utf8_bin
  30. """
  31. expr = _literal_as_binds(expression)
  32. return BinaryExpression(
  33. expr,
  34. _literal_as_text(collation),
  35. operators.collate, type_=expr.type)
  36. def between(expr, lower_bound, upper_bound, symmetric=False):
  37. """Produce a ``BETWEEN`` predicate clause.
  38. E.g.::
  39. from sqlalchemy import between
  40. stmt = select([users_table]).where(between(users_table.c.id, 5, 7))
  41. Would produce SQL resembling::
  42. SELECT id, name FROM user WHERE id BETWEEN :id_1 AND :id_2
  43. The :func:`.between` function is a standalone version of the
  44. :meth:`.ColumnElement.between` method available on all
  45. SQL expressions, as in::
  46. stmt = select([users_table]).where(users_table.c.id.between(5, 7))
  47. All arguments passed to :func:`.between`, including the left side
  48. column expression, are coerced from Python scalar values if a
  49. the value is not a :class:`.ColumnElement` subclass. For example,
  50. three fixed values can be compared as in::
  51. print(between(5, 3, 7))
  52. Which would produce::
  53. :param_1 BETWEEN :param_2 AND :param_3
  54. :param expr: a column expression, typically a :class:`.ColumnElement`
  55. instance or alternatively a Python scalar expression to be coerced
  56. into a column expression, serving as the left side of the ``BETWEEN``
  57. expression.
  58. :param lower_bound: a column or Python scalar expression serving as the
  59. lower bound of the right side of the ``BETWEEN`` expression.
  60. :param upper_bound: a column or Python scalar expression serving as the
  61. upper bound of the right side of the ``BETWEEN`` expression.
  62. :param symmetric: if True, will render " BETWEEN SYMMETRIC ". Note
  63. that not all databases support this syntax.
  64. .. versionadded:: 0.9.5
  65. .. seealso::
  66. :meth:`.ColumnElement.between`
  67. """
  68. expr = _literal_as_binds(expr)
  69. return expr.between(lower_bound, upper_bound, symmetric=symmetric)
  70. def literal(value, type_=None):
  71. r"""Return a literal clause, bound to a bind parameter.
  72. Literal clauses are created automatically when non-
  73. :class:`.ClauseElement` objects (such as strings, ints, dates, etc.) are
  74. used in a comparison operation with a :class:`.ColumnElement` subclass,
  75. such as a :class:`~sqlalchemy.schema.Column` object. Use this function
  76. to force the generation of a literal clause, which will be created as a
  77. :class:`BindParameter` with a bound value.
  78. :param value: the value to be bound. Can be any Python object supported by
  79. the underlying DB-API, or is translatable via the given type argument.
  80. :param type\_: an optional :class:`~sqlalchemy.types.TypeEngine` which
  81. will provide bind-parameter translation for this literal.
  82. """
  83. return BindParameter(None, value, type_=type_, unique=True)
  84. def outparam(key, type_=None):
  85. """Create an 'OUT' parameter for usage in functions (stored procedures),
  86. for databases which support them.
  87. The ``outparam`` can be used like a regular function parameter.
  88. The "output" value will be available from the
  89. :class:`~sqlalchemy.engine.ResultProxy` object via its ``out_parameters``
  90. attribute, which returns a dictionary containing the values.
  91. """
  92. return BindParameter(
  93. key, None, type_=type_, unique=False, isoutparam=True)
  94. def not_(clause):
  95. """Return a negation of the given clause, i.e. ``NOT(clause)``.
  96. The ``~`` operator is also overloaded on all
  97. :class:`.ColumnElement` subclasses to produce the
  98. same result.
  99. """
  100. return operators.inv(_literal_as_binds(clause))
  101. @inspection._self_inspects
  102. class ClauseElement(Visitable):
  103. """Base class for elements of a programmatically constructed SQL
  104. expression.
  105. """
  106. __visit_name__ = 'clause'
  107. _annotations = {}
  108. supports_execution = False
  109. _from_objects = []
  110. bind = None
  111. _is_clone_of = None
  112. is_selectable = False
  113. is_clause_element = True
  114. description = None
  115. _order_by_label_element = None
  116. _is_from_container = False
  117. def _clone(self):
  118. """Create a shallow copy of this ClauseElement.
  119. This method may be used by a generative API. Its also used as
  120. part of the "deep" copy afforded by a traversal that combines
  121. the _copy_internals() method.
  122. """
  123. c = self.__class__.__new__(self.__class__)
  124. c.__dict__ = self.__dict__.copy()
  125. ClauseElement._cloned_set._reset(c)
  126. ColumnElement.comparator._reset(c)
  127. # this is a marker that helps to "equate" clauses to each other
  128. # when a Select returns its list of FROM clauses. the cloning
  129. # process leaves around a lot of remnants of the previous clause
  130. # typically in the form of column expressions still attached to the
  131. # old table.
  132. c._is_clone_of = self
  133. return c
  134. @property
  135. def _constructor(self):
  136. """return the 'constructor' for this ClauseElement.
  137. This is for the purposes for creating a new object of
  138. this type. Usually, its just the element's __class__.
  139. However, the "Annotated" version of the object overrides
  140. to return the class of its proxied element.
  141. """
  142. return self.__class__
  143. @util.memoized_property
  144. def _cloned_set(self):
  145. """Return the set consisting all cloned ancestors of this
  146. ClauseElement.
  147. Includes this ClauseElement. This accessor tends to be used for
  148. FromClause objects to identify 'equivalent' FROM clauses, regardless
  149. of transformative operations.
  150. """
  151. s = util.column_set()
  152. f = self
  153. while f is not None:
  154. s.add(f)
  155. f = f._is_clone_of
  156. return s
  157. def __getstate__(self):
  158. d = self.__dict__.copy()
  159. d.pop('_is_clone_of', None)
  160. return d
  161. def _annotate(self, values):
  162. """return a copy of this ClauseElement with annotations
  163. updated by the given dictionary.
  164. """
  165. return Annotated(self, values)
  166. def _with_annotations(self, values):
  167. """return a copy of this ClauseElement with annotations
  168. replaced by the given dictionary.
  169. """
  170. return Annotated(self, values)
  171. def _deannotate(self, values=None, clone=False):
  172. """return a copy of this :class:`.ClauseElement` with annotations
  173. removed.
  174. :param values: optional tuple of individual values
  175. to remove.
  176. """
  177. if clone:
  178. # clone is used when we are also copying
  179. # the expression for a deep deannotation
  180. return self._clone()
  181. else:
  182. # if no clone, since we have no annotations we return
  183. # self
  184. return self
  185. def _execute_on_connection(self, connection, multiparams, params):
  186. if self.supports_execution:
  187. return connection._execute_clauseelement(self, multiparams, params)
  188. else:
  189. raise exc.ObjectNotExecutableError(self)
  190. def unique_params(self, *optionaldict, **kwargs):
  191. """Return a copy with :func:`bindparam()` elements replaced.
  192. Same functionality as ``params()``, except adds `unique=True`
  193. to affected bind parameters so that multiple statements can be
  194. used.
  195. """
  196. return self._params(True, optionaldict, kwargs)
  197. def params(self, *optionaldict, **kwargs):
  198. """Return a copy with :func:`bindparam()` elements replaced.
  199. Returns a copy of this ClauseElement with :func:`bindparam()`
  200. elements replaced with values taken from the given dictionary::
  201. >>> clause = column('x') + bindparam('foo')
  202. >>> print clause.compile().params
  203. {'foo':None}
  204. >>> print clause.params({'foo':7}).compile().params
  205. {'foo':7}
  206. """
  207. return self._params(False, optionaldict, kwargs)
  208. def _params(self, unique, optionaldict, kwargs):
  209. if len(optionaldict) == 1:
  210. kwargs.update(optionaldict[0])
  211. elif len(optionaldict) > 1:
  212. raise exc.ArgumentError(
  213. "params() takes zero or one positional dictionary argument")
  214. def visit_bindparam(bind):
  215. if bind.key in kwargs:
  216. bind.value = kwargs[bind.key]
  217. bind.required = False
  218. if unique:
  219. bind._convert_to_unique()
  220. return cloned_traverse(self, {}, {'bindparam': visit_bindparam})
  221. def compare(self, other, **kw):
  222. r"""Compare this ClauseElement to the given ClauseElement.
  223. Subclasses should override the default behavior, which is a
  224. straight identity comparison.
  225. \**kw are arguments consumed by subclass compare() methods and
  226. may be used to modify the criteria for comparison.
  227. (see :class:`.ColumnElement`)
  228. """
  229. return self is other
  230. def _copy_internals(self, clone=_clone, **kw):
  231. """Reassign internal elements to be clones of themselves.
  232. Called during a copy-and-traverse operation on newly
  233. shallow-copied elements to create a deep copy.
  234. The given clone function should be used, which may be applying
  235. additional transformations to the element (i.e. replacement
  236. traversal, cloned traversal, annotations).
  237. """
  238. pass
  239. def get_children(self, **kwargs):
  240. r"""Return immediate child elements of this :class:`.ClauseElement`.
  241. This is used for visit traversal.
  242. \**kwargs may contain flags that change the collection that is
  243. returned, for example to return a subset of items in order to
  244. cut down on larger traversals, or to return child items from a
  245. different context (such as schema-level collections instead of
  246. clause-level).
  247. """
  248. return []
  249. def self_group(self, against=None):
  250. """Apply a 'grouping' to this :class:`.ClauseElement`.
  251. This method is overridden by subclasses to return a
  252. "grouping" construct, i.e. parenthesis. In particular
  253. it's used by "binary" expressions to provide a grouping
  254. around themselves when placed into a larger expression,
  255. as well as by :func:`.select` constructs when placed into
  256. the FROM clause of another :func:`.select`. (Note that
  257. subqueries should be normally created using the
  258. :meth:`.Select.alias` method, as many platforms require
  259. nested SELECT statements to be named).
  260. As expressions are composed together, the application of
  261. :meth:`self_group` is automatic - end-user code should never
  262. need to use this method directly. Note that SQLAlchemy's
  263. clause constructs take operator precedence into account -
  264. so parenthesis might not be needed, for example, in
  265. an expression like ``x OR (y AND z)`` - AND takes precedence
  266. over OR.
  267. The base :meth:`self_group` method of :class:`.ClauseElement`
  268. just returns self.
  269. """
  270. return self
  271. @util.dependencies("sqlalchemy.engine.default")
  272. def compile(self, default, bind=None, dialect=None, **kw):
  273. """Compile this SQL expression.
  274. The return value is a :class:`~.Compiled` object.
  275. Calling ``str()`` or ``unicode()`` on the returned value will yield a
  276. string representation of the result. The
  277. :class:`~.Compiled` object also can return a
  278. dictionary of bind parameter names and values
  279. using the ``params`` accessor.
  280. :param bind: An ``Engine`` or ``Connection`` from which a
  281. ``Compiled`` will be acquired. This argument takes precedence over
  282. this :class:`.ClauseElement`'s bound engine, if any.
  283. :param column_keys: Used for INSERT and UPDATE statements, a list of
  284. column names which should be present in the VALUES clause of the
  285. compiled statement. If ``None``, all columns from the target table
  286. object are rendered.
  287. :param dialect: A ``Dialect`` instance from which a ``Compiled``
  288. will be acquired. This argument takes precedence over the `bind`
  289. argument as well as this :class:`.ClauseElement`'s bound engine,
  290. if any.
  291. :param inline: Used for INSERT statements, for a dialect which does
  292. not support inline retrieval of newly generated primary key
  293. columns, will force the expression used to create the new primary
  294. key value to be rendered inline within the INSERT statement's
  295. VALUES clause. This typically refers to Sequence execution but may
  296. also refer to any server-side default generation function
  297. associated with a primary key `Column`.
  298. :param compile_kwargs: optional dictionary of additional parameters
  299. that will be passed through to the compiler within all "visit"
  300. methods. This allows any custom flag to be passed through to
  301. a custom compilation construct, for example. It is also used
  302. for the case of passing the ``literal_binds`` flag through::
  303. from sqlalchemy.sql import table, column, select
  304. t = table('t', column('x'))
  305. s = select([t]).where(t.c.x == 5)
  306. print s.compile(compile_kwargs={"literal_binds": True})
  307. .. versionadded:: 0.9.0
  308. .. seealso::
  309. :ref:`faq_sql_expression_string`
  310. """
  311. if not dialect:
  312. if bind:
  313. dialect = bind.dialect
  314. elif self.bind:
  315. dialect = self.bind.dialect
  316. bind = self.bind
  317. else:
  318. dialect = default.StrCompileDialect()
  319. return self._compiler(dialect, bind=bind, **kw)
  320. def _compiler(self, dialect, **kw):
  321. """Return a compiler appropriate for this ClauseElement, given a
  322. Dialect."""
  323. return dialect.statement_compiler(dialect, self, **kw)
  324. def __str__(self):
  325. if util.py3k:
  326. return str(self.compile())
  327. else:
  328. return unicode(self.compile()).encode('ascii', 'backslashreplace')
  329. def __and__(self, other):
  330. """'and' at the ClauseElement level.
  331. .. deprecated:: 0.9.5 - conjunctions are intended to be
  332. at the :class:`.ColumnElement`. level
  333. """
  334. return and_(self, other)
  335. def __or__(self, other):
  336. """'or' at the ClauseElement level.
  337. .. deprecated:: 0.9.5 - conjunctions are intended to be
  338. at the :class:`.ColumnElement`. level
  339. """
  340. return or_(self, other)
  341. def __invert__(self):
  342. if hasattr(self, 'negation_clause'):
  343. return self.negation_clause
  344. else:
  345. return self._negate()
  346. def _negate(self):
  347. return UnaryExpression(
  348. self.self_group(against=operators.inv),
  349. operator=operators.inv,
  350. negate=None)
  351. def __bool__(self):
  352. raise TypeError("Boolean value of this clause is not defined")
  353. __nonzero__ = __bool__
  354. def __repr__(self):
  355. friendly = self.description
  356. if friendly is None:
  357. return object.__repr__(self)
  358. else:
  359. return '<%s.%s at 0x%x; %s>' % (
  360. self.__module__, self.__class__.__name__, id(self), friendly)
  361. class ColumnElement(operators.ColumnOperators, ClauseElement):
  362. """Represent a column-oriented SQL expression suitable for usage in the
  363. "columns" clause, WHERE clause etc. of a statement.
  364. While the most familiar kind of :class:`.ColumnElement` is the
  365. :class:`.Column` object, :class:`.ColumnElement` serves as the basis
  366. for any unit that may be present in a SQL expression, including
  367. the expressions themselves, SQL functions, bound parameters,
  368. literal expressions, keywords such as ``NULL``, etc.
  369. :class:`.ColumnElement` is the ultimate base class for all such elements.
  370. A wide variety of SQLAlchemy Core functions work at the SQL expression
  371. level, and are intended to accept instances of :class:`.ColumnElement` as
  372. arguments. These functions will typically document that they accept a
  373. "SQL expression" as an argument. What this means in terms of SQLAlchemy
  374. usually refers to an input which is either already in the form of a
  375. :class:`.ColumnElement` object, or a value which can be **coerced** into
  376. one. The coercion rules followed by most, but not all, SQLAlchemy Core
  377. functions with regards to SQL expressions are as follows:
  378. * a literal Python value, such as a string, integer or floating
  379. point value, boolean, datetime, ``Decimal`` object, or virtually
  380. any other Python object, will be coerced into a "literal bound
  381. value". This generally means that a :func:`.bindparam` will be
  382. produced featuring the given value embedded into the construct; the
  383. resulting :class:`.BindParameter` object is an instance of
  384. :class:`.ColumnElement`. The Python value will ultimately be sent
  385. to the DBAPI at execution time as a paramterized argument to the
  386. ``execute()`` or ``executemany()`` methods, after SQLAlchemy
  387. type-specific converters (e.g. those provided by any associated
  388. :class:`.TypeEngine` objects) are applied to the value.
  389. * any special object value, typically ORM-level constructs, which
  390. feature a method called ``__clause_element__()``. The Core
  391. expression system looks for this method when an object of otherwise
  392. unknown type is passed to a function that is looking to coerce the
  393. argument into a :class:`.ColumnElement` expression. The
  394. ``__clause_element__()`` method, if present, should return a
  395. :class:`.ColumnElement` instance. The primary use of
  396. ``__clause_element__()`` within SQLAlchemy is that of class-bound
  397. attributes on ORM-mapped classes; a ``User`` class which contains a
  398. mapped attribute named ``.name`` will have a method
  399. ``User.name.__clause_element__()`` which when invoked returns the
  400. :class:`.Column` called ``name`` associated with the mapped table.
  401. * The Python ``None`` value is typically interpreted as ``NULL``,
  402. which in SQLAlchemy Core produces an instance of :func:`.null`.
  403. A :class:`.ColumnElement` provides the ability to generate new
  404. :class:`.ColumnElement`
  405. objects using Python expressions. This means that Python operators
  406. such as ``==``, ``!=`` and ``<`` are overloaded to mimic SQL operations,
  407. and allow the instantiation of further :class:`.ColumnElement` instances
  408. which are composed from other, more fundamental :class:`.ColumnElement`
  409. objects. For example, two :class:`.ColumnClause` objects can be added
  410. together with the addition operator ``+`` to produce
  411. a :class:`.BinaryExpression`.
  412. Both :class:`.ColumnClause` and :class:`.BinaryExpression` are subclasses
  413. of :class:`.ColumnElement`::
  414. >>> from sqlalchemy.sql import column
  415. >>> column('a') + column('b')
  416. <sqlalchemy.sql.expression.BinaryExpression object at 0x101029dd0>
  417. >>> print column('a') + column('b')
  418. a + b
  419. .. seealso::
  420. :class:`.Column`
  421. :func:`.expression.column`
  422. """
  423. __visit_name__ = 'column'
  424. primary_key = False
  425. foreign_keys = []
  426. _label = None
  427. """The named label that can be used to target
  428. this column in a result set.
  429. This label is almost always the label used when
  430. rendering <expr> AS <label> in a SELECT statement. It also
  431. refers to a name that this column expression can be located from
  432. in a result set.
  433. For a regular Column bound to a Table, this is typically the label
  434. <tablename>_<columnname>. For other constructs, different rules
  435. may apply, such as anonymized labels and others.
  436. """
  437. key = None
  438. """the 'key' that in some circumstances refers to this object in a
  439. Python namespace.
  440. This typically refers to the "key" of the column as present in the
  441. ``.c`` collection of a selectable, e.g. sometable.c["somekey"] would
  442. return a Column with a .key of "somekey".
  443. """
  444. _key_label = None
  445. """A label-based version of 'key' that in some circumstances refers
  446. to this object in a Python namespace.
  447. _key_label comes into play when a select() statement is constructed with
  448. apply_labels(); in this case, all Column objects in the ``.c`` collection
  449. are rendered as <tablename>_<columnname> in SQL; this is essentially the
  450. value of ._label. But to locate those columns in the ``.c`` collection,
  451. the name is along the lines of <tablename>_<key>; that's the typical
  452. value of .key_label.
  453. """
  454. _render_label_in_columns_clause = True
  455. """A flag used by select._columns_plus_names that helps to determine
  456. we are actually going to render in terms of "SELECT <col> AS <label>".
  457. This flag can be returned as False for some Column objects that want
  458. to be rendered as simple "SELECT <col>"; typically columns that don't have
  459. any parent table and are named the same as what the label would be
  460. in any case.
  461. """
  462. _resolve_label = None
  463. """The name that should be used to identify this ColumnElement in a
  464. select() object when "label resolution" logic is used; this refers
  465. to using a string name in an expression like order_by() or group_by()
  466. that wishes to target a labeled expression in the columns clause.
  467. The name is distinct from that of .name or ._label to account for the case
  468. where anonymizing logic may be used to change the name that's actually
  469. rendered at compile time; this attribute should hold onto the original
  470. name that was user-assigned when producing a .label() construct.
  471. """
  472. _allow_label_resolve = True
  473. """A flag that can be flipped to prevent a column from being resolvable
  474. by string label name."""
  475. _alt_names = ()
  476. def self_group(self, against=None):
  477. if (against in (operators.and_, operators.or_, operators._asbool) and
  478. self.type._type_affinity
  479. is type_api.BOOLEANTYPE._type_affinity):
  480. return AsBoolean(self, operators.istrue, operators.isfalse)
  481. elif (against in (operators.any_op, operators.all_op)):
  482. return Grouping(self)
  483. else:
  484. return self
  485. def _negate(self):
  486. if self.type._type_affinity is type_api.BOOLEANTYPE._type_affinity:
  487. # TODO: see the note in AsBoolean that it seems to assume
  488. # the element is the True_() / False_() constant, so this
  489. # is too broad
  490. return AsBoolean(self, operators.isfalse, operators.istrue)
  491. else:
  492. return super(ColumnElement, self)._negate()
  493. @util.memoized_property
  494. def type(self):
  495. return type_api.NULLTYPE
  496. @util.memoized_property
  497. def comparator(self):
  498. try:
  499. comparator_factory = self.type.comparator_factory
  500. except AttributeError:
  501. raise TypeError(
  502. "Object %r associated with '.type' attribute "
  503. "is not a TypeEngine class or object" % self.type)
  504. else:
  505. return comparator_factory(self)
  506. def __getattr__(self, key):
  507. try:
  508. return getattr(self.comparator, key)
  509. except AttributeError:
  510. raise AttributeError(
  511. 'Neither %r object nor %r object has an attribute %r' % (
  512. type(self).__name__,
  513. type(self.comparator).__name__,
  514. key)
  515. )
  516. def operate(self, op, *other, **kwargs):
  517. return op(self.comparator, *other, **kwargs)
  518. def reverse_operate(self, op, other, **kwargs):
  519. return op(other, self.comparator, **kwargs)
  520. def _bind_param(self, operator, obj, type_=None):
  521. return BindParameter(None, obj,
  522. _compared_to_operator=operator,
  523. type_=type_,
  524. _compared_to_type=self.type, unique=True)
  525. @property
  526. def expression(self):
  527. """Return a column expression.
  528. Part of the inspection interface; returns self.
  529. """
  530. return self
  531. @property
  532. def _select_iterable(self):
  533. return (self, )
  534. @util.memoized_property
  535. def base_columns(self):
  536. return util.column_set(c for c in self.proxy_set
  537. if not hasattr(c, '_proxies'))
  538. @util.memoized_property
  539. def proxy_set(self):
  540. s = util.column_set([self])
  541. if hasattr(self, '_proxies'):
  542. for c in self._proxies:
  543. s.update(c.proxy_set)
  544. return s
  545. def shares_lineage(self, othercolumn):
  546. """Return True if the given :class:`.ColumnElement`
  547. has a common ancestor to this :class:`.ColumnElement`."""
  548. return bool(self.proxy_set.intersection(othercolumn.proxy_set))
  549. def _compare_name_for_result(self, other):
  550. """Return True if the given column element compares to this one
  551. when targeting within a result row."""
  552. return hasattr(other, 'name') and hasattr(self, 'name') and \
  553. other.name == self.name
  554. def _make_proxy(
  555. self, selectable, name=None, name_is_truncatable=False, **kw):
  556. """Create a new :class:`.ColumnElement` representing this
  557. :class:`.ColumnElement` as it appears in the select list of a
  558. descending selectable.
  559. """
  560. if name is None:
  561. name = self.anon_label
  562. if self.key:
  563. key = self.key
  564. else:
  565. try:
  566. key = str(self)
  567. except exc.UnsupportedCompilationError:
  568. key = self.anon_label
  569. else:
  570. key = name
  571. co = ColumnClause(
  572. _as_truncated(name) if name_is_truncatable else name,
  573. type_=getattr(self, 'type', None),
  574. _selectable=selectable
  575. )
  576. co._proxies = [self]
  577. if selectable._is_clone_of is not None:
  578. co._is_clone_of = \
  579. selectable._is_clone_of.columns.get(key)
  580. selectable._columns[key] = co
  581. return co
  582. def compare(self, other, use_proxies=False, equivalents=None, **kw):
  583. """Compare this ColumnElement to another.
  584. Special arguments understood:
  585. :param use_proxies: when True, consider two columns that
  586. share a common base column as equivalent (i.e. shares_lineage())
  587. :param equivalents: a dictionary of columns as keys mapped to sets
  588. of columns. If the given "other" column is present in this
  589. dictionary, if any of the columns in the corresponding set() pass
  590. the comparison test, the result is True. This is used to expand the
  591. comparison to other columns that may be known to be equivalent to
  592. this one via foreign key or other criterion.
  593. """
  594. to_compare = (other, )
  595. if equivalents and other in equivalents:
  596. to_compare = equivalents[other].union(to_compare)
  597. for oth in to_compare:
  598. if use_proxies and self.shares_lineage(oth):
  599. return True
  600. elif hash(oth) == hash(self):
  601. return True
  602. else:
  603. return False
  604. def cast(self, type_):
  605. """Produce a type cast, i.e. ``CAST(<expression> AS <type>)``.
  606. This is a shortcut to the :func:`~.expression.cast` function.
  607. .. versionadded:: 1.0.7
  608. """
  609. return Cast(self, type_)
  610. def label(self, name):
  611. """Produce a column label, i.e. ``<columnname> AS <name>``.
  612. This is a shortcut to the :func:`~.expression.label` function.
  613. if 'name' is None, an anonymous label name will be generated.
  614. """
  615. return Label(name, self, self.type)
  616. @util.memoized_property
  617. def anon_label(self):
  618. """provides a constant 'anonymous label' for this ColumnElement.
  619. This is a label() expression which will be named at compile time.
  620. The same label() is returned each time anon_label is called so
  621. that expressions can reference anon_label multiple times, producing
  622. the same label name at compile time.
  623. the compiler uses this function automatically at compile time
  624. for expressions that are known to be 'unnamed' like binary
  625. expressions and function calls.
  626. """
  627. while self._is_clone_of is not None:
  628. self = self._is_clone_of
  629. return _anonymous_label(
  630. '%%(%d %s)s' % (id(self), getattr(self, 'name', 'anon'))
  631. )
  632. class BindParameter(ColumnElement):
  633. r"""Represent a "bound expression".
  634. :class:`.BindParameter` is invoked explicitly using the
  635. :func:`.bindparam` function, as in::
  636. from sqlalchemy import bindparam
  637. stmt = select([users_table]).\
  638. where(users_table.c.name == bindparam('username'))
  639. Detailed discussion of how :class:`.BindParameter` is used is
  640. at :func:`.bindparam`.
  641. .. seealso::
  642. :func:`.bindparam`
  643. """
  644. __visit_name__ = 'bindparam'
  645. _is_crud = False
  646. def __init__(self, key, value=NO_ARG, type_=None,
  647. unique=False, required=NO_ARG,
  648. quote=None, callable_=None,
  649. isoutparam=False,
  650. _compared_to_operator=None,
  651. _compared_to_type=None):
  652. r"""Produce a "bound expression".
  653. The return value is an instance of :class:`.BindParameter`; this
  654. is a :class:`.ColumnElement` subclass which represents a so-called
  655. "placeholder" value in a SQL expression, the value of which is
  656. supplied at the point at which the statement in executed against a
  657. database connection.
  658. In SQLAlchemy, the :func:`.bindparam` construct has
  659. the ability to carry along the actual value that will be ultimately
  660. used at expression time. In this way, it serves not just as
  661. a "placeholder" for eventual population, but also as a means of
  662. representing so-called "unsafe" values which should not be rendered
  663. directly in a SQL statement, but rather should be passed along
  664. to the :term:`DBAPI` as values which need to be correctly escaped
  665. and potentially handled for type-safety.
  666. When using :func:`.bindparam` explicitly, the use case is typically
  667. one of traditional deferment of parameters; the :func:`.bindparam`
  668. construct accepts a name which can then be referred to at execution
  669. time::
  670. from sqlalchemy import bindparam
  671. stmt = select([users_table]).\
  672. where(users_table.c.name == bindparam('username'))
  673. The above statement, when rendered, will produce SQL similar to::
  674. SELECT id, name FROM user WHERE name = :username
  675. In order to populate the value of ``:username`` above, the value
  676. would typically be applied at execution time to a method
  677. like :meth:`.Connection.execute`::
  678. result = connection.execute(stmt, username='wendy')
  679. Explicit use of :func:`.bindparam` is also common when producing
  680. UPDATE or DELETE statements that are to be invoked multiple times,
  681. where the WHERE criterion of the statement is to change on each
  682. invocation, such as::
  683. stmt = (users_table.update().
  684. where(user_table.c.name == bindparam('username')).
  685. values(fullname=bindparam('fullname'))
  686. )
  687. connection.execute(
  688. stmt, [{"username": "wendy", "fullname": "Wendy Smith"},
  689. {"username": "jack", "fullname": "Jack Jones"},
  690. ]
  691. )
  692. SQLAlchemy's Core expression system makes wide use of
  693. :func:`.bindparam` in an implicit sense. It is typical that Python
  694. literal values passed to virtually all SQL expression functions are
  695. coerced into fixed :func:`.bindparam` constructs. For example, given
  696. a comparison operation such as::
  697. expr = users_table.c.name == 'Wendy'
  698. The above expression will produce a :class:`.BinaryExpression`
  699. construct, where the left side is the :class:`.Column` object
  700. representing the ``name`` column, and the right side is a
  701. :class:`.BindParameter` representing the literal value::
  702. print(repr(expr.right))
  703. BindParameter('%(4327771088 name)s', 'Wendy', type_=String())
  704. The expression above will render SQL such as::
  705. user.name = :name_1
  706. Where the ``:name_1`` parameter name is an anonymous name. The
  707. actual string ``Wendy`` is not in the rendered string, but is carried
  708. along where it is later used within statement execution. If we
  709. invoke a statement like the following::
  710. stmt = select([users_table]).where(users_table.c.name == 'Wendy')
  711. result = connection.execute(stmt)
  712. We would see SQL logging output as::
  713. SELECT "user".id, "user".name
  714. FROM "user"
  715. WHERE "user".name = %(name_1)s
  716. {'name_1': 'Wendy'}
  717. Above, we see that ``Wendy`` is passed as a parameter to the database,
  718. while the placeholder ``:name_1`` is rendered in the appropriate form
  719. for the target database, in this case the PostgreSQL database.
  720. Similarly, :func:`.bindparam` is invoked automatically
  721. when working with :term:`CRUD` statements as far as the "VALUES"
  722. portion is concerned. The :func:`.insert` construct produces an
  723. ``INSERT`` expression which will, at statement execution time,
  724. generate bound placeholders based on the arguments passed, as in::
  725. stmt = users_table.insert()
  726. result = connection.execute(stmt, name='Wendy')
  727. The above will produce SQL output as::
  728. INSERT INTO "user" (name) VALUES (%(name)s)
  729. {'name': 'Wendy'}
  730. The :class:`.Insert` construct, at compilation/execution time,
  731. rendered a single :func:`.bindparam` mirroring the column
  732. name ``name`` as a result of the single ``name`` parameter
  733. we passed to the :meth:`.Connection.execute` method.
  734. :param key:
  735. the key (e.g. the name) for this bind param.
  736. Will be used in the generated
  737. SQL statement for dialects that use named parameters. This
  738. value may be modified when part of a compilation operation,
  739. if other :class:`BindParameter` objects exist with the same
  740. key, or if its length is too long and truncation is
  741. required.
  742. :param value:
  743. Initial value for this bind param. Will be used at statement
  744. execution time as the value for this parameter passed to the
  745. DBAPI, if no other value is indicated to the statement execution
  746. method for this particular parameter name. Defaults to ``None``.
  747. :param callable\_:
  748. A callable function that takes the place of "value". The function
  749. will be called at statement execution time to determine the
  750. ultimate value. Used for scenarios where the actual bind
  751. value cannot be determined at the point at which the clause
  752. construct is created, but embedded bind values are still desirable.
  753. :param type\_:
  754. A :class:`.TypeEngine` class or instance representing an optional
  755. datatype for this :func:`.bindparam`. If not passed, a type
  756. may be determined automatically for the bind, based on the given
  757. value; for example, trivial Python types such as ``str``,
  758. ``int``, ``bool``
  759. may result in the :class:`.String`, :class:`.Integer` or
  760. :class:`.Boolean` types being autoamtically selected.
  761. The type of a :func:`.bindparam` is significant especially in that
  762. the type will apply pre-processing to the value before it is
  763. passed to the database. For example, a :func:`.bindparam` which
  764. refers to a datetime value, and is specified as holding the
  765. :class:`.DateTime` type, may apply conversion needed to the
  766. value (such as stringification on SQLite) before passing the value
  767. to the database.
  768. :param unique:
  769. if True, the key name of this :class:`.BindParameter` will be
  770. modified if another :class:`.BindParameter` of the same name
  771. already has been located within the containing
  772. expression. This flag is used generally by the internals
  773. when producing so-called "anonymous" bound expressions, it
  774. isn't generally applicable to explicitly-named :func:`.bindparam`
  775. constructs.
  776. :param required:
  777. If ``True``, a value is required at execution time. If not passed,
  778. it defaults to ``True`` if neither :paramref:`.bindparam.value`
  779. or :paramref:`.bindparam.callable` were passed. If either of these
  780. parameters are present, then :paramref:`.bindparam.required`
  781. defaults to ``False``.
  782. .. versionchanged:: 0.8 If the ``required`` flag is not specified,
  783. it will be set automatically to ``True`` or ``False`` depending
  784. on whether or not the ``value`` or ``callable`` parameters
  785. were specified.
  786. :param quote:
  787. True if this parameter name requires quoting and is not
  788. currently known as a SQLAlchemy reserved word; this currently
  789. only applies to the Oracle backend, where bound names must
  790. sometimes be quoted.
  791. :param isoutparam:
  792. if True, the parameter should be treated like a stored procedure
  793. "OUT" parameter. This applies to backends such as Oracle which
  794. support OUT parameters.
  795. .. seealso::
  796. :ref:`coretutorial_bind_param`
  797. :ref:`coretutorial_insert_expressions`
  798. :func:`.outparam`
  799. """
  800. if isinstance(key, ColumnClause):
  801. type_ = key.type
  802. key = key.key
  803. if required is NO_ARG:
  804. required = (value is NO_ARG and callable_ is None)
  805. if value is NO_ARG:
  806. value = None
  807. if quote is not None:
  808. key = quoted_name(key, quote)
  809. if unique:
  810. self.key = _anonymous_label('%%(%d %s)s' % (id(self), key
  811. or 'param'))
  812. else:
  813. self.key = key or _anonymous_label('%%(%d param)s'
  814. % id(self))
  815. # identifying key that won't change across
  816. # clones, used to identify the bind's logical
  817. # identity
  818. self._identifying_key = self.key
  819. # key that was passed in the first place, used to
  820. # generate new keys
  821. self._orig_key = key or 'param'
  822. self.unique = unique
  823. self.value = value
  824. self.callable = callable_
  825. self.isoutparam = isoutparam
  826. self.required = required
  827. if type_ is None:
  828. if _compared_to_type is not None:
  829. self.type = \
  830. _compared_to_type.coerce_compared_value(
  831. _compared_to_operator, value)
  832. else:
  833. self.type = type_api._resolve_value_to_type(value)
  834. elif isinstance(type_, type):
  835. self.type = type_()
  836. else:
  837. self.type = type_
  838. def _with_value(self, value):
  839. """Return a copy of this :class:`.BindParameter` with the given value
  840. set.
  841. """
  842. cloned = self._clone()
  843. cloned.value = value
  844. cloned.callable = None
  845. cloned.required = False
  846. if cloned.type is type_api.NULLTYPE:
  847. cloned.type = type_api._resolve_value_to_type(value)
  848. return cloned
  849. @property
  850. def effective_value(self):
  851. """Return the value of this bound parameter,
  852. taking into account if the ``callable`` parameter
  853. was set.
  854. The ``callable`` value will be evaluated
  855. and returned if present, else ``value``.
  856. """
  857. if self.callable:
  858. return self.callable()
  859. else:
  860. return self.value
  861. def _clone(self):
  862. c = ClauseElement._clone(self)
  863. if self.unique:
  864. c.key = _anonymous_label('%%(%d %s)s' % (id(c), c._orig_key
  865. or 'param'))
  866. return c
  867. def _convert_to_unique(self):
  868. if not self.unique:
  869. self.unique = True
  870. self.key = _anonymous_label(
  871. '%%(%d %s)s' % (id(self), self._orig_key or 'param'))
  872. def compare(self, other, **kw):
  873. """Compare this :class:`BindParameter` to the given
  874. clause."""
  875. return isinstance(other, BindParameter) \
  876. and self.type._compare_type_affinity(other.type) \
  877. and self.value == other.value \
  878. and self.callable == other.callable
  879. def __getstate__(self):
  880. """execute a deferred value for serialization purposes."""
  881. d = self.__dict__.copy()
  882. v = self.value
  883. if self.callable:
  884. v = self.callable()
  885. d['callable'] = None
  886. d['value'] = v
  887. return d
  888. def __repr__(self):
  889. return 'BindParameter(%r, %r, type_=%r)' % (self.key,
  890. self.value, self.type)
  891. class TypeClause(ClauseElement):
  892. """Handle a type keyword in a SQL statement.
  893. Used by the ``Case`` statement.
  894. """
  895. __visit_name__ = 'typeclause'
  896. def __init__(self, type):
  897. self.type = type
  898. class TextClause(Executable, ClauseElement):
  899. """Represent a literal SQL text fragment.
  900. E.g.::
  901. from sqlalchemy import text
  902. t = text("SELECT * FROM users")
  903. result = connection.execute(t)
  904. The :class:`.Text` construct is produced using the :func:`.text`
  905. function; see that function for full documentation.
  906. .. seealso::
  907. :func:`.text`
  908. """
  909. __visit_name__ = 'textclause'
  910. _bind_params_regex = re.compile(r'(?<![:\w\x5c]):(\w+)(?!:)', re.UNICODE)
  911. _execution_options = \
  912. Executable._execution_options.union(
  913. {'autocommit': PARSE_AUTOCOMMIT})
  914. @property
  915. def _select_iterable(self):
  916. return (self,)
  917. @property
  918. def selectable(self):
  919. # allows text() to be considered by
  920. # _interpret_as_from
  921. return self
  922. _hide_froms = []
  923. # help in those cases where text() is
  924. # interpreted in a column expression situation
  925. key = _label = _resolve_label = None
  926. _allow_label_resolve = False
  927. def __init__(
  928. self,
  929. text,
  930. bind=None):
  931. self._bind = bind
  932. self._bindparams = {}
  933. def repl(m):
  934. self._bindparams[m.group(1)] = BindParameter(m.group(1))
  935. return ':%s' % m.group(1)
  936. # scan the string and search for bind parameter names, add them
  937. # to the list of bindparams
  938. self.text = self._bind_params_regex.sub(repl, text)
  939. @classmethod
  940. def _create_text(self, text, bind=None, bindparams=None,
  941. typemap=None, autocommit=None):
  942. r"""Construct a new :class:`.TextClause` clause, representing
  943. a textual SQL string directly.
  944. E.g.::
  945. from sqlalchemy import text
  946. t = text("SELECT * FROM users")
  947. result = connection.execute(t)
  948. The advantages :func:`.text` provides over a plain string are
  949. backend-neutral support for bind parameters, per-statement
  950. execution options, as well as
  951. bind parameter and result-column typing behavior, allowing
  952. SQLAlchemy type constructs to play a role when executing
  953. a statement that is specified literally. The construct can also
  954. be provided with a ``.c`` collection of column elements, allowing
  955. it to be embedded in other SQL expression constructs as a subquery.
  956. Bind parameters are specified by name, using the format ``:name``.
  957. E.g.::
  958. t = text("SELECT * FROM users WHERE id=:user_id")
  959. result = connection.execute(t, user_id=12)
  960. For SQL statements where a colon is required verbatim, as within
  961. an inline string, use a backslash to escape::
  962. t = text("SELECT * FROM users WHERE name='\:username'")
  963. The :class:`.TextClause` construct includes methods which can
  964. provide information about the bound parameters as well as the column
  965. values which would be returned from the textual statement, assuming
  966. it's an executable SELECT type of statement. The
  967. :meth:`.TextClause.bindparams` method is used to provide bound
  968. parameter detail, and :meth:`.TextClause.columns` method allows
  969. specification of return columns including names and types::
  970. t = text("SELECT * FROM users WHERE id=:user_id").\
  971. bindparams(user_id=7).\
  972. columns(id=Integer, name=String)
  973. for id, name in connection.execute(t):
  974. print(id, name)
  975. The :func:`.text` construct is used in cases when
  976. a literal string SQL fragment is specified as part of a larger query,
  977. such as for the WHERE clause of a SELECT statement::
  978. s = select([users.c.id, users.c.name]).where(text("id=:user_id"))
  979. result = connection.execute(s, user_id=12)
  980. :func:`.text` is also used for the construction
  981. of a full, standalone statement using plain text.
  982. As such, SQLAlchemy refers
  983. to it as an :class:`.Executable` object, and it supports
  984. the :meth:`Executable.execution_options` method. For example,
  985. a :func:`.text` construct that should be subject to "autocommit"
  986. can be set explicitly so using the
  987. :paramref:`.Connection.execution_options.autocommit` option::
  988. t = text("EXEC my_procedural_thing()").\
  989. execution_options(autocommit=True)
  990. Note that SQLAlchemy's usual "autocommit" behavior applies to
  991. :func:`.text` constructs implicitly - that is, statements which begin
  992. with a phrase such as ``INSERT``, ``UPDATE``, ``DELETE``,
  993. or a variety of other phrases specific to certain backends, will
  994. be eligible for autocommit if no transaction is in progress.
  995. :param text:
  996. the text of the SQL statement to be created. use ``:<param>``
  997. to specify bind parameters; they will be compiled to their
  998. engine-specific format.
  999. :param autocommit:
  1000. Deprecated. Use .execution_options(autocommit=<True|False>)
  1001. to set the autocommit option.
  1002. :param bind:
  1003. an optional connection or engine to be used for this text query.
  1004. :param bindparams:
  1005. Deprecated. A list of :func:`.bindparam` instances used to
  1006. provide information about parameters embedded in the statement.
  1007. This argument now invokes the :meth:`.TextClause.bindparams`
  1008. method on the construct before returning it. E.g.::
  1009. stmt = text("SELECT * FROM table WHERE id=:id",
  1010. bindparams=[bindparam('id', value=5, type_=Integer)])
  1011. Is equivalent to::
  1012. stmt = text("SELECT * FROM table WHERE id=:id").\
  1013. bindparams(bindparam('id', value=5, type_=Integer))
  1014. .. deprecated:: 0.9.0 the :meth:`.TextClause.bindparams` method
  1015. supersedes the ``bindparams`` argument to :func:`.text`.
  1016. :param typemap:
  1017. Deprecated. A dictionary mapping the names of columns
  1018. represented in the columns clause of a ``SELECT`` statement
  1019. to type objects,
  1020. which will be used to perform post-processing on columns within
  1021. the result set. This parameter now invokes the
  1022. :meth:`.TextClause.columns` method, which returns a
  1023. :class:`.TextAsFrom` construct that gains a ``.c`` collection and
  1024. can be embedded in other expressions. E.g.::
  1025. stmt = text("SELECT * FROM table",
  1026. typemap={'id': Integer, 'name': String},
  1027. )
  1028. Is equivalent to::
  1029. stmt = text("SELECT * FROM table").columns(id=Integer,
  1030. name=String)
  1031. Or alternatively::
  1032. from sqlalchemy.sql import column
  1033. stmt = text("SELECT * FROM table").columns(
  1034. column('id', Integer),
  1035. column('name', String)
  1036. )
  1037. .. deprecated:: 0.9.0 the :meth:`.TextClause.columns` method
  1038. supersedes the ``typemap`` argument to :func:`.text`.
  1039. .. seealso::
  1040. :ref:`sqlexpression_text` - in the Core tutorial
  1041. :ref:`orm_tutorial_literal_sql` - in the ORM tutorial
  1042. """
  1043. stmt = TextClause(text, bind=bind)
  1044. if bindparams:
  1045. stmt = stmt.bindparams(*bindparams)
  1046. if typemap:
  1047. stmt = stmt.columns(**typemap)
  1048. if autocommit is not None:
  1049. util.warn_deprecated('autocommit on text() is deprecated. '
  1050. 'Use .execution_options(autocommit=True)')
  1051. stmt = stmt.execution_options(autocommit=autocommit)
  1052. return stmt
  1053. @_generative
  1054. def bindparams(self, *binds, **names_to_values):
  1055. """Establish the values and/or types of bound parameters within
  1056. this :class:`.TextClause` construct.
  1057. Given a text construct such as::
  1058. from sqlalchemy import text
  1059. stmt = text("SELECT id, name FROM user WHERE name=:name "
  1060. "AND timestamp=:timestamp")
  1061. the :meth:`.TextClause.bindparams` method can be used to establish
  1062. the initial value of ``:name`` and ``:timestamp``,
  1063. using simple keyword arguments::
  1064. stmt = stmt.bindparams(name='jack',
  1065. timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5))
  1066. Where above, new :class:`.BindParameter` objects
  1067. will be generated with the names ``name`` and ``timestamp``, and
  1068. values of ``jack`` and ``datetime.datetime(2012, 10, 8, 15, 12, 5)``,
  1069. respectively. The types will be
  1070. inferred from the values given, in this case :class:`.String` and
  1071. :class:`.DateTime`.
  1072. When specific typing behavior is needed, the positional ``*binds``
  1073. argument can be used in which to specify :func:`.bindparam` constructs
  1074. directly. These constructs must include at least the ``key``
  1075. argument, then an optional value and type::
  1076. from sqlalchemy import bindparam
  1077. stmt = stmt.bindparams(
  1078. bindparam('name', value='jack', type_=String),
  1079. bindparam('timestamp', type_=DateTime)
  1080. )
  1081. Above, we specified the type of :class:`.DateTime` for the
  1082. ``timestamp`` bind, and the type of :class:`.String` for the ``name``
  1083. bind. In the case of ``name`` we also set the default value of
  1084. ``"jack"``.
  1085. Additional bound parameters can be supplied at statement execution
  1086. time, e.g.::
  1087. result = connection.execute(stmt,
  1088. timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5))
  1089. The :meth:`.TextClause.bindparams` method can be called repeatedly,
  1090. where it will re-use existing :class:`.BindParameter` objects to add
  1091. new information. For example, we can call
  1092. :meth:`.TextClause.bindparams` first with typing information, and a
  1093. second time with value information, and it will be combined::
  1094. stmt = text("SELECT id, name FROM user WHERE name=:name "
  1095. "AND timestamp=:timestamp")
  1096. stmt = stmt.bindparams(
  1097. bindparam('name', type_=String),
  1098. bindparam('timestamp', type_=DateTime)
  1099. )
  1100. stmt = stmt.bindparams(
  1101. name='jack',
  1102. timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5)
  1103. )
  1104. .. versionadded:: 0.9.0 The :meth:`.TextClause.bindparams` method
  1105. supersedes the argument ``bindparams`` passed to
  1106. :func:`~.expression.text`.
  1107. """
  1108. self._bindparams = new_params = self._bindparams.copy()
  1109. for bind in binds:
  1110. try:
  1111. existing = new_params[bind.key]
  1112. except KeyError:
  1113. raise exc.ArgumentError(
  1114. "This text() construct doesn't define a "
  1115. "bound parameter named %r" % bind.key)
  1116. else:
  1117. new_params[existing.key] = bind
  1118. for key, value in names_to_values.items():
  1119. try:
  1120. existing = new_params[key]
  1121. except KeyError:
  1122. raise exc.ArgumentError(
  1123. "This text() construct doesn't define a "
  1124. "bound parameter named %r" % key)
  1125. else:
  1126. new_params[key] = existing._with_value(value)
  1127. @util.dependencies('sqlalchemy.sql.selectable')
  1128. def columns(self, selectable, *cols, **types):
  1129. """Turn this :class:`.TextClause` object into a :class:`.TextAsFrom`
  1130. object that can be embedded into another statement.
  1131. This function essentially bridges the gap between an entirely
  1132. textual SELECT statement and the SQL expression language concept
  1133. of a "selectable"::
  1134. from sqlalchemy.sql import column, text
  1135. stmt = text("SELECT id, name FROM some_table")
  1136. stmt = stmt.columns(column('id'), column('name')).alias('st')
  1137. stmt = select([mytable]).\
  1138. select_from(
  1139. mytable.join(stmt, mytable.c.name == stmt.c.name)
  1140. ).where(stmt.c.id > 5)
  1141. Above, we pass a series of :func:`.column` elements to the
  1142. :meth:`.TextClause.columns` method positionally. These :func:`.column`
  1143. elements now become first class elements upon the :attr:`.TextAsFrom.c`
  1144. column collection, just like any other selectable.
  1145. The column expressions we pass to :meth:`.TextClause.columns` may
  1146. also be typed; when we do so, these :class:`.TypeEngine` objects become
  1147. the effective return type of the column, so that SQLAlchemy's
  1148. result-set-processing systems may be used on the return values.
  1149. This is often needed for types such as date or boolean types, as well
  1150. as for unicode processing on some dialect configurations::
  1151. stmt = text("SELECT id, name, timestamp FROM some_table")
  1152. stmt = stmt.columns(
  1153. column('id', Integer),
  1154. column('name', Unicode),
  1155. column('timestamp', DateTime)
  1156. )
  1157. for id, name, timestamp in connection.execute(stmt):
  1158. print(id, name, timestamp)
  1159. As a shortcut to the above syntax, keyword arguments referring to
  1160. types alone may be used, if only type conversion is needed::
  1161. stmt = text("SELECT id, name, timestamp FROM some_table")
  1162. stmt = stmt.columns(
  1163. id=Integer,
  1164. name=Unicode,
  1165. timestamp=DateTime
  1166. )
  1167. for id, name, timestamp in connection.execute(stmt):
  1168. print(id, name, timestamp)
  1169. The positional form of :meth:`.TextClause.columns` also provides
  1170. the unique feature of **positional column targeting**, which is
  1171. particularly useful when using the ORM with complex textual queries.
  1172. If we specify the columns from our model to :meth:`.TextClause.columns`,
  1173. the result set will match to those columns positionally, meaning the
  1174. name or origin of the column in the textual SQL doesn't matter::
  1175. stmt = text("SELECT users.id, addresses.id, users.id, "
  1176. "users.name, addresses.email_address AS email "
  1177. "FROM users JOIN addresses ON users.id=addresses.user_id "
  1178. "WHERE users.id = 1").columns(
  1179. User.id,
  1180. Address.id,
  1181. Address.user_id,
  1182. User.name,
  1183. Address.email_address
  1184. )
  1185. query = session.query(User).from_statement(stmt).options(
  1186. contains_eager(User.addresses))
  1187. .. versionadded:: 1.1 the :meth:`.TextClause.columns` method now
  1188. offers positional column targeting in the result set when
  1189. the column expressions are passed purely positionally.
  1190. The :meth:`.TextClause.columns` method provides a direct
  1191. route to calling :meth:`.FromClause.alias` as well as
  1192. :meth:`.SelectBase.cte` against a textual SELECT statement::
  1193. stmt = stmt.columns(id=Integer, name=String).cte('st')
  1194. stmt = select([sometable]).where(sometable.c.id == stmt.c.id)
  1195. .. versionadded:: 0.9.0 :func:`.text` can now be converted into a
  1196. fully featured "selectable" construct using the
  1197. :meth:`.TextClause.columns` method. This method supersedes the
  1198. ``typemap`` argument to :func:`.text`.
  1199. """
  1200. positional_input_cols = [
  1201. ColumnClause(col.key, types.pop(col.key))
  1202. if col.key in types
  1203. else col
  1204. for col in cols
  1205. ]
  1206. keyed_input_cols = [
  1207. ColumnClause(key, type_) for key, type_ in types.items()]
  1208. return selectable.TextAsFrom(
  1209. self,
  1210. positional_input_cols + keyed_input_cols,
  1211. positional=bool(positional_input_cols) and not keyed_input_cols)
  1212. @property
  1213. def type(self):
  1214. return type_api.NULLTYPE
  1215. @property
  1216. def comparator(self):
  1217. return self.type.comparator_factory(self)
  1218. def self_group(self, against=None):
  1219. if against is operators.in_op:
  1220. return Grouping(self)
  1221. else:
  1222. return self
  1223. def _copy_internals(self, clone=_clone, **kw):
  1224. self._bindparams = dict((b.key, clone(b, **kw))
  1225. for b in self._bindparams.values())
  1226. def get_children(self, **kwargs):
  1227. return list(self._bindparams.values())
  1228. def compare(self, other):
  1229. return isinstance(other, TextClause) and other.text == self.text
  1230. class Null(ColumnElement):
  1231. """Represent the NULL keyword in a SQL statement.
  1232. :class:`.Null` is accessed as a constant via the
  1233. :func:`.null` function.
  1234. """
  1235. __visit_name__ = 'null'
  1236. @util.memoized_property
  1237. def type(self):
  1238. return type_api.NULLTYPE
  1239. @classmethod
  1240. def _instance(cls):
  1241. """Return a constant :class:`.Null` construct."""
  1242. return Null()
  1243. def compare(self, other):
  1244. return isinstance(other, Null)
  1245. class False_(ColumnElement):
  1246. """Represent the ``false`` keyword, or equivalent, in a SQL statement.
  1247. :class:`.False_` is accessed as a constant via the
  1248. :func:`.false` function.
  1249. """
  1250. __visit_name__ = 'false'
  1251. @util.memoized_property
  1252. def type(self):
  1253. return type_api.BOOLEANTYPE
  1254. def _negate(self):
  1255. return True_()
  1256. @classmethod
  1257. def _instance(cls):
  1258. """Return a :class:`.False_` construct.
  1259. E.g.::
  1260. >>> from sqlalchemy import false
  1261. >>> print select([t.c.x]).where(false())
  1262. SELECT x FROM t WHERE false
  1263. A backend which does not support true/false constants will render as
  1264. an expression against 1 or 0::
  1265. >>> print select([t.c.x]).where(false())
  1266. SELECT x FROM t WHERE 0 = 1
  1267. The :func:`.true` and :func:`.false` constants also feature
  1268. "short circuit" operation within an :func:`.and_` or :func:`.or_`
  1269. conjunction::
  1270. >>> print select([t.c.x]).where(or_(t.c.x > 5, true()))
  1271. SELECT x FROM t WHERE true
  1272. >>> print select([t.c.x]).where(and_(t.c.x > 5, false()))
  1273. SELECT x FROM t WHERE false
  1274. .. versionchanged:: 0.9 :func:`.true` and :func:`.false` feature
  1275. better integrated behavior within conjunctions and on dialects
  1276. that don't support true/false constants.
  1277. .. seealso::
  1278. :func:`.true`
  1279. """
  1280. return False_()
  1281. def compare(self, other):
  1282. return isinstance(other, False_)
  1283. class True_(ColumnElement):
  1284. """Represent the ``true`` keyword, or equivalent, in a SQL statement.
  1285. :class:`.True_` is accessed as a constant via the
  1286. :func:`.true` function.
  1287. """
  1288. __visit_name__ = 'true'
  1289. @util.memoized_property
  1290. def type(self):
  1291. return type_api.BOOLEANTYPE
  1292. def _negate(self):
  1293. return False_()
  1294. @classmethod
  1295. def _ifnone(cls, other):
  1296. if other is None:
  1297. return cls._instance()
  1298. else:
  1299. return other
  1300. @classmethod
  1301. def _instance(cls):
  1302. """Return a constant :class:`.True_` construct.
  1303. E.g.::
  1304. >>> from sqlalchemy import true
  1305. >>> print select([t.c.x]).where(true())
  1306. SELECT x FROM t WHERE true
  1307. A backend which does not support true/false constants will render as
  1308. an expression against 1 or 0::
  1309. >>> print select([t.c.x]).where(true())
  1310. SELECT x FROM t WHERE 1 = 1
  1311. The :func:`.true` and :func:`.false` constants also feature
  1312. "short circuit" operation within an :func:`.and_` or :func:`.or_`
  1313. conjunction::
  1314. >>> print select([t.c.x]).where(or_(t.c.x > 5, true()))
  1315. SELECT x FROM t WHERE true
  1316. >>> print select([t.c.x]).where(and_(t.c.x > 5, false()))
  1317. SELECT x FROM t WHERE false
  1318. .. versionchanged:: 0.9 :func:`.true` and :func:`.false` feature
  1319. better integrated behavior within conjunctions and on dialects
  1320. that don't support true/false constants.
  1321. .. seealso::
  1322. :func:`.false`
  1323. """
  1324. return True_()
  1325. def compare(self, other):
  1326. return isinstance(other, True_)
  1327. class ClauseList(ClauseElement):
  1328. """Describe a list of clauses, separated by an operator.
  1329. By default, is comma-separated, such as a column listing.
  1330. """
  1331. __visit_name__ = 'clauselist'
  1332. def __init__(self, *clauses, **kwargs):
  1333. self.operator = kwargs.pop('operator', operators.comma_op)
  1334. self.group = kwargs.pop('group', True)
  1335. self.group_contents = kwargs.pop('group_contents', True)
  1336. text_converter = kwargs.pop(
  1337. '_literal_as_text',
  1338. _expression_literal_as_text)
  1339. if self.group_contents:
  1340. self.clauses = [
  1341. text_converter(clause).self_group(against=self.operator)
  1342. for clause in clauses]
  1343. else:
  1344. self.clauses = [
  1345. text_converter(clause)
  1346. for clause in clauses]
  1347. def __iter__(self):
  1348. return iter(self.clauses)
  1349. def __len__(self):
  1350. return len(self.clauses)
  1351. @property
  1352. def _select_iterable(self):
  1353. return iter(self)
  1354. def append(self, clause):
  1355. if self.group_contents:
  1356. self.clauses.append(_literal_as_text(clause).
  1357. self_group(against=self.operator))
  1358. else:
  1359. self.clauses.append(_literal_as_text(clause))
  1360. def _copy_internals(self, clone=_clone, **kw):
  1361. self.clauses = [clone(clause, **kw) for clause in self.clauses]
  1362. def get_children(self, **kwargs):
  1363. return self.clauses
  1364. @property
  1365. def _from_objects(self):
  1366. return list(itertools.chain(*[c._from_objects for c in self.clauses]))
  1367. def self_group(self, against=None):
  1368. if self.group and operators.is_precedent(self.operator, against):
  1369. return Grouping(self)
  1370. else:
  1371. return self
  1372. def compare(self, other, **kw):
  1373. """Compare this :class:`.ClauseList` to the given :class:`.ClauseList`,
  1374. including a comparison of all the clause items.
  1375. """
  1376. if not isinstance(other, ClauseList) and len(self.clauses) == 1:
  1377. return self.clauses[0].compare(other, **kw)
  1378. elif isinstance(other, ClauseList) and \
  1379. len(self.clauses) == len(other.clauses) and \
  1380. self.operator is other.operator:
  1381. if self.operator in (operators.and_, operators.or_):
  1382. completed = set()
  1383. for clause in self.clauses:
  1384. for other_clause in set(other.clauses).difference(completed):
  1385. if clause.compare(other_clause, **kw):
  1386. completed.add(other_clause)
  1387. break
  1388. return len(completed) == len(other.clauses)
  1389. else:
  1390. for i in range(0, len(self.clauses)):
  1391. if not self.clauses[i].compare(other.clauses[i], **kw):
  1392. return False
  1393. else:
  1394. return True
  1395. else:
  1396. return False
  1397. class BooleanClauseList(ClauseList, ColumnElement):
  1398. __visit_name__ = 'clauselist'
  1399. def __init__(self, *arg, **kw):
  1400. raise NotImplementedError(
  1401. "BooleanClauseList has a private constructor")
  1402. @classmethod
  1403. def _construct(cls, operator, continue_on, skip_on, *clauses, **kw):
  1404. convert_clauses = []
  1405. clauses = [
  1406. _expression_literal_as_text(clause)
  1407. for clause in
  1408. util.coerce_generator_arg(clauses)
  1409. ]
  1410. for clause in clauses:
  1411. if isinstance(clause, continue_on):
  1412. continue
  1413. elif isinstance(clause, skip_on):
  1414. return clause.self_group(against=operators._asbool)
  1415. convert_clauses.append(clause)
  1416. if len(convert_clauses) == 1:
  1417. return convert_clauses[0].self_group(against=operators._asbool)
  1418. elif not convert_clauses and clauses:
  1419. return clauses[0].self_group(against=operators._asbool)
  1420. convert_clauses = [c.self_group(against=operator)
  1421. for c in convert_clauses]
  1422. self = cls.__new__(cls)
  1423. self.clauses = convert_clauses
  1424. self.group = True
  1425. self.operator = operator
  1426. self.group_contents = True
  1427. self.type = type_api.BOOLEANTYPE
  1428. return self
  1429. @classmethod
  1430. def and_(cls, *clauses):
  1431. """Produce a conjunction of expressions joined by ``AND``.
  1432. E.g.::
  1433. from sqlalchemy import and_
  1434. stmt = select([users_table]).where(
  1435. and_(
  1436. users_table.c.name == 'wendy',
  1437. users_table.c.enrolled == True
  1438. )
  1439. )
  1440. The :func:`.and_` conjunction is also available using the
  1441. Python ``&`` operator (though note that compound expressions
  1442. need to be parenthesized in order to function with Python
  1443. operator precedence behavior)::
  1444. stmt = select([users_table]).where(
  1445. (users_table.c.name == 'wendy') &
  1446. (users_table.c.enrolled == True)
  1447. )
  1448. The :func:`.and_` operation is also implicit in some cases;
  1449. the :meth:`.Select.where` method for example can be invoked multiple
  1450. times against a statement, which will have the effect of each
  1451. clause being combined using :func:`.and_`::
  1452. stmt = select([users_table]).\
  1453. where(users_table.c.name == 'wendy').\
  1454. where(users_table.c.enrolled == True)
  1455. .. seealso::
  1456. :func:`.or_`
  1457. """
  1458. return cls._construct(operators.and_, True_, False_, *clauses)
  1459. @classmethod
  1460. def or_(cls, *clauses):
  1461. """Produce a conjunction of expressions joined by ``OR``.
  1462. E.g.::
  1463. from sqlalchemy import or_
  1464. stmt = select([users_table]).where(
  1465. or_(
  1466. users_table.c.name == 'wendy',
  1467. users_table.c.name == 'jack'
  1468. )
  1469. )
  1470. The :func:`.or_` conjunction is also available using the
  1471. Python ``|`` operator (though note that compound expressions
  1472. need to be parenthesized in order to function with Python
  1473. operator precedence behavior)::
  1474. stmt = select([users_table]).where(
  1475. (users_table.c.name == 'wendy') |
  1476. (users_table.c.name == 'jack')
  1477. )
  1478. .. seealso::
  1479. :func:`.and_`
  1480. """
  1481. return cls._construct(operators.or_, False_, True_, *clauses)
  1482. @property
  1483. def _select_iterable(self):
  1484. return (self, )
  1485. def self_group(self, against=None):
  1486. if not self.clauses:
  1487. return self
  1488. else:
  1489. return super(BooleanClauseList, self).self_group(against=against)
  1490. def _negate(self):
  1491. return ClauseList._negate(self)
  1492. and_ = BooleanClauseList.and_
  1493. or_ = BooleanClauseList.or_
  1494. class Tuple(ClauseList, ColumnElement):
  1495. """Represent a SQL tuple."""
  1496. def __init__(self, *clauses, **kw):
  1497. """Return a :class:`.Tuple`.
  1498. Main usage is to produce a composite IN construct::
  1499. from sqlalchemy import tuple_
  1500. tuple_(table.c.col1, table.c.col2).in_(
  1501. [(1, 2), (5, 12), (10, 19)]
  1502. )
  1503. .. warning::
  1504. The composite IN construct is not supported by all backends,
  1505. and is currently known to work on PostgreSQL and MySQL,
  1506. but not SQLite. Unsupported backends will raise
  1507. a subclass of :class:`~sqlalchemy.exc.DBAPIError` when such
  1508. an expression is invoked.
  1509. """
  1510. clauses = [_literal_as_binds(c) for c in clauses]
  1511. self._type_tuple = [arg.type for arg in clauses]
  1512. self.type = kw.pop('type_', self._type_tuple[0]
  1513. if self._type_tuple else type_api.NULLTYPE)
  1514. super(Tuple, self).__init__(*clauses, **kw)
  1515. @property
  1516. def _select_iterable(self):
  1517. return (self, )
  1518. def _bind_param(self, operator, obj, type_=None):
  1519. return Tuple(*[
  1520. BindParameter(None, o, _compared_to_operator=operator,
  1521. _compared_to_type=compared_to_type, unique=True,
  1522. type_=type_)
  1523. for o, compared_to_type in zip(obj, self._type_tuple)
  1524. ]).self_group()
  1525. class Case(ColumnElement):
  1526. """Represent a ``CASE`` expression.
  1527. :class:`.Case` is produced using the :func:`.case` factory function,
  1528. as in::
  1529. from sqlalchemy import case
  1530. stmt = select([users_table]).\
  1531. where(
  1532. case(
  1533. [
  1534. (users_table.c.name == 'wendy', 'W'),
  1535. (users_table.c.name == 'jack', 'J')
  1536. ],
  1537. else_='E'
  1538. )
  1539. )
  1540. Details on :class:`.Case` usage is at :func:`.case`.
  1541. .. seealso::
  1542. :func:`.case`
  1543. """
  1544. __visit_name__ = 'case'
  1545. def __init__(self, whens, value=None, else_=None):
  1546. r"""Produce a ``CASE`` expression.
  1547. The ``CASE`` construct in SQL is a conditional object that
  1548. acts somewhat analogously to an "if/then" construct in other
  1549. languages. It returns an instance of :class:`.Case`.
  1550. :func:`.case` in its usual form is passed a list of "when"
  1551. constructs, that is, a list of conditions and results as tuples::
  1552. from sqlalchemy import case
  1553. stmt = select([users_table]).\
  1554. where(
  1555. case(
  1556. [
  1557. (users_table.c.name == 'wendy', 'W'),
  1558. (users_table.c.name == 'jack', 'J')
  1559. ],
  1560. else_='E'
  1561. )
  1562. )
  1563. The above statement will produce SQL resembling::
  1564. SELECT id, name FROM user
  1565. WHERE CASE
  1566. WHEN (name = :name_1) THEN :param_1
  1567. WHEN (name = :name_2) THEN :param_2
  1568. ELSE :param_3
  1569. END
  1570. When simple equality expressions of several values against a single
  1571. parent column are needed, :func:`.case` also has a "shorthand" format
  1572. used via the
  1573. :paramref:`.case.value` parameter, which is passed a column
  1574. expression to be compared. In this form, the :paramref:`.case.whens`
  1575. parameter is passed as a dictionary containing expressions to be
  1576. compared against keyed to result expressions. The statement below is
  1577. equivalent to the preceding statement::
  1578. stmt = select([users_table]).\
  1579. where(
  1580. case(
  1581. {"wendy": "W", "jack": "J"},
  1582. value=users_table.c.name,
  1583. else_='E'
  1584. )
  1585. )
  1586. The values which are accepted as result values in
  1587. :paramref:`.case.whens` as well as with :paramref:`.case.else_` are
  1588. coerced from Python literals into :func:`.bindparam` constructs.
  1589. SQL expressions, e.g. :class:`.ColumnElement` constructs, are accepted
  1590. as well. To coerce a literal string expression into a constant
  1591. expression rendered inline, use the :func:`.literal_column` construct,
  1592. as in::
  1593. from sqlalchemy import case, literal_column
  1594. case(
  1595. [
  1596. (
  1597. orderline.c.qty > 100,
  1598. literal_column("'greaterthan100'")
  1599. ),
  1600. (
  1601. orderline.c.qty > 10,
  1602. literal_column("'greaterthan10'")
  1603. )
  1604. ],
  1605. else_=literal_column("'lessthan10'")
  1606. )
  1607. The above will render the given constants without using bound
  1608. parameters for the result values (but still for the comparison
  1609. values), as in::
  1610. CASE
  1611. WHEN (orderline.qty > :qty_1) THEN 'greaterthan100'
  1612. WHEN (orderline.qty > :qty_2) THEN 'greaterthan10'
  1613. ELSE 'lessthan10'
  1614. END
  1615. :param whens: The criteria to be compared against,
  1616. :paramref:`.case.whens` accepts two different forms, based on
  1617. whether or not :paramref:`.case.value` is used.
  1618. In the first form, it accepts a list of 2-tuples; each 2-tuple
  1619. consists of ``(<sql expression>, <value>)``, where the SQL
  1620. expression is a boolean expression and "value" is a resulting value,
  1621. e.g.::
  1622. case([
  1623. (users_table.c.name == 'wendy', 'W'),
  1624. (users_table.c.name == 'jack', 'J')
  1625. ])
  1626. In the second form, it accepts a Python dictionary of comparison
  1627. values mapped to a resulting value; this form requires
  1628. :paramref:`.case.value` to be present, and values will be compared
  1629. using the ``==`` operator, e.g.::
  1630. case(
  1631. {"wendy": "W", "jack": "J"},
  1632. value=users_table.c.name
  1633. )
  1634. :param value: An optional SQL expression which will be used as a
  1635. fixed "comparison point" for candidate values within a dictionary
  1636. passed to :paramref:`.case.whens`.
  1637. :param else\_: An optional SQL expression which will be the evaluated
  1638. result of the ``CASE`` construct if all expressions within
  1639. :paramref:`.case.whens` evaluate to false. When omitted, most
  1640. databases will produce a result of NULL if none of the "when"
  1641. expressions evaluate to true.
  1642. """
  1643. try:
  1644. whens = util.dictlike_iteritems(whens)
  1645. except TypeError:
  1646. pass
  1647. if value is not None:
  1648. whenlist = [
  1649. (_literal_as_binds(c).self_group(),
  1650. _literal_as_binds(r)) for (c, r) in whens
  1651. ]
  1652. else:
  1653. whenlist = [
  1654. (_no_literals(c).self_group(),
  1655. _literal_as_binds(r)) for (c, r) in whens
  1656. ]
  1657. if whenlist:
  1658. type_ = list(whenlist[-1])[-1].type
  1659. else:
  1660. type_ = None
  1661. if value is None:
  1662. self.value = None
  1663. else:
  1664. self.value = _literal_as_binds(value)
  1665. self.type = type_
  1666. self.whens = whenlist
  1667. if else_ is not None:
  1668. self.else_ = _literal_as_binds(else_)
  1669. else:
  1670. self.else_ = None
  1671. def _copy_internals(self, clone=_clone, **kw):
  1672. if self.value is not None:
  1673. self.value = clone(self.value, **kw)
  1674. self.whens = [(clone(x, **kw), clone(y, **kw))
  1675. for x, y in self.whens]
  1676. if self.else_ is not None:
  1677. self.else_ = clone(self.else_, **kw)
  1678. def get_children(self, **kwargs):
  1679. if self.value is not None:
  1680. yield self.value
  1681. for x, y in self.whens:
  1682. yield x
  1683. yield y
  1684. if self.else_ is not None:
  1685. yield self.else_
  1686. @property
  1687. def _from_objects(self):
  1688. return list(itertools.chain(*[x._from_objects for x in
  1689. self.get_children()]))
  1690. def literal_column(text, type_=None):
  1691. r"""Produce a :class:`.ColumnClause` object that has the
  1692. :paramref:`.column.is_literal` flag set to True.
  1693. :func:`.literal_column` is similar to :func:`.column`, except that
  1694. it is more often used as a "standalone" column expression that renders
  1695. exactly as stated; while :func:`.column` stores a string name that
  1696. will be assumed to be part of a table and may be quoted as such,
  1697. :func:`.literal_column` can be that, or any other arbitrary column-oriented
  1698. expression.
  1699. :param text: the text of the expression; can be any SQL expression.
  1700. Quoting rules will not be applied. To specify a column-name expression
  1701. which should be subject to quoting rules, use the :func:`column`
  1702. function.
  1703. :param type\_: an optional :class:`~sqlalchemy.types.TypeEngine`
  1704. object which will
  1705. provide result-set translation and additional expression semantics for
  1706. this column. If left as None the type will be NullType.
  1707. .. seealso::
  1708. :func:`.column`
  1709. :func:`.text`
  1710. :ref:`sqlexpression_literal_column`
  1711. """
  1712. return ColumnClause(text, type_=type_, is_literal=True)
  1713. class Cast(ColumnElement):
  1714. """Represent a ``CAST`` expression.
  1715. :class:`.Cast` is produced using the :func:`.cast` factory function,
  1716. as in::
  1717. from sqlalchemy import cast, Numeric
  1718. stmt = select([
  1719. cast(product_table.c.unit_price, Numeric(10, 4))
  1720. ])
  1721. Details on :class:`.Cast` usage is at :func:`.cast`.
  1722. .. seealso::
  1723. :func:`.cast`
  1724. """
  1725. __visit_name__ = 'cast'
  1726. def __init__(self, expression, type_):
  1727. """Produce a ``CAST`` expression.
  1728. :func:`.cast` returns an instance of :class:`.Cast`.
  1729. E.g.::
  1730. from sqlalchemy import cast, Numeric
  1731. stmt = select([
  1732. cast(product_table.c.unit_price, Numeric(10, 4))
  1733. ])
  1734. The above statement will produce SQL resembling::
  1735. SELECT CAST(unit_price AS NUMERIC(10, 4)) FROM product
  1736. The :func:`.cast` function performs two distinct functions when
  1737. used. The first is that it renders the ``CAST`` expression within
  1738. the resulting SQL string. The second is that it associates the given
  1739. type (e.g. :class:`.TypeEngine` class or instance) with the column
  1740. expression on the Python side, which means the expression will take
  1741. on the expression operator behavior associated with that type,
  1742. as well as the bound-value handling and result-row-handling behavior
  1743. of the type.
  1744. .. versionchanged:: 0.9.0 :func:`.cast` now applies the given type
  1745. to the expression such that it takes effect on the bound-value,
  1746. e.g. the Python-to-database direction, in addition to the
  1747. result handling, e.g. database-to-Python, direction.
  1748. An alternative to :func:`.cast` is the :func:`.type_coerce` function.
  1749. This function performs the second task of associating an expression
  1750. with a specific type, but does not render the ``CAST`` expression
  1751. in SQL.
  1752. :param expression: A SQL expression, such as a :class:`.ColumnElement`
  1753. expression or a Python string which will be coerced into a bound
  1754. literal value.
  1755. :param type_: A :class:`.TypeEngine` class or instance indicating
  1756. the type to which the ``CAST`` should apply.
  1757. .. seealso::
  1758. :func:`.type_coerce` - Python-side type coercion without emitting
  1759. CAST.
  1760. """
  1761. self.type = type_api.to_instance(type_)
  1762. self.clause = _literal_as_binds(expression, type_=self.type)
  1763. self.typeclause = TypeClause(self.type)
  1764. def _copy_internals(self, clone=_clone, **kw):
  1765. self.clause = clone(self.clause, **kw)
  1766. self.typeclause = clone(self.typeclause, **kw)
  1767. def get_children(self, **kwargs):
  1768. return self.clause, self.typeclause
  1769. @property
  1770. def _from_objects(self):
  1771. return self.clause._from_objects
  1772. class TypeCoerce(ColumnElement):
  1773. """Represent a Python-side type-coercion wrapper.
  1774. :class:`.TypeCoerce` supplies the :func:`.expression.type_coerce`
  1775. function; see that function for usage details.
  1776. .. versionchanged:: 1.1 The :func:`.type_coerce` function now produces
  1777. a persistent :class:`.TypeCoerce` wrapper object rather than
  1778. translating the given object in place.
  1779. .. seealso::
  1780. :func:`.expression.type_coerce`
  1781. """
  1782. __visit_name__ = 'type_coerce'
  1783. def __init__(self, expression, type_):
  1784. """Associate a SQL expression with a particular type, without rendering
  1785. ``CAST``.
  1786. E.g.::
  1787. from sqlalchemy import type_coerce
  1788. stmt = select([
  1789. type_coerce(log_table.date_string, StringDateTime())
  1790. ])
  1791. The above construct will produce a :class:`.TypeCoerce` object, which
  1792. renders SQL that labels the expression, but otherwise does not
  1793. modify its value on the SQL side::
  1794. SELECT date_string AS anon_1 FROM log
  1795. When result rows are fetched, the ``StringDateTime`` type
  1796. will be applied to result rows on behalf of the ``date_string`` column.
  1797. The rationale for the "anon_1" label is so that the type-coerced
  1798. column remains separate in the list of result columns vs. other
  1799. type-coerced or direct values of the target column. In order to
  1800. provide a named label for the expression, use
  1801. :meth:`.ColumnElement.label`::
  1802. stmt = select([
  1803. type_coerce(
  1804. log_table.date_string, StringDateTime()).label('date')
  1805. ])
  1806. A type that features bound-value handling will also have that behavior
  1807. take effect when literal values or :func:`.bindparam` constructs are
  1808. passed to :func:`.type_coerce` as targets.
  1809. For example, if a type implements the
  1810. :meth:`.TypeEngine.bind_expression`
  1811. method or :meth:`.TypeEngine.bind_processor` method or equivalent,
  1812. these functions will take effect at statement compilation/execution
  1813. time when a literal value is passed, as in::
  1814. # bound-value handling of MyStringType will be applied to the
  1815. # literal value "some string"
  1816. stmt = select([type_coerce("some string", MyStringType)])
  1817. :func:`.type_coerce` is similar to the :func:`.cast` function,
  1818. except that it does not render the ``CAST`` expression in the resulting
  1819. statement.
  1820. :param expression: A SQL expression, such as a :class:`.ColumnElement`
  1821. expression or a Python string which will be coerced into a bound
  1822. literal value.
  1823. :param type_: A :class:`.TypeEngine` class or instance indicating
  1824. the type to which the expression is coerced.
  1825. .. seealso::
  1826. :func:`.cast`
  1827. """
  1828. self.type = type_api.to_instance(type_)
  1829. self.clause = _literal_as_binds(expression, type_=self.type)
  1830. def _copy_internals(self, clone=_clone, **kw):
  1831. self.clause = clone(self.clause, **kw)
  1832. self.__dict__.pop('typed_expression', None)
  1833. def get_children(self, **kwargs):
  1834. return self.clause,
  1835. @property
  1836. def _from_objects(self):
  1837. return self.clause._from_objects
  1838. @util.memoized_property
  1839. def typed_expression(self):
  1840. if isinstance(self.clause, BindParameter):
  1841. bp = self.clause._clone()
  1842. bp.type = self.type
  1843. return bp
  1844. else:
  1845. return self.clause
  1846. class Extract(ColumnElement):
  1847. """Represent a SQL EXTRACT clause, ``extract(field FROM expr)``."""
  1848. __visit_name__ = 'extract'
  1849. def __init__(self, field, expr, **kwargs):
  1850. """Return a :class:`.Extract` construct.
  1851. This is typically available as :func:`.extract`
  1852. as well as ``func.extract`` from the
  1853. :data:`.func` namespace.
  1854. """
  1855. self.type = type_api.INTEGERTYPE
  1856. self.field = field
  1857. self.expr = _literal_as_binds(expr, None)
  1858. def _copy_internals(self, clone=_clone, **kw):
  1859. self.expr = clone(self.expr, **kw)
  1860. def get_children(self, **kwargs):
  1861. return self.expr,
  1862. @property
  1863. def _from_objects(self):
  1864. return self.expr._from_objects
  1865. class _label_reference(ColumnElement):
  1866. """Wrap a column expression as it appears in a 'reference' context.
  1867. This expression is any that inclues an _order_by_label_element,
  1868. which is a Label, or a DESC / ASC construct wrapping a Label.
  1869. The production of _label_reference() should occur when an expression
  1870. is added to this context; this includes the ORDER BY or GROUP BY of a
  1871. SELECT statement, as well as a few other places, such as the ORDER BY
  1872. within an OVER clause.
  1873. """
  1874. __visit_name__ = 'label_reference'
  1875. def __init__(self, element):
  1876. self.element = element
  1877. def _copy_internals(self, clone=_clone, **kw):
  1878. self.element = clone(self.element, **kw)
  1879. @property
  1880. def _from_objects(self):
  1881. return ()
  1882. class _textual_label_reference(ColumnElement):
  1883. __visit_name__ = 'textual_label_reference'
  1884. def __init__(self, element):
  1885. self.element = element
  1886. @util.memoized_property
  1887. def _text_clause(self):
  1888. return TextClause._create_text(self.element)
  1889. class UnaryExpression(ColumnElement):
  1890. """Define a 'unary' expression.
  1891. A unary expression has a single column expression
  1892. and an operator. The operator can be placed on the left
  1893. (where it is called the 'operator') or right (where it is called the
  1894. 'modifier') of the column expression.
  1895. :class:`.UnaryExpression` is the basis for several unary operators
  1896. including those used by :func:`.desc`, :func:`.asc`, :func:`.distinct`,
  1897. :func:`.nullsfirst` and :func:`.nullslast`.
  1898. """
  1899. __visit_name__ = 'unary'
  1900. def __init__(self, element, operator=None, modifier=None,
  1901. type_=None, negate=None, wraps_column_expression=False):
  1902. self.operator = operator
  1903. self.modifier = modifier
  1904. self.element = element.self_group(
  1905. against=self.operator or self.modifier)
  1906. self.type = type_api.to_instance(type_)
  1907. self.negate = negate
  1908. self.wraps_column_expression = wraps_column_expression
  1909. @classmethod
  1910. def _create_nullsfirst(cls, column):
  1911. """Produce the ``NULLS FIRST`` modifier for an ``ORDER BY`` expression.
  1912. :func:`.nullsfirst` is intended to modify the expression produced
  1913. by :func:`.asc` or :func:`.desc`, and indicates how NULL values
  1914. should be handled when they are encountered during ordering::
  1915. from sqlalchemy import desc, nullsfirst
  1916. stmt = select([users_table]).\
  1917. order_by(nullsfirst(desc(users_table.c.name)))
  1918. The SQL expression from the above would resemble::
  1919. SELECT id, name FROM user ORDER BY name DESC NULLS FIRST
  1920. Like :func:`.asc` and :func:`.desc`, :func:`.nullsfirst` is typically
  1921. invoked from the column expression itself using
  1922. :meth:`.ColumnElement.nullsfirst`, rather than as its standalone
  1923. function version, as in::
  1924. stmt = (select([users_table]).
  1925. order_by(users_table.c.name.desc().nullsfirst())
  1926. )
  1927. .. seealso::
  1928. :func:`.asc`
  1929. :func:`.desc`
  1930. :func:`.nullslast`
  1931. :meth:`.Select.order_by`
  1932. """
  1933. return UnaryExpression(
  1934. _literal_as_label_reference(column),
  1935. modifier=operators.nullsfirst_op,
  1936. wraps_column_expression=False)
  1937. @classmethod
  1938. def _create_nullslast(cls, column):
  1939. """Produce the ``NULLS LAST`` modifier for an ``ORDER BY`` expression.
  1940. :func:`.nullslast` is intended to modify the expression produced
  1941. by :func:`.asc` or :func:`.desc`, and indicates how NULL values
  1942. should be handled when they are encountered during ordering::
  1943. from sqlalchemy import desc, nullslast
  1944. stmt = select([users_table]).\
  1945. order_by(nullslast(desc(users_table.c.name)))
  1946. The SQL expression from the above would resemble::
  1947. SELECT id, name FROM user ORDER BY name DESC NULLS LAST
  1948. Like :func:`.asc` and :func:`.desc`, :func:`.nullslast` is typically
  1949. invoked from the column expression itself using
  1950. :meth:`.ColumnElement.nullslast`, rather than as its standalone
  1951. function version, as in::
  1952. stmt = select([users_table]).\
  1953. order_by(users_table.c.name.desc().nullslast())
  1954. .. seealso::
  1955. :func:`.asc`
  1956. :func:`.desc`
  1957. :func:`.nullsfirst`
  1958. :meth:`.Select.order_by`
  1959. """
  1960. return UnaryExpression(
  1961. _literal_as_label_reference(column),
  1962. modifier=operators.nullslast_op,
  1963. wraps_column_expression=False)
  1964. @classmethod
  1965. def _create_desc(cls, column):
  1966. """Produce a descending ``ORDER BY`` clause element.
  1967. e.g.::
  1968. from sqlalchemy import desc
  1969. stmt = select([users_table]).order_by(desc(users_table.c.name))
  1970. will produce SQL as::
  1971. SELECT id, name FROM user ORDER BY name DESC
  1972. The :func:`.desc` function is a standalone version of the
  1973. :meth:`.ColumnElement.desc` method available on all SQL expressions,
  1974. e.g.::
  1975. stmt = select([users_table]).order_by(users_table.c.name.desc())
  1976. :param column: A :class:`.ColumnElement` (e.g. scalar SQL expression)
  1977. with which to apply the :func:`.desc` operation.
  1978. .. seealso::
  1979. :func:`.asc`
  1980. :func:`.nullsfirst`
  1981. :func:`.nullslast`
  1982. :meth:`.Select.order_by`
  1983. """
  1984. return UnaryExpression(
  1985. _literal_as_label_reference(column),
  1986. modifier=operators.desc_op,
  1987. wraps_column_expression=False)
  1988. @classmethod
  1989. def _create_asc(cls, column):
  1990. """Produce an ascending ``ORDER BY`` clause element.
  1991. e.g.::
  1992. from sqlalchemy import asc
  1993. stmt = select([users_table]).order_by(asc(users_table.c.name))
  1994. will produce SQL as::
  1995. SELECT id, name FROM user ORDER BY name ASC
  1996. The :func:`.asc` function is a standalone version of the
  1997. :meth:`.ColumnElement.asc` method available on all SQL expressions,
  1998. e.g.::
  1999. stmt = select([users_table]).order_by(users_table.c.name.asc())
  2000. :param column: A :class:`.ColumnElement` (e.g. scalar SQL expression)
  2001. with which to apply the :func:`.asc` operation.
  2002. .. seealso::
  2003. :func:`.desc`
  2004. :func:`.nullsfirst`
  2005. :func:`.nullslast`
  2006. :meth:`.Select.order_by`
  2007. """
  2008. return UnaryExpression(
  2009. _literal_as_label_reference(column),
  2010. modifier=operators.asc_op,
  2011. wraps_column_expression=False)
  2012. @classmethod
  2013. def _create_distinct(cls, expr):
  2014. """Produce an column-expression-level unary ``DISTINCT`` clause.
  2015. This applies the ``DISTINCT`` keyword to an individual column
  2016. expression, and is typically contained within an aggregate function,
  2017. as in::
  2018. from sqlalchemy import distinct, func
  2019. stmt = select([func.count(distinct(users_table.c.name))])
  2020. The above would produce an expression resembling::
  2021. SELECT COUNT(DISTINCT name) FROM user
  2022. The :func:`.distinct` function is also available as a column-level
  2023. method, e.g. :meth:`.ColumnElement.distinct`, as in::
  2024. stmt = select([func.count(users_table.c.name.distinct())])
  2025. The :func:`.distinct` operator is different from the
  2026. :meth:`.Select.distinct` method of :class:`.Select`,
  2027. which produces a ``SELECT`` statement
  2028. with ``DISTINCT`` applied to the result set as a whole,
  2029. e.g. a ``SELECT DISTINCT`` expression. See that method for further
  2030. information.
  2031. .. seealso::
  2032. :meth:`.ColumnElement.distinct`
  2033. :meth:`.Select.distinct`
  2034. :data:`.func`
  2035. """
  2036. expr = _literal_as_binds(expr)
  2037. return UnaryExpression(
  2038. expr, operator=operators.distinct_op,
  2039. type_=expr.type, wraps_column_expression=False)
  2040. @property
  2041. def _order_by_label_element(self):
  2042. if self.modifier in (operators.desc_op, operators.asc_op):
  2043. return self.element._order_by_label_element
  2044. else:
  2045. return None
  2046. @property
  2047. def _from_objects(self):
  2048. return self.element._from_objects
  2049. def _copy_internals(self, clone=_clone, **kw):
  2050. self.element = clone(self.element, **kw)
  2051. def get_children(self, **kwargs):
  2052. return self.element,
  2053. def compare(self, other, **kw):
  2054. """Compare this :class:`UnaryExpression` against the given
  2055. :class:`.ClauseElement`."""
  2056. return (
  2057. isinstance(other, UnaryExpression) and
  2058. self.operator == other.operator and
  2059. self.modifier == other.modifier and
  2060. self.element.compare(other.element, **kw)
  2061. )
  2062. def _negate(self):
  2063. if self.negate is not None:
  2064. return UnaryExpression(
  2065. self.element,
  2066. operator=self.negate,
  2067. negate=self.operator,
  2068. modifier=self.modifier,
  2069. type_=self.type,
  2070. wraps_column_expression=self.wraps_column_expression)
  2071. elif self.type._type_affinity is type_api.BOOLEANTYPE._type_affinity:
  2072. return UnaryExpression(
  2073. self.self_group(against=operators.inv),
  2074. operator=operators.inv,
  2075. type_=type_api.BOOLEANTYPE,
  2076. wraps_column_expression=self.wraps_column_expression,
  2077. negate=None)
  2078. else:
  2079. return ClauseElement._negate(self)
  2080. def self_group(self, against=None):
  2081. if self.operator and operators.is_precedent(self.operator, against):
  2082. return Grouping(self)
  2083. else:
  2084. return self
  2085. class CollectionAggregate(UnaryExpression):
  2086. """Forms the basis for right-hand collection operator modifiers
  2087. ANY and ALL.
  2088. The ANY and ALL keywords are available in different ways on different
  2089. backends. On PostgreSQL, they only work for an ARRAY type. On
  2090. MySQL, they only work for subqueries.
  2091. """
  2092. @classmethod
  2093. def _create_any(cls, expr):
  2094. """Produce an ANY expression.
  2095. This may apply to an array type for some dialects (e.g. postgresql),
  2096. or to a subquery for others (e.g. mysql). e.g.::
  2097. # postgresql '5 = ANY (somearray)'
  2098. expr = 5 == any_(mytable.c.somearray)
  2099. # mysql '5 = ANY (SELECT value FROM table)'
  2100. expr = 5 == any_(select([table.c.value]))
  2101. .. versionadded:: 1.1
  2102. .. seealso::
  2103. :func:`.expression.all_`
  2104. """
  2105. expr = _literal_as_binds(expr)
  2106. if expr.is_selectable and hasattr(expr, 'as_scalar'):
  2107. expr = expr.as_scalar()
  2108. expr = expr.self_group()
  2109. return CollectionAggregate(
  2110. expr, operator=operators.any_op,
  2111. type_=type_api.NULLTYPE, wraps_column_expression=False)
  2112. @classmethod
  2113. def _create_all(cls, expr):
  2114. """Produce an ALL expression.
  2115. This may apply to an array type for some dialects (e.g. postgresql),
  2116. or to a subquery for others (e.g. mysql). e.g.::
  2117. # postgresql '5 = ALL (somearray)'
  2118. expr = 5 == all_(mytable.c.somearray)
  2119. # mysql '5 = ALL (SELECT value FROM table)'
  2120. expr = 5 == all_(select([table.c.value]))
  2121. .. versionadded:: 1.1
  2122. .. seealso::
  2123. :func:`.expression.any_`
  2124. """
  2125. expr = _literal_as_binds(expr)
  2126. if expr.is_selectable and hasattr(expr, 'as_scalar'):
  2127. expr = expr.as_scalar()
  2128. expr = expr.self_group()
  2129. return CollectionAggregate(
  2130. expr, operator=operators.all_op,
  2131. type_=type_api.NULLTYPE, wraps_column_expression=False)
  2132. # operate and reverse_operate are hardwired to
  2133. # dispatch onto the type comparator directly, so that we can
  2134. # ensure "reversed" behavior.
  2135. def operate(self, op, *other, **kwargs):
  2136. if not operators.is_comparison(op):
  2137. raise exc.ArgumentError(
  2138. "Only comparison operators may be used with ANY/ALL")
  2139. kwargs['reverse'] = True
  2140. return self.comparator.operate(operators.mirror(op), *other, **kwargs)
  2141. def reverse_operate(self, op, other, **kwargs):
  2142. # comparison operators should never call reverse_operate
  2143. assert not operators.is_comparison(op)
  2144. raise exc.ArgumentError(
  2145. "Only comparison operators may be used with ANY/ALL")
  2146. class AsBoolean(UnaryExpression):
  2147. def __init__(self, element, operator, negate):
  2148. self.element = element
  2149. self.type = type_api.BOOLEANTYPE
  2150. self.operator = operator
  2151. self.negate = negate
  2152. self.modifier = None
  2153. self.wraps_column_expression = True
  2154. def self_group(self, against=None):
  2155. return self
  2156. def _negate(self):
  2157. # TODO: this assumes the element is the True_() or False_()
  2158. # object, but this assumption isn't enforced and
  2159. # ColumnElement._negate() can send any number of expressions here
  2160. return self.element._negate()
  2161. class BinaryExpression(ColumnElement):
  2162. """Represent an expression that is ``LEFT <operator> RIGHT``.
  2163. A :class:`.BinaryExpression` is generated automatically
  2164. whenever two column expressions are used in a Python binary expression::
  2165. >>> from sqlalchemy.sql import column
  2166. >>> column('a') + column('b')
  2167. <sqlalchemy.sql.expression.BinaryExpression object at 0x101029dd0>
  2168. >>> print column('a') + column('b')
  2169. a + b
  2170. """
  2171. __visit_name__ = 'binary'
  2172. def __init__(self, left, right, operator, type_=None,
  2173. negate=None, modifiers=None):
  2174. # allow compatibility with libraries that
  2175. # refer to BinaryExpression directly and pass strings
  2176. if isinstance(operator, util.string_types):
  2177. operator = operators.custom_op(operator)
  2178. self._orig = (left, right)
  2179. self.left = left.self_group(against=operator)
  2180. self.right = right.self_group(against=operator)
  2181. self.operator = operator
  2182. self.type = type_api.to_instance(type_)
  2183. self.negate = negate
  2184. if modifiers is None:
  2185. self.modifiers = {}
  2186. else:
  2187. self.modifiers = modifiers
  2188. def __bool__(self):
  2189. if self.operator in (operator.eq, operator.ne):
  2190. return self.operator(hash(self._orig[0]), hash(self._orig[1]))
  2191. else:
  2192. raise TypeError("Boolean value of this clause is not defined")
  2193. __nonzero__ = __bool__
  2194. @property
  2195. def is_comparison(self):
  2196. return operators.is_comparison(self.operator)
  2197. @property
  2198. def _from_objects(self):
  2199. return self.left._from_objects + self.right._from_objects
  2200. def _copy_internals(self, clone=_clone, **kw):
  2201. self.left = clone(self.left, **kw)
  2202. self.right = clone(self.right, **kw)
  2203. def get_children(self, **kwargs):
  2204. return self.left, self.right
  2205. def compare(self, other, **kw):
  2206. """Compare this :class:`BinaryExpression` against the
  2207. given :class:`BinaryExpression`."""
  2208. return (
  2209. isinstance(other, BinaryExpression) and
  2210. self.operator == other.operator and
  2211. (
  2212. self.left.compare(other.left, **kw) and
  2213. self.right.compare(other.right, **kw) or
  2214. (
  2215. operators.is_commutative(self.operator) and
  2216. self.left.compare(other.right, **kw) and
  2217. self.right.compare(other.left, **kw)
  2218. )
  2219. )
  2220. )
  2221. def self_group(self, against=None):
  2222. if operators.is_precedent(self.operator, against):
  2223. return Grouping(self)
  2224. else:
  2225. return self
  2226. def _negate(self):
  2227. if self.negate is not None:
  2228. return BinaryExpression(
  2229. self.left,
  2230. self.right,
  2231. self.negate,
  2232. negate=self.operator,
  2233. type_=self.type,
  2234. modifiers=self.modifiers)
  2235. else:
  2236. return super(BinaryExpression, self)._negate()
  2237. class Slice(ColumnElement):
  2238. """Represent SQL for a Python array-slice object.
  2239. This is not a specific SQL construct at this level, but
  2240. may be interpreted by specific dialects, e.g. PostgreSQL.
  2241. """
  2242. __visit_name__ = 'slice'
  2243. def __init__(self, start, stop, step):
  2244. self.start = start
  2245. self.stop = stop
  2246. self.step = step
  2247. self.type = type_api.NULLTYPE
  2248. def self_group(self, against=None):
  2249. assert against is operator.getitem
  2250. return self
  2251. class IndexExpression(BinaryExpression):
  2252. """Represent the class of expressions that are like an "index" operation.
  2253. """
  2254. pass
  2255. class Grouping(ColumnElement):
  2256. """Represent a grouping within a column expression"""
  2257. __visit_name__ = 'grouping'
  2258. def __init__(self, element):
  2259. self.element = element
  2260. self.type = getattr(element, 'type', type_api.NULLTYPE)
  2261. def self_group(self, against=None):
  2262. return self
  2263. @property
  2264. def _key_label(self):
  2265. return self._label
  2266. @property
  2267. def _label(self):
  2268. return getattr(self.element, '_label', None) or self.anon_label
  2269. def _copy_internals(self, clone=_clone, **kw):
  2270. self.element = clone(self.element, **kw)
  2271. def get_children(self, **kwargs):
  2272. return self.element,
  2273. @property
  2274. def _from_objects(self):
  2275. return self.element._from_objects
  2276. def __getattr__(self, attr):
  2277. return getattr(self.element, attr)
  2278. def __getstate__(self):
  2279. return {'element': self.element, 'type': self.type}
  2280. def __setstate__(self, state):
  2281. self.element = state['element']
  2282. self.type = state['type']
  2283. def compare(self, other, **kw):
  2284. return isinstance(other, Grouping) and \
  2285. self.element.compare(other.element)
  2286. RANGE_UNBOUNDED = util.symbol("RANGE_UNBOUNDED")
  2287. RANGE_CURRENT = util.symbol("RANGE_CURRENT")
  2288. class Over(ColumnElement):
  2289. """Represent an OVER clause.
  2290. This is a special operator against a so-called
  2291. "window" function, as well as any aggregate function,
  2292. which produces results relative to the result set
  2293. itself. It's supported only by certain database
  2294. backends.
  2295. """
  2296. __visit_name__ = 'over'
  2297. order_by = None
  2298. partition_by = None
  2299. def __init__(
  2300. self, element, partition_by=None,
  2301. order_by=None, range_=None, rows=None):
  2302. """Produce an :class:`.Over` object against a function.
  2303. Used against aggregate or so-called "window" functions,
  2304. for database backends that support window functions.
  2305. :func:`~.expression.over` is usually called using
  2306. the :meth:`.FunctionElement.over` method, e.g.::
  2307. func.row_number().over(order_by=mytable.c.some_column)
  2308. Would produce::
  2309. ROW_NUMBER() OVER(ORDER BY some_column)
  2310. Ranges are also possible using the :paramref:`.expression.over.range_`
  2311. and :paramref:`.expression.over.rows` parameters. These
  2312. mutually-exclusive parameters each accept a 2-tuple, which contains
  2313. a combination of integers and None::
  2314. func.row_number().over(order_by=my_table.c.some_column, range_=(None, 0))
  2315. The above would produce::
  2316. ROW_NUMBER() OVER(ORDER BY some_column RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  2317. A value of None indicates "unbounded", a
  2318. value of zero indicates "current row", and negative / positive
  2319. integers indicate "preceding" and "following":
  2320. * RANGE BETWEEN 5 PRECEDING AND 10 FOLLOWING::
  2321. func.row_number().over(order_by='x', range_=(-5, 10))
  2322. * ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW::
  2323. func.row_number().over(order_by='x', rows=(None, 0))
  2324. * RANGE BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING::
  2325. func.row_number().over(order_by='x', range_=(-2, None))
  2326. .. versionadded:: 1.1 support for RANGE / ROWS within a window
  2327. :param element: a :class:`.FunctionElement`, :class:`.WithinGroup`,
  2328. or other compatible construct.
  2329. :param partition_by: a column element or string, or a list
  2330. of such, that will be used as the PARTITION BY clause
  2331. of the OVER construct.
  2332. :param order_by: a column element or string, or a list
  2333. of such, that will be used as the ORDER BY clause
  2334. of the OVER construct.
  2335. :param range_: optional range clause for the window. This is a
  2336. tuple value which can contain integer values or None, and will
  2337. render a RANGE BETWEEN PRECEDING / FOLLOWING clause
  2338. .. versionadded:: 1.1
  2339. :param rows: optional rows clause for the window. This is a tuple
  2340. value which can contain integer values or None, and will render
  2341. a ROWS BETWEEN PRECEDING / FOLLOWING clause.
  2342. .. versionadded:: 1.1
  2343. This function is also available from the :data:`~.expression.func`
  2344. construct itself via the :meth:`.FunctionElement.over` method.
  2345. .. seealso::
  2346. :data:`.expression.func`
  2347. :func:`.expression.within_group`
  2348. """
  2349. self.element = element
  2350. if order_by is not None:
  2351. self.order_by = ClauseList(
  2352. *util.to_list(order_by),
  2353. _literal_as_text=_literal_as_label_reference)
  2354. if partition_by is not None:
  2355. self.partition_by = ClauseList(
  2356. *util.to_list(partition_by),
  2357. _literal_as_text=_literal_as_label_reference)
  2358. if range_:
  2359. self.range_ = self._interpret_range(range_)
  2360. if rows:
  2361. raise exc.ArgumentError(
  2362. "'range_' and 'rows' are mutually exclusive")
  2363. else:
  2364. self.rows = None
  2365. elif rows:
  2366. self.rows = self._interpret_range(rows)
  2367. self.range_ = None
  2368. else:
  2369. self.rows = self.range_ = None
  2370. def _interpret_range(self, range_):
  2371. if not isinstance(range_, tuple) or len(range_) != 2:
  2372. raise exc.ArgumentError("2-tuple expected for range/rows")
  2373. if range_[0] is None:
  2374. preceding = RANGE_UNBOUNDED
  2375. else:
  2376. try:
  2377. preceding = int(range_[0])
  2378. except ValueError:
  2379. raise exc.ArgumentError(
  2380. "Integer or None expected for preceding value")
  2381. else:
  2382. if preceding > 0:
  2383. raise exc.ArgumentError(
  2384. "Preceding value must be a "
  2385. "negative integer, zero, or None")
  2386. elif preceding < 0:
  2387. preceding = literal(abs(preceding))
  2388. else:
  2389. preceding = RANGE_CURRENT
  2390. if range_[1] is None:
  2391. following = RANGE_UNBOUNDED
  2392. else:
  2393. try:
  2394. following = int(range_[1])
  2395. except ValueError:
  2396. raise exc.ArgumentError(
  2397. "Integer or None expected for following value")
  2398. else:
  2399. if following < 0:
  2400. raise exc.ArgumentError(
  2401. "Following value must be a positive "
  2402. "integer, zero, or None")
  2403. elif following > 0:
  2404. following = literal(following)
  2405. else:
  2406. following = RANGE_CURRENT
  2407. return preceding, following
  2408. @property
  2409. def func(self):
  2410. """the element referred to by this :class:`.Over`
  2411. clause.
  2412. .. deprecated:: 1.1 the ``func`` element has been renamed to
  2413. ``.element``. The two attributes are synonymous though
  2414. ``.func`` is read-only.
  2415. """
  2416. return self.element
  2417. @util.memoized_property
  2418. def type(self):
  2419. return self.element.type
  2420. def get_children(self, **kwargs):
  2421. return [c for c in
  2422. (self.element, self.partition_by, self.order_by)
  2423. if c is not None]
  2424. def _copy_internals(self, clone=_clone, **kw):
  2425. self.element = clone(self.element, **kw)
  2426. if self.partition_by is not None:
  2427. self.partition_by = clone(self.partition_by, **kw)
  2428. if self.order_by is not None:
  2429. self.order_by = clone(self.order_by, **kw)
  2430. @property
  2431. def _from_objects(self):
  2432. return list(itertools.chain(
  2433. *[c._from_objects for c in
  2434. (self.element, self.partition_by, self.order_by)
  2435. if c is not None]
  2436. ))
  2437. class WithinGroup(ColumnElement):
  2438. """Represent a WITHIN GROUP (ORDER BY) clause.
  2439. This is a special operator against so-called
  2440. so-called "ordered set aggregate" and "hypothetical
  2441. set aggregate" functions, including ``percentile_cont()``,
  2442. ``rank()``, ``dense_rank()``, etc.
  2443. It's supported only by certain database backends, such as PostgreSQL,
  2444. Oracle and MS SQL Server.
  2445. The :class:`.WithinGroup` consturct extracts its type from the
  2446. method :meth:`.FunctionElement.within_group_type`. If this returns
  2447. ``None``, the function's ``.type`` is used.
  2448. """
  2449. __visit_name__ = 'withingroup'
  2450. order_by = None
  2451. def __init__(self, element, *order_by):
  2452. r"""Produce a :class:`.WithinGroup` object against a function.
  2453. Used against so-called "ordered set aggregate" and "hypothetical
  2454. set aggregate" functions, including :class:`.percentile_cont`,
  2455. :class:`.rank`, :class:`.dense_rank`, etc.
  2456. :func:`~.expression.within_group` is usually called using
  2457. the :meth:`.FunctionElement.within_group` method, e.g.::
  2458. from sqlalchemy import within_group
  2459. stmt = select([
  2460. department.c.id,
  2461. func.percentile_cont(0.5).within_group(
  2462. department.c.salary.desc()
  2463. )
  2464. ])
  2465. The above statement would produce SQL similar to
  2466. ``SELECT department.id, percentile_cont(0.5)
  2467. WITHIN GROUP (ORDER BY department.salary DESC)``.
  2468. :param element: a :class:`.FunctionElement` construct, typically
  2469. generated by :data:`~.expression.func`.
  2470. :param \*order_by: one or more column elements that will be used
  2471. as the ORDER BY clause of the WITHIN GROUP construct.
  2472. .. versionadded:: 1.1
  2473. .. seealso::
  2474. :data:`.expression.func`
  2475. :func:`.expression.over`
  2476. """
  2477. self.element = element
  2478. if order_by is not None:
  2479. self.order_by = ClauseList(
  2480. *util.to_list(order_by),
  2481. _literal_as_text=_literal_as_label_reference)
  2482. def over(self, partition_by=None, order_by=None):
  2483. """Produce an OVER clause against this :class:`.WithinGroup`
  2484. construct.
  2485. This function has the same signature as that of
  2486. :meth:`.FunctionElement.over`.
  2487. """
  2488. return Over(self, partition_by=partition_by, order_by=order_by)
  2489. @util.memoized_property
  2490. def type(self):
  2491. wgt = self.element.within_group_type(self)
  2492. if wgt is not None:
  2493. return wgt
  2494. else:
  2495. return self.element.type
  2496. def get_children(self, **kwargs):
  2497. return [c for c in
  2498. (self.func, self.order_by)
  2499. if c is not None]
  2500. def _copy_internals(self, clone=_clone, **kw):
  2501. self.element = clone(self.element, **kw)
  2502. if self.order_by is not None:
  2503. self.order_by = clone(self.order_by, **kw)
  2504. @property
  2505. def _from_objects(self):
  2506. return list(itertools.chain(
  2507. *[c._from_objects for c in
  2508. (self.element, self.order_by)
  2509. if c is not None]
  2510. ))
  2511. class FunctionFilter(ColumnElement):
  2512. """Represent a function FILTER clause.
  2513. This is a special operator against aggregate and window functions,
  2514. which controls which rows are passed to it.
  2515. It's supported only by certain database backends.
  2516. Invocation of :class:`.FunctionFilter` is via
  2517. :meth:`.FunctionElement.filter`::
  2518. func.count(1).filter(True)
  2519. .. versionadded:: 1.0.0
  2520. .. seealso::
  2521. :meth:`.FunctionElement.filter`
  2522. """
  2523. __visit_name__ = 'funcfilter'
  2524. criterion = None
  2525. def __init__(self, func, *criterion):
  2526. """Produce a :class:`.FunctionFilter` object against a function.
  2527. Used against aggregate and window functions,
  2528. for database backends that support the "FILTER" clause.
  2529. E.g.::
  2530. from sqlalchemy import funcfilter
  2531. funcfilter(func.count(1), MyClass.name == 'some name')
  2532. Would produce "COUNT(1) FILTER (WHERE myclass.name = 'some name')".
  2533. This function is also available from the :data:`~.expression.func`
  2534. construct itself via the :meth:`.FunctionElement.filter` method.
  2535. .. versionadded:: 1.0.0
  2536. .. seealso::
  2537. :meth:`.FunctionElement.filter`
  2538. """
  2539. self.func = func
  2540. self.filter(*criterion)
  2541. def filter(self, *criterion):
  2542. """Produce an additional FILTER against the function.
  2543. This method adds additional criteria to the initial criteria
  2544. set up by :meth:`.FunctionElement.filter`.
  2545. Multiple criteria are joined together at SQL render time
  2546. via ``AND``.
  2547. """
  2548. for criterion in list(criterion):
  2549. criterion = _expression_literal_as_text(criterion)
  2550. if self.criterion is not None:
  2551. self.criterion = self.criterion & criterion
  2552. else:
  2553. self.criterion = criterion
  2554. return self
  2555. def over(self, partition_by=None, order_by=None):
  2556. """Produce an OVER clause against this filtered function.
  2557. Used against aggregate or so-called "window" functions,
  2558. for database backends that support window functions.
  2559. The expression::
  2560. func.rank().filter(MyClass.y > 5).over(order_by='x')
  2561. is shorthand for::
  2562. from sqlalchemy import over, funcfilter
  2563. over(funcfilter(func.rank(), MyClass.y > 5), order_by='x')
  2564. See :func:`~.expression.over` for a full description.
  2565. """
  2566. return Over(self, partition_by=partition_by, order_by=order_by)
  2567. @util.memoized_property
  2568. def type(self):
  2569. return self.func.type
  2570. def get_children(self, **kwargs):
  2571. return [c for c in
  2572. (self.func, self.criterion)
  2573. if c is not None]
  2574. def _copy_internals(self, clone=_clone, **kw):
  2575. self.func = clone(self.func, **kw)
  2576. if self.criterion is not None:
  2577. self.criterion = clone(self.criterion, **kw)
  2578. @property
  2579. def _from_objects(self):
  2580. return list(itertools.chain(
  2581. *[c._from_objects for c in (self.func, self.criterion)
  2582. if c is not None]
  2583. ))
  2584. class Label(ColumnElement):
  2585. """Represents a column label (AS).
  2586. Represent a label, as typically applied to any column-level
  2587. element using the ``AS`` sql keyword.
  2588. """
  2589. __visit_name__ = 'label'
  2590. def __init__(self, name, element, type_=None):
  2591. """Return a :class:`Label` object for the
  2592. given :class:`.ColumnElement`.
  2593. A label changes the name of an element in the columns clause of a
  2594. ``SELECT`` statement, typically via the ``AS`` SQL keyword.
  2595. This functionality is more conveniently available via the
  2596. :meth:`.ColumnElement.label` method on :class:`.ColumnElement`.
  2597. :param name: label name
  2598. :param obj: a :class:`.ColumnElement`.
  2599. """
  2600. if isinstance(element, Label):
  2601. self._resolve_label = element._label
  2602. while isinstance(element, Label):
  2603. element = element.element
  2604. if name:
  2605. self.name = name
  2606. self._resolve_label = self.name
  2607. else:
  2608. self.name = _anonymous_label(
  2609. '%%(%d %s)s' % (id(self), getattr(element, 'name', 'anon'))
  2610. )
  2611. self.key = self._label = self._key_label = self.name
  2612. self._element = element
  2613. self._type = type_
  2614. self._proxies = [element]
  2615. def __reduce__(self):
  2616. return self.__class__, (self.name, self._element, self._type)
  2617. @util.memoized_property
  2618. def _allow_label_resolve(self):
  2619. return self.element._allow_label_resolve
  2620. @property
  2621. def _order_by_label_element(self):
  2622. return self
  2623. @util.memoized_property
  2624. def type(self):
  2625. return type_api.to_instance(
  2626. self._type or getattr(self._element, 'type', None)
  2627. )
  2628. @util.memoized_property
  2629. def element(self):
  2630. return self._element.self_group(against=operators.as_)
  2631. def self_group(self, against=None):
  2632. sub_element = self._element.self_group(against=against)
  2633. if sub_element is not self._element:
  2634. return Label(self.name,
  2635. sub_element,
  2636. type_=self._type)
  2637. else:
  2638. return self
  2639. @property
  2640. def primary_key(self):
  2641. return self.element.primary_key
  2642. @property
  2643. def foreign_keys(self):
  2644. return self.element.foreign_keys
  2645. def get_children(self, **kwargs):
  2646. return self.element,
  2647. def _copy_internals(self, clone=_clone, anonymize_labels=False, **kw):
  2648. self._element = clone(self._element, **kw)
  2649. self.__dict__.pop('element', None)
  2650. self.__dict__.pop('_allow_label_resolve', None)
  2651. if anonymize_labels:
  2652. self.name = self._resolve_label = _anonymous_label(
  2653. '%%(%d %s)s' % (
  2654. id(self), getattr(self.element, 'name', 'anon'))
  2655. )
  2656. self.key = self._label = self._key_label = self.name
  2657. @property
  2658. def _from_objects(self):
  2659. return self.element._from_objects
  2660. def _make_proxy(self, selectable, name=None, **kw):
  2661. e = self.element._make_proxy(selectable,
  2662. name=name if name else self.name)
  2663. e._proxies.append(self)
  2664. if self._type is not None:
  2665. e.type = self._type
  2666. return e
  2667. class ColumnClause(Immutable, ColumnElement):
  2668. """Represents a column expression from any textual string.
  2669. The :class:`.ColumnClause`, a lightweight analogue to the
  2670. :class:`.Column` class, is typically invoked using the
  2671. :func:`.column` function, as in::
  2672. from sqlalchemy import column
  2673. id, name = column("id"), column("name")
  2674. stmt = select([id, name]).select_from("user")
  2675. The above statement would produce SQL like::
  2676. SELECT id, name FROM user
  2677. :class:`.ColumnClause` is the immediate superclass of the schema-specific
  2678. :class:`.Column` object. While the :class:`.Column` class has all the
  2679. same capabilities as :class:`.ColumnClause`, the :class:`.ColumnClause`
  2680. class is usable by itself in those cases where behavioral requirements
  2681. are limited to simple SQL expression generation. The object has none of
  2682. the associations with schema-level metadata or with execution-time
  2683. behavior that :class:`.Column` does, so in that sense is a "lightweight"
  2684. version of :class:`.Column`.
  2685. Full details on :class:`.ColumnClause` usage is at :func:`.column`.
  2686. .. seealso::
  2687. :func:`.column`
  2688. :class:`.Column`
  2689. """
  2690. __visit_name__ = 'column'
  2691. onupdate = default = server_default = server_onupdate = None
  2692. _memoized_property = util.group_expirable_memoized_property()
  2693. def __init__(self, text, type_=None, is_literal=False, _selectable=None):
  2694. """Produce a :class:`.ColumnClause` object.
  2695. The :class:`.ColumnClause` is a lightweight analogue to the
  2696. :class:`.Column` class. The :func:`.column` function can
  2697. be invoked with just a name alone, as in::
  2698. from sqlalchemy import column
  2699. id, name = column("id"), column("name")
  2700. stmt = select([id, name]).select_from("user")
  2701. The above statement would produce SQL like::
  2702. SELECT id, name FROM user
  2703. Once constructed, :func:`.column` may be used like any other SQL
  2704. expression element such as within :func:`.select` constructs::
  2705. from sqlalchemy.sql import column
  2706. id, name = column("id"), column("name")
  2707. stmt = select([id, name]).select_from("user")
  2708. The text handled by :func:`.column` is assumed to be handled
  2709. like the name of a database column; if the string contains mixed case,
  2710. special characters, or matches a known reserved word on the target
  2711. backend, the column expression will render using the quoting
  2712. behavior determined by the backend. To produce a textual SQL
  2713. expression that is rendered exactly without any quoting,
  2714. use :func:`.literal_column` instead, or pass ``True`` as the
  2715. value of :paramref:`.column.is_literal`. Additionally, full SQL
  2716. statements are best handled using the :func:`.text` construct.
  2717. :func:`.column` can be used in a table-like
  2718. fashion by combining it with the :func:`.table` function
  2719. (which is the lightweight analogue to :class:`.Table`) to produce
  2720. a working table construct with minimal boilerplate::
  2721. from sqlalchemy import table, column, select
  2722. user = table("user",
  2723. column("id"),
  2724. column("name"),
  2725. column("description"),
  2726. )
  2727. stmt = select([user.c.description]).where(user.c.name == 'wendy')
  2728. A :func:`.column` / :func:`.table` construct like that illustrated
  2729. above can be created in an
  2730. ad-hoc fashion and is not associated with any
  2731. :class:`.schema.MetaData`, DDL, or events, unlike its
  2732. :class:`.Table` counterpart.
  2733. .. versionchanged:: 1.0.0 :func:`.expression.column` can now
  2734. be imported from the plain ``sqlalchemy`` namespace like any
  2735. other SQL element.
  2736. :param text: the text of the element.
  2737. :param type: :class:`.types.TypeEngine` object which can associate
  2738. this :class:`.ColumnClause` with a type.
  2739. :param is_literal: if True, the :class:`.ColumnClause` is assumed to
  2740. be an exact expression that will be delivered to the output with no
  2741. quoting rules applied regardless of case sensitive settings. the
  2742. :func:`.literal_column()` function essentially invokes
  2743. :func:`.column` while passing ``is_literal=True``.
  2744. .. seealso::
  2745. :class:`.Column`
  2746. :func:`.literal_column`
  2747. :func:`.table`
  2748. :func:`.text`
  2749. :ref:`sqlexpression_literal_column`
  2750. """
  2751. self.key = self.name = text
  2752. self.table = _selectable
  2753. self.type = type_api.to_instance(type_)
  2754. self.is_literal = is_literal
  2755. def _compare_name_for_result(self, other):
  2756. if self.is_literal or \
  2757. self.table is None or self.table._textual or \
  2758. not hasattr(other, 'proxy_set') or (
  2759. isinstance(other, ColumnClause) and
  2760. (other.is_literal or
  2761. other.table is None or
  2762. other.table._textual)
  2763. ):
  2764. return (hasattr(other, 'name') and self.name == other.name) or \
  2765. (hasattr(other, '_label') and self._label == other._label)
  2766. else:
  2767. return other.proxy_set.intersection(self.proxy_set)
  2768. def _get_table(self):
  2769. return self.__dict__['table']
  2770. def _set_table(self, table):
  2771. self._memoized_property.expire_instance(self)
  2772. self.__dict__['table'] = table
  2773. table = property(_get_table, _set_table)
  2774. @_memoized_property
  2775. def _from_objects(self):
  2776. t = self.table
  2777. if t is not None:
  2778. return [t]
  2779. else:
  2780. return []
  2781. @util.memoized_property
  2782. def description(self):
  2783. if util.py3k:
  2784. return self.name
  2785. else:
  2786. return self.name.encode('ascii', 'backslashreplace')
  2787. @_memoized_property
  2788. def _key_label(self):
  2789. if self.key != self.name:
  2790. return self._gen_label(self.key)
  2791. else:
  2792. return self._label
  2793. @_memoized_property
  2794. def _label(self):
  2795. return self._gen_label(self.name)
  2796. @_memoized_property
  2797. def _render_label_in_columns_clause(self):
  2798. return self.table is not None
  2799. def _gen_label(self, name):
  2800. t = self.table
  2801. if self.is_literal:
  2802. return None
  2803. elif t is not None and t.named_with_column:
  2804. if getattr(t, 'schema', None):
  2805. label = t.schema.replace('.', '_') + "_" + \
  2806. t.name + "_" + name
  2807. else:
  2808. label = t.name + "_" + name
  2809. # propagate name quoting rules for labels.
  2810. if getattr(name, "quote", None) is not None:
  2811. if isinstance(label, quoted_name):
  2812. label.quote = name.quote
  2813. else:
  2814. label = quoted_name(label, name.quote)
  2815. elif getattr(t.name, "quote", None) is not None:
  2816. # can't get this situation to occur, so let's
  2817. # assert false on it for now
  2818. assert not isinstance(label, quoted_name)
  2819. label = quoted_name(label, t.name.quote)
  2820. # ensure the label name doesn't conflict with that
  2821. # of an existing column
  2822. if label in t.c:
  2823. _label = label
  2824. counter = 1
  2825. while _label in t.c:
  2826. _label = label + "_" + str(counter)
  2827. counter += 1
  2828. label = _label
  2829. return _as_truncated(label)
  2830. else:
  2831. return name
  2832. def _bind_param(self, operator, obj, type_=None):
  2833. return BindParameter(self.key, obj,
  2834. _compared_to_operator=operator,
  2835. _compared_to_type=self.type,
  2836. type_=type_,
  2837. unique=True)
  2838. def _make_proxy(self, selectable, name=None, attach=True,
  2839. name_is_truncatable=False, **kw):
  2840. # propagate the "is_literal" flag only if we are keeping our name,
  2841. # otherwise its considered to be a label
  2842. is_literal = self.is_literal and (name is None or name == self.name)
  2843. c = self._constructor(
  2844. _as_truncated(name or self.name) if
  2845. name_is_truncatable else
  2846. (name or self.name),
  2847. type_=self.type,
  2848. _selectable=selectable,
  2849. is_literal=is_literal
  2850. )
  2851. if name is None:
  2852. c.key = self.key
  2853. c._proxies = [self]
  2854. if selectable._is_clone_of is not None:
  2855. c._is_clone_of = \
  2856. selectable._is_clone_of.columns.get(c.key)
  2857. if attach:
  2858. selectable._columns[c.key] = c
  2859. return c
  2860. class _IdentifiedClause(Executable, ClauseElement):
  2861. __visit_name__ = 'identified'
  2862. _execution_options = \
  2863. Executable._execution_options.union({'autocommit': False})
  2864. def __init__(self, ident):
  2865. self.ident = ident
  2866. class SavepointClause(_IdentifiedClause):
  2867. __visit_name__ = 'savepoint'
  2868. class RollbackToSavepointClause(_IdentifiedClause):
  2869. __visit_name__ = 'rollback_to_savepoint'
  2870. class ReleaseSavepointClause(_IdentifiedClause):
  2871. __visit_name__ = 'release_savepoint'
  2872. class quoted_name(util.MemoizedSlots, util.text_type):
  2873. """Represent a SQL identifier combined with quoting preferences.
  2874. :class:`.quoted_name` is a Python unicode/str subclass which
  2875. represents a particular identifier name along with a
  2876. ``quote`` flag. This ``quote`` flag, when set to
  2877. ``True`` or ``False``, overrides automatic quoting behavior
  2878. for this identifier in order to either unconditionally quote
  2879. or to not quote the name. If left at its default of ``None``,
  2880. quoting behavior is applied to the identifier on a per-backend basis
  2881. based on an examination of the token itself.
  2882. A :class:`.quoted_name` object with ``quote=True`` is also
  2883. prevented from being modified in the case of a so-called
  2884. "name normalize" option. Certain database backends, such as
  2885. Oracle, Firebird, and DB2 "normalize" case-insensitive names
  2886. as uppercase. The SQLAlchemy dialects for these backends
  2887. convert from SQLAlchemy's lower-case-means-insensitive convention
  2888. to the upper-case-means-insensitive conventions of those backends.
  2889. The ``quote=True`` flag here will prevent this conversion from occurring
  2890. to support an identifier that's quoted as all lower case against
  2891. such a backend.
  2892. The :class:`.quoted_name` object is normally created automatically
  2893. when specifying the name for key schema constructs such as
  2894. :class:`.Table`, :class:`.Column`, and others. The class can also be
  2895. passed explicitly as the name to any function that receives a name which
  2896. can be quoted. Such as to use the :meth:`.Engine.has_table` method with
  2897. an unconditionally quoted name::
  2898. from sqlaclchemy import create_engine
  2899. from sqlalchemy.sql.elements import quoted_name
  2900. engine = create_engine("oracle+cx_oracle://some_dsn")
  2901. engine.has_table(quoted_name("some_table", True))
  2902. The above logic will run the "has table" logic against the Oracle backend,
  2903. passing the name exactly as ``"some_table"`` without converting to
  2904. upper case.
  2905. .. versionadded:: 0.9.0
  2906. """
  2907. __slots__ = 'quote', 'lower', 'upper'
  2908. def __new__(cls, value, quote):
  2909. if value is None:
  2910. return None
  2911. # experimental - don't bother with quoted_name
  2912. # if quote flag is None. doesn't seem to make any dent
  2913. # in performance however
  2914. # elif not sprcls and quote is None:
  2915. # return value
  2916. elif isinstance(value, cls) and (
  2917. quote is None or value.quote == quote
  2918. ):
  2919. return value
  2920. self = super(quoted_name, cls).__new__(cls, value)
  2921. self.quote = quote
  2922. return self
  2923. def __reduce__(self):
  2924. return quoted_name, (util.text_type(self), self.quote)
  2925. def _memoized_method_lower(self):
  2926. if self.quote:
  2927. return self
  2928. else:
  2929. return util.text_type(self).lower()
  2930. def _memoized_method_upper(self):
  2931. if self.quote:
  2932. return self
  2933. else:
  2934. return util.text_type(self).upper()
  2935. def __repr__(self):
  2936. backslashed = self.encode('ascii', 'backslashreplace')
  2937. if not util.py2k:
  2938. backslashed = backslashed.decode('ascii')
  2939. return "'%s'" % backslashed
  2940. class _truncated_label(quoted_name):
  2941. """A unicode subclass used to identify symbolic "
  2942. "names that may require truncation."""
  2943. __slots__ = ()
  2944. def __new__(cls, value, quote=None):
  2945. quote = getattr(value, "quote", quote)
  2946. # return super(_truncated_label, cls).__new__(cls, value, quote, True)
  2947. return super(_truncated_label, cls).__new__(cls, value, quote)
  2948. def __reduce__(self):
  2949. return self.__class__, (util.text_type(self), self.quote)
  2950. def apply_map(self, map_):
  2951. return self
  2952. class conv(_truncated_label):
  2953. """Mark a string indicating that a name has already been converted
  2954. by a naming convention.
  2955. This is a string subclass that indicates a name that should not be
  2956. subject to any further naming conventions.
  2957. E.g. when we create a :class:`.Constraint` using a naming convention
  2958. as follows::
  2959. m = MetaData(naming_convention={
  2960. "ck": "ck_%(table_name)s_%(constraint_name)s"
  2961. })
  2962. t = Table('t', m, Column('x', Integer),
  2963. CheckConstraint('x > 5', name='x5'))
  2964. The name of the above constraint will be rendered as ``"ck_t_x5"``.
  2965. That is, the existing name ``x5`` is used in the naming convention as the
  2966. ``constraint_name`` token.
  2967. In some situations, such as in migration scripts, we may be rendering
  2968. the above :class:`.CheckConstraint` with a name that's already been
  2969. converted. In order to make sure the name isn't double-modified, the
  2970. new name is applied using the :func:`.schema.conv` marker. We can
  2971. use this explicitly as follows::
  2972. m = MetaData(naming_convention={
  2973. "ck": "ck_%(table_name)s_%(constraint_name)s"
  2974. })
  2975. t = Table('t', m, Column('x', Integer),
  2976. CheckConstraint('x > 5', name=conv('ck_t_x5')))
  2977. Where above, the :func:`.schema.conv` marker indicates that the constraint
  2978. name here is final, and the name will render as ``"ck_t_x5"`` and not
  2979. ``"ck_t_ck_t_x5"``
  2980. .. versionadded:: 0.9.4
  2981. .. seealso::
  2982. :ref:`constraint_naming_conventions`
  2983. """
  2984. __slots__ = ()
  2985. class _defer_name(_truncated_label):
  2986. """mark a name as 'deferred' for the purposes of automated name
  2987. generation.
  2988. """
  2989. __slots__ = ()
  2990. def __new__(cls, value):
  2991. if value is None:
  2992. return _NONE_NAME
  2993. elif isinstance(value, conv):
  2994. return value
  2995. else:
  2996. return super(_defer_name, cls).__new__(cls, value)
  2997. def __reduce__(self):
  2998. return self.__class__, (util.text_type(self), )
  2999. class _defer_none_name(_defer_name):
  3000. """indicate a 'deferred' name that was ultimately the value None."""
  3001. __slots__ = ()
  3002. _NONE_NAME = _defer_none_name("_unnamed_")
  3003. # for backwards compatibility in case
  3004. # someone is re-implementing the
  3005. # _truncated_identifier() sequence in a custom
  3006. # compiler
  3007. _generated_label = _truncated_label
  3008. class _anonymous_label(_truncated_label):
  3009. """A unicode subclass used to identify anonymously
  3010. generated names."""
  3011. __slots__ = ()
  3012. def __add__(self, other):
  3013. return _anonymous_label(
  3014. quoted_name(
  3015. util.text_type.__add__(self, util.text_type(other)),
  3016. self.quote)
  3017. )
  3018. def __radd__(self, other):
  3019. return _anonymous_label(
  3020. quoted_name(
  3021. util.text_type.__add__(util.text_type(other), self),
  3022. self.quote)
  3023. )
  3024. def apply_map(self, map_):
  3025. if self.quote is not None:
  3026. # preserve quoting only if necessary
  3027. return quoted_name(self % map_, self.quote)
  3028. else:
  3029. # else skip the constructor call
  3030. return self % map_
  3031. def _as_truncated(value):
  3032. """coerce the given value to :class:`._truncated_label`.
  3033. Existing :class:`._truncated_label` and
  3034. :class:`._anonymous_label` objects are passed
  3035. unchanged.
  3036. """
  3037. if isinstance(value, _truncated_label):
  3038. return value
  3039. else:
  3040. return _truncated_label(value)
  3041. def _string_or_unprintable(element):
  3042. if isinstance(element, util.string_types):
  3043. return element
  3044. else:
  3045. try:
  3046. return str(element)
  3047. except Exception:
  3048. return "unprintable element %r" % element
  3049. def _expand_cloned(elements):
  3050. """expand the given set of ClauseElements to be the set of all 'cloned'
  3051. predecessors.
  3052. """
  3053. return itertools.chain(*[x._cloned_set for x in elements])
  3054. def _select_iterables(elements):
  3055. """expand tables into individual columns in the
  3056. given list of column expressions.
  3057. """
  3058. return itertools.chain(*[c._select_iterable for c in elements])
  3059. def _cloned_intersection(a, b):
  3060. """return the intersection of sets a and b, counting
  3061. any overlap between 'cloned' predecessors.
  3062. The returned set is in terms of the entities present within 'a'.
  3063. """
  3064. all_overlap = set(_expand_cloned(a)).intersection(_expand_cloned(b))
  3065. return set(elem for elem in a
  3066. if all_overlap.intersection(elem._cloned_set))
  3067. def _cloned_difference(a, b):
  3068. all_overlap = set(_expand_cloned(a)).intersection(_expand_cloned(b))
  3069. return set(elem for elem in a
  3070. if not all_overlap.intersection(elem._cloned_set))
  3071. @util.dependencies("sqlalchemy.sql.functions")
  3072. def _labeled(functions, element):
  3073. if not hasattr(element, 'name') or \
  3074. isinstance(element, functions.FunctionElement):
  3075. return element.label(None)
  3076. else:
  3077. return element
  3078. def _is_column(col):
  3079. """True if ``col`` is an instance of :class:`.ColumnElement`."""
  3080. return isinstance(col, ColumnElement)
  3081. def _find_columns(clause):
  3082. """locate Column objects within the given expression."""
  3083. cols = util.column_set()
  3084. traverse(clause, {}, {'column': cols.add})
  3085. return cols
  3086. # there is some inconsistency here between the usage of
  3087. # inspect() vs. checking for Visitable and __clause_element__.
  3088. # Ideally all functions here would derive from inspect(),
  3089. # however the inspect() versions add significant callcount
  3090. # overhead for critical functions like _interpret_as_column_or_from().
  3091. # Generally, the column-based functions are more performance critical
  3092. # and are fine just checking for __clause_element__(). It is only
  3093. # _interpret_as_from() where we'd like to be able to receive ORM entities
  3094. # that have no defined namespace, hence inspect() is needed there.
  3095. def _column_as_key(element):
  3096. if isinstance(element, util.string_types):
  3097. return element
  3098. if hasattr(element, '__clause_element__'):
  3099. element = element.__clause_element__()
  3100. try:
  3101. return element.key
  3102. except AttributeError:
  3103. return None
  3104. def _clause_element_as_expr(element):
  3105. if hasattr(element, '__clause_element__'):
  3106. return element.__clause_element__()
  3107. else:
  3108. return element
  3109. def _literal_as_label_reference(element):
  3110. if isinstance(element, util.string_types):
  3111. return _textual_label_reference(element)
  3112. elif hasattr(element, '__clause_element__'):
  3113. element = element.__clause_element__()
  3114. return _literal_as_text(element)
  3115. def _literal_and_labels_as_label_reference(element):
  3116. if isinstance(element, util.string_types):
  3117. return _textual_label_reference(element)
  3118. elif hasattr(element, '__clause_element__'):
  3119. element = element.__clause_element__()
  3120. if isinstance(element, ColumnElement) and \
  3121. element._order_by_label_element is not None:
  3122. return _label_reference(element)
  3123. else:
  3124. return _literal_as_text(element)
  3125. def _expression_literal_as_text(element):
  3126. return _literal_as_text(element, warn=True)
  3127. def _literal_as_text(element, warn=False):
  3128. if isinstance(element, Visitable):
  3129. return element
  3130. elif hasattr(element, '__clause_element__'):
  3131. return element.__clause_element__()
  3132. elif isinstance(element, util.string_types):
  3133. if warn:
  3134. util.warn_limited(
  3135. "Textual SQL expression %(expr)r should be "
  3136. "explicitly declared as text(%(expr)r)",
  3137. {"expr": util.ellipses_string(element)})
  3138. return TextClause(util.text_type(element))
  3139. elif isinstance(element, (util.NoneType, bool)):
  3140. return _const_expr(element)
  3141. else:
  3142. raise exc.ArgumentError(
  3143. "SQL expression object or string expected, got object of type %r "
  3144. "instead" % type(element)
  3145. )
  3146. def _no_literals(element):
  3147. if hasattr(element, '__clause_element__'):
  3148. return element.__clause_element__()
  3149. elif not isinstance(element, Visitable):
  3150. raise exc.ArgumentError("Ambiguous literal: %r. Use the 'text()' "
  3151. "function to indicate a SQL expression "
  3152. "literal, or 'literal()' to indicate a "
  3153. "bound value." % element)
  3154. else:
  3155. return element
  3156. def _is_literal(element):
  3157. return not isinstance(element, Visitable) and \
  3158. not hasattr(element, '__clause_element__')
  3159. def _only_column_elements_or_none(element, name):
  3160. if element is None:
  3161. return None
  3162. else:
  3163. return _only_column_elements(element, name)
  3164. def _only_column_elements(element, name):
  3165. if hasattr(element, '__clause_element__'):
  3166. element = element.__clause_element__()
  3167. if not isinstance(element, ColumnElement):
  3168. raise exc.ArgumentError(
  3169. "Column-based expression object expected for argument "
  3170. "'%s'; got: '%s', type %s" % (name, element, type(element)))
  3171. return element
  3172. def _literal_as_binds(element, name=None, type_=None):
  3173. if hasattr(element, '__clause_element__'):
  3174. return element.__clause_element__()
  3175. elif not isinstance(element, Visitable):
  3176. if element is None:
  3177. return Null()
  3178. else:
  3179. return BindParameter(name, element, type_=type_, unique=True)
  3180. else:
  3181. return element
  3182. _guess_straight_column = re.compile(r'^\w\S*$', re.I)
  3183. def _interpret_as_column_or_from(element):
  3184. if isinstance(element, Visitable):
  3185. return element
  3186. elif hasattr(element, '__clause_element__'):
  3187. return element.__clause_element__()
  3188. insp = inspection.inspect(element, raiseerr=False)
  3189. if insp is None:
  3190. if isinstance(element, (util.NoneType, bool)):
  3191. return _const_expr(element)
  3192. elif hasattr(insp, "selectable"):
  3193. return insp.selectable
  3194. # be forgiving as this is an extremely common
  3195. # and known expression
  3196. if element == "*":
  3197. guess_is_literal = True
  3198. elif isinstance(element, (numbers.Number)):
  3199. return ColumnClause(str(element), is_literal=True)
  3200. else:
  3201. element = str(element)
  3202. # give into temptation, as this fact we are guessing about
  3203. # is not one we've previously ever needed our users tell us;
  3204. # but let them know we are not happy about it
  3205. guess_is_literal = not _guess_straight_column.match(element)
  3206. util.warn_limited(
  3207. "Textual column expression %(column)r should be "
  3208. "explicitly declared with text(%(column)r), "
  3209. "or use %(literal_column)s(%(column)r) "
  3210. "for more specificity",
  3211. {
  3212. "column": util.ellipses_string(element),
  3213. "literal_column": "literal_column"
  3214. if guess_is_literal else "column"
  3215. })
  3216. return ColumnClause(
  3217. element,
  3218. is_literal=guess_is_literal)
  3219. def _const_expr(element):
  3220. if isinstance(element, (Null, False_, True_)):
  3221. return element
  3222. elif element is None:
  3223. return Null()
  3224. elif element is False:
  3225. return False_()
  3226. elif element is True:
  3227. return True_()
  3228. else:
  3229. raise exc.ArgumentError(
  3230. "Expected None, False, or True"
  3231. )
  3232. def _type_from_args(args):
  3233. for a in args:
  3234. if not a.type._isnull:
  3235. return a.type
  3236. else:
  3237. return type_api.NULLTYPE
  3238. def _corresponding_column_or_error(fromclause, column,
  3239. require_embedded=False):
  3240. c = fromclause.corresponding_column(column,
  3241. require_embedded=require_embedded)
  3242. if c is None:
  3243. raise exc.InvalidRequestError(
  3244. "Given column '%s', attached to table '%s', "
  3245. "failed to locate a corresponding column from table '%s'"
  3246. %
  3247. (column,
  3248. getattr(column, 'table', None),
  3249. fromclause.description)
  3250. )
  3251. return c
  3252. class AnnotatedColumnElement(Annotated):
  3253. def __init__(self, element, values):
  3254. Annotated.__init__(self, element, values)
  3255. ColumnElement.comparator._reset(self)
  3256. for attr in ('name', 'key', 'table'):
  3257. if self.__dict__.get(attr, False) is None:
  3258. self.__dict__.pop(attr)
  3259. def _with_annotations(self, values):
  3260. clone = super(AnnotatedColumnElement, self)._with_annotations(values)
  3261. ColumnElement.comparator._reset(clone)
  3262. return clone
  3263. @util.memoized_property
  3264. def name(self):
  3265. """pull 'name' from parent, if not present"""
  3266. return self._Annotated__element.name
  3267. @util.memoized_property
  3268. def table(self):
  3269. """pull 'table' from parent, if not present"""
  3270. return self._Annotated__element.table
  3271. @util.memoized_property
  3272. def key(self):
  3273. """pull 'key' from parent, if not present"""
  3274. return self._Annotated__element.key
  3275. @util.memoized_property
  3276. def info(self):
  3277. return self._Annotated__element.info
  3278. @util.memoized_property
  3279. def anon_label(self):
  3280. return self._Annotated__element.anon_label