3
W­E^>.  ã               @   sr  d dl mZ d dl mZ d dlmZmZ ejejjejj	fZ
ejejjejjfZejejjfZdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeejƒdd„ ƒZdd„ Z d d!„ Z!d"d#„ Z"eej#ƒd$d%„ ƒZ$eej%ƒd&d'„ ƒZ&eƒ d(d)„ ƒZ'eej(ƒd*d+„ ƒZ)d,d-„ Z*eƒ d.d/„ ƒZ+d0d1„ Z,d2d3„ Z-d4d5„ d6d5„ d7d8d8fd9d:„Z.d7S );é    )Úsql)Útokens)ÚrecurseÚimtc       	      C   sº   g }d}x¬t t| ƒƒD ]œ\}}|| }|jr.q|jrLt||ƒ rLt||ƒ q|j|jŽ rd|j|ƒ q|j|j	Ž ry|j
ƒ }W n tk
r’   wY nX |}| j|||ƒ ||| 7 }qW dS )z*Groups Tokens that have beginning and end.r   N)Ú	enumerateÚlistÚis_whitespaceÚis_groupÚ
isinstanceÚ_group_matchingÚmatchÚM_OPENÚappendÚM_CLOSEÚpopÚ
IndexErrorÚgroup_tokens)	ÚtlistÚclsZopensÚtidx_offsetÚidxÚtokenÚtidxZopen_idxZ	close_idx© r   ú</tmp/pip-build-78s3knkw/sqlparse/sqlparse/engine/grouping.pyr      s&    
r   c             C   s   t | tjƒ d S )N)r   r   ÚSquareBrackets)r   r   r   r   Úgroup_brackets5   s    r   c             C   s   t | tjƒ d S )N)r   r   ÚParenthesis)r   r   r   r   Úgroup_parenthesis9   s    r   c             C   s   t | tjƒ d S )N)r   r   ÚCase)r   r   r   r   Ú
group_case=   s    r    c             C   s   t | tjƒ d S )N)r   r   ÚIf)r   r   r   r   Úgroup_ifA   s    r"   c             C   s   t | tjƒ d S )N)r   r   ÚFor)r   r   r   r   Ú	group_forE   s    r$   c             C   s   t | tjƒ d S )N)r   r   ZBegin)r   r   r   r   Úgroup_beginI   s    r%   c             C   s8   dd„ }dd„ }dd„ }| }}t | tj||||ƒ d S )Nc             S   s   | j tjdƒS )Nz::)r   ÚTÚPunctuation)r   r   r   r   r   N   s    zgroup_typecasts.<locals>.matchc             S   s   | d k	S )Nr   )r   r   r   r   ÚvalidQ   s    zgroup_typecasts.<locals>.validc             S   s   ||fS )Nr   )r   Úpidxr   Únidxr   r   r   ÚpostT   s    zgroup_typecasts.<locals>.post)Ú_groupr   Ú
Identifier)r   r   r(   r+   Ú
valid_prevÚ
valid_nextr   r   r   Úgroup_typecastsM   s
    r0   c             C   s8   dd„ }dd„ }dd„ }dd„ }t | tj||||ƒ d S )	Nc             S   s   | j tjdƒS )NÚ.)r   r&   r'   )r   r   r   r   r   \   s    zgroup_period.<locals>.matchc             S   s(   t jt jf}tjtjjf}t| ||d�S )N)ÚiÚt)r   r   r-   r&   ÚNameÚStringÚSymbolr   )r   ÚsqlclsÚttypesr   r   r   r.   _   s    z group_period.<locals>.valid_prevc             S   s   dS )NTr   )r   r   r   r   r/   d   s    z group_period.<locals>.valid_nextc             S   sT   t jt jf}tjtjjtjf}|d k	r.| | nd }t|||d�}|rL||fS ||fS )N)r2   r3   )	r   r   ÚFunctionr&   r4   r5   r6   ÚWildcardr   )r   r)   r   r*   r7   r8   Únext_r/   r   r   r   r+   h   s
    zgroup_period.<locals>.post)r,   r   r-   )r   r   r.   r/   r+   r   r   r   Úgroup_period[   s
    	r<   c             C   s8   dd„ }dd„ }dd„ }dd„ }t | tj||||ƒ d S )	Nc             S   s   | j o| jdkS )NZAS)Ú
is_keywordÚ
normalized)r   r   r   r   r   u   s    zgroup_as.<locals>.matchc             S   s   | j dkp| j S )NÚNULL)r>   r=   )r   r   r   r   r.   x   s    zgroup_as.<locals>.valid_prevc             S   s"   t jt jf}t| |d� o | d k	S )N)r3   )r&   ZDMLZDDLr   )r   r8   r   r   r   r/   {   s    zgroup_as.<locals>.valid_nextc             S   s   ||fS )Nr   )r   r)   r   r*   r   r   r   r+      s    zgroup_as.<locals>.post)r,   r   r-   )r   r   r.   r/   r+   r   r   r   Úgroup_ast   s
    r@   c             C   s8   dd„ }dd„ }dd„ }| }}t | tj||||ƒ d S )Nc             S   s   | j tjdƒS )Nz:=)r   r&   Ú
Assignment)r   r   r   r   r   †   s    zgroup_assignment.<locals>.matchc             S   s   | d k	o| j tjkS )N)Úttyper&   ÚKeyword)r   r   r   r   r(   ‰   s    zgroup_assignment.<locals>.validc             S   s,   t jdf}| j||d�\}}|p"|}||fS )Nú;)Úmr   )r&   r'   Útoken_next_by)r   r)   r   r*   Zm_semicolonZsnidxÚ_r   r   r   r+   Œ   s    
zgroup_assignment.<locals>.post)r,   r   rA   )r   r   r(   r+   r.   r/   r   r   r   Úgroup_assignment…   s
    rH   c          	      sb   t jt jt jt jf‰ tt t ‰dd„ }‡ ‡fdd„}dd„ }| }}t| t j	||||dd� d S )	Nc             S   s   | j tjjkS )N)rB   r&   ÚOperatorÚ
Comparison)r   r   r   r   r   ›   s    zgroup_comparison.<locals>.matchc                s2   t | ˆˆ d�rdS | r*| jr*| jdkr*dS dS d S )N)r3   r2   Tr?   F)r   r=   r>   )r   )r7   r8   r   r   r(   ž   s
    zgroup_comparison.<locals>.validc             S   s   ||fS )Nr   )r   r)   r   r*   r   r   r   r+   ¦   s    zgroup_comparison.<locals>.postF)Úextend)
r   r   r9   r-   Ú	OperationÚT_NUMERICALÚT_STRINGÚT_NAMEr,   rJ   )r   r   r(   r+   r.   r/   r   )r7   r8   r   Úgroup_comparison–   s    
rP   c             C   sN   t jjt jf}| j|d�\}}x*|rH| jtj||ƒ | j||d�\}}q W d S )N)r3   )r3   r   )r&   r5   r6   r4   rF   r   r   r-   )r   r8   r   r   r   r   r   Úgroup_identifier®   s
    rQ   c          
      sb   t jt jt jf‰ tjtjjf‰dd„ }‡ ‡fdd„}dd„ }dd„ }t| t j||||d	d
d� d S )Nc             S   s   t | tjƒS )N)r
   r   r   )r   r   r   r   r   ¼   s    zgroup_arrays.<locals>.matchc                s   t | ˆ ˆd�S )N)r2   r3   )r   )r   )r7   r8   r   r   r.   ¿   s    z group_arrays.<locals>.valid_prevc             S   s   dS )NTr   )r   r   r   r   r/   Â   s    z group_arrays.<locals>.valid_nextc             S   s   ||fS )Nr   )r   r)   r   r*   r   r   r   r+   Å   s    zgroup_arrays.<locals>.postTF)rK   r   )	r   r   r-   r9   r&   r4   r5   r6   r,   )r   r   r.   r/   r+   r   )r7   r8   r   Úgroup_arrays¸   s    
rR   c          	      sf   t t t ‰tjtjtjtjtjf‰ dd„ }‡ ‡fdd„}dd„ }| }}t	| tj||||dd� d S )	Nc             S   s   t | tjtjfd�S )N)r3   )r   r&   rI   r:   )r   r   r   r   r   Ñ   s    zgroup_operator.<locals>.matchc                s   t | ˆ ˆd�S )N)r2   r3   )r   )r   )r7   r8   r   r   r(   Ô   s    zgroup_operator.<locals>.validc             S   s   t j| | _||fS )N)r&   rI   rB   )r   r)   r   r*   r   r   r   r+   ×   s    zgroup_operator.<locals>.postF)rK   )
rM   rN   rO   r   r   r   r9   r-   rL   r,   )r   r   r(   r+   r.   r/   r   )r7   r8   r   Úgroup_operatorÌ   s    
rS   c          	      s†   t jdf‰ tjtjtjtjtjtjf‰t	t
 t t jt jt jf ‰dd„ }‡ ‡‡fdd„}dd„ }| }}t| tj||||d	d
� d S )NÚnullÚrolec             S   s   | j tjdƒS )Nú,)r   r&   r'   )r   r   r   r   r   ç   s    z$group_identifier_list.<locals>.matchc                s   t | ˆˆ ˆd�S )N)r2   rE   r3   )r   )r   )Úm_roler7   r8   r   r   r(   ê   s    z$group_identifier_list.<locals>.validc             S   s   ||fS )Nr   )r   r)   r   r*   r   r   r   r+   í   s    z#group_identifier_list.<locals>.postT)rK   )rT   rU   )r&   rC   r   r9   r   r-   rJ   ZIdentifierListrL   rM   rN   rO   ÚCommentr:   r,   )r   r   r(   r+   r.   r/   r   )rW   r7   r8   r   Úgroup_identifier_listà   s    

rY   c             C   st   | j tjd�\}}x\|rn| jdd„ |d�\}}|d k	rX| j|dd�\}}| jtj||ƒ | j tj|d�\}}qW d S )N)r3   c             S   s   t | tjd�p| jS )N)r3   )r   r&   rX   r   )Ztkr   r   r   Ú<lambda>ú   s    z group_comments.<locals>.<lambda>)r   F)Zskip_ws)r3   r   )rF   r&   rX   Ztoken_not_matchingÚ
token_prevr   r   )r   r   r   ÚeidxÚendr   r   r   Úgroup_commentsõ   s    r^   c             C   sŠ   | j tjjd�\}}xp|r„| j tjj|d�\}}|d krD| jd }n| j|d  }| j|ƒ}| jtj||ƒ | j tjj|d�\}}qW d S )N)rE   )rE   r   é   éÿÿÿÿ)	rF   r   ÚWherer   r   Z_groupable_tokensr   Ztoken_indexr   )r   r   r   r\   r]   r   r   r   Úgroup_where  s    
rb   c             C   s‚   t jt jt jt jt jt jf}| j|tj	d�\}}xL|r|| j
|ƒ\}}t|t jƒrd| jt j||dd� | j|tj	|d�\}}q2W d S )N)r2   r3   T)rK   )r2   r3   r   )r   r   r9   r   r-   rL   rJ   rF   r&   ÚNumberÚ
token_nextr
   r   )r   ZI_ALIASr   r   r*   r;   r   r   r   Úgroup_aliased  s    re   c             C   sž   d}d}x*| j D ] }|jdkr"d}|jdkrd}qW |r@|r@d S | jtjd�\}}xF|r˜| j|ƒ\}}t|tjƒr‚| j	tj
||ƒ | jtj|d�\}}qTW d S )NFZCREATETZTABLE)r3   )r3   r   )r   ÚvaluerF   r&   r4   rd   r
   r   r   r   r9   )r   Z
has_createZ	has_tableZ	tmp_tokenr   r   r*   r;   r   r   r   Úgroup_functions   s    

rg   c             C   sl   | j tjjd�\}}xR|rf| j|ƒ\}}t|tjtjd�rN| j	tj||ƒ |}| j tjj|d�\}}qW dS )z,Group together Identifier and Asc/Desc token)r3   )r2   r3   )r3   r   N)
rF   r&   rC   ZOrderr[   r   r   r-   rc   r   )r   r   r   r)   Úprev_r   r   r   Úgroup_order4  s    ri   c             C   sf   | j tjd�\}}xN|r`| j|ƒ\}}t|tjƒrJ| jtj||dd� |}| j tj|d�\}}qW d S )N)r2   T)rK   )r2   r   )rF   r   rX   r[   r
   Z	TokenListr   )r   r   r   r)   rh   r   r   r   Úalign_comments?  s    rj   c             C   sf   | j tjdfd�\}}|}d}x&|rDt|tjƒr4|}| j|ƒ\}}q W |dkrb| jtj||dd� d S )NZVALUES)rE   r_   T)rK   r`   r`   )	rF   r&   rC   r
   r   r   rd   r   ÚValues)r   r   r   Z	start_idxZend_idxr   r   r   Úgroup_valuesJ  s    rl   c             C   sF   x@t ttttttttt	t
tttttttttttgD ]}|| ƒ q2W | S )N)r^   r   r   r    r"   r$   r%   rg   rb   r<   rR   rQ   ri   r0   rS   rP   r@   re   rH   rj   rY   rl   )ÚstmtÚfuncr   r   r   ÚgroupV  s2    
ro   c             C   s   dS )NTr   )r3   r   r   r   rZ   x  s    rZ   c             C   s   dS )NTr   )r3   r   r   r   rZ   y  s    NTc          	   C   sÔ   d}d\}	}
xÂt t| ƒƒD ]²\}}|| }|jr2q|r\|jr\t||ƒ r\t|||||||ƒ ||ƒrÂ| j|ƒ\}}|
rÂ||
ƒrÂ||ƒrÂ|| |	||ƒ\}}| j||||d�}||| 7 }|| }	}
q|| }	}
qW dS )zDGroups together tokens that are joined by a middle token. i.e. x < yr   N)rK   )NN)r   r   r   r	   r
   r,   rd   r   )r   r   r   r.   r/   r+   rK   r   r   r)   rh   r   r   r   r*   r;   Zfrom_idxZto_idxÚgrpr   r   r   r,   w  s"    	
r,   )/Zsqlparser   r   r&   Zsqlparse.utilsr   r   rc   ZIntegerZFloatrM   r5   ÚSingler6   rN   r4   ZPlaceholderrO   r   r   r   r    r"   r$   r%   r0   r<   r@   rH   rP   r-   rQ   rR   rS   rY   rX   r^   ra   rb   re   r9   rg   ri   rj   rl   ro   r,   r   r   r   r   Ú<module>	   sD   #
"