U
    x^                     @   s   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 ddlmZ d	d
lm	Z	 d	dlm
Z
 G dd deZG dd deZG dd deZG dd deZG dd deZeZdS )a  
.. dialect:: mysql+mysqlconnector
    :name: MySQL Connector/Python
    :dbapi: myconnpy
    :connectstring: mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
    :url: https://pypi.org/project/mysql-connector-python/

.. note::

    The MySQL Connector/Python DBAPI has had many issues since its release,
    some of which may remain unresolved, and the mysqlconnector dialect is
    **not tested as part of SQLAlchemy's continuous integration**.
    The recommended MySQL dialects are mysqlclient and PyMySQL.

    N   )BIT)MySQLCompiler)MySQLDialect)MySQLExecutionContext)MySQLIdentifierPreparer   )
processors)utilc                   @   s   e Zd Zdd ZdS )$MySQLExecutionContext_mysqlconnectorc                 C   s   | j jS N)cursorZ	lastrowidself r   P/tmp/pip-install-dq5v43_d/SQLAlchemy/sqlalchemy/dialects/mysql/mysqlconnector.pyget_lastrowid$   s    z2MySQLExecutionContext_mysqlconnector.get_lastrowidN)__name__
__module____qualname__r   r   r   r   r   r   #   s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )MySQLCompiler_mysqlconnectorc                 K   sT   | j jr,| j|jf|d | j|jf| S | j|jf|d | j|jf| S d S )Nz %% z % )dialect_mysqlconnector_double_percentsprocessleftright)r   binaryoperatorkwr   r   r   visit_mod_binary)   s    z-MySQLCompiler_mysqlconnector.visit_mod_binaryc                 C   s   | j jr|ddS |S d S N%z%%r   r   replacer   textr   r   r   post_process_text7   s    z.MySQLCompiler_mysqlconnector.post_process_textc                 C   s   | j jr|ddS |S d S r    r"   r$   r   r   r   escape_literal_column=   s    z2MySQLCompiler_mysqlconnector.escape_literal_columnN)r   r   r   r   r&   r'   r   r   r   r   r   (   s   r   c                   @   s.   e Zd Zedd Zejdd Zdd ZdS )&MySQLIdentifierPreparer_mysqlconnectorc                 C   s   | j jS r   )r   r   r   r   r   r   _double_percentsE   s    z7MySQLIdentifierPreparer_mysqlconnector._double_percentsc                 C   s   d S r   r   r   valuer   r   r   r)   I   s    c                 C   s,   | | j| j}| jjr$| ddS |S d S r    )r#   Zescape_quoteZescape_to_quoter   r   r*   r   r   r   _escape_identifierM   s    z9MySQLIdentifierPreparer_mysqlconnector._escape_identifierN)r   r   r   propertyr)   setterr,   r   r   r   r   r(   D   s
   

r(   c                   @   s   e Zd Zdd ZdS )_myconnpyBITc                 C   s   dS )z0MySQL-connector already converts mysql bits, so.Nr   )r   r   Zcoltyper   r   r   result_processorV   s    z_myconnpyBIT.result_processorN)r   r   r   r0   r   r   r   r   r/   U   s   r/   c                       s   e Zd ZdZdZdZdZdZdZe	Z
eZeZeejeeiZ fddZdd Zedd	 Zejd
d Zedd Zdd Zdd Zejdd Zejdd Z dd Z!dd Z"dd Z#d(ddZ$d)dd Z%e&d!d"d#d$d%gZ' fd&d'Z(  Z)S )*MySQLDialect_mysqlconnectorZmysqlconnectorTformatc                    s$   t t| j|| t| j| _d S r   )superr1   __init__r	   Z(to_conditional_unicode_processor_factorydescription_encodingZ_description_decoder)r   argr   	__class__r   r   r4   n   s
    z$MySQLDialect_mysqlconnector.__init__c                 C   s   dS )NFr   r   
connectionr   r   r   _check_unicode_descriptionw   s    z6MySQLDialect_mysqlconnector._check_unicode_descriptionc                 C   s   dS )Nzlatin-1r   r   r   r   r   r5   |   s    z0MySQLDialect_mysqlconnector.description_encodingc                 C   s   t jp| jdkS N)   r   r
   Zpy3k_mysqlconnector_version_infor   r   r   r   supports_unicode_statements   s    z7MySQLDialect_mysqlconnector.supports_unicode_statementsc                 C   s   ddl m} |S )Nr   )	connector)ZmysqlrA   )clsrA   r   r   r   dbapi   s    z!MySQLDialect_mysqlconnector.dbapic              
   C   sX   z| d W n@ | jjk
rN } z| ||d r<W Y 
dS  W 5 d }~X Y nX dS d S )NFT)ZpingrC   Erroris_disconnect)r   Zdbapi_connectionerrr   r   r   do_ping   s    
z#MySQLDialect_mysqlconnector.do_pingc                 C   sb  |j dd}||j t|dt t|dt t|dt t|dt t|dt t|dt t|d	t t|d
t t|dt t|dt t|dt t|dt t|dt t|dt t|dt t|dt |dd | jd k	rZz2ddl	m
} |d| }||jO }||d< W n tk
rX   Y nX g |gS )Nuser)usernameZallow_local_infile
autocommitZbufferedcompressZconnection_timeoutconnect_timeoutZconsume_resultsZ
force_ipv6Zget_warningsZpool_reset_sessionZ	pool_sizeZraise_on_warningsrawZssl_verify_certZuse_pureZuse_unicodeTr   )
ClientFlagclient_flags)Ztranslate_connect_argsupdatequeryr
   Zcoerce_kw_typeboolint
setdefaultrC   Zmysql.connector.constantsrN   getget_defaultZ
FOUND_ROWS	Exception)r   urloptsrN   rO   r   r   r   create_connect_args   s>     
z/MySQLDialect_mysqlconnector.create_connect_argsc                 C   sF   | j rBt| j drBtd| j j}|rBtdd |dddD S d S )N__version__z(\d+)\.(\d+)(?:\.(\d+))?c                 s   s   | ]}|d k	rt |V  qd S r   )rS   ).0xr   r   r   	<genexpr>   s      zKMySQLDialect_mysqlconnector._mysqlconnector_version_info.<locals>.<genexpr>r   r=   r   )rC   hasattrrematchr[   tuplegroup)r   mr   r   r   r?      s    z8MySQLDialect_mysqlconnector._mysqlconnector_version_infoc                 C   s   t j o| jdk S r<   r>   r   r   r   r   r      s    z;MySQLDialect_mysqlconnector._mysqlconnector_double_percentsc                 C   s   |j jS r   )r:   charsetr9   r   r   r   _detect_charset   s    z+MySQLDialect_mysqlconnector._detect_charsetc                 C   s   |j S r   )errno)r   	exceptionr   r   r   _extract_error_code   s    z/MySQLDialect_mysqlconnector._extract_error_codec                 C   sH   d}| j j| j jf}t||r@|j|kp>dt|kp>dt|kS dS d S )N)i  i  i  i  i  i   zMySQL Connection not available.z$Connection to MySQL is not availableF)rC   ZOperationalErrorZInterfaceError
isinstancerg   str)r   er:   r   Zerrnos
exceptionsr   r   r   rE      s    



z)MySQLDialect_mysqlconnector.is_disconnectNc                 C   s   |  S r   )Zfetchallr   rpre   r   r   r   _compat_fetchall   s    z,MySQLDialect_mysqlconnector._compat_fetchallc                 C   s   |  S r   )Zfetchonern   r   r   r   _compat_fetchone   s    z,MySQLDialect_mysqlconnector._compat_fetchoneZSERIALIZABLEzREAD UNCOMMITTEDzREAD COMMITTEDzREPEATABLE READ
AUTOCOMMITc                    s,   |dkrd|_ nd|_ tt| || d S )Nrr   TF)rJ   r3   r1   _set_isolation_level)r   r:   levelr7   r   r   rs      s    
 z0MySQLDialect_mysqlconnector._set_isolation_level)N)N)*r   r   r   ZdriverZsupports_unicode_bindsZsupports_sane_rowcountZsupports_sane_multi_rowcountZsupports_native_decimalZdefault_paramstyler   Zexecution_ctx_clsr   Zstatement_compilerr(   preparerr
   Zupdate_copyr   Zcolspecsr   r/   r4   r;   r-   r5   Zmemoized_propertyr@   classmethodrC   rG   rZ   r?   r   rf   ri   rE   rp   rq   setZ_isolation_lookuprs   __classcell__r   r   r7   r   r1   \   sL   	


)




r1   )__doc__r`   baser   r   r   r   r    r	   r
   r   r   r(   r/   r1   r   r   r   r   r   <module>   s    