U
    eT                     @  s  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 ddlmZ zddlZddlZW n ek
r   dZY nX dZzd	d
lmZ W n ek
r   dZY nX 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' Z(dZ)dZ*e+dZ,dZ-edkr:eneefZ.de/d< i Z0de/d< dddddZ1dddddZ2ddddd d!Z3dd"dd#d$Z4G d%d dZ5G d&d' d'Z6dS )(z5Implementing pooling of connections to MySQL servers.    )annotationsN)TracebackType)AnyDictNoReturnOptionalTupleTypeUnion)uuid4FT   )CMySQLConnection)MySQLConnection)CNX_POOL_ARGSDEFAULT_CONFIGURATION)ErrorInterfaceErrorNotSupportedError	PoolErrorProgrammingError)read_option_files    @   z[^a-zA-Z0-9._:\-*$#]z0MySQL Connector/Python C Extension not availablezUnion[type, Tuple[type, ...]]MYSQL_CNX_CLASSzDict[str, MySQLConnectionPool]_CONNECTION_POOLSr   PooledMySQLConnectionkwargsreturnc               	   K  s   d| kr| d nt f | }| ddkr8tdkr8tttR |tkrVtf | t|< n4tt| trt| j	}d| kr| d |krt
dW 5 Q R X zt|  W S  tk
r   td| ddY nX dS )	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   )r   r   Z
check_size r,   X/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/mysql/connector/pooling.py_get_pooled_connectionM   s&    

r.   z?Union[PooledMySQLConnection, MySQLConnection, CMySQLConnection]c               
   K  s<  |   }z|d }W n tk
r2   tddY nX |d= tdddddd	d
ddg	}d}|D ]}t| | }|rt|dkrdnd}d|}td| d| t|dr|d7 }|dd|d< |d dk s|d dkrt	d|d  t
|d ts\t	d|d  q\d|  k r0t|k r<n ntdi }	g }
t|dd ddD ]D}|d |	kr|g|	|d < |
|d  n|	|d  | qV|
D ]}|	| }tt|D ]r}t|d }td|}||}|  }|| |dd ztf |W     S  tk
r&   Y nX qqt	ddS )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: 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                 S  s   | d S )Nr6   r,   xr,   r,   r-   <lambda>       z*_get_failover_connection.<locals>.<lambda>T)keyreversez,Unable to connect to any of the target hosts)copyKeyError
ValueErrorsetkeyslenjoinhasattrr$   r   r)   intr   sortedappendrangerandomrandintpopupdateconnectr   )r   configr/   Zsupport_cnx_argsZpriority_countserverdiffarglstZserver_directoryZserver_priority_listr6   Zfailover_list_lastindex
new_configr,   r,   r-   _get_failover_connectionj   s~    

 

rZ   )argsr   r   c                    s  d kr  dnd}t|ts(td|rts:tdd krJtdd krZtdd	 krjtd
d kr~td  d< ztj d d}W n. tj	j
k
r   td d  ddY nX g }|D ](}||jjdd|j|j|jd q|jdd d dd |D  d	< d kr8 d  d<   d d krVtf  }tf |S d	 krjtf  S z(t fddtD rtf  W S W n tk
r   Y nX  dd}d krވ d= |stdkrtttr|st|  S t|  S )a  Create or get a MySQL connection object.

    In its simpliest form, connect() will open a connection to a
    MySQL server and return a MySQLConnection object.

    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.

    Returns MySQLConnection or PooledMySQLConnection.
    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 documentationr4   z<Using Unix domain sockets with DNS SRV lookup is not allowedr3   z;Specifying a port number with DNS SRV lookup is not allowedr/   zASpecifying multiple hostnames with DNS SRV look up is not allowedr2   ZSRVz Unable to locate any hosts for 'r"   NT)Zomit_final_dot)r2   r3   r6   weightc                 S  s   | d | d  fS )Nr6   r]   r,   r:   r,   r,   r-   r<     r=   zconnect.<locals>.<lambda>)r>   c                 S  s   g | ]}|d  |d dqS )r2   r3   )r2   r3   r,   ).0srvr,   r,   r-   
<listcomp>  s    zconnect.<locals>.<listcomp>Zread_default_fileZoption_filesc                 3  s   | ]}| kV  qd S Nr,   )r^   r>   r   r,   r-   	<genexpr>  s     zconnect.<locals>.<genexpr>r    )rN   r)   boolr   HAVE_DNSPYTHONr   dnsresolverquery	exceptionZDNSExceptionrJ   targetZto_textr3   r6   r]   sortr   rP   rZ   anyr   r.   	NameErrorr$   r   r%   r&   r   )r[   r   r\   Zsrv_recordsr/   r_   rY   r    r,   rb   r-   rP      s    
	









rP   strc               	   K  sP   g }dD ]0}z| t| |  W q tk
r6   Y qX q|sFtdd|S )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.
    )r2   r3   r0   r5   z.Failed generating pool name; specify pool_namerV   )rJ   rn   rA   r   rF   )r   partsr>   r,   r,   r-   r#   )  s    r#   c                   @  s   e Zd ZdZddddddZd dd	d
ZdddddddZdddddZddddZe	dddddZ
eddddZdS )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.
    r(   (Union[MySQLConnection, CMySQLConnection]None)poolcnxr   c                 C  s4   t |tstdt |ts$td|| _|| _dS )zInitialize

        The pool argument must be an instance of MySQLConnectionPoll. cnx
        if an instance of MySQLConnection.
        z$pool should be a MySQLConnectionPoolzcnx should be a MySQLConnectionN)r)   r(   r+   r   	_cnx_pool_cnx)selfrr   rs   r,   r,   r-   __init__P  s    

zPooledMySQLConnection.__init__r   c                 C  s   | S ra   r,   rv   r,   r,   r-   	__enter___  s    zPooledMySQLConnection.__enter__zType[BaseException]BaseExceptionr   )exc_type	exc_value	tracebackr   c                 C  s   |    d S ra   )close)rv   r|   r}   r~   r,   r,   r-   __exit__b  s    zPooledMySQLConnection.__exit__r   )attrr   c                 C  s   t | j|S )z0Calls attributes of the MySQLConnection instance)getattrru   )rv   r   r,   r,   r-   __getattr__j  s    z!PooledMySQLConnection.__getattr__c              	   C  s4   z| j}| j jr|  W 5 | j | d| _X dS )aY  Do not close, but add connection back to pool

        The close() method does not close the connection with the
        MySQL server. The connection is added back to the pool so it
        can be reused.

        When the pool is configured to reset the session, the session
        state will be cleared by re-authenticating the user.
        N)rt   add_connectionru   reset_sessionrv   rs   r,   r,   r-   r   n  s    
zPooledMySQLConnection.closer   r   c                  K  s   t ddS )z&Configuration is done through the poolzKConfiguration for pooled connections should be done through the pool itselfN)r   rb   r,   r,   r-   rQ     s    zPooledMySQLConnection.configrn   c                 C  s   | j jS z&Return the name of the connection pool)rt   r   ry   r,   r,   r-   r     s    zPooledMySQLConnection.pool_nameN)__name__
__module____qualname____doc__rw   rz   r   r   r   staticmethodrQ   propertyr   r,   r,   r,   r-   r   A  s   c                   @  s   e Zd ZdZd*ddddd	d
ddZeddddZeddddZeddddZdd	dddZ	dd	dddZ
dd	dddZdd	dd d!Zd+d"d	dd#d$Zd%dd&d'Zddd(d)ZdS ),r(   z*Class defining a pool of MySQL connections   NTrH   zOptional[str]rd   r   rq   )r!   r   pool_reset_sessionr   r   c                 K  s   d| _ d| _|| _| | | |p,tf | i | _t| j | _	t
 | _|r|| jf | d}|| j k r||   |d7 }q`dS )zInitialize

        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.
        Nr   r   )
_pool_size
_pool_name_reset_session_set_pool_size_set_pool_namer#   _cnx_configqueueQueue
_cnx_queuer   _config_version
set_configr   )rv   r!   r   r   r   cntr,   r,   r-   rw     s    


zMySQLConnectionPool.__init__rn   rx   c                 C  s   | j S r   )r   ry   r,   r,   r-   r     s    zMySQLConnectionPool.pool_namec                 C  s   | j S )z0Return number of connections managed by the pool)r   ry   r,   r,   r-   r!     s    zMySQLConnectionPool.pool_sizec                 C  s   | j S )zReturn whether to reset session)r   ry   r,   r,   r-   r     s    z!MySQLConnectionPool.reset_sessionr   c                 K  st   |sdS t ^ z$t }|jf | || _t | _W n2 tk
rd } ztd| |W 5 d}~X Y nX W 5 Q R X dS )aT  Set the connection configuration for MySQLConnection instances

        This method sets the configuration used for creating MySQLConnection
        instances. See MySQLConnection for valid connection arguments.

        Raises PoolError when a connection argument is not valid, missing
        or not supported by MySQLConnection.
        Nz$Connection configuration not valid: )r'   rP   rQ   r   r   r   r+   r   )rv   r   Ztest_cnxerrr,   r,   r-   r     s    	zMySQLConnectionPool.set_config)r!   r   c                 C  s(   |dks|t krtdt  || _dS )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   )rv   r!   r,   r,   r-   r     s
    z"MySQLConnectionPool._set_pool_size)r   r   c                 C  s@   t |rtd| dt|tkr6td| d|| _dS )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+   rE   CNX_POOL_MAXNAMESIZEr   )rv   r   r,   r,   r-   r     s
    
z"MySQLConnectionPool._set_pool_namerp   )rs   r   c              
   C  sZ   t |tstdz| jj|dd W n. tjk
rT } ztd|W 5 d}~X Y nX dS )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.
        3Connection instance not subclass of MySQLConnectionFblock'Failed adding connection; queue is fullN)r)   r   r   r   putr   Fullrv   rs   r   r,   r,   r-   _queue_connection  s    	
z%MySQLConnectionPool._queue_connectionz2Optional[Union[MySQLConnection, CMySQLConnection]]c              	   C  s   t  | jstd| j r&td|s~tf | j}z(| jr\| jd r\| dk r\tdW n t	k
rr   Y nX | j
|_nt|tstd| | W 5 Q R X dS )a  Add 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.

        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)r         z^Pool reset session is not supported with compression for MySQL server version 5.7.2 or earlierr   N)r'   r   r   r   fullrP   r   Zget_server_versionr   rA   r   pool_config_versionr)   r   r   r   r,   r,   r-   r     s2    



z"MySQLConnectionPool.add_connectionr   c                 C  s   t  z| jjdd}W n. tjk
rF } ztd|W 5 d}~X Y nX | r\| j|jkr|j	f | j
 z|  W n  tk
r   | |  Y nX | j|_t| |W  5 Q R  S Q R X dS )aw  Get 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.

        Raises PoolError on errors.

        Returns a PooledMySQLConnection instance.
        Fr   z)Failed getting connection; pool exhaustedN)r'   r   r$   r   Emptyr   Zis_connectedr   r   rQ   r   Z	reconnectr   r   r   r   r,   r,   r-   r*   2  s"    

z"MySQLConnectionPool.get_connectionc              
   C  s   t  d}| j}| rz |jdd}|  |d7 }W q tjk
r^   | Y W  5 Q R  S  tk
rr    Y q tk
r   Y qX q|W  5 Q R  S Q R X dS )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$   Z
disconnectr   r   r   r   )rv   r   Zcnxqrs   r,   r,   r-   _remove_connectionsT  s    
z'MySQLConnectionPool._remove_connections)r   NT)N)r   r   r   r   rw   r   r   r!   r   r   r   r   r   r   r*   r   r,   r,   r,   r-   r(     s&        /"r(   )7r   
__future__r   r   rL   re	threadingtypesr   typingr   r   r   r   r   r	   r
   uuidr   Zdns.exceptionrf   Zdns.resolverr%   re   Zconnection_cextr   
connectionr   	constantsr   r   errorsr   r   r   r   r   Zoptionfilesr   RLockr'   r   r   compiler   r&   r   __annotations__r   r.   rZ   rP   r#   r   r(   r,   r,   r,   r-   <module>   sH   $



]bM