3
(h+              5   @   s  d Z yddlZddlmZ W n ek
r4   dZY nX dZyddlZddlZW n ek
rf   dZ	Y nX dZ	ddl
Z
ddlmZ ddlm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mZmZmZmZ ddlmZmZm Z m!Z!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4 i Z5dZ6dd Z7dd Z8dd Z9e9Z:ej;Z<ej=Z>dddddddddddd d!d"d#d$d%d&d'dd(d)d*d+d,d-d.d/d/d0d1d2d3d4d5d6d7g%Z?dS )8z9
MySQL Connector/Python - MySQL driver written in Python
    N   )CMySQLConnectionFT)version)MySQLConnection)DEFAULT_CONFIGURATION)ErrorWarningInterfaceErrorDatabaseErrorNotSupportedError	DataErrorIntegrityErrorProgrammingErrorOperationalErrorInternalErrorcustom_error_exception	PoolError)	FieldFlag	FieldTypeCharacterSetRefreshOption
ClientFlag)DateTime	TimestampBinaryDateFromTicksTimestampFromTicksTimeFromTicksSTRINGBINARYNUMBERDATETIMEROWIDapilevelthreadsafety
paramstyle)read_option_filesz0MySQL Connector/Python C Extension not availablec              K   s   ddl m}m}m} y| d }W n tk
r>   |f | }Y nX d| kr`| d  r`t r`tt|R |tkr~|f | t|< n4t	t| |rt| j
}d| kr| d |krtdW dQ R X yt| j S  tk
r   tdj|Y nX dS )	z Return a pooled MySQL connectionr   )MySQLConnectionPoolgenerate_pool_nameCONNECTION_POOL_LOCK	pool_nameuse_pure	pool_sizez)Size can not be changed for active pools.Nz)Failed getting connection from pool '{0}')Zpoolingr(   r)   r*   KeyError	HAVE_CEXTImportErrorERROR_NO_CEXT_CONNECTION_POOLS
isinstancer-   r   get_connectionAttributeErrorr	   format)kwargsr(   r)   r*   r+   Z
check_size r8   L/tmp/pip-install-q3hcpn_q/mysql-connector-python/mysql/connector/__init__.py_get_pooled_connectionH   s*    
r:   c              K   s8  | j  }y|d }W n tk
r0   tdY nX |d= tddddddd	d
dg	}d}x|D ]}t|j | }|rtdjt|dkrdnddj|t|dr|d7 }|j	dd|d< |d dk s|d dkrt
dj|d t|d ts\t
dj|d q\W d|  k o"t|k n  r2td|jdd dd i }g }xL|D ]D}|d |kr|g||d < |j|d  n||d  j| qRW x|D ]}	||	 }
xxtt|
D ]h}t|
d }tjd|}|
j|}|j  }|j| |jdd y
tf |S  tk
r   Y nX qW qW t
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 provideduserpasswordhostportunix_socketZdatabaser+   r-   priorityr   z4Unsupported connection argument {0} in failover: {1}r   s z, d   z;Priority value should be in the range of 0 to 100, got : {}zFPriority 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 )NrA   r8   )xr8   r8   r9   <lambda>   s    z*_get_failover_connection.<locals>.<lambda>T)keyreverseNz,Unable to connect to any of the target hosts)copyr.   
ValueErrorsetkeysr6   lenjoinhasattrgetr	   r3   intr   sortappendrangerandomrandintpopupdateconnectr   )r7   configr;   Zsupport_cnx_argsZpriority_countserverZdiffZserver_directoryZserver_priority_listrA   Zfailover_list_lastindex
new_configr8   r8   r9   _get_failover_connectionl   sh    


 




r`   c        	         s
  d kr j 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< ytjj d d}W n* tj	j
k
r   tdj d Y nX g }x0|D ](}|j|jjdd|j|j|jd qW |jdd d dd |D  d	< d kr8 d  d<  j d d krVtf  }tf |S d	 krjtf  S y2ddlm} t fdd|D rtf  S W n tk
r   Y nX  jdd}d kr d= | rt rtttr | r t|  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 documentationr@   z<Using Unix domain sockets with DNS SRV lookup is not allowedr?   z;Specifying a port number with DNS SRV lookup is not allowedr;   zASpecifying multiple hostnames with DNS SRV look up is not allowedr>   ZSRVz$Unable to locate any hosts for '{0}'T)Zomit_final_dot)r>   r?   rA   weightc             S   s   | d | d  fS )NrA   rb   r8   )rE   r8   r8   r9   rF      s    zconnect.<locals>.<lambda>)rG   c             S   s   g | ]}|d  |d dqS )r>   r?   )r>   r?   r8   ).0srvr8   r8   r9   
<listcomp>   s   zconnect.<locals>.<listcomp>Zread_default_fileZoption_filesr   )CNX_POOL_ARGSc                s   g | ]}| kqS r8   r8   )rc   rG   )r7   r8   r9   re      s    r,   )!rW   r3   boolr	   HAVE_DNSPYTHONr   dnsresolverquery	exceptionZDNSExceptionr6   rS   targetZto_textr?   rA   rb   rR   r'   rY   r`   	constantsrf   anyr:   	NameErrorrP   r/   r0   r1   r   r   )	argsr7   ra   Zsrv_recordsr;   rd   r_   rf   r,   r8   )r7   r9   rY      sf    










rY   r   Connectr   r   r   r   r   r   r/   r   r   r	   r
   r   r   r   r   r   r   r$   r%   r&   r   r   r   r   r   r   r   r   r    r!   r"   r#   r   )@__doc__Z_mysql_connectorZconnection_cextr   r0   r/   Zdns.resolverri   Zdns.exceptionrh   rU   rC   r   
connectionr   rn   r   errorsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   Zdbapir   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   Zoptionfilesr'   r2   r1   r:   r`   rY   rr   VERSION__version_info__ZVERSION_TEXT__version____all__r8   r8   r8   r9   <module>   sR   

8D$NV
