
    h$L                        d Z ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
 ddlmZmZ e
 G d de             Ze
 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      Z G d# d$e      Z G d% d&e      Z G d' d(e      Zy))zFThis module contains classes representing syntactical elements of SQL.    )print_functionNtokens)string_types	text_typeunicode_compatible)imtremove_quotesc                   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 unchange 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)r   r   r   r   r   TKeywordr   
Whitespacer   upperr   )selfr   r   s      O/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/sqlparse/sql.py__init__zToken.__init__   sf    % 

199,!ZZ1<<7+/??%++-    c                     | j                   S Nr   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}>id )_get_repr_name_get_repr_value
startswithendswithformatr%   locals)r   clsr   qs       r   __repr__zToken.__repr__/   si    !!#$$&$$S)ennS.ADt;4;; %$x%!8% 	%r   c                 P    t        | j                        j                  d      d   S )N.)strr   splitr    s    r   r'   zToken._get_repr_name7   s     4::$$S)"--r   c                 r    t        |       }t        |      dkD  r|d d dz   }t        j                  dd|      S )N      z...z\s+ )r   lenresub)r   raws     r   r(   zToken._get_repr_value:   s8    os8a<bq'E/Cvvfc3''r   c              #      K   |  yw)zResolve subgroups.Nr&   r    s    r   flattenzToken.flatten@   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   )r:   compile).0vflags     r   	<genexpr>zToken.match.<locals>.<genexpr>Z   s     :abjjD):s    #TFc              3   <   K   | ]  }|j                           y wr   )r   )rB   rC   s     r   rE   zToken.match.<locals>.<genexpr>b   s     0Aaggi0s   )r   
isinstancer   r   r:   
IGNORECASEsearchr   )r   r   valuesregextype_matchedpatternrD   s         @r   matchzToken.matchD   s     zzU*v~fl+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)r   rG   )r   	group_clsr   s      r   withinzToken.withinf   s0     &),]]F  r   c                      | j                   |k(  S )z<Returns ``True`` if this token is a direct child of *other*.r   )r   others     r   is_child_ofzToken.is_child_ofs   s    {{e##r   c                 J    | j                   }|r||k(  ry|j                   }|ry)z7Returns ``True`` if *other* is in this tokens ancestry.TFrS   )r   rT   r   s      r   has_ancestorzToken.has_ancestorw   s+    ]]F  r   NF)__name__
__module____qualname____doc__	__slots__r   r!   r/   r'   r(   r>   rN   rQ   rU   rW   r&   r   r   r   r      sB    .IF%.( )D$r   r   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 | _         |D cg c]  }t        |d|        c} t        t        |   d t        |              d| _        y c c}w )Nr   T)r   setattrsuperr_   r   r   r   )r   r   token	__class__s      r   r   zTokenList.__init__   sF    l5;<E$	'<i'io> 	=s   Ac                 N    dj                  d | j                         D              S )N c              3   4   K   | ]  }|j                     y wr   r   )rB   rc   s     r   rE   z$TokenList.__str__.<locals>.<genexpr>   s     @@s   )joinr>   r    s    r   r!   zTokenList.__str__   s    xx@@@@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   )typerY   r    s    r   r'   zTokenList._get_repr_name   s    Dz"""r   c           	      t   d|z  }t        | j                        D ]  \  }}|j                         }|j                         }|j	                  d      r|j                  d      rdnd}	t         dj                  di t               |       |j                  s~|||k  s|j                  ||dz   |        y)	zPretty-print the object tree.z | r#   r$   z${indent}{idx:2d} {cls} {q}{value}{q})fileN   r&   )	enumerater   r'   r(   r)   r*   printr+   r,   r   _pprint_tree)
r   	max_depthdepthfindentidxrc   r-   r   r.   s
             r   rv   zTokenList._pprint_tree   s    %#DKK0 		<JC&&(C))+E((-%..2E4A 96&"H&,-/ ~~9#4	8I""9eai;		<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)r>   r9   r   )r   offsetr{   rc   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                         D ]  }|  )| . yw)znGenerator yielding ungrouped tokens.

        This method is recursively called for all child tokens.
        N)r   r   r>   )r   rc   rm   s      r   r>   zTokenList.flatten   sC     
 [[ 	E~~!MMO DJ 	s   =?c              #   P   K   | j                   D ]  }|j                  s|  y wr   )r   r   r   rc   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                 2   |yt        |t        t        f      s|f}|r@|J t        |dz
  dd      D ](  }| j                  |   }|D ]  } ||      s||fc c S  * yt        | j                  || |      D ]  \  }}|D ]  } ||      s||fc c S   y)znext token that match functionsN   r2   )startNN)rG   listtupleranger   rt   )r   funcsr   r~   reverser{   rc   funcs           r   _token_matchingzTokenList._token_matching   s    =%$/HE;;UQYB/ *C(! *DE{"Ez)** 	 (E#(>eL *
U! *DE{"Ez)** 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)tir   r	   r   Commenttkskip_cmskip_wss    r   <lambda>z'TokenList.token_first.<locals>.<lambda>   3    !=R-=-= !N!(!LSqyyG-LO r   rs   r   )r   r   r   r   s    `` r   token_firstzTokenList.token_first   s!    O##E*1--r   c                 F    fd}|dz  }| j                  |||      S )Nc                      t        |       S r   )r	   )r   r   mr   s    r   r   z)TokenList.token_next_by.<locals>.<lambda>   s    3r1a+ r   rs   r   )r   r   r   r   r{   r~   r   s    ```   r   token_next_byzTokenList.token_next_by   s&    +q##E344r   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   )rG   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 )Nrs   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   rs   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>.<lambda>  r   r   r   r   )r   r{   r   r   r   r   s     ``  r   r   zTokenList.token_next  s6     ;qO##E3#AAr   c                     t        |t              r|n| j                  |      }|| j                  |d j	                  |      z   S )zReturn list index of token.N)rG   inttoken_indexr   index)r   rc   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*.rs   )r   rG   extendr   r   r   )r   grp_clsr   r~   include_endr   	start_idxend_idx	subtokensgrprc   s              r   group_tokenszTokenList.group_tokens  s     	I&# j0IM':ICJJi(IM'12!%(CIIg6I)$C.1UDKK	'*CJ 	EEL	 
r   c                     t        |t              s| j                  |      }| |_        | j                  j                  ||       y)zInserts *token* before *where*.N)rG   r   r   r   r   insert)r   whererc   s      r   insert_beforezTokenList.insert_before9  s7    %%$$U+E5%(r   c                     t        |t              s| j                  |      }| j                  ||      \  }}| |_        || j
                  j                  |       y| j
                  j                  ||       y)zInserts *token* after *where*.r   N)rG   r   r   r   r   r   appendr   )r   r   rc   r   nidxnext_s         r   insert_afterzTokenList.insert_after@  sd    %%$$U+EooeWo=e=KKu%KKtU+r   c                 &    | j                         duS )z(Returns ``True`` if an alias is present.N)	get_aliasr    s    r   	has_aliaszTokenList.has_aliasK  s    ~~t++r   c                    | j                  t        j                  df      \  }}|| j                  |dz   d      S | j                  t        j                        \  }}t        | j                        dkD  r|| j                  d	      S yy)
z2Returns the alias for this identifier or ``None``.ASr   Nrs   T)keywordsr   r   r   )r   r   r   _get_first_namer   r9   r   )r   kw_idxkw_wss        r   r   zTokenList.get_aliasO  s     ''199d*;'<
>''
T'BB ""Q\\"22t{{aBN'''55 %3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   get_real_namer    s    r   get_namezTokenList.get_name\  s     ~~74#5#5#77r   c                 n    | j                  t        j                  df      \  }}| j                  |      S )z7Returns the real name (object name) of this identifier.r1   r   )r   r   Punctuationr   )r   dot_idxr   s      r   r   zTokenList.get_real_namee  s5     ''1==#*>'?
##G,,r   c                     | j                  t        j                  df      \  }}| j                  |      \  }}|t	        |j
                        S dS )zsReturn name of the parent object if any.

        A parent object is identified by the first occuring dot.
        r1   r   N)r   r   r   r   r
   r   )r   r   r   prev_s       r   get_parent_namezTokenList.get_parent_namek  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 ]O  }|j                  |v rt        |j                        c S t        |t        t        f      s?|j                         c S  y)z/Returns the name of the first token with a nameN)r   reversedr   NameWildcardStringSymbolr   r   r   r
   r   rG   
IdentifierFunctionr   )r   r{   r   r   r   typesrc   s          r   r   zTokenList._get_first_namet  s     '*ST"t{{%,&!&QXX__5LL# 	(E{{e#$U[[11EJ#9:~~''		(r   r   )Nr   N)r   NF)TF)NNNr2   N)TFF)r   )T)NFF)"rY   rZ   r[   r\   r]   r   r!   rk   rn   r'   rv   r   r>   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)rd   s   @r   r_   r_      s     IA!!#<

  ,.5
0
3EB8
 =A!<)	,,68-I(r   r_   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                  |      }| j                  |d      \  }}t        |t        t        f      rI| j                  |d      \  }}|j                  t        j                  j                  k(  r|j                  S y)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   r   r   r   DMLDDLr   CTEr   r   rG   r   IdentifierList)r   first_tokenfidxtidxrc   r   dml_keywords          r   get_typezStatement.get_type  s     &&t&4 199==!))--"@@)))!))--/ ##K0D//$/=KD%%*n!=>!%t!D;$$		5&111 r   N)rY   rZ   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   rc   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   r   )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   Orderr   )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 listsrs   r2   N)r   rG   SquareBracketsr   s     r   get_array_indiceszIdentifier.get_array_indices  s6      [[ 	)E%0ll1R((	)s    77N)rY   rZ   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   r   rN   r   r   r   s     r   get_identifierszIdentifierList.get_identifiers  s:     
 [[ 	E''5;;q}}c+J	s   A A AN)rY   rZ   r[   r\   r  r&   r   r   r   r     s
    9r   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 Nrs   r2   r   r    s    r   r   zParenthesis._groupable_tokens      {{1R  r   N
rY   rZ   r[   r\   r   r   M_OPENM_CLOSEr   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rY   rZ   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rY   rZ   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   r  r    s"    YY**Fii#Gr   r  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 )Nr2   r   r    s    r   rightzComparison.right  s    {{2r   N)rY   rZ   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 comment.c                     | j                   xr4 | j                   d   j                  t        j                  j                  k(  S r#  )r   r   r   r   	Multiliner    s    r   is_multilinezComment.is_multiline
  s.    {{Jt{{1~33qyy7J7JJJr   N)rY   rZ   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GROUPLIMITUNIONz	UNION ALLEXCEPTHAVING	RETURNINGINTONr  r&   r   r   r,  r,    s&    YYFii ' 'Gr   r,  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.
        rs   r   r8  WHENTHENELSENr9  r2   r   )r   rN   r   r   r   r   r   )r   r   	CONDITIONVALUEretmoderc   s          r   	get_caseszCase.get_cases  s-   
 	[[ 	)E{{199f-U[[ALL8QYY/

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

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

B8$ y B
!!%(B
!!%(?	)D 
r   NrX   )	rY   rZ   r[   r\   r   r   r  r  rB  r&   r   r   r7  r7    s&    KYYFiiG-r   r7  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.r2   )r   r   )	r   rG   r   r  r	   r   r   r   Literal)r   parenthesisrc   s      r   get_parameterszFunction.get_parametersN  sa    kk"o '' 	!E%0,,..Ux4		By 		!
 	r   N)rY   rZ   r[   r\   rG  r&   r   r   r   r   K  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.BEGINr9  Nr  r&   r   r   rI  rI  Y  s!    YYFiiGr   rI  c                       e Zd ZdZy)	OperationzGrouping of operationsNr  r&   r   r   rL  rL  _  s     r   rL  ) r\   
__future__r   r:   sqlparser   r   sqlparse.compatr   r   r   sqlparse.utilsr	   r
   objectr   r_   r   r   r   r  r  r  r  r  r!  r   r,  r7  r   rI  rL  r&   r   r   <module>rR     s   M % 	   G G - kF k k\ @( @( @(F#	 #L) )>
Y 
!) !!Y !+ +" "$) $	 	Ki K'I '29 2jy I !	 !r   