U
    ڲg0R                     @   s  d Z ddlZddlmZ ddlmZ ddlmZm	Z	 G dd dZ
G dd	 d	ZG d
d deZG dd deZG dd de
eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZG d(d) d)eZG d*d+ d+e
eZG d,d- d-eZG d.d/ d/eZG d0d1 d1eZG d2d3 d3eZ dS )4zFThis module contains classes representing syntactical elements of SQL.    Ntokens)SQLParseError)imtremove_quotesc                   @   s    e Zd ZdZdd Zdd ZdS )NameAliasMixinz'Implements get_real_name and get_alias.c                 C   s$   | j tjdfd\}}| j|ddS )7Returns the real name (object name) of this identifier..mT)	real_name)token_next_byTPunctuation_get_first_name)selfdot_idx_ r   0/tmp/pip-unpacked-wheel-4xfyt83m/sqlparse/sql.pyget_real_name   s    zNameAliasMixin.get_real_namec                 C   sh   | j tjdfd\}}|dk	r0| j|d ddS | j tjd\}}t| jdkrd|dk	rd| jdd	S dS )
2Returns the alias for this identifier or ``None``.ZASr
   N   T)keywordst   reverse)r   r   Keywordr   
Whitespacelenr   )r   Zkw_idxkwr   wsr   r   r   	get_alias   s    zNameAliasMixin.get_aliasN)__name__
__module____qualname____doc__r   r$   r   r   r   r   r      s   r   c                   @   sf   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dddZdd Zdd Zdd ZdS )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.
    )valuettypeparent
normalized
is_keywordis_groupis_whitespace
is_newlinec                 C   s`   t |}|| _|| _d | _d| _|tjk| _| jtjk| _	| jtj
k| _| jrV| n|| _d S )NF)strr*   r+   r,   r/   r   r   r.   r    r0   ZNewliner1   upperr-   )r   r+   r*   r   r   r   __init__3   s    zToken.__init__c                 C   s   | j S Nr*   r   r   r   r   __str__>   s    zToken.__str__c                 C   sF   |   }|  }|dr(|dr(dnd}djf dt| it S )N'"z"<{cls} {q}{value}{q} at 0x{id:2X}>id)_get_repr_name_get_repr_value
startswithendswithformatr;   locals)r   clsr*   qr   r   r   __repr__E   s    zToken.__repr__c                 C   s   t | jdd S )Nr	   )r2   r+   splitr7   r   r   r   r<   M   s    zToken._get_repr_namec                 C   s2   t | }t|dkr$|d d d }tdd|S )N      z...z\s+ )r2   r!   resub)r   rawr   r   r   r=   P   s    zToken._get_repr_valuec                 c   s
   | V  dS )zResolve subgroups.Nr   r7   r   r   r   flattenV   s    zToken.flattenFc                    s   | j |k}|r|dkr|S t|tr*|f}|rp| jr:tjnd  fdd|D }|D ]}|| jrT dS qTdS | jrdd |D }| j|kS )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.
        Nr   c                 3   s   | ]}t | V  qd S r5   )rJ   compile.0vflagr   r   	<genexpr>p   s     zToken.match.<locals>.<genexpr>TFc                 s   s   | ]}|  V  qd S r5   )r3   rO   r   r   r   rT   x   s     )r+   
isinstancer2   r.   rJ   
IGNORECASEsearchr-   )r   r+   valuesregexZtype_matchedpatternr   rR   r   matchZ   s    

zToken.matchc                 C   s$   | j }|r t||rdS |j }qdS )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,   rU   )r   Z	group_clsr,   r   r   r   within|   s    
zToken.withinc                 C   s
   | j |kS )z<Returns ``True`` if this token is a direct child of *other*.r,   )r   otherr   r   r   is_child_of   s    zToken.is_child_ofc                 C   s"   | j }|r||krdS |j }qdS )z7Returns ``True`` if *other* is in this tokens ancestry.TFr]   )r   r^   r,   r   r   r   has_ancestor   s    zToken.has_ancestorN)F)r%   r&   r'   r(   	__slots__r4   r8   rD   r<   r=   rM   r[   r\   r_   r`   r   r   r   r   r)   (   s   
"r)   c                       s  e Zd ZdZdZd? fdd	Zdd Zdd	 Zd
d Zdd Z	d@ddZ
dd Zdd Zdd Zedd ZdAddZdBddZdCd!d"Zd#d$ Zd%d& ZdDd'd(ZdEd)d*ZdFd+d,ZdGd-d.Zd/d0 ZdHd1d2Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< ZdId=d>Z   Z!S )J	TokenListzwA group of tokens.

    It has an additional instance attribute ``tokens`` which holds a
    list of child-tokens.
    r   Nc                    s:   |pg  _  fdd j D  t d t  d _d S )Nc                    s   g | ]}t |d  qS r]   )setattrrP   tokenr7   r   r   
<listcomp>   s     z&TokenList.__init__.<locals>.<listcomp>T)r   superr4   r2   r/   )r   r   	__class__r7   r   r4      s    
zTokenList.__init__c                 C   s   d dd |  D S )N c                 s   s   | ]}|j V  qd S r5   r6   rd   r   r   r   rT      s     z$TokenList.__str__.<locals>.<genexpr>)joinrM   r7   r   r   r   r8      s    zTokenList.__str__c                 C   s
   t | jS r5   )iterr   r7   r   r   r   __iter__   s    zTokenList.__iter__c                 C   s
   | j | S r5   r   )r   itemr   r   r   __getitem__   s    zTokenList.__getitem__c                 C   s
   t | jS r5   )typer%   r7   r   r   r   r<      s    zTokenList._get_repr_namer   rj   c                 C   s   t | j}t| jD ]\}}| }| }	||d k}
|
r@dnd}|	dr\|	dr\dnd}tdjf t	 |d |j
r|dks||k r|
rd	nd
}|||d |||  qdS )zPretty-print the object tree.r   z`- z|- r9   r:   z${_pre}{pre}{idx} {cls} {q}{value}{q})fileNz   z|  )r!   r   	enumerater<   r=   r>   r?   printr@   rA   r/   _pprint_tree)r   Z	max_depthdepthf_preZtoken_countidxre   rB   r*   lastprerC   Z
parent_prer   r   r   rt      s    
zTokenList._pprint_treec                 C   sH   d}|   D ]6}|t|j }||  kr2|k r>n n|  S |}qdS )z-Returns the token that is on position offset.r   N)rM   r!   r*   )r   offsetrx   re   endr   r   r   get_token_at_offset   s    zTokenList.get_token_at_offsetc              
   c   s^   z,| j D ] }|jr"| E dH  q|V  qW n, tk
rX } ztd|W 5 d}~X Y nX dS )znGenerator yielding ungrouped tokens.

        This method is recursively called for all child tokens.
        Nz Maximum recursion depth exceeded)r   r/   rM   RecursionErrorr   )r   re   errr   r   r   rM      s    
zTokenList.flattenc                 c   s   | j D ]}|jr|V  qd S r5   )r   r/   r   re   r   r   r   get_sublists   s    
zTokenList.get_sublistsc                 C   s   | j S r5   r   r7   r   r   r   _groupable_tokens   s    zTokenList._groupable_tokensFc           	      C   s   |dkrdS t |ttfs |f}|rB|dks0tt|d dd}n|dkrTt| j}t||}|D ]0}| j| }|D ]}||rt||f    S qtqbdS )znext token that match functionsNr   rE   NN)rU   listtupleAssertionErrorranger!   r   )	r   funcsstartr|   r   Zindexesrx   re   funcr   r   r   _token_matching   s     


zTokenList._token_matchingTc                    s    fdd}|  |d S )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                    s    r
| j p ot| tjtd S N)r   ir0   r   r   CommentZtkskip_cmskip_wsr   r   matcher  s    
z&TokenList.token_first.<locals>.matcherr   r   )r   r   r   r   r   r   r   token_first   s    
zTokenList.token_firstrE   c                    s"   |d7 }|   fdd||S )Nr   c                    s   t |  S r5   )r   r   r   r   r   r   r   <lambda>      z)TokenList.token_next_by.<locals>.<lambda>r   )r   r   r   r   rx   r|   r   r   r   r     s    zTokenList.token_next_byc                 C   s2   t |ttfs|fn|}dd |D }| ||S )Nc                    s   g | ]  fd dqS )c                    s
    |  S r5   r   r   r   r   r   r     r   z9TokenList.token_not_matching.<locals>.<listcomp>.<lambda>r   )rP   r   r   r   rf     s     z0TokenList.token_not_matching.<locals>.<listcomp>)rU   r   r   r   r   r   rx   r   r   r   token_not_matching  s    zTokenList.token_not_matchingc                 C   s   |  ||d S )Nr   r   r   r   r   r   token_matching  s    zTokenList.token_matchingc                 C   s   | j |||ddS )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)r   rx   r   r   r   r   r   
token_prev  s    zTokenList.token_prevc                    s2   |dkrdS |d7 } fdd}| j |||dS )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.
        Nr   r   c                    s    r
| j p ot| tjtd S r   r   r   r   r   r   r   .  s    
z%TokenList.token_next.<locals>.matcherr   r   )r   rx   r   r   r   r   r   r   r   r   #  s
    zTokenList.token_nextc                 C   s0   t |tr|n| |}|| j|d | S )zReturn list index of token.N)rU   inttoken_indexr   index)r   re   r   r   r   r   r   3  s    zTokenList.token_indexc                 C   s   |}| j | }|| }|rbt||rb| j |d | }|}	|	j | | j |d |= t||	_n,| j || }||}	|	g| j ||< | |	_|D ]
}
|	|
_q|	S )z+Replace tokens by an instance of *grp_cls*.r   )r   rU   extendr2   r*   r,   )r   Zgrp_clsr   r|   Zinclude_endr   Z	start_idxZend_idxZ	subtokensgrpre   r   r   r   group_tokens8  s     
zTokenList.group_tokensc                 C   s,   t |ts| |}| |_| j|| dS )zInserts *token* before *where*.N)rU   r   r   r,   r   insert)r   wherere   r   r   r   insert_beforeV  s    

zTokenList.insert_beforec                 C   sT   t |ts| |}| j||d\}}| |_|dkrB| j| n| j|| dS )zInserts *token* after *where*.r   N)rU   r   r   r   r,   r   appendr   )r   r   re   r   nidxnext_r   r   r   insert_after]  s    

zTokenList.insert_afterc                 C   s   |   dk	S )z(Returns ``True`` if an alias is present.N)r$   r7   r   r   r   	has_aliash  s    zTokenList.has_aliasc                 C   s   dS )r   Nr   r7   r   r   r   r$   l  s    zTokenList.get_aliasc                 C   s   |   p|  S )a  Returns 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$   r   r7   r   r   r   get_namep  s    zTokenList.get_namec                 C   s   dS )r   Nr   r7   r   r   r   r   y  s    zTokenList.get_real_namec                 C   s:   | j tjdfd\}}| |\}}|dk	r6t|jS dS )ztReturn name of the parent object if any.

        A parent object is identified by the first occurring dot.
        r	   r
   N)r   r   r   r   r   r*   )r   r   r   Zprev_r   r   r   get_parent_name}  s    zTokenList.get_parent_namec                 C   s   |r| j |d n| j }|r$t|n|}tjtjtjjg}|rJ|tj |D ]B}|j	|krjt
|j  S t|ttfrN|r| n|   S qNdS )z/Returns the name of the first token with a nameN)r   reversedr   NameWildcardStringZSymbolr   r   r+   r   r*   rU   
IdentifierFunctionr   r   )r   rx   r   r   r   r   typesre   r   r   r   r     s    
zTokenList._get_first_name)N)Nr   Nrj   )r   NF)TF)NNNrE   N)TF)TFF)r   )TF)T)NFFF)"r%   r&   r'   r(   ra   r4   r8   rm   ro   r<   rt   r}   rM   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r   r   r   __classcell__r   r   rh   r   rb      sD   
	







  

		  rb   c                   @   s   e Zd ZdZdd ZdS )	StatementzRepresents a SQL statement.c                 C   s   | j dd}|dkrdS |jtjjtjjfkr4|jS |jtjjkr| |}|dk	r| j	|dd\}}t
|ttfrL| j	|dd\}}|dk	rL|jtjjkrL|jS qLdS )aM  Returns 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)r   NUNKNOWNr   )r   r+   r   r   ZDMLZDDLr-   ZCTEr   r   rU   r   IdentifierList)r   re   Ztidxr   r   r   get_type  s     

zStatement.get_typeN)r%   r&   r'   r(   r   r   r   r   r   r     s   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   zNRepresents an identifier.

    Identifiers may have aliases or typecasts.
    c                 C   s   | j tjd\}}|dk	S )z7Return ``True`` if this identifier contains a wildcard.r   N)r   r   r   )r   r   re   r   r   r   is_wildcard  s    zIdentifier.is_wildcardc                 C   s6   | j tjdfd\}}| j|dd\}}|r2|jS dS )z<Returns the typecast or ``None`` of this object as a string.z::r
   Fr   N)r   r   r   r   r*   )r   Zmidxmarkerr   r   r   r   r   get_typecast  s    zIdentifier.get_typecastc                 C   s"   | j tjjd\}}|r|jS dS )z5Returns the ordering or ``None`` as uppercase string.r   N)r   r   r   ZOrderr-   )r   r   Zorderingr   r   r   get_ordering  s    zIdentifier.get_orderingc                 c   s*   | j D ]}t|tr|j dd V  qdS )z(Returns an iterator of index token listsr   rE   N)r   rU   SquareBracketsr   r   r   r   get_array_indices  s    

zIdentifier.get_array_indicesN)r%   r&   r'   r(   r   r   r   r   r   r   r   r   r     s
   r   c                   @   s   e Zd ZdZdd ZdS )r   z.A list of :class:`~sqlparse.sql.Identifier`'s.c                 c   s*   | j D ]}|js|tjds|V  qdS )zkReturns the identifiers.

        Whitespaces and punctuations are not included in this generator.
        ,N)r   r0   r[   r   r   r   r   r   r   get_identifiers  s    
zIdentifierList.get_identifiersN)r%   r&   r'   r(   r   r   r   r   r   r     s   r   c                   @   s<   e Zd ZdZejjdfejdfgZej	j
dfZejdfZdS )TypedLiteralzEA typed literal, such as "date '2001-09-28'" or "interval '2 hours'".N	TIMESTAMP)ZDAYZHOURZMINUTEZMONTHZSECONDZYEAR)r%   r&   r'   r(   r   r   ZBuiltinr   M_OPENr   SingleM_CLOSEZM_EXTENDr   r   r   r   r     s   r   c                   @   s0   e Zd ZdZejdfZejdfZedd Z	dS )ParenthesiszTokens between parenthesis.()c                 C   s   | j dd S Nr   rE   r   r7   r   r   r   r     s    zParenthesis._groupable_tokensN
r%   r&   r'   r(   r   r   r   r   r   r   r   r   r   r   r     s
   

r   c                   @   s0   e Zd ZdZejdfZejdfZedd Z	dS )r   zTokens between square brackets[]c                 C   s   | j dd S r   r   r7   r   r   r   r     s    z SquareBrackets._groupable_tokensNr   r   r   r   r   r     s
   

r   c                   @   s   e Zd ZdZdS )
Assignmentz An assignment like 'var := val;'Nr%   r&   r'   r(   r   r   r   r   r     s   r   c                   @   s$   e Zd ZdZejdfZejdfZdS )Ifz7An 'if' clause with possible 'else if' or 'else' parts.ZIFzEND IFNr%   r&   r'   r(   r   r   r   r   r   r   r   r   r     s   
r   c                   @   s$   e Zd ZdZejdfZejdfZdS )ForzA 'FOR' loop.)ZFORZFOREACHzEND LOOPNr   r   r   r   r   r     s   
r   c                   @   s(   e Zd ZdZedd Zedd ZdS )
Comparisonz/A comparison used for example in WHERE clauses.c                 C   s
   | j d S Nr   r   r7   r   r   r   left  s    zComparison.leftc                 C   s
   | j d S )NrE   r   r7   r   r   r   right  s    zComparison.rightN)r%   r&   r'   r(   r   r   r   r   r   r   r   r     s
   
r   c                   @   s   e Zd ZdZdd ZdS )r   z
A comment.c                 C   s   | j o| j d jtjjkS r   )r   r+   r   r   Z	Multiliner7   r   r   r   is_multiline&  s    zComment.is_multilineN)r%   r&   r'   r(   r   r   r   r   r   r   #  s   r   c                   @   s$   e Zd ZdZejdfZejdfZdS )WherezA WHERE clause.ZWHERE)	ORDER BYzGROUP BYLIMITZUNIONz	UNION ALLZEXCEPTHAVINGZ	RETURNINGZINTONr   r   r   r   r   r   *  s   
r   c                   @   s   e Zd ZdZejdfZdS )OverzAn OVER clause.ZOVERN)r%   r&   r'   r(   r   r   r   r   r   r   r   r   2  s   r   c                   @   s$   e Zd ZdZejdfZejdfZdS )HavingzA HAVING clause.r   )r   r   Nr   r   r   r   r   r   7  s   
r   c                   @   s.   e Zd ZdZejdfZejdfZdddZdS )	CasezAA CASE statement with one or more WHEN and possibly an ELSE part.CASEENDFc                 C   s   d}d}g }|}| j D ]}|tjdr,qn~|r@|jtjkr@qnj|tjdrb|g g f |}nH|tjdrv|}n4|tjdr|dg f |}n|tjdrd}|r|s|g g f ||kr|d	 d
 | q||kr|d	 d | q|S )zeReturns a list of 2-tuples (condition, value).

        If an ELSE exists condition is None.
        r   r   r   ZWHENZTHENELSENr   rE   r   )r   r[   r   r   r+   r    r   )r   r   Z	CONDITIONZVALUEretmodere   r   r   r   	get_casesB  s4    
zCase.get_casesN)F)	r%   r&   r'   r(   r   r   r   r   r   r   r   r   r   r   =  s   

r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zA function or procedure call.c                 C   sZ   | j tdd }g }|jD ]:}t|tr4|   S t|ttt	ft
jdr|| q|S )zReturn a list of parameters.r   r   )r   r   )r   r   r   rU   r   r   r   r   r   r   r   Literalr   )r   Zparenthesisresultre   r   r   r   get_parametersu  s    

zFunction.get_parametersc                 C   s"   | j td}|sdS |d jd S )zReturn the window if it exists.r   Nr   rE   )r   r   r   )r   Zover_clauser   r   r   
get_window  s    zFunction.get_windowN)r%   r&   r'   r(   r   r   r   r   r   r   r   r  s   r   c                   @   s$   e Zd ZdZejdfZejdfZdS )BeginzA BEGIN/END block.ZBEGINr   Nr   r   r   r   r   r     s   
r   c                   @   s   e Zd ZdZdS )	OperationzGrouping of operationsNr   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )ValueszGrouping of valuesNr   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )CommandzGrouping of CLI commands.Nr   r   r   r   r   r     s   r   )!r(   rJ   Zsqlparser   r   Zsqlparse.exceptionsr   Zsqlparse.utilsr   r   r   r)   rb   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s:   o  (

5