a
    c	g                     @   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eZeZdS )aH  


.. dialect:: mysql+pyodbc
    :name: PyODBC
    :dbapi: pyodbc
    :connectstring: mysql+pyodbc://<username>:<password>@<dsnname>
    :url: https://pypi.org/project/pyodbc/

.. note::

    The PyODBC for MySQL dialect is **not tested as part of
    SQLAlchemy's continuous integration**.
    The recommended MySQL dialects are mysqlclient and PyMySQL.
    However, if you want to use the mysql+pyodbc dialect and require
    full support for ``utf8mb4`` characters (including supplementary
    characters like emoji) be sure to use a current release of
    MySQL Connector/ODBC and specify the "ANSI" (**not** "Unicode")
    version of the driver in your DSN or connection string.

Pass through exact pyodbc connection string::

    import urllib
    connection_string = (
        'DRIVER=MySQL ODBC 8.0 ANSI Driver;'
        'SERVER=localhost;'
        'PORT=3307;'
        'DATABASE=mydb;'
        'UID=root;'
        'PWD=(whatever);'
        'charset=utf8mb4;'
    )
    params = urllib.parse.quote_plus(connection_string)
    connection_uri = "mysql+pyodbc:///?odbc_connect=%s" % params

    N   )MySQLDialect)MySQLExecutionContext)TIME   )exc)util)PyODBCConnector)Timec                   @   s   e Zd Zdd ZdS )_pyodbcTIMEc                 C   s   dd }|S )Nc                 S   s   | S N )valuer   r   c/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/sqlalchemy/dialects/mysql/pyodbc.pyprocess<   s    z-_pyodbcTIME.result_processor.<locals>.processr   )selfdialectZcoltyper   r   r   r   result_processor;   s    z_pyodbcTIME.result_processorN)__name__
__module____qualname__r   r   r   r   r   r   :   s   r   c                   @   s   e Zd Zdd ZdS )MySQLExecutionContext_pyodbcc                 C   s*   |   }|d | d }|  |S )NzSELECT LAST_INSERT_ID()r   )Zcreate_cursorexecuteZfetchoneclose)r   cursorZ	lastrowidr   r   r   get_lastrowidD   s
    
z*MySQLExecutionContext_pyodbc.get_lastrowidN)r   r   r   r   r   r   r   r   r   C   s   r   c                       sV   e Zd ZdZeejee	iZdZ
eZdZdd Zdd Zdd Z fd	d
Z  ZS )MySQLDialect_pyodbcTZMySQLc                 C   sD   d| _ z| |d}|r|W S W n tjy4   Y n0 td dS )z:Sniff out the character set in use for connection results.NZcharacter_set_clientz@Could not detect the connection character set.  Assuming latin1.latin1)Z_connection_charsetZ_fetch_settingr   Z
DBAPIErrorr   warn)r   
connectionr   r   r   r   _detect_charsetT   s    
z#MySQLDialect_pyodbc._detect_charsetc                 C   s   t | |S r   )r   _get_server_version_info)r   r   r   r   r   r!   m   s    z,MySQLDialect_pyodbc._get_server_version_infoc                 C   s4   t dt|j}|d}|r,t|S d S d S )Nz	\((\d+)\)r   )recompilesearchstrargsgroupint)r   	exceptionmcr   r   r   _extract_error_codep   s
    
z'MySQLDialect_pyodbc._extract_error_codec                    s   t     fdd}|S )Nc                    sD    d ur |  d}d}| j |dd | j |dd | jdd d S )Nr   izutf-8)encoding)ZsetdecodingZsetencoding)connZpyodbc_SQL_CHARZpyodbc_SQL_WCHARZsuper_r   r   
on_connect{   s    z2MySQLDialect_pyodbc.on_connect.<locals>.on_connect)superr0   )r   r0   	__class__r/   r   r0   x   s    
zMySQLDialect_pyodbc.on_connect)r   r   r   Zsupports_statement_cacher   Zupdate_copyr   Zcolspecsr
   r   Zsupports_unicode_statementsr   Zexecution_ctx_clsZpyodbc_driver_namer    r!   r,   r0   __classcell__r   r   r2   r   r   L   s   r   )__doc__r"   baser   r   typesr    r   r   Zconnectors.pyodbcr	   Zsql.sqltypesr
   r   r   r   r   r   r   r   r   <module>
   s   %		>