U
    r\jH                     @   s@   d 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.

   )MySQLDialect_mysqldb   )langhelpers)py3kc                       sz   e Zd ZdZdZdZdZdZej	dd Z
edd Zej	dd	 Zd
d Zd fdd	Z fddZerrdd Z  ZS )MySQLDialect_pymysqlpymysqlTNc              	   C   s8   zt dj}|j| _W dS  ttfk
r2   Y dS X d S )Nzpymysql.cursorsTF)
__import__cursorsZSSCursorZ	_sscursorImportErrorAttributeError)selfr	    r   X/root/pica-gateway/venv/lib/python3.8/site-packages/sqlalchemy/dialects/mysql/pymysql.pysupports_server_side_cursorsE   s    
z1MySQLDialect_pymysql.supports_server_side_cursorsc                 C   s   t dS )Nr   )r   )clsr   r   r   dbapiN   s    zMySQLDialect_pymysql.dbapic              	   C   s   zt djj}W n ttfk
r*   Y dS X t|j}z|jd }W n t	k
r\   Y dS X |dkoz|j
 pz|j
d dk	S d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   FZ	reconnect    N)r   connections
Connectionr
   r   r   Zget_callable_argspecpingargs
IndexErrordefaults)r   r   ZinspZreconnect_argr   r   r   _send_false_to_pingR   s    z(MySQLDialect_pymysql._send_false_to_pingc                 C   s   | j r|d n|  dS )NFT)r   r   )r   Zdbapi_connectionr   r   r   
_ping_impln   s    zMySQLDialect_pymysql._ping_implc                    s&   |d krt dd}tt| j||dS )Nuser)username)_translate_args)dictsuperr   create_connect_args)r   urlr   	__class__r   r   r    v   s    

 z(MySQLDialect_pymysql.create_connect_argsc                    sJ   t t| |||rdS t|| jjrBt| }d|kp@d|kS dS d S )NTzalready closedzconnection was killedF)r   r   is_disconnect
isinstancer   Errorstrlower)r   e
connectioncursorZstr_er"   r   r   r$   }   s    
  z"MySQLDialect_pymysql.is_disconnectc                 C   s$   t |jd tr|jd }|jd S )Nr   )r%   r   	Exception)r   	exceptionr   r   r   _extract_error_code   s    
z(MySQLDialect_pymysql._extract_error_code)N)__name__
__module____qualname__ZdriverZsupports_statement_cacheZdescription_encodingZsupports_unicode_statementsZsupports_unicode_bindsr   Zmemoized_propertyr   classmethodr   r   r   r    r$   r   r.   __classcell__r   r   r"   r   r   9   s    


r   N)__doc__Zmysqldbr   utilr   r   r   dialectr   r   r   r   <module>   s
   ,Y