U
    hq'                     @  s  d Z ddlm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 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 ddlmZ e
rVd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! 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%Z+G d&d' d'e+eZ,G d(d) d)e+eZ-G d*d+ d+eZ.G d,d- d-eZ/G d.d/ d/ee/Z0e/Z1e0Z2dS )0a,  
.. 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/

Driver Status
-------------

MySQL Connector/Python is supported as of SQLAlchemy 2.0.39 to the
degree which the driver is functional.   There are still ongoing issues
with features such as server side cursors which remain disabled until
upstream issues are repaired.

.. warning:: The MySQL Connector/Python driver published by Oracle is subject
   to frequent, major regressions of essential functionality such as being able
   to correctly persist simple binary strings which indicate it is not well
   tested.  The SQLAlchemy project is not able to maintain this dialect fully as
   regressions in the driver prevent it from being included in continuous
   integration.

.. versionchanged:: 2.0.39

    The MySQL Connector/Python dialect has been updated to support the
    latest version of this DBAPI.   Previously, MySQL Connector/Python
    was not fully supported.  However, support remains limited due to ongoing
    regressions introduced in this driver.

Connecting to MariaDB with MySQL Connector/Python
--------------------------------------------------

MySQL Connector/Python may attempt to pass an incompatible collation to the
database when connecting to MariaDB.  Experimentation has shown that using
``?charset=utf8mb4&collation=utfmb4_general_ci`` or similar MariaDB-compatible
charset/collation will allow connectivity.


    )annotationsN)Any)cast)Optional)Sequence)Tuple)TYPE_CHECKING)Union   )MariaDBIdentifierPreparer)MySQLCompiler)MySQLDialect)MySQLExecutionContext)MySQLIdentifierPreparer)MariaDBDialect)BIT   )util)
Connection)CursorResult)ConnectArgsType)DBAPIConnection)DBAPICursor)DBAPIModule)IsolationLevel)PoolProxiedConnection)Row)URL)BinaryExpressionc                   @  s(   e Zd ZddddZddddZdS )$MySQLExecutionContext_mysqlconnectorr   returnc                 C  s   | j jddS )NFbufferedZ_dbapi_connectioncursorself r(   L/tmp/pip-unpacked-wheel-y9ln43lq/sqlalchemy/dialects/mysql/mysqlconnector.pycreate_server_side_cursorT   s    z>MySQLExecutionContext_mysqlconnector.create_server_side_cursorc                 C  s   | j jddS )NTr"   r$   r&   r(   r(   r)   create_default_cursorW   s    z:MySQLExecutionContext_mysqlconnector.create_default_cursorN)__name__
__module____qualname__r*   r+   r(   r(   r(   r)   r   S   s   r   c                   @  s    e Zd ZdddddddZdS )MySQLCompiler_mysqlconnectorzBinaryExpression[Any]r   str)binaryoperatorkwr!   c                 K  s$   | j |jf|d | j |jf| S )Nz % )processleftright)r'   r1   r2   r3   r(   r(   r)   visit_mod_binary\   s    z-MySQLCompiler_mysqlconnector.visit_mod_binaryN)r,   r-   r.   r7   r(   r(   r(   r)   r/   [   s   r/   c                   @  sD   e Zd ZeddddZejdddddZd	d	dd
dZdS )'IdentifierPreparerCommon_mysqlconnectorboolr    c                 C  s   dS )NFr(   r&   r(   r(   r)   _double_percentsg   s    z8IdentifierPreparerCommon_mysqlconnector._double_percentsr   None)valuer!   c                 C  s   d S Nr(   r'   r<   r(   r(   r)   r:   k   s    r0   c                 C  s   | | j| j}|S r=   )replaceZescape_quoteZescape_to_quoter>   r(   r(   r)   _escape_identifiero   s
    z:IdentifierPreparerCommon_mysqlconnector._escape_identifierN)r,   r-   r.   propertyr:   setterr@   r(   r(   r(   r)   r8   f   s
   r8   c                   @  s   e Zd ZdS )&MySQLIdentifierPreparer_mysqlconnectorNr,   r-   r.   r(   r(   r(   r)   rC   w   s   rC   c                   @  s   e Zd ZdS )(MariaDBIdentifierPreparer_mysqlconnectorNrD   r(   r(   r(   r)   rE   }   s   rE   c                   @  s   e Zd ZddddddZdS )_myconnpyBITr   r;   )dialectcoltyper!   c                 C  s   dS )z0MySQL-connector already converts mysql bits, so.Nr(   )r'   rG   rH   r(   r(   r)   result_processor   s    z_myconnpyBIT.result_processorN)r,   r-   r.   rI   r(   r(   r(   r)   rF      s   rF   c                      s0  e Zd ZU dZdZdZdZdZdZdZ	dZ
eZeZeZded< eejeeiZeddd	d
ZdddddZdddddZejddddZdddddZdddd d!Zd"d#d$dd%d&d'Z d=d)d*d+d,d-d.Z!d>d)d*d/d,d0d1Z"dd2d3d4d5Z#ddd3d6d7Z$dd8d9d: fd;d<Z%  Z&S )?MySQLDialect_mysqlconnectorZmysqlconnectorTFformatztype[MySQLIdentifierPreparer]preparerr   r    c                 C  s   t dtdjS )Nr   zmysql.connector)r   
__import__Z	connector)clsr(   r(   r)   import_dbapi   s    z(MySQLDialect_mysqlconnector.import_dbapir   r9   )dbapi_connectionr!   c                 C  s   | d dS )NFT)Zping)r'   rP   r(   r(   r)   do_ping   s    
z#MySQLDialect_mysqlconnector.do_pingr   r   )urlr!   c                 C  sr  |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 t|dt d|d< | jd k	rjz8ddlm	} |j
}|d| }||jO }||d< W n tk
rh   Y nX g |fS )Nuser)usernameZallow_local_infile
autocommitr#   Zclient_flag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   )	constantsclient_flags)Ztranslate_connect_argsupdatequeryr   Zcoerce_kw_typer9   intdbapiZmysql.connectorrY   
ClientFlaggetget_defaultZ
FOUND_ROWS	Exception)r'   rR   optsrY   r_   rZ   r(   r(   r)   create_connect_args   sB     
z/MySQLDialect_mysqlconnector.create_connect_argszOptional[Tuple[int, ...]]c                 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=   )r]   ).0xr(   r(   r)   	<genexpr>   s      zKMySQLDialect_mysqlconnector._mysqlconnector_version_info.<locals>.<genexpr>r
      r   )r^   hasattrrematchre   tuplegroup)r'   mr(   r(   r)   _mysqlconnector_version_info   s
    z8MySQLDialect_mysqlconnector._mysqlconnector_version_infor   r0   )
connectionr!   c                 C  s   |j jS r=   )rq   charset)r'   rq   r(   r(   r)   _detect_charset   s    z+MySQLDialect_mysqlconnector._detect_charsetBaseExceptionr]   )	exceptionr!   c                 C  s   |j S r=   )errno)r'   ru   r(   r(   r)   _extract_error_code   s    z/MySQLDialect_mysqlconnector._extract_error_coderb   z7Optional[Union[PoolProxiedConnection, DBAPIConnection]]zOptional[DBAPICursor])erq   r%   r!   c                 C  sN   d}| j j| j j| j jf}t||rF|j|kpDdt|kpDd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)Zloaded_dbapiZOperationalErrorZInterfaceErrorZProgrammingError
isinstancerv   r0   )r'   rx   rq   r%   Zerrnos
exceptionsr(   r(   r)   is_disconnect   s    



z)MySQLDialect_mysqlconnector.is_disconnectNzCursorResult[Tuple[Any, ...]]zOptional[str]zSequence[Row[Tuple[Any, ...]]])rprr   r!   c                 C  s   |  S r=   )Zfetchallr'   r|   rr   r(   r(   r)   _compat_fetchall   s    z,MySQLDialect_mysqlconnector._compat_fetchallzOptional[Row[Tuple[Any, ...]]]c                 C  s   |  S r=   )Zfetchoner}   r(   r(   r)   _compat_fetchone  s    z,MySQLDialect_mysqlconnector._compat_fetchonezSequence[IsolationLevel])
dbapi_connr!   c                 C  s   dS )N)ZSERIALIZABLEzREAD UNCOMMITTEDzREAD COMMITTEDzREPEATABLE READ
AUTOCOMMITr(   r'   r   r(   r(   r)   get_isolation_level_values  s    z6MySQLDialect_mysqlconnector.get_isolation_level_valuesc                 C  s
   t |jS r=   )r9   rU   r   r(   r(   r)   detect_autocommit_setting  s    z5MySQLDialect_mysqlconnector.detect_autocommit_settingr   r;   )rP   levelr!   c                   s(   |dkrd|_ nd|_ t || d S )Nr   TF)rU   superset_isolation_level)r'   rP   r   	__class__r(   r)   r     s    z/MySQLDialect_mysqlconnector.set_isolation_level)N)N)'r,   r-   r.   Zdriversupports_statement_cacheZsupports_sane_rowcountZsupports_sane_multi_rowcountZsupports_native_decimalZsupports_native_bitZsupports_server_side_cursorsZdefault_paramstyler/   Zstatement_compilerr   Zexecution_ctx_clsrC   rL   __annotations__r   Zupdate_copyr   Zcolspecsr   rF   classmethodrO   rQ   rd   Zmemoized_propertyrp   rs   rw   r{   r~   r   r   r   r   __classcell__r(   r(   r   r)   rJ      s:   

1 
 rJ   c                   @  s   e Zd ZdZdZeZdS )MariaDBDialect_mysqlconnectorTFN)r,   r-   r.   r   Z_allows_uuid_bindsrE   rL   r(   r(   r(   r)   r   %  s   r   )3__doc__
__future__r   rk   typingr   r   r   r   r   r   r	   baser   r   r   r   r   Zmariadbr   typesr    r   Zengine.baser   Zengine.cursorr   Zengine.interfacesr   r   r   r   r   r   Z
engine.rowr   Z
engine.urlr   Zsql.elementsr   r   r/   r8   rC   rE   rF   rJ   r   rG   Zmariadb_dialectr(   r(   r(   r)   <module>	   sb   '
 
  
 