
    +hO                        d Z ddlZddlmZ ddlmZmZ  G d d      Z G d d      Z	 G d	 d
e	      Z
 G d de
      Z G d dee
      Z G d de
      Z G d de
      Z G d de
      Z G d de
      Z G d de
      Z G d de
      Z G d de
      Z G d de
      Z G d d e
      Z G d! d"e
      Z G d# d$e
      Z G d% d&e
      Z G d' d(ee
      Z G d) d*e
      Z G d+ d,e
      Z G d- d.e
      Z G d/ d0e
      Zy)1zFThis module contains classes representing syntactical elements of SQL.    Ntokens)imtremove_quotesc                       e Zd ZdZd Zd Zy)NameAliasMixinz'Implements get_real_name and get_alias.c                 r    | j                  t        j                  df      \  }}| j                  |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_s      ?/var/www/html/venv/lib/python3.12/site-packages/sqlparse/sql.pyget_real_namezNameAliasMixin.get_real_name   s:     ''1==#*>'?
##Gt#<<    c                    | j                  t        j                  df      \  }}|| j                  |dz   d      S | j                  t        j                        \  }}t        | j                        dkD  r|| j                  d	      S yy)
2Returns the alias for this identifier or ``None``.ASr   N   T)keywordst   reverse)r   r   Keywordr   
Whitespacelenr   )r   kw_idxkwr   wss        r   	get_aliaszNameAliasMixin.get_alias   s     ''199d*;'<
>''
T'BB ""Q\\"22t{{aBN'''55 %3r   N)__name__
__module____qualname____doc__r   r)    r   r   r   r      s    1=6r   r   c                   R    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
dd	Zd
 Zd Zd Zy)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_whitespacec                    t        |      }|| _        || _        d | _        d| _        |t
        j                  v | _        | j                  t
        j                  v | _	        | j                  r|j                         | _        y || _        y )NF)strr1   r2   r3   r6   r   r#   r5   r$   r7   upperr4   )r   r2   r1   s      r   __init__zToken.__init__2   se    E


199,!ZZ1<<7+/??%++-r   c                     | j                   S Nr1   r   s    r   __str__zToken.__str__<   s    zzr   c                     | j                         }| j                         }|j                  d      r|j                  d      rdnd} dj                  ddt        |       it               S )N'"z"<{cls} {q}{value}{q} at 0x{id:2X}>idr.   )_get_repr_name_get_repr_value
startswithendswithformatrD   locals)r   clsr1   qs       r   __repr__zToken.__repr__C   si    !!#$$&##C(U^^C-@Cc:3:: %$x%!8% 	%r   c                 P    t        | j                        j                  d      d   S )Nr   )r9   r2   splitr?   s    r   rE   zToken._get_repr_nameK   s     4::$$S)"--r   c                 r    t        |       }t        |      dkD  r|d d dz   }t        j                  dd|      S )N      z...z\s+ )r9   r%   resub)r   raws     r   rF   zToken._get_repr_valueN   s8    $is8a<bq'E/Cvvfc3''r   c              #      K   |  yw)zResolve subgroups.Nr.   r?   s    r   flattenzToken.flattenT   s     
s   c                 8   | j                   |u }|r||S t        |t              r|f}|rN| j                  rt        j
                  ndfd|D        }|D ]  }|j                  | j                        s y y| j                  r	d |D        }| j                  |v 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.
        r   c              3   J   K   | ]  }t        j                  |        y wr=   )rU   compile).0vflags     r   	<genexpr>zToken.match.<locals>.<genexpr>n   s     :abjjD):s    #TFc              3   <   K   | ]  }|j                           y wr=   )r:   )r]   r^   s     r   r`   zToken.match.<locals>.<genexpr>v   s     0Aaggi0s   )r2   
isinstancer9   r5   rU   
IGNORECASEsearchr4   )r   r2   valuesregextype_matchedpatternr_   s         @r   matchzToken.matchX   s     zzU*v~fc"YF$(OO2==D:6:F!  >>$//2  ??00F&((r   c                 X    | j                   }|rt        ||      ry|j                   }|ry)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)r3   rb   )r   	group_clsr3   s      r   withinzToken.withinz   s0     &),]]F  r   c                      | j                   |k(  S )z<Returns ``True`` if this token is a direct child of *other*.r3   )r   others     r   is_child_ofzToken.is_child_of   s    {{e##r   c                 J    | j                   }|r||k(  ry|j                   }|ry)z7Returns ``True`` if *other* is in this tokens ancestry.TFrn   )r   ro   r3   s      r   has_ancestorzToken.has_ancestor   s+    ]]F  r   NF)r*   r+   r,   r-   	__slots__r;   r@   rM   rE   rF   rY   ri   rl   rp   rr   r.   r   r   r0   r0   '   sB    .IF%.( )D$r   r0   c                        e Zd ZdZdZd fd	Zd Zd Zd Zd Z	ddZ
d	 Zd
 Zd Zed        Zd dZd!dZd"dZd Zd Zd!dZd#dZd$dZ	 	 d!dZd Zd%dZd Zd Zd Zd Zd Z	 	 d&dZ  xZ!S )'	TokenListzwA group of tokens.

    It has an additional instance attribute ``tokens`` which holds a
    list of child-tokens.
    r   c                     |xs g | _         | j                   D cg c]  }t        |d|        c} t        |   d t	        |              d| _        y c c}w )Nr3   T)r   setattrsuperr;   r9   r6   )r   r   token	__class__s      r   r;   zTokenList.__init__   sI    l59[[AE$	'As4y) 	Bs   Ac                 N    dj                  d | j                         D              S )N c              3   4   K   | ]  }|j                     y wr=   r>   )r]   rz   s     r   r`   z$TokenList.__str__.<locals>.<genexpr>   s     ?uu{{?s   )joinrY   r?   s    r   r@   zTokenList.__str__   s    ww????r   c                 ,    t        | j                        S r=   )iterr   r?   s    r   __iter__zTokenList.__iter__   s    DKK  r   c                      | j                   |   S r=   r   )r   items     r   __getitem__zTokenList.__getitem__   s    {{4  r   c                 ,    t        |       j                  S r=   )typer*   r?   s    r   rE   zTokenList._get_repr_name   s    Dz"""r   c           	         t        | j                        }t        | j                        D ]  \  }}|j                         }|j	                         }	||dz
  k(  }
|
rdnd}|	j                  d      r|	j                  d      rdnd}t         dj                  di t               |       |j                  s|||k  s|
rd	nd
}|j                  ||dz   |||z           y)zPretty-print the object tree.r   z`- z|- rB   rC   z${_pre}{pre}{idx} {cls} {q}{value}{q})fileNz   z|  r.   )r%   r   	enumeraterE   rF   rG   rH   printrI   rJ   r6   _pprint_tree)r   	max_depthdepthf_pretoken_countidxrz   rK   r1   lastprerL   
parent_pres                 r   r   zTokenList._pprint_tree   s    $++&#DKK0 	OJC&&(C))+E;?+D%UC'',1D#A 86&"H&,-/ ~~9#4	8I&*U
""9eaiD:<MN	Or   c                     d}| j                         D ],  }|t        |j                        z   }||cxk  r|k  r|c S  |}. y)z-Returns the token that is on position offset.r   N)rY   r%   r1   )r   offsetr   rz   ends        r   get_token_at_offsetzTokenList.get_token_at_offset   sK    \\^ 	EEKK((Cf"s" #C		r   c              #      K   | j                   D ]+  }|j                  r|j                         E d{    (| - y7 w)znGenerator yielding ungrouped tokens.

        This method is recursively called for all child tokens.
        N)r   r6   rY   r   rz   s     r   rY   zTokenList.flatten   s9     
 [[ 	E~~ ==?**		*s   /A >A c              #   P   K   | j                   D ]  }|j                  s|  y wr=   )r   r6   r   s     r   get_sublistszTokenList.get_sublists   s%     [[ 	E~~	s   &&c                     | j                   S r=   r   r?   s    r   _groupable_tokenszTokenList._groupable_tokens   s    {{r   c                 
   |yt        |t        t        f      s|f}|r|J t        |dz
  dd      }n#|t	        | j
                        }t        ||      }|D ](  }| j
                  |   }|D ]  } ||      s||fc c S  * y)znext token that match functionsNr    rO   NN)rb   listtupleranger%   r   )	r   funcsstartr   r"   indexesr   rz   funcs	            r   _token_matchingzTokenList._token_matching   s    =%$/HE;;EAIr2.G{$++&E3'G 	&CKK$E &;:%&	&
 r   c                 :    fd}| j                  |      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                 r    xr | j                   xs$ xr  t        | t        j                  t               S N)r   ir7   r   r   Commenttkskip_cmskip_wss    r   matcherz&TokenList.token_first.<locals>.matcher  8     5R%5%5 I#GB!))w(GJ Jr   r   r   )r   r   r   r   s    `` r   token_firstzTokenList.token_first   s!    	J ##G,Q//r   c                 B    |dz  }| j                  fd||      S )Nr   c                      t        |       S r=   )r   )r   r   r   r   s    r   <lambda>z)TokenList.token_next_by.<locals>.<lambda>
  s    s2q!Q/? r   r   )r   r   r   r   r   r   s    ```  r   r   zTokenList.token_next_by  s"    q##$?cJJr   c                     t        |t        t        f      s|fn|}|D cg c]  fd	 }}| j                  ||      S c c}w )Nc                      |        S r=   r.   )r   r   s    r   r   z.TokenList.token_not_matching.<locals>.<lambda>  s    RL r   )rb   r   r   r   )r   r   r   r   s      `r   token_not_matchingzTokenList.token_not_matching  sE     *54- @e5:;T(;;##E3// <s   Ac                 ,    | j                  ||      d   S )Nr   r   )r   r   r   s      r   token_matchingzTokenList.token_matching  s    ##E3/22r   c                 ,    | j                  |||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   r   r   r   s       r   
token_prevzTokenList.token_prev  s     sGWtDDr   c                 J    |y|dz  }fd}| j                  |||      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.
        r   r   c                 r    xr | j                   xs$ xr  t        | t        j                  t               S r   r   r   s    r   r   z%TokenList.token_next.<locals>.matcher)  r   r   r!   r   )r   r   r   r   r   r   s     ``  r   r   zTokenList.token_next  s6     ;q	J ##GS(#CCr   c                     t        |t              r|n| j                  |      }|| j                  |d j	                  |      z   S )zReturn list index of token.N)rb   inttoken_indexr   index)r   rz   r   s      r   r   zTokenList.token_index.  s>    #E3/T5E5Ee5Lt{{56*00777r   c                 j   |}| j                   |   }||z   }|r^t        ||      rR| j                   |dz   | }|}	|	j                   j                  |       | j                   |dz   |= t        |      |	_        n.| j                   || } ||      }	|	g| j                   || | |	_        |D ]	  }
|	|
_         |	S )z+Replace tokens by an instance of *grp_cls*.r   )r   rb   extendr9   r1   r3   )r   grp_clsr   r   include_endr   	start_idxend_idx	subtokensgrprz   s              r   group_tokenszTokenList.group_tokens3  s     	I&# j0IM':ICJJi(IM'12E
CIIg6I)$C.1UDKK	'*CJ 	EEL	 
r   c                     t        |t              s| j                  |      }| |_        | j                  j                  ||       y)zInserts *token* before *where*.N)rb   r   r   r3   r   insert)r   whererz   s      r   insert_beforezTokenList.insert_beforeQ  s7    %%$$U+E5%(r   c                     t        |t              s| j                  |      }| j                  ||      \  }}| |_        || j
                  j                  |       y| j
                  j                  ||       y)zInserts *token* after *where*.r   N)rb   r   r   r   r3   r   appendr   )r   r   rz   r   nidxnext_s         r   insert_afterzTokenList.insert_afterX  sd    %%$$U+EooeWo=e=KKu%KKtU+r   c                 &    | j                         duS )z(Returns ``True`` if an alias is present.N)r)   r?   s    r   	has_aliaszTokenList.has_aliasc  s    ~~t++r   c                      y)r   Nr.   r?   s    r   r)   zTokenList.get_aliasg      r   c                 F    | j                         xs | j                         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   r?   s    r   get_namezTokenList.get_namek  s     ~~74#5#5#77r   c                      y)r
   Nr.   r?   s    r   r   zTokenList.get_real_namet  r   r   c                     | j                  t        j                  df      \  }}| j                  |      \  }}|t	        |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   r1   )r   r   r   prev_s       r   get_parent_namezTokenList.get_parent_namex  sO    
 ''1==#*>'?
??7+5-2->}U[[)HDHr   c                    |r| j                   |d n| j                   }|rt        |      n|}t        j                  t        j                  t        j
                  j                  g}|r|j                  t        j                         |D ]c  }|j                  |v rt        |j                        c S t        |t        t        f      s?|r|j                         c S |j!                         c S  y)z/Returns the name of the first token with a nameN)r   reversedr   NameWildcardStringSymbolr   r#   r2   r   r1   rb   
IdentifierFunctionr   r   )r   r   r"   r   r   r   typesrz   s           r   r   zTokenList._get_first_name  s     '*ST"t{{%,&!&QXX__5LL# 	PE{{e#$U[[11EJ#9:09u**,Ou~~?OO		Pr   r=   )Nr   Nr}   )r   NF)TF)NNNrO   N)TFF)r   )T)NFFF)"r*   r+   r,   r-   rt   r;   r@   r   r   rE   r   r   rY   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r)   r   r   r   r   __classcell__)r{   s   @r   rv   rv      s     I@!!#O$	
  ,0K0
3ED 8
 =A!<)	,,8I AF"'Pr   rv   c                       e Zd ZdZd Zy)	StatementzRepresents a SQL statement.c                 .   | j                  d      }|y|j                  t        j                  j                  t        j                  j
                  fv r|j                  S |j                  t        j                  j                  k(  r| j                  |      }|z| j                  |d      \  }}t        |t        t        f      rK| j                  |d      \  }}|3|j                  t        j                  j                  k(  r|j                  S |zy)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   UNKNOWNr   )r   r2   r   r#   DMLDDLr4   CTEr   r   rb   r   IdentifierList)r   rz   tidxs      r   get_typezStatement.get_type  s        .= [[QYY]]AIIMM::###[[AIIMM) ##E*D""oodDoAeej.%AB"&//$/"EKD%( %qyy}} <$/// " r   N)r*   r+   r,   r-   r   r.   r   r   r   r     s
    %"r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   zNRepresents an identifier.

    Identifiers may have aliases or typecasts.
    c                 P    | j                  t        j                        \  }}|duS )z7Return ``True`` if this identifier contains a wildcard.r   N)r   r   r   )r   r   rz   s      r   is_wildcardzIdentifier.is_wildcard  s(    %%

%35D  r   c                     | j                  t        j                  df      \  }}| j                  |d      \  }}|r|j                  S dS )z<Returns the typecast or ``None`` of this object as a string.z::r   Fr   N)r   r   r   r   r1   )r   midxmarkerr   r   s        r   get_typecastzIdentifier.get_typecast  sI    ))Q]]D,A)BfoodEo:e#u{{--r   c                 |    | j                  t        j                  j                        \  }}|r|j                  S dS )z5Returns the ordering or ``None`` as uppercase string.r   N)r   r   r#   Orderr4   )r   r   orderings      r   get_orderingzIdentifier.get_ordering  s4    ((199??(;8&.x""8D8r   c              #   r   K   | j                   D ]$  }t        |t              s|j                   dd  & yw)z(Returns an iterator of index token listsr   rO   N)r   rb   SquareBracketsr   s     r   get_array_indiceszIdentifier.get_array_indices  s6      [[ 	)E%0ll1R((	)s    77N)r*   r+   r,   r-   r   r  r  r	  r.   r   r   r   r     s    
!
.9
)r   r   c                       e Zd ZdZd Zy)r   z.A list of :class:`~sqlparse.sql.Identifier`'s.c              #      K   | j                   D ]4  }|j                  r|j                  t        j                  d      r1| 6 yw)zkReturns the identifiers.

        Whitespaces and punctuations are not included in this generator.
        ,N)r   r7   ri   r   r   r   s     r   get_identifierszIdentifierList.get_identifiers  s:     
 [[ 	E''5;;q}}c+J	s   A A AN)r*   r+   r,   r-   r  r.   r   r   r   r     s
    9r   r   c                       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y)TypedLiteralzEA typed literal, such as "date '2001-09-28'" or "interval '2 hours'".N	TIMESTAMP)DAYHOURMINUTEMONTHSECONDYEAR)r*   r+   r,   r-   r   r   Builtinr#   M_OPENr   SingleM_CLOSEM_EXTENDr.   r   r   r  r    sF    Ovv~~t$qyy+&>?Fhhoot#GyyNNHr   r  c                   X    e Zd ZdZej
                  dfZej
                  dfZed        Z	y)ParenthesiszTokens between parenthesis.()c                      | j                   dd S Nr   rO   r   r?   s    r   r   zParenthesis._groupable_tokens      {{1R  r   N
r*   r+   r,   r-   r   r   r  r  r   r   r.   r   r   r  r    s3    %]]CFmmS G! !r   r  c                   X    e Zd ZdZej
                  dfZej
                  dfZed        Z	y)r  zTokens between square brackets[]c                      | j                   dd S r!  r   r?   s    r   r   z SquareBrackets._groupable_tokens  r"  r   Nr#  r.   r   r   r  r    s3    (]]CFmmS G! !r   r  c                       e Zd ZdZy)
Assignmentz An assignment like 'var := val;'Nr*   r+   r,   r-   r.   r   r   r)  r)    s    *r   r)  c                   H    e Zd ZdZej
                  dfZej
                  dfZy)Ifz7An 'if' clause with possible 'else if' or 'else' parts.IFzEND IFNr*   r+   r,   r-   r   r#   r  r  r.   r   r   r,  r,    s     AYY_Fii!Gr   r,  c                   H    e Zd ZdZej
                  dfZej
                  dfZy)ForzA 'FOR' loop.)FORFOREACHzEND LOOPNr.  r.   r   r   r0  r0    s"    YY**Fii#Gr   r0  c                   0    e Zd ZdZed        Zed        Zy)
Comparisonz/A comparison used for example in WHERE clauses.c                      | j                   d   S Nr   r   r?   s    r   leftzComparison.left  s    {{1~r   c                      | j                   d   S )NrO   r   r?   s    r   rightzComparison.right  s    {{2r   N)r*   r+   r,   r-   r   r7  r9  r.   r   r   r4  r4    s+    9   r   r4  c                       e Zd ZdZd Zy)r   z
A comment.c                     | j                   xr4 | j                   d   j                  t        j                  j                  k(  S r6  )r   r2   r   r   	Multiliner?   s    r   is_multilinezComment.is_multiline!  s.    {{Jt{{1~33qyy7J7JJJr   N)r*   r+   r,   r-   r=  r.   r   r   r   r     s    Kr   r   c                   H    e Zd ZdZej
                  dfZej
                  dfZy)WherezA WHERE clause.WHERE)	ORDER BYzGROUP BYLIMITUNIONz	UNION ALLEXCEPTHAVING	RETURNINGINTONr.  r.   r   r   r?  r?  %  s&    YYFii ' 'Gr   r?  c                   H    e Zd ZdZej
                  dfZej
                  dfZy)HavingzA HAVING clause.rE  )rA  rB  Nr.  r.   r   r   rI  rI  -  s"    YY Fii..Gr   rI  c                   P    e Zd ZdZej
                  dfZej
                  dfZddZy)CasezAA CASE statement with one or more WHEN and possibly an ELSE part.CASEENDc                    d}d}g }|}| j                   D ]G  }|j                  t        j                  d      r%|r|j                  t        j
                  v rD|j                  t        j                  d      r|j                  g g f       |}n{|j                  t        j                  d      r|}nX|j                  t        j                  d      r|j                  dg f       |}n"|j                  t        j                  d      rd}|r|s|j                  g g f       ||k(  r|d	   d
   j                  |       *||k(  s1|d	   d   j                  |       J |S )zeReturns a list of 2-tuples (condition, value).

        If an ELSE exists condition is None.
        r   r    rL  WHENTHENELSENrM  rO   r   )r   ri   r   r#   r2   r$   r   )r   r   	CONDITIONVALUEretmoderz   s          r   	get_caseszCase.get_cases8  s-   
 	[[ 	)E{{199f-U[[ALL8QYY/

B8$ QYY/QYY/

D":&QYY. C

B8$ y B
!!%(B
!!%(?	)D 
r   Nrs   )	r*   r+   r,   r-   r   r#   r  r  rV  r.   r   r   rK  rK  3  s&    KYYFiiG-r   rK  c                       e Zd ZdZd Zy)r   zA function or procedure call.c                     | j                   d   }|j                   D ]O  }t        |t              r|j                         c S t	        |t
        t        ft        j                        sL|gc S  g S )zReturn a list of parameters.rO   )r   r   )	r   rb   r   r  r   r   r   r   Literal)r   parenthesisrz   s      r   get_parameterszFunction.get_parametersk  sa    kk"o '' 	!E%0,,..Ux4		By 		!
 	r   N)r*   r+   r,   r-   r[  r.   r   r   r   r   h  s
    'r   r   c                   H    e Zd ZdZej
                  dfZej
                  dfZy)BeginzA BEGIN/END block.BEGINrM  Nr.  r.   r   r   r]  r]  v  s!    YYFiiGr   r]  c                       e Zd ZdZy)	OperationzGrouping of operationsNr*  r.   r   r   r`  r`  |  s     r   r`  c                       e Zd ZdZy)ValueszGrouping of valuesNr*  r.   r   r   rb  rb    s    r   rb  c                       e Zd ZdZy)CommandzGrouping of CLI commands.Nr*  r.   r   r   rd  rd    s    #r   rd  )r-   rU   sqlparser   r   sqlparse.utilsr   r   r   r0   rv   r   r   r   r  r  r  r)  r,  r0  r4  r   r?  rI  rK  r   r]  r`  rb  rd  r.   r   r   <module>rg     s*   M 	   -6 6.k k\{P {P|%	 %P) )>
Y 
O9 O!) !!Y !+ +" "$) $	 	Ki K'I '/Y /29 2j~y I !	 !Y $i $r   