o
     h                     @  s   d 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r^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eZdS )a  

.. dialect:: mysql+pymysql
    :name: PyMySQL
    :dbapi: pymysql
    :connectstring: mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
    :url: https://pymysql.readthedocs.io/

Unicode
-------

Please see :ref:`mysql_unicode` for current recommendations on unicode
handling.

.. _pymysql_ssl:

SSL Connections
------------------

The PyMySQL DBAPI accepts the same SSL arguments as that of MySQLdb,
described at :ref:`mysqldb_ssl`.   See that section for additional examples.

If the server uses an automatically-generated certificate that is self-signed
or does not match the host name (as seen from the client), it may also be
necessary to indicate ``ssl_check_hostname=false`` in PyMySQL::

    connection_uri = (
        "mysql+pymysql://scott:tiger@192.168.0.134/test"
        "?ssl_ca=/home/gord/client-ssl/ca.pem"
        "&ssl_cert=/home/gord/client-ssl/client-cert.pem"
        "&ssl_key=/home/gord/client-ssl/client-key.pem"
        "&ssl_check_hostname=false"
    )

MySQL-Python Compatibility
--------------------------

The pymysql DBAPI is a pure Python port of the MySQL-python (MySQLdb) driver,
and targets 100% compatibility.   Most behavioral notes for MySQL-python apply
to the pymysql driver as well.

    )annotations)Any)Dict)Optional)TYPE_CHECKING)Union   )MySQLDialect_mysqldb   )langhelpers)Literal)ConnectArgsType)DBAPIConnection)DBAPICursor)DBAPIModule)PoolProxiedConnection)URLc                      s~   e Zd ZdZdZdZejd&ddZe	d'd	d
Z
ejd&ddZd(ddZ	d)d* fddZd+ fdd Zd,d$d%Z  ZS )-MySQLDialect_pymysqlpymysqlTNreturnboolc              	   C  s2   zt dj}|j| _W dS  ttfy   Y dS w )Nzpymysql.cursorsTF)
__import__cursorsSSCursor	_sscursorImportErrorAttributeError)selfr    r   T/var/www/html/venv/lib/python3.10/site-packages/sqlalchemy/dialects/mysql/pymysql.pysupports_server_side_cursorsN   s   
z1MySQLDialect_pymysql.supports_server_side_cursorsr   c                 C  s   t dS )Nr   )r   )clsr   r   r   import_dbapiW   s   z!MySQLDialect_pymysql.import_dbapic              	   C  sx   zt djj}W n ttfy   Y dS w t|j}z|jd }W n
 t	y,   Y dS w |dko;|j
 p;|j
d duS )a  determine if pymysql has deprecated, changed the default of,
        or removed the 'reconnect' argument of connection.ping().

        See #10492 and
        https://github.com/PyMySQL/mysqlclient/discussions/651#discussioncomment-7308971
        for background.

        zpymysql.connectionsTr   F	reconnectr   )r   connections
Connectionr   r   r   get_callable_argspecpingargs
IndexErrordefaults)r   r%   inspreconnect_argr   r   r   _send_false_to_ping[   s$   z(MySQLDialect_pymysql._send_false_to_pingdbapi_connectionr   Literal[True]c                 C  s    | j r
|d dS |  dS )NFT)r-   r'   )r   r.   r   r   r   do_pingw   s
   
zMySQLDialect_pymysql.do_pingurlr   _translate_argsOptional[Dict[str, Any]]r   c                   s"   |d u r	t dd}t j||dS )Nuser)username)r2   )dictsupercreate_connect_args)r   r1   r2   	__class__r   r   r8      s
   
z(MySQLDialect_pymysql.create_connect_argseDBAPIModule.Error
connection7Optional[Union[PoolProxiedConnection, DBAPIConnection]]cursorOptional[DBAPICursor]c                   sB   t  |||r
dS t|| jjrt| }d|v pd|v S dS )NTzalready closedzconnection was killedF)r7   is_disconnect
isinstanceloaded_dbapiErrorstrlower)r   r;   r=   r?   str_er9   r   r   rA      s   z"MySQLDialect_pymysql.is_disconnect	exceptionBaseExceptionr   c                 C  s$   t |jd tr|jd }|jd S )Nr   )rB   r(   	Exception)r   rH   r   r   r   _extract_error_code   s   

z(MySQLDialect_pymysql._extract_error_code)r   r   )r   r   )r.   r   r   r/   )N)r1   r   r2   r3   r   r   )r;   r<   r=   r>   r?   r@   r   r   )rH   rI   r   r   )__name__
__module____qualname__driversupports_statement_cachedescription_encodingr   memoized_propertyr    classmethodr"   r-   r0   r8   rA   rK   __classcell__r   r   r9   r   r   H   s    
		r   N)__doc__
__future__r   typingr   r   r   r   r   mysqldbr	   utilr   util.typingr   engine.interfacesr   r   r   r   r   
engine.urlr   r   dialectr   r   r   r   <module>   s&   *V