o
     h&                     @  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
rd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ZdddZdS )	$MySQLExecutionContext_mysqlconnectorreturnr   c                 C     | j jddS )NFbuffered_dbapi_connectioncursorself r)   [/var/www/html/venv/lib/python3.10/site-packages/sqlalchemy/dialects/mysql/mysqlconnector.pycreate_server_side_cursorT      z>MySQLExecutionContext_mysqlconnector.create_server_side_cursorc                 C  r!   )NTr"   r$   r'   r)   r)   r*   create_default_cursorW   r,   z:MySQLExecutionContext_mysqlconnector.create_default_cursorN)r    r   )__name__
__module____qualname__r+   r-   r)   r)   r)   r*   r   S   s    
r   c                   @  s   e Zd Zddd	Zd
S )MySQLCompiler_mysqlconnectorbinaryBinaryExpression[Any]operatorr   kwr    strc                 K  s,   | j |jfi |d | j |jfi | S )Nz % )processleftright)r(   r2   r4   r5   r)   r)   r*   visit_mod_binary\   s   z-MySQLCompiler_mysqlconnector.visit_mod_binaryN)r2   r3   r4   r   r5   r   r    r6   )r.   r/   r0   r:   r)   r)   r)   r*   r1   [       r1   c                   @  s4   e Zd ZedddZejdddZdd
dZdS )'IdentifierPreparerCommon_mysqlconnectorr    boolc                 C     dS )NFr)   r'   r)   r)   r*   _double_percentsg      z8IdentifierPreparerCommon_mysqlconnector._double_percentsvaluer   Nonec                 C  s   d S Nr)   r(   rA   r)   r)   r*   r?   k   r@   r6   c                 C  s   | | j| j}|S rC   )replaceescape_quoteescape_to_quoterD   r)   r)   r*   _escape_identifiero   s
   z:IdentifierPreparerCommon_mysqlconnector._escape_identifierN)r    r=   )rA   r   r    rB   )rA   r6   r    r6   )r.   r/   r0   propertyr?   setterrH   r)   r)   r)   r*   r<   f   s    r<   c                   @     e Zd ZdS )&MySQLIdentifierPreparer_mysqlconnectorNr.   r/   r0   r)   r)   r)   r*   rL   w       rL   c                   @  rK   )(MariaDBIdentifierPreparer_mysqlconnectorNrM   r)   r)   r)   r*   rO   }   rN   rO   c                   @  s   e Zd Zd	ddZdS )
_myconnpyBITdialectr   coltyper    rB   c                 C  r>   )z0MySQL-connector already converts mysql bits, so.Nr)   )r(   rQ   rR   r)   r)   r*   result_processor      z_myconnpyBIT.result_processorN)rQ   r   rR   r   r    rB   )r.   r/   r0   rS   r)   r)   r)   r*   rP      r;   rP   c                      s   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
Zd>ddZd?ddZejd@ddZdAddZdBd d!ZdCd'd(Z 	)dDdEd/d0Z!	)dDdFd2d3Z"dGd6d7Z#dH fd;d<Z$  Z%S )IMySQLDialect_mysqlconnectormysqlconnectorTFformatztype[MySQLIdentifierPreparer]preparerr    r   c                 C  s   t dtdjS )Nr   zmysql.connector)r   
__import__	connector)clsr)   r)   r*   import_dbapi   s   z(MySQLDialect_mysqlconnector.import_dbapidbapi_connectionr   r=   c                 C  s   | d dS )NFT)ping)r(   r]   r)   r)   r*   do_ping   s   
z#MySQLDialect_mysqlconnector.do_pingurlr   r   c                 C  sx  |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 urzddlm	} |j
}|d| }||jO }||d< W g |fS  ty   Y g |fS w g |fS )Nuser)usernameallow_local_infile
autocommitr#   client_flagcompressconnection_timeoutconnect_timeoutconsume_results
force_ipv6get_warningspool_reset_session	pool_sizeraise_on_warningsrawssl_verify_certuse_pureuse_unicodeTr   )	constantsclient_flags)translate_connect_argsupdatequeryr   coerce_kw_typer=   intdbapimysql.connectorrs   
ClientFlaggetget_default
FOUND_ROWS	Exception)r(   r`   optsrs   r|   rt   r)   r)   r*   create_connect_args   sF   


z/MySQLDialect_mysqlconnector.create_connect_argsOptional[Tuple[int, ...]]c                 C  sF   | j r!t| j dr!td| j j}|r!tdd |dddD S d S )N__version__z(\d+)\.(\d+)(?:\.(\d+))?c                 s  s     | ]}|d urt |V  qd S rC   )ry   ).0xr)   r)   r*   	<genexpr>   s    zKMySQLDialect_mysqlconnector._mysqlconnector_version_info.<locals>.<genexpr>r
      r   )rz   hasattrrematchr   tuplegroup)r(   mr)   r)   r*   _mysqlconnector_version_info   s
   z8MySQLDialect_mysqlconnector._mysqlconnector_version_info
connectionr   r6   c                 C  s   |j jS rC   )r   charset)r(   r   r)   r)   r*   _detect_charset   s   z+MySQLDialect_mysqlconnector._detect_charset	exceptionBaseExceptionry   c                 C  s   |j S rC   )errno)r(   r   r)   r)   r*   _extract_error_code   s   z/MySQLDialect_mysqlconnector._extract_error_codeer   7Optional[Union[PoolProxiedConnection, DBAPIConnection]]r&   Optional[DBAPICursor]c                 C  sJ   d}| j j| j j| j jf}t||r#|j|v p"dt|v p"dt|v S dS )N)i  i  i  i  i  i   zMySQL Connection not available.z$Connection to MySQL is not availableF)loaded_dbapiOperationalErrorInterfaceErrorProgrammingError
isinstancer   r6   )r(   r   r   r&   errnos
exceptionsr)   r)   r*   is_disconnect   s   



z)MySQLDialect_mysqlconnector.is_disconnectNrpCursorResult[Tuple[Any, ...]]r   Optional[str]Sequence[Row[Tuple[Any, ...]]]c                 C     |  S rC   )fetchallr(   r   r   r)   r)   r*   _compat_fetchall      z,MySQLDialect_mysqlconnector._compat_fetchallOptional[Row[Tuple[Any, ...]]]c                 C  r   rC   )fetchoner   r)   r)   r*   _compat_fetchone  r   z,MySQLDialect_mysqlconnector._compat_fetchone
dbapi_connSequence[IsolationLevel]c                 C  r>   )N)SERIALIZABLEzREAD UNCOMMITTEDzREAD COMMITTEDzREPEATABLE READ
AUTOCOMMITr)   )r(   r   r)   r)   r*   get_isolation_level_values  rT   z6MySQLDialect_mysqlconnector.get_isolation_level_valueslevelr   rB   c                   s*   |dkr	d|_ d S d|_ t || d S )Nr   TF)rd   superset_isolation_level)r(   r]   r   	__class__r)   r*   r     s   
z/MySQLDialect_mysqlconnector.set_isolation_level)r    r   )r]   r   r    r=   )r`   r   r    r   )r    r   )r   r   r    r6   )r   r   r    ry   )r   r   r   r   r&   r   r    r=   rC   )r   r   r   r   r    r   )r   r   r   r   r    r   )r   r   r    r   )r]   r   r   r   r    rB   )&r.   r/   r0   driversupports_statement_cachesupports_sane_rowcountsupports_sane_multi_rowcountsupports_native_decimalsupports_native_bitsupports_server_side_cursorsdefault_paramstyler1   statement_compilerr   execution_ctx_clsrL   rX   __annotations__r   update_copyr   colspecsr   rP   classmethodr\   r_   r   memoized_propertyr   r   r   r   r   r   r   r   __classcell__r)   r)   r   r*   rU      s:   
 


1




rU   c                   @  s   e Zd ZdZdZeZdS )MariaDBDialect_mysqlconnectorTFN)r.   r/   r0   r   _allows_uuid_bindsrO   rX   r)   r)   r)   r*   r   "  s    r   )3__doc__
__future__r   r   typingr   r   r   r   r   r   r	   baser   r   r   r   r   mariadbr   typesr    r   engine.baser   engine.cursorr   engine.interfacesr   r   r   r   r   r   
engine.rowr   
engine.urlr   sql.elementsr   r   r1   r<   rL   rO   rP   rU   r   rQ   mariadb_dialectr)   r)   r)   r*   <module>   s^   '

 
