o {;fP@sdZddlZddlmZddlmZddlmZm Z GdddZ Gdd d Z Gd d d e Z Gd d d e Z Gddde e ZGddde ZGddde ZGddde ZGddde ZGddde ZGddde ZGddde ZGddde ZGd d!d!e ZGd"d#d#e ZGd$d%d%e ZGd&d'd'e ZGd(d)d)e e ZGd*d+d+e ZGd,d-d-e ZGd.d/d/e ZGd0d1d1e ZdS)2zFThis module contains classes representing syntactical elements of SQL.Ntokens) SQLParseError)imt remove_quotesc@s eZdZdZddZddZdS)NameAliasMixinz'Implements get_real_name and get_alias.cCs$|jtjdfd\}}|j|ddS)7Returns the real name (object name) of this identifier..mT) real_name) token_next_byT Punctuation_get_first_name)selfdot_idx_r./usr/lib/python3/dist-packages/sqlparse/sql.py get_real_nameszNameAliasMixin.get_real_namecCsl|jtjdfd\}}|dur|j|dddS|jtjd\}}t|jdkr2|dur4|jdd SdSdS) 2Returns the alias for this identifier or ``None``.ASr NT)keywordstreverse)r rKeywordr Whitespacelenr)rkw_idxkwrwsrrr get_aliass zNameAliasMixin.get_aliasN)__name__ __module__ __qualname____doc__rr&rrrrrs rc@sfeZdZdZdZddZddZddZd d Zd d Z d dZ dddZ ddZ ddZ ddZdS)TokenzBase class for all other classes in this module. It represents a single token and has two instance attributes: ``value`` is the unchanged value of the token and ``ttype`` is the type of the token. )valuettypeparent normalized is_keywordis_group is_whitespacecCsXt|}||_||_d|_d|_|tjv|_|jtjv|_ |jr'| |_ dS||_ dS)NF) strr,r-r.r1rr r0r!r2upperr/)rr-r,rrr__init__3s zToken.__init__cC|jSNr,rrrr__str__=sz Token.__str__cCsF|}|}|dr|drdnd}djddt|itS)N'"z"<{cls} {q}{value}{q} at 0x{id:2X}>idr)_get_repr_name_get_repr_value startswithendswithformatr=locals)rclsr,qrrr__repr__DszToken.__repr__cCst|jddS)Nr )r3r-splitr9rrrr>LszToken._get_repr_namecCs2t|}t|dkr|ddd}tdd|S)Nz...z\s+ )r3r"resub)rrawrrrr?Os zToken._get_repr_valueccs |VdS)zResolve subgroups.Nrr9rrrflattenUs z Token.flattenFcs|j|u}|r |dur |St|tr|f}|r8|jrtjndfdd|D}|D] }||jr5dSq*dS|jrBdd|D}|j|vS)a,Checks whether the token matches the given arguments. *ttype* is a token type. If this token doesn't match the given token type. *values* is a list of possible values for this token. The values are OR'ed together so if only one of the values matches ``True`` is returned. Except for keyword tokens the comparison is case-sensitive. For convenience it's OK to pass in a single string. If *regex* is ``True`` (default is ``False``) the given values are treated as regular expressions. Nrc3s|] }t|VqdSr7)rLcompile.0vflagrr oszToken.match..TFcss|]}|VqdSr7)r4rQrrrrVws)r- isinstancer3r0rL IGNORECASEsearchr/)rr-valuesregex type_matchedpatternrrTrmatchYs    z Token.matchcCs&|j}|rt||r dS|j}|sdS)zReturns ``True`` if this token is within *group_cls*. Use this method for example to check if an identifier is within a function: ``t.within(sql.Function)``. TF)r.rW)r group_clsr.rrrwithin{s z Token.withincCs |j|kS)zr?rOr^r`rcrerrrrr+(s  " r+cseZdZdZdZd?fdd ZddZdd Zd d Zd d Z d@ddZ ddZ ddZ ddZ eddZdAddZdBddZdCd!d"Zd#d$Zd%d&ZdBd'd(ZdDd)d*ZdEd+d,Z  dBd-d.Zd/d0ZdFd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Z  dGd=d>Z Z!S)H TokenListzwA group of tokens. It has an additional instance attribute ``tokens`` which holds a list of child-tokens. rNcs:|pg_fddjDtdtd_dS)Ncsg|]}t|dqSra)setattrrRtokenr9rr z&TokenList.__init__..T)rsuperr5r3r1)rr __class__r9rr5s  zTokenList.__init__cCsddd|DS)Ncss|]}|jVqdSr7r8rjrrrrVsz$TokenList.__str__..)joinrOr9rrrr:szTokenList.__str__cCs t|jSr7)iterrr9rrr__iter__ zTokenList.__iter__cCs |j|Sr7r)ritemrrr __getitem__ruzTokenList.__getitem__cCs t|jSr7)typer'r9rrrr>ruzTokenList._get_repr_namerrqcCst|j}t|jD]P\}}|}|} ||dk} | r dnd} | dr.| dr.dnd} tdjd it |d|j rZ|dusH||krZ| rLd nd } | ||d||| q dS) zPretty-print the object tree.rz`- z|- r;r<z${_pre}{pre}{idx} {cls} {q}{value}{q})fileNz z| r) r"r enumerater>r?r@rAprintrBrCr1 _pprint_tree)r max_depthdepthf_pre token_countidxrkrDr,lastprerE parent_prerrrr|s&    zTokenList._pprint_treecCsDd}|D]}|t|j}||kr|kr|S|}qdS)z-Returns the token that is on position offset.rN)rOr"r,)roffsetrrkendrrrget_token_at_offsets zTokenList.get_token_at_offsetc csTz|jD]}|jr|EdHq|VqWdSty)}ztd|d}~ww)znGenerator yielding ungrouped tokens. This method is recursively called for all child tokens. Nz Maximum recursion depth exceeded)rr1rORecursionErrorr)rrkerrrrrrOs  zTokenList.flattenccs|jD]}|jr |VqdSr7)rr1rrkrrr get_sublistss  zTokenList.get_sublistscCr6r7rr9rrr_groupable_tokensszTokenList._groupable_tokensFcCs|durdSt|ttfs|f}|r;|dusJt|dddD]}|j|}|D]}||r7||fSq)q dSt|j|||dD]\}}|D]}||rZ||fSqLqFdS)znext token that match functionsNrrG)startNN)rWlisttuplerangerrz)rfuncsrrrrrkfuncrrr_token_matchings*  zTokenList._token_matchingTcsfdd}||dS)zReturns the first child token. If *skip_ws* is ``True`` (the default), whitespace tokens are ignored. if *skip_cm* is ``True`` (default: ``False``), comments are ignored too. c r|jpot|tjtd SN)rir2rrCommenttkskip_cmskip_wsrrmatcher z&TokenList.token_first..matcherrr)rrrrrrr token_firsts zTokenList.token_firstrGcs"|d7}|fdd||S)Nrcst|Sr7)rrrr rrrsz)TokenList.token_next_by..r)rrr rrrrrrr szTokenList.token_next_bycCs2t|ttfs |fn|}dd|D}|||S)Ncsg|]fddqS)cs | Sr7rrrrrrs z9TokenList.token_not_matching...r)rRrrrrlrmz0TokenList.token_not_matching..)rWrrrrrrrrrtoken_not_matchings zTokenList.token_not_matchingcCs|||dS)Nrrrrrrtoken_matchingszTokenList.token_matchingcCs|j|||ddS)zReturns the previous token relative to *idx*. If *skip_ws* is ``True`` (the default) whitespace tokens are ignored. If *skip_cm* is ``True`` comments are ignored. ``None`` is returned if there's no previous token. T)_reverse) token_next)rrrrrrr token_prevszTokenList.token_prevcs2|durdS|d7}fdd}|j|||dS)zReturns the next token relative to *idx*. If *skip_ws* is ``True`` (the default) whitespace tokens are ignored. If *skip_cm* is ``True`` comments are ignored. ``None`` is returned if there's no next token. Nrrcrrrrrrrr-rz%TokenList.token_next..matcherrr)rrrrrrrrrr"s zTokenList.token_nextcCs0t|tr|n||}||j|d|S)zReturn list index of token.N)rWint token_indexrindex)rrkrrrrr2szTokenList.token_indexc Cs|}|j|}||}|r1t||r1|j|d|}|} | j||j|d|=t|| _n|j||}||} | g|j||<|| _|D]} | | _qI| S)z+Replace tokens by an instance of *grp_cls*.r)rrWextendr3r,r.) rgrp_clsrr include_endr start_idxend_idx subtokensgrprkrrr group_tokens7s    zTokenList.group_tokenscCs,t|ts ||}||_|j||dS)zInserts *token* before *where*.N)rWrrr.rinsert)rwhererkrrr insert_beforeUs  zTokenList.insert_beforecCsVt|ts ||}|j||d\}}||_|dur"|j|dS|j||dS)zInserts *token* after *where*.rN)rWrrrr.rappendr)rrrkrnidxnext_rrr insert_after\s  zTokenList.insert_aftercCs |duS)z(Returns ``True`` if an alias is present.N)r&r9rrr has_aliasgs zTokenList.has_aliascCdS)rNrr9rrrr&kzTokenList.get_aliascCs|p|S)aReturns the name of this identifier. This is either it's alias or it's real name. The returned valued can be considered as the name under which the object corresponding to this identifier is known within the current statement. )r&rr9rrrget_nameoszTokenList.get_namecCr)rNrr9rrrrxrzTokenList.get_real_namecCs:|jtjdfd\}}||\}}|durt|jSdS)ztReturn name of the parent object if any. A parent object is identified by the first occurring dot. r r N)r rrrrr,)rrrprev_rrrget_parent_name|szTokenList.get_parent_namecCs|r |j|dn|j}|rt|n|}tjtjtjjg}|r%|tj|D]#}|j |vr5t |j St |t tfrJ|rD|S|Sq'dS)z/Returns the name of the first token with a nameN)rreversedrNameWildcardStringSymbolrr r-rr,rW IdentifierFunctionrr)rrrrr rtypesrkrrrrs  zTokenList._get_first_namer7)NrNrq)rNF)TF)NNNrGN)TFF)r)T)NFFF)"r'r(r)r*rgr5r:rtrwr>r|rrOrpropertyrrrr rrrrrrrrrr&rrrr __classcell__rrrorrhsF             rhc@eZdZdZddZdS) StatementzRepresents a SQL statement.cCs|jdd}|dur dS|jtjjtjjfvr|jS|jtjjkrM||}|j |dd\}}t |t t frM|j |dd\}}|durM|jtjjkrM|jSdS)aMReturns the type of a statement. The returned value is a string holding an upper-cased reprint of the first DML or DDL keyword. If the first token in this group isn't a DML or DDL keyword "UNKNOWN" is returned. Whitespaces and comments at the beginning of the statement are ignored. T)rNUNKNOWNr) rr-rr DMLDDLr/CTErrrWrIdentifierList)r first_tokenfidxtidxrkr dml_keywordrrrget_types  zStatement.get_typeN)r'r(r)r*rrrrrr rc@s0eZdZdZddZddZddZdd Zd S) rzNRepresents an identifier. Identifiers may have aliases or typecasts. cCs|jtjd\}}|duS)z7Return ``True`` if this identifier contains a wildcard.rN)r rr)rrrkrrr is_wildcardszIdentifier.is_wildcardcCs6|jtjdfd\}}|j|dd\}}|r|jSdS)zs:  n'    5