
    ,he=                     @   U d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ dZ	 d	d
dddddddddddddZ	  ej                  dej                   ej"                  z        Zej&                  ed<   	  G d d      Z	 d#dededeeddf   fd Zd!edee   fd"Zy)$z8Classes and methods utilized to work with MySQL Scripts.    N)deque)Deque	GeneratorOptional   )InterfaceError)MySQLScriptPartition   ;s   \$s   \^s   \?s   \(s   \)s   \[s   \]s   \{s   \}s   \.s   \|s   \+s   \-s   \*)$^?()[]{}.|+-*sA   (delimiter\s+)(?=(?:[^"'`]*(?:"[^"]*"|'[^']*'|`[^`]*`))*[^"'`]*$))flagsDELIMITER_PATTERNc                       e Zd ZdZdZdeddfdZdededee   fd	Ze	d
e
defd       Ze	d
e
defd       Ze	dedee   fd       Ze	dedefd       Ze	dedefd       Zddedee   fdZdefdZy)MySQLScriptSplitterzBreaks a MySQL script into single statements.

    It strips custom delimiters and comments along the way, except for comments
    representing a MySQL extension or optimizer hint.
    s3   (?=(?:[^"'`]*(?:"[^"]*"|'[^']*'|`[^`]*`))*[^"'`]*$)
sql_scriptreturnNc                 <    || _         d| _        d| _        i | _        y)zConstructor.N)_code_single_stmts_mappable_stmts_re_sql_split_stmts)selfr   s     \/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/mysql/connector/_scripting.py__init__zMySQLScriptSplitter.__init__N   s!    
486:<>     code	delimiterc                    g }|| j                   vrd|v rt        d      |j                         D cg c]&  }t        j	                  ||j                               ( }}t        j                  dj                  |      | j                  z         | j                   |<   | j                   |   j                  |      D ]&  }|j                         }|s|j                  |       ( |S c c}w )z Split code context by delimiter.   \z5The backslash (\) character is not a valid delimiter.r'   )r#   r   decodeDELIMITER_RESERVED_SYMBOLSgetencoderecompilejoin_regex_sql_split_stmtssplitstripappend)r$   r(   r)   snippetschardelimiter_patternsnippetsnippet_strips           r%   _split_statementz$MySQLScriptSplitter._split_statementU   s    D444	!$L 
 &,,.! +..tT[[]C! ! 35***+d.I.II3D$$Y/ //	:@@F 	/G#MMOM.	/
 !s   +C&r8   c                 H    t        j                  t        |             d   dv S )z;Validates whether `char` is a white-space character or not.r   >   Zunicodedatacategorychrr8   s    r%   is_white_space_charz'MySQLScriptSplitter.is_white_space_charm   #     ##CI.q1U::r'   c                 H    t        j                  t        |             d   dv S )z7Validates whether `char` is a control character or not.r   >   Cr?   rC   s    r%   is_control_charz#MySQLScriptSplitter.is_control_charr   rE   r'   stringc                 .    t        j                  d|       S )z6Split `string` by any control character or whitespace.s   [\s\x00-\x1f\x7f-\x9f])r0   r4   )rI   s    r%   $split_by_control_char_or_white_spacez8MySQLScriptSplitter.split_by_control_char_or_white_spacew   s     xx2F;;r'   c                 :    t        j                  t        |       duS )zBValidates whether `code` has the delimiter command pattern or not.N)r0   searchr   r(   s    r%   has_delimiterz!MySQLScriptSplitter.has_delimiter|   s     yy*D1==r'   c                 F   dt         dt        dt        fd}dt         dt        fd}dt         dt        dt        fd}t        d      }d\  }}t	        d	      t	        d
      t	        d      }	}}|t        |       k  r|Qd}
 ||dd | |         rd}
n ||dd       rd}
n ||dd | |         rd}
|
|
dv r3|t        |       k  rh| |   |k7  r`|dz  }|t        |       k  rM| |   |k7  rnC|dz   t        |       k  r-| ||dz    dk7  r"|dz  }|dz   t        |       k  r| ||dz    dk7  r"|dz  }t        t        |
            D ]  }|j                           |rtt        j                  |d         st        j                  |d         rD|j                          |r2t        j                  |d         r+t        j                  |d         rDc|| |   ||	fv r| |   }n|| |   ||hv rd}|j                  | |          |dz  }|t        |       k  rt        |      S )a  Remove MySQL comments which include `--`-style, `#`-style
        and `C`-style comments.

        A `--`-style comment spans from `--` to the end of the line.
        It requires the second dash to be
        followed by at least one whitespace or control character
        (such as a space, tab, newline, and so on).

        A `#`-style comment spans from `#` to the end of the line.

        A C-style comment spans from a `/*` sequence to the following `*/`
        sequence, as in the C programming language. This syntax enables a
        comment to extend over multiple lines because the beginning and
        closing sequences need not be on the same line.

        **NOTE: Only C-style comments representing MySQL extensions or
        optimizer hints are preserved**. E.g.,

        ```
        /*! MySQL-specific code */

        /*+ MySQL-specific code */
        ```

        *For Reference Manual- MySQL Comments*, see
        https://dev.mysql.com/doc/refman/en/comments.html.
        b_strb_charr   c                 h    | dk(  xr, t         j                  |      xs t         j                  |      S )Ns   --)r   rH   rD   rQ   rR   s     r%   is_dash_stylez:MySQLScriptSplitter.remove_comments.<locals>.is_dash_style   s5    E> #33F; C&::6Br'   c                     | dk(  S )N   # )rQ   s    r%   is_hash_stylez:MySQLScriptSplitter.remove_comments.<locals>.is_hash_style   s    D= r'   c                 @    | dk(  xr |t        d      t        d      hvS )Ns   /*!r   )ordrT   s     r%   
is_c_stylez7MySQLScriptSplitter.remove_comments.<locals>.is_c_style   s#    E>HfSXs3x4H&HHr'   r'   )r   N
'"N--#z/*)rb   rd   r      s   */)bytesintbool	bytearrayr\   lenrangepopr   rH   rD   r6   )r(   rU   rY   r]   bufiliteral_ctx
line_breaksingle_quotedouble_quotestyle_s               r%   remove_commentsz#MySQLScriptSplitter.remove_comments   sa   <	 	 	 		! 	!4 	!	Ie 	IS 	IT 	I n ;14TCHc#h,L
#d)m" RS473 E"3rs8,EBC$q'2 E$+#d)mQ:0EFA  #d)mQ:0E  !ec$i/DQUOu4LFA  !ec$i/DQUOu4LQ"3u:. "	" +;;CGD.BB3r7K		 +;;CGD.BB3r7K "tAw<2N'N"1g(T!Wj8Q-Q"JJtAwFAG #d)mJ Szr'   ru   c                    | j                   | j                   S g | _         t        }g }d}|r!t        j                  | j                        }n| j                  }t        j                  t        |      D ]  }t        j                  t        |      rq| j                  |      d   }|j                          | j                   j                  | j                  dj                  |      |             ||t        |      d g}}n|j                  |       |} |r;| j                   j                  | j                  dj                  |      |             | j                   S )a  Splits the given script text into a sequence of individual statements.

        The word DELIMITER and any of its lower and upper case combinations
        such as delimiter, DeLiMiter, etc., are considered reserved words by
        the connector. Users must quote these when included in multi statements
        for other purposes different from declaring an actual statement delimiter;
        e.g., as names for tables, columns, variables, in comments, etc.

        ```
        CREATE TABLE `delimiter` (begin INT, end INT); -- I am a `DELimiTer` comment
        ```

        If they are not quoted, the statement-mapping will not produce the expected
        experience.

        See https://dev.mysql.com/doc/refman/8.0/en/keywords.html to know more
        about quoting a reserved word.

        *Note that comments are always ignored as they are not considered to be
        part of statements, with one exeception; **C-style comments representing
        MySQL extensions or optimizer hints are preserved***.
        Nr'   rN   )patternrI   r       )r(   r)   )r!   DEFAULT_DELIMITERr   ru   r    r0   r4   r   rM   rK   rl   extendr<   r2   rj   r6   )r$   ru   r)   rm   prevr(   currnext_delimiters           r%   split_scriptz MySQLScriptSplitter.split_script   sN   0 )%%%  %	 &66DJJ6GD::D HH%6tD 	D yy*D1 "&!J!J4!PQR!S 	 ""))))tyy~)S "0$s>7J7L2M1N3	 

4  D1	6 %%%%499S>Y%O !!!r'   c                 8    | j                   j                  d      S )Nutf-8)r    r,   )r$   s    r%   __repr__zMySQLScriptSplitter.__repr__   s    zz  ))r'   )T)__name__
__module____qualname____doc__r3   rf   r&   listr<   staticmethodrg   rh   rD   rH   rK   rO   ru   r~   strr   rX   r'   r%   r   r   E   s    X?5 ?T ?U u e 0 ;# ;$ ; ; ;c ;d ; ; <U <tE{ < < >E >d > > Qe Q Q QfI"D I"DK I"V*# *r'   r   sql_codemap_resultsr   c           
   #     K   t         j                  |       s|st        t        g       |        yt        |       }|j	                         }|st        t        dg      d       y|s(t        t        |      dj                  |             yd}d t        |      D        }|D ]X  }||kD  r-t        dj                  |||       t        |||              t        ||   t        ||   g             |d	z   }Z |t        |      k  r@t        dj                  ||t        |             t        ||t        |                    yyw)
a  Breaks a MySQL script into sub-scripts.

    If the given script uses `DELIMITER` statements (which are not recognized
    by MySQL Server), the connector will parse such statements to remove them
    from the script and substitute delimiters as needed. This pre-processing
    may cause a performance hit when using long scripts. Note that when enabling
    `map_results`, the script is expected to use `DELIMITER` statements in order
    to split the script into multiple query strings.

    Args:
        sql_code: MySQL script.
        map_results: If True, each sub-script is `statement-result` mappable.

    Returns:
        A generator of typed dictionaries with keys `single_stmts` and `mappable_stmts`.

        If mapping disabled and no delimiters detected, it returns a 1-item generator,
        the field `single_stmts` is an empty list and the `mappable_stmt` field
        corresponds to the unmodified script, that may be mappable.

        If mapping disabled and delimiters detected, it returns a 1-item generator,
        the field `single_stmts` is a list including all the single statements
        found in the script and the `mappable_stmt` field corresponds to the processed
        script (delimiters are stripped) that may be mappable.

        If maping enabled, the script is broken into mappable partitions. It returns
        an N-item generator (as many items as computed partitions), the field
        `single_stmts` is a list including all the single statements of the partition
        and the `mappable_stmt` field corresponds to the sub-script (partition) that
        is guaranteed to be mappable.

    Raises:
        `InterfaceError` if an invalid delimiter string is found.
    )single_stmtsmappable_stmtN)r   r'   s   ;
r   c              3   T   K   | ]   \  }}|d d j                         dk(  s| " y w)N   s   CALL )upper).0jstmts      r%   	<genexpr>z(split_multi_statement.<locals>.<genexpr>j  s(     W71d$r(..:Jh:VQWs   (()r   r   r   )r   rO   r	   r   r~   r2   	enumeraterj   )r   r   tokstmtsrn   partition_idsr   s          r%   split_multi_statementr   $  s_    L ,,X6{ #b	RR

2C E  #seCPP #uV[[5G
 	
 	 	
AWi&6WM q5$"(++eAaj"9!&uQqz!2 #(az1B
 	
 E 	3u:~ $kk%CJ*?@"5SZ#89	
 s   EEscriptc                    t        j                  dt        j                  |       t         j                        }|rt        |d         dk7  rt        g       S t        |D cg c]  }|d   j                  d       c}      S c c}w )al  Scans the MySQL script looking for `filenames` (one for each
    `LOCAL INFILE` statement found).

    Arguments:
        script: a MySQL script that may include one or more `LOCAL INFILE` statements.

    Returns:
        filenames: a list of filenames (one for each `LOCAL INFILE` statement found).
        An empty list is returned if no matches are found.
    s2   LOCAL\s+INFILE\s+(["'])((?:\\\1|(?:(?!\1)).)*)(\1))rw   rI   r   r      r   r   )r0   findallr   ru   
IGNORECASErj   r   r,   )r   matchesmatchs      r%   get_local_infile_filenamesr     sp     jjJ"226:mmG
 c'!*o*Ry @u%(//'*@AA@s   !B)F)r   r0   r@   collectionsr   typingr   r   r   errorsr   typesr	   ry   r-   r1   r   	MULTILINEr   Pattern__annotations__r   rf   rh   r   r   r   rX   r'   r%   <module>r      s   : ? 	   - - " ' , 
													   E *

M
--",,
&! 2::  D\* \*B Z
Z
Z
 #T4/0Z
zBu Bs Br'   