o
    g($                     @   s   d Z g dZddlZddlm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	dlmZ z
ddlZddlZW n eyG   dZY nw dZzd	dlmZ W n ey]   dZY nw dededefddZdedefddZdS )z8MySQL Connector/Python - MySQL driver written in Python.)CMySQLConnectionMySQLConnectionconnect    N)Any   )DEFAULT_CONFIGURATION)ErrorInterfaceErrorProgrammingError)ERROR_NO_CEXT   )MySQLConnectionAbstract)r   FT)r   argskwargsreturnc                     s  d|v r
| dnd}t|tstd|rtstdd|v r%tdd|v r-tdd	|v r5td
d|vr?td |d< ztj|d d}W n tj	j
y^   td|d  ddw g }|D ]}||jjdd|j|j|jd qc|jdd d dd |D |d	< d	|v rtdi |I dH S |dd}d|v r|d= |stdu rtttr|st| i |}nt| i |}| I dH  |S )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
        a `CMySQLConnection`) 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.aio import MySQLConnection, HAVE_CEXT
        >>>
        >>> cnx1 = await mysql.connector.aio.connect(user='joe', database='test')
        >>> cnx2 = MySQLConnection(user='joe', database='test')
        >>> await cnx2.connect()
        >>>
        >>> cnx3 = None
        >>> if HAVE_CEXT:
        >>>     from mysql.connector.aio 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 documentationunix_socketz<Using Unix domain sockets with DNS SRV lookup is not allowedportz;Specifying a port number with DNS SRV lookup is not allowedfailoverzASpecifying multiple hostnames with DNS SRV look up is not allowedhostSRVz Unable to locate any hosts for ''NT)omit_final_dot)r   r   priorityweightc                 S   s   | d | d  fS )Nr   r    xr   r   V/var/www/html/api-tag/env/lib/python3.10/site-packages/mysql/connector/aio/__init__.py<lambda>   s    zconnect.<locals>.<lambda>)keyc                 S   s   g | ]}|d  |d dqS )r   r   )r   r   r   ).0srvr   r   r   
<listcomp>   s    zconnect.<locals>.<listcomp>use_purer   )pop
isinstanceboolr	   HAVE_DNSPYTHONr   dnsresolverquery	exceptionDNSExceptionappendtargetto_textr   r   r   sort_get_failover_connectiongetr   ImportErrorr   r   r   )r   r   r   srv_recordsr   r"   r$   cnxr   r   r   r   :   sr   (
	
r   c               
      s  |   }z|d }W n ty   tddw |d= tg d}d}|D ][}t| | }|rJt|dkr9dnd}d	|}td
| d| t|drS|d7 }|dd|d< |d dk sg|d dkrpt	d|d  t
|d tst	d|d  q%d|  k rt|k rtd i }	g }
t|dd ddD ] }|d |	vr|g|	|d < |
|d  q|	|d  | q|
D ]H}|	| }tt|D ];}t|d }td|}||}|  }|| |dd ztdi |I dH W     S  ty   Y qw qt	d)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.
    r   zfailover argument not providedN)	userpasswordr   r   r   database	pool_name	pool_sizer   r   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                 S   s   | d S )Nr   r   r   r   r   r   r      s    z*_get_failover_connection.<locals>.<lambda>T)r    reversez,Unable to connect to any of the target hostsr   )copyKeyError
ValueErrorsetkeyslenjoinhasattrr3   r	   r&   intr
   sortedr.   rangerandomrandintr%   updater   r   )r   configr   support_cnx_argspriority_countserverdiffarglstserver_directoryserver_priority_listr   failover_list_lastindex
new_configr   r   r   r2      s   




r2   )__doc____all__rK   typingr   	constantsr   errorsr   r	   r
   poolingr   	abstractsr   
connectionr   dns.exceptionr)   dns.resolverr4   r(   connection_cextr   r   r2   r   r   r   r   <module>   s.   m