
    ,hb                       U d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZmZmZmZmZmZmZ ddlmZ 	 ddlZddlZdZ	 d	d
lmZ d	dlmZ d	dlmZmZ d	dl m!Z!m"Z"m#Z#m$Z$m%Z% d	dl&m'Z' e
rd	dl(m)Z)  ejT                         Z+dZ,dZ- ej\                  d      Z/dZ0eeneefZ1de2d<   i Z3de2d<   d dZ4	 	 	 	 d!dZ5	 	 	 	 	 	 d"dZ6d#dZ7 G d d      Z8 G d d      Z9y# e$ r dZY w xY w# e$ r dZY w xY w)$z5Implementing pooling of connections to MySQL servers.    )annotationsN)TracebackType)TYPE_CHECKINGAnyDictNoReturnOptionalTupleTypeUnion)uuid4TF   )CMySQLConnection)MySQLConnection)CNX_POOL_ARGSDEFAULT_CONFIGURATION)ErrorInterfaceErrorNotSupportedError	PoolErrorProgrammingError)read_option_files)MySQLConnectionAbstract    @   z[^a-zA-Z0-9._:\-*$#]z0MySQL Connector/Python C Extension not availablezUnion[type, Tuple[type, ...]]MYSQL_CNX_CLASSzDict[str, MySQLConnectionPool]_CONNECTION_POOLSPooledMySQLConnectionc                    d| v r| d   n
t        d	i | }| j                  d      du rt        t        t              t
        5  |t        vrt        d	i | t        |<   nAt        t        |   t              r*t        |   j                  }d| v r| d   |k7  rt        d      ddd       	 t        |   j                         S # 1 sw Y   !xY w# t        $ r t        d| d      dw xY w)
z!Return a pooled MySQL connection.	pool_nameuse_pureFN	pool_sizez)Size can not be changed for active pools.z%Failed getting connection from pool '' )generate_pool_namegetr   ImportErrorERROR_NO_CEXTCONNECTION_POOL_LOCKr   MySQLConnectionPool
isinstancer"   r   get_connectionAttributeErrorr   )kwargsr    
check_sizes      Y/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/mysql/connector/pooling.py_get_pooled_connectionr1   P   s     +f4{:L:Vv:V  zz*&+;+C-(( 
 M--+>+H+Hi())46IJ*95??Jf$)<
)J KLLM +::<<M M  3I;a@
	s   AC )C  C	C&c                 `   | j                         }	 |d   }|d= t        g d      }d}|D ]  }t        |j	                               |z
  }|r4t        |      dkD  rdnd}d	j                  |      }t        d
| d|       t        |d      r|dz  }|j                  dd      |d<   |d   dk  s|d   dkD  rt        d|d          t        |d   t              rt        d|d           d|cxk  rt        |      k  rt        d       i }	g }
t        |d d      D ]>  }|d   |	vr|g|	|d   <   |
j                  |d          (|	|d      j                  |       @ |
D ]  }|	|   }t        t        |            D ]y  }t        |      dz
  }t!        j"                  d|      }|j%                  |      }|j                         }|j'                  |       |j%                  dd       	 t)        di |c c S   t        d      # t        $ r t        d      dw xY w# t*        $ r Y w xY w)a  Return a MySQL connection and try to failover if needed.

    An InterfaceError is raise when no MySQL is available. ValueError is
    raised when the failover server configuration contains an illegal
    connection argument. Supported arguments are user, password, host, port,
    unix_socket and database. ValueError is also raised when the failover
    argument was not provided.

    Returns MySQLConnection instance.
    failoverzfailover argument not providedN)	userpasswordhostportunix_socketdatabaser    r"   priorityr   r   s z, z Unsupported connection argument z in failover: r:   d   z9Priority value should be in the range of 0 to 100, got : zDPriority value should be an integer in the range of 0 to 100, got : z\You must either assign no priority to any of the routers or give a priority for every routerc                    | d   S )Nr:   r$   xs    r0   <lambda>z*_get_failover_connection.<locals>.<lambda>   s
    :     T)keyreversez,Unable to connect to any of the target hostsr$   )copyKeyError
ValueErrorsetkeyslenjoinhasattrr&   r   r+   intr   sortedappendrangerandomrandintpopupdateconnectr   )r.   configr3   support_cnx_argspriority_countserverdiffarglstserver_directoryserver_priority_listr:   failover_list_lastindex
new_configs                    r0   _get_failover_connectionrd   m   s    [[]FE*% 	z
	
 N 6;;=!$44TQ#BC))D/C23%~cUK  6:&aN#ZZ
C8z*!VJ%7#%= 
+,.  &,c2 $Z013 %. 	>)CM)
 	
 * '>M @*%554:8VJ/0 ''z(:;VJ/077?@ ) (2s=)* 	A}%)DNN1d+E"&&u-FJf%NN:t,,,,	 G
HHU  E9:DEL  s   H +
H!H!	H-,H-c                 ^   dv rj                  d      nd}t        |t              st        d      |rt        st        d      dv rt        d      dv rt        d      d	v rt        d
      dvrt
        d   d<   	 t        j                  j                  d   d      }g }|D ]P  }|j                  |j                  j                  d      |j                  |j                  |j                   d       R |j#                  d        |D cg c]  }|d   |d   d c}d	<   dv rd   d<   j                  d       dv rt%        di }t'        di |S d	v rt)        di S 	 t+        fdt,        D              rt/        di S 	 j3                  dd      }dv rd= |st4        t7        t8              t4        r|st5        | i S t;        | i S # t        j                  j                  $ r t        dd    d      dw xY wc c}w # t0        $ r Y w xY w)a  Creates or gets a MySQL connection object.

    In its simpliest form, `connect()` will open a connection to a
    MySQL server and return a `MySQLConnectionAbstract` subclass
    object such as `MySQLConnection` or `CMySQLConnection`.

    When any connection pooling arguments are given, for example `pool_name`
    or `pool_size`, a pool is created or a previously one is used to return
    a `PooledMySQLConnection`.

    Args:
        *args: N/A.
        **kwargs: For a complete list of possible arguments, see [1]. If no arguments
                  are given, it uses the already configured or default values.

    Returns:
        A `MySQLConnectionAbstract` subclass instance (such as `MySQLConnection` or
        `CMySQLConnection`) or a `PooledMySQLConnection` instance.

    Examples:
        A connection with the MySQL server can be established using either the
        `mysql.connector.connect()` method or a `MySQLConnectionAbstract` subclass:
        ```
        >>> from mysql.connector import MySQLConnection, HAVE_CEXT
        >>>
        >>> cnx1 = mysql.connector.connect(user='joe', database='test')
        >>> cnx2 = MySQLConnection(user='joe', database='test')
        >>>
        >>> cnx3 = None
        >>> if HAVE_CEXT:
        >>>     from mysql.connector import CMySQLConnection
        >>>     cnx3 = CMySQLConnection(user='joe', database='test')
        ```

    References:
        [1]: https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
    dns_srvFz(The value of 'dns-srv' must be a booleanztMySQL host configuration requested DNS SRV. This requires the Python dnspython module. Please refer to documentationr8   z<Using Unix domain sockets with DNS SRV lookup is not allowedr7   z;Specifying a port number with DNS SRV lookup is not allowedr3   zASpecifying multiple hostnames with DNS SRV look up is not allowedr6   SRVz Unable to locate any hosts for 'r#   NT)omit_final_dot)r6   r7   r:   weightc                    | d   | d    fS )Nr:   ri   r$   r?   s    r0   rA   zconnect.<locals>.<lambda>   s    Qz]Qx[L$A rB   )rC   )r6   r7   read_default_fileoption_filesc              3  &   K   | ]  }|v  
 y wNr$   ).0rC   r.   s     r0   	<genexpr>zconnect.<locals>.<genexpr>4  s     6sf}6s   r!   r$   )rS   r+   boolr   HAVE_DNSPYTHONr   dnsresolverquery	exceptionDNSExceptionrO   targetto_textr7   r:   ri   sortr   rU   rd   anyr   r1   	NameErrorr&   r   r'   r(   r   )argsr.   rf   srv_recordsr3   srvrc   r!   s    `      r0   rU   rU      s   R (1F':fjj#Ggt$GHH 8 
 F" N  V M   S  26:F6N	,,,,VF^UCK  	COOJJ..d.CHH #!jj		 	ABBJ
;>S[#f+6
z
 f$!'(;!<~

&'&00
$$$ V'1&11666)3F33 7 zz*e,HV:,4m,,000D+F++g }})) 	 26&>2B!D	"
*  s$   #G' H5"H  '1H 	H,+H,c                     g }dD ]   }	 |j                  t        | |                " |st        d      dj	                  |      S # t        $ r Y Lw xY w)zGenerate a pool name

    This function takes keyword arguments, usually the connection
    arguments for MySQLConnection, and tries to generate a name for
    a pool.

    Raises PoolError when no name can be generated.

    Returns a string.
    )r6   r7   r4   r9   z.Failed generating pool name; specify pool_namer`   )rO   strrF   r   rK   )r.   partsrC   s      r0   r%   r%   F  se     E3 	LLVC[)* HII88E?  		s   A	AAc                  l    e Zd ZdZd
dZddZ	 	 	 	 	 	 	 	 ddZddZddZe	dd       Z
edd       Zy	)r   a  Class holding a MySQL Connection in a pool

    PooledMySQLConnection is used by MySQLConnectionPool to return an
    instance holding a MySQL connection. It works like a MySQLConnection
    except for methods like close() and config().

    The close()-method will add the connection back to the pool rather
    than disconnecting from the MySQL server.

    Configuring the connection have to be done through the MySQLConnectionPool
    method set_config(). Using config() on pooled connection will raise a
    PoolError.

    Attributes:
        pool_name (str): Returns the name of the connection pool to which the
                         connection belongs.
    c                    t        |t              st        d      t        |t              st        d      || _        || _        y)zConstructor.

        Args:
            pool: A `MySQLConnectionPool` instance.
            cnx: A `MySQLConnectionAbstract` subclass instance.
        z$pool should be a MySQLConnectionPoolzcnx should be a MySQLConnectionN)r+   r*   r-   r   	_cnx_pool_cnx)selfpoolcnxs      r0   __init__zPooledMySQLConnection.__init__q  s?     $ 34 !GHH#/ !BCC.2-0	rB   c                    | S rn   r$   r   s    r0   	__enter__zPooledMySQLConnection.__enter__  s    rB   c                $    | j                          y rn   )close)r   exc_type	exc_value	tracebacks       r0   __exit__zPooledMySQLConnection.__exit__  s     	

rB   c                .    t        | j                  |      S )z0Calls attributes of the MySQLConnection instance)getattrr   )r   attrs     r0   __getattr__z!PooledMySQLConnection.__getattr__  s    tyy$''rB   c                    	 | j                   }| j                  j                  r|j                          | j                  j                  |       d| _         y# | j                  j                         d| _         w xY w)a  Do not close, but adds connection back to pool.

        For a pooled connection, close() does not actually close it but returns it
        to the pool and makes it available for subsequent connection requests. If the
        pool configuration parameters are changed, a returned connection is closed
        and reopened with the new configuration before being returned from the pool
        again in response to a connection request.
        N)r   r   reset_sessionadd_connectionr   r   s     r0   r   zPooledMySQLConnection.close  s_    	))C~~++!!#NN))#.DI NN))#.DIs   2A $A;c                     t        d      )zConfiguration is done through the pool.

        For pooled connections, the `config()` method raises a `PoolError`
        exception. Configuration for pooled connections should be done
        using the pool object.
        zKConfiguration for pooled connections should be done through the pool itself)r   )r.   s    r0   rV   zPooledMySQLConnection.config  s     
 	
rB   c                .    | j                   j                  S )zHReturns the name of the connection pool to which the connection belongs.)r   r    r   s    r0   r    zPooledMySQLConnection.pool_name  s     ~~'''rB   N)r   r*   r   r   returnNoner   r   )r   zType[BaseException]r   BaseExceptionr   r   r   r   )r   r   r   r   )r   r   )r.   r   r   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   staticmethodrV   propertyr    r$   rB   r0   r   r   ^  sn    $1% ! !	
 
(" 

 

 ( (rB   c                      e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZedd       Zedd       Ze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y)r*   z*Class defining a pool of MySQL connectionsNc                   d| _         d| _        || _        | j                  |       | j	                  |xs t        di |       i | _        t        j                  | j                         | _	        t               | _        |rJ | j                  di | d}|| j                   k  r&| j                          |dz  }|| j                   k  r%yyy)a  Constructor.

        Initialize a MySQL connection pool with a maximum number of
        connections set to `pool_size`. The rest of the keywords
        arguments, kwargs, are configuration arguments for MySQLConnection
        instances.

        Args:
            pool_name: The pool name. If this argument is not given, Connector/Python
                       automatically generates the name, composed from whichever of
                       the host, port, user, and database connection arguments are
                       given in kwargs, in that order.
            pool_size:  The pool size. If this argument is not given, the default is 5.
            pool_reset_session: Whether to reset session variables when the connection
                                is returned to the pool.
            **kwargs: Optional additional connection arguments, as described in [1].

        Examples:
            ```
            >>> dbconfig = {
            >>>     "database": "test",
            >>>     "user":     "joe",
            >>> }
            >>> cnxpool = mysql.connector.pooling.MySQLConnectionPool(pool_name = "mypool",
            >>>                                                       pool_size = 3,
            >>>                                                       **dbconfig)
            ```

        References:
            [1]: https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
        Nr   r   r$   )
_pool_size
_pool_name_reset_session_set_pool_size_set_pool_namer%   _cnx_configqueueQueue
_cnx_queuer   _config_version
set_configr   )r   r"   r    pool_reset_sessionr.   cnts         r0   r   zMySQLConnectionPool.__init__  s    L *.)-0I&IE);)Ef)EF+-@EOOA
  %wDOO%f%C'##%q ' rB   c                    | j                   S )z(Returns the name of the connection pool.)r   r   s    r0   r    zMySQLConnectionPool.pool_name       rB   c                    | j                   S )z2Returns number of connections managed by the pool.)r   r   s    r0   r"   zMySQLConnectionPool.pool_size  r   rB   c                    | j                   S )z!Returns whether to reset session.)r   r   s    r0   r   z!MySQLConnectionPool.reset_session  s     """rB   c                    |syt         5  	 t               } |j                  di | || _        t	               | _        	 ddd       y# t        $ r}t        d|       |d}~ww xY w# 1 sw Y   yxY w)a  Set the connection configuration for `MySQLConnectionAbstract` subclass instances.

        This method sets the configuration used for creating `MySQLConnectionAbstract`
        subclass instances such as `MySQLConnection`. See [1] for valid
        connection arguments.

        Args:
            **kwargs: Connection arguments - for a complete list of possible
                      arguments, see [1].

        Raises:
            PoolError: When a connection argument is not valid, missing
                       or not supported by `MySQLConnectionAbstract`.

        References:
            [1]: https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
        Nz$Connection configuration not valid: r$   )r)   rU   rV   r   r   r   r-   r   )r   r.   test_cnxerrs       r0   r   zMySQLConnectionPool.set_config  s    $ ! 	WW"9)&)#) ',w$	W 	W " W"Fse LMSVVW	W 	Ws(   A(2A	A%A  A%%A((A1c                R    |dk  s	|t         kD  rt        dt                || _        y)a  Set the size of the pool

        This method sets the size of the pool but it will not resize the pool.

        Raises an AttributeError when the pool_size is not valid. Invalid size
        is 0, negative or higher than pooling.CNX_POOL_MAXSIZE.
        r   z8Pool size should be higher than 0 and lower or equal to N)CNX_POOL_MAXSIZEr-   r   )r   r"   s     r0   r   z"MySQLConnectionPool._set_pool_size  s7     >Y)99 J#$&  $rB   c                    t         j                  |      rt        d| d      t        |      t        kD  rt        d| d      || _        y)a  Set the name of the pool.

        This method checks the validity and sets the name of the pool.

        Raises an AttributeError when pool_name contains illegal characters
        ([^a-zA-Z0-9._\-*$#]) or is longer than pooling.CNX_POOL_MAXNAMESIZE.
        zPool name 'z' contains illegal charactersz' is too longN)CNX_POOL_NAMEREGEXsearchr-   rJ   CNX_POOL_MAXNAMESIZEr   )r   r    s     r0   r   z"MySQLConnectionPool._set_pool_name)  sO     $$Y/ ;yk9V!WXXy>00 ;yk!GHH#rB   c                    t        |t              st        d      	 | j                  j	                  |d       y# t
        j                  $ r}t        d      |d}~ww xY w)zPut connection back in the queue

        This method is putting a connection back in the queue. It will not
        acquire a lock as the methods using _queue_connection() will have it
        set.

        Raises `PoolError` on errors.
        ;Connection instance not subclass of MySQLConnectionAbstractFblock'Failed adding connection; queue is fullN)r+   r   r   r   putr   Fullr   r   r   s      r0   _queue_connectionz%MySQLConnectionPool._queue_connection7  s_     #/M 	POO51zz 	PEFCO	Ps   ; AAAc                   t         5  | j                  st        d      | j                  j	                         rt        d      |s]t        di | j                  }	 | j                  r)| j                  d   r|j                  dk  rt        d      | j                  |_        nt        |t              st        d      | j                  |       ddd       y# t        $ r Y Rw xY w# 1 sw Y   yxY w)	a  Adds a connection to the pool.

        This method instantiates a `MySQLConnection` using the configuration
        passed when initializing the `MySQLConnectionPool` instance or using
        the `set_config()` method.
        If cnx is a `MySQLConnection` instance, it will be added to the
        queue.

        Args:
            cnx: The `MySQLConnectionAbstract` subclass object to be added to
                 the pool. If this argument is missing (aka `None`), the pool
                 creates a new connection and adds it.

        Raises:
            PoolError: When no configuration is set, when no more
                       connection can be added (maximum reached) or when the connection
                       can not be instantiated.
        z&Connection configuration not availabler   compress)         z^Pool reset session is not supported with compression for MySQL server version 5.7.2 or earlierr   Nr$   )r)   r   r   r   fullrU   r   server_versionr   rF   r   pool_config_versionr+   r   r   r   s     r0   r   z"MySQLConnectionPool.add_connectionJ  s    & " 	(## HII##% IJJ1 0 01++ ,,Z8..:/)  +/*>*>'!#7#U  ""3'=	( 	((   )	( 	(s0   AC'5C>C'	C$!C'#C$$C''C0c                   t         5  	 | j                  j                  d      }|j                         r| j                  |j                  k7  r> |j                  di | j                   	 |j                          | j                  |_        t        | |      cddd       S # t        j                  $ r}t        d      |d}~ww xY w# t        $ r | j                  |        w xY w# 1 sw Y   yxY w)a  Gets a connection from the pool.

        This method returns an PooledMySQLConnection instance which
        has a reference to the pool that created it, and the next available
        MySQL connection.

        When the MySQL connection is not connect, a reconnect is attempted.

        Returns:
            A `PooledMySQLConnection` instance.

        Raises:
            PoolError: On errors.
        Fr   z)Failed getting connection; pool exhaustedNr$   )r)   r   r&   r   Emptyr   is_connectedr   r   rV   r   	reconnectr   r   r   r   s      r0   r,   z"MySQLConnectionPool.get_connection}  s     " 	4Voo)))6
 $$&''3+B+BB

.T--.MMO
 +/*>*>'(s3'	4 	4 ;; V KLRUUV & **3/	4 	4sF   C'B!AC'+C;C'!C4C  CC'C$$C''C0c                Z   t         5  d}| j                  }|j                         r9	 |j                  d      }|j	                          |dz  }|j                         r9|cddd       S # t
        j                  $ r |cY cddd       S t        $ r  t        $ r Y Ow xY w# 1 sw Y   yxY w)zClose all connections

        This method closes all connections. It returns the number
        of connections it closed.

        Used mostly for tests.

        Returns int.
        r   Fr   r   N)	r)   r   qsizer&   
disconnectr   r   r   r   )r   r   cnxqr   s       r0   _remove_connectionsz'MySQLConnectionPool._remove_connections  s     " 	C??D**,
(((/CNN$1HC	 **, !	 	 {{ J	 	 !  	 	s@   B!'A*B!B!*B?B!
BB!BB!!B*)r   NT)
r"   rM   r    zOptional[str]r   rq   r.   r   r   r   r   )r   rM   )r   rq   )r.   r   r   r   )r"   rM   r   r   )r    r   r   r   )r   r   r   r   rn   )r   z!Optional[MySQLConnectionAbstract]r   r   r   )r   r   r   r   r   r   r    r"   r   r   r   r   r   r   r,   r   r$   rB   r0   r*   r*     s    4 #'#'	66 !6 !	6
 6 
6p     # #W<$$P&1(f"4HrB   r*   )r.   r   r   r   )r.   r   r   5Union[PooledMySQLConnection, MySQLConnectionAbstract])r}   r   r.   r   r   r   )r.   r   r   r   ):r   
__future__r   r   rQ   re	threadingtypesr   typingr   r   r   r   r	   r
   r   r   uuidr   dns.exceptionrs   dns.resolverrr   r'   connection_cextr   
connectionr   	constantsr   r   errorsr   r   r   r   r   optionfilesr   	abstractsr   RLockr)   r   r   compiler   r(   r   __annotations__r   r1   rd   rU   r%   r   r*   r$   rB   r0   <module>r      sJ  : < "   	   S S S  N1 ( ;  +2&y(   RZZ 78 B'/OoGW5X .  57 1 6:ZIZI:ZIzy,y,y,:y,x0Q( Q(hI IM  N  s#   C" C/ "C,+C,/C98C9