U
    ÌZjO  ã                   @   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
 G d
d„ deƒZG dd„ dee	jƒZG dd„ dee	jƒZG dd„ deƒZG dd„ deƒZeZdS )aO  
.. dialect:: firebird+kinterbasdb
    :name: kinterbasdb
    :dbapi: kinterbasdb
    :connectstring: firebird+kinterbasdb://user:password@host:port/path/to/db[?key=value&key=value...]
    :url: https://firebirdsql.org/index.php?op=devel&sub=python

Arguments
----------

The Kinterbasdb backend accepts the ``enable_rowcount`` and ``retaining``
arguments accepted by the :mod:`sqlalchemy.dialects.firebird.fdb` dialect.
In addition, it also accepts the following:

* ``type_conv`` - select the kind of mapping done on the types: by default
  SQLAlchemy uses 200 with Unicode, datetime and decimal support.  See
  the linked documents below for further information.

* ``concurrency_level`` - set the backend policy with regards to threading
  issues: by default SQLAlchemy uses policy 1.  See the linked documents
  below for further information.

.. seealso::

    https://sourceforge.net/projects/kinterbasdb

    https://kinterbasdb.sourceforge.net/dist_docs/usage.html#adv_param_conv_dynamic_type_translation

    https://kinterbasdb.sourceforge.net/dist_docs/usage.html#special_issue_concurrency

é    N)Úmatché   )Ú	FBDialect)ÚFBExecutionContexté   )Útypes)Úutilc                   @   s   e Zd Zdd„ ZdS )Ú_kinterbasdb_numericc                 C   s   dd„ }|S )Nc                 S   s   t | tjƒrt| ƒS | S d S ©N)Ú
isinstanceÚdecimalÚDecimalÚstr)Úvalue© r   úk/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/sqlalchemy/dialects/firebird/kinterbasdb.pyÚprocess3   s    z4_kinterbasdb_numeric.bind_processor.<locals>.processr   )ÚselfÚdialectr   r   r   r   Úbind_processor2   s    z#_kinterbasdb_numeric.bind_processorN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r	   1   s   r	   c                   @   s   e Zd ZdS )Ú_FBNumeric_kinterbasdbN©r   r   r   r   r   r   r   r   <   s   r   c                   @   s   e Zd ZdS )Ú_FBFloat_kinterbasdbNr   r   r   r   r   r   @   s   r   c                   @   s   e Zd Zedd„ ƒZdS )ÚFBExecutionContext_kinterbasdbc                 C   s"   | j  d| jj¡r| jjS dS d S )NÚenable_rowcountéÿÿÿÿ)Zexecution_optionsÚgetr   r   ÚcursorÚrowcount)r   r   r   r   r!   E   s     ÿz'FBExecutionContext_kinterbasdb.rowcountN)r   r   r   Úpropertyr!   r   r   r   r   r   D   s   r   c                       s–   e Zd ZdZdZdZdZeZdZ	e
 ejejeejei¡Zd‡ fdd„	Zedd	„ ƒZddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚFBDialect_kinterbasdbÚkinterbasdbTFéÈ   r   c                    s8   t t| ƒjf |Ž || _|| _|| _|| _|r4d| _d S )NT)Úsuperr#   Ú__init__r   Ú	type_convÚconcurrency_levelÚ	retainingÚsupports_sane_rowcount)r   r(   r)   r   r*   Úkwargs©Ú	__class__r   r   r'   `   s    zFBDialect_kinterbasdb.__init__c                 C   s   t dƒS )Nr$   )Ú
__import__)Úclsr   r   r   Údbapip   s    zFBDialect_kinterbasdb.dbapiNc                 C   s   |  ||pg ¡ d S r
   )Úexecute)r   r    Z	statementÚ
parametersÚcontextr   r   r   Ú
do_executet   s    z FBDialect_kinterbasdb.do_executec                 C   s   |  | j¡ d S r
   )Úrollbackr*   ©r   Zdbapi_connectionr   r   r   Údo_rollbacky   s    z!FBDialect_kinterbasdb.do_rollbackc                 C   s   |  | j¡ d S r
   )Úcommitr*   r7   r   r   r   Ú	do_commit|   s    zFBDialect_kinterbasdb.do_commitc                 C   s´   |j dd�}| d¡r4d|d |d f |d< |d= | |j¡ t |dt¡ | d| j¡}| d| j	¡}| j
d k	r¬t| j
dd ƒ}|d kr˜t| j
d	d
ƒ}|s¬| j
j||d� g |fS )NÚuser)ÚusernameÚportz%s/%sÚhostr(   r)   ÚinitializedÚ_initializedF)r(   r)   )Ztranslate_connect_argsr   ÚupdateÚqueryr   Zcoerce_kw_typeÚintÚpopr(   r)   r1   ÚgetattrÚinit)r   ÚurlÚoptsr(   r)   r?   r   r   r   Úcreate_connect_args   s*    
 ÿ
 ÿz)FBDialect_kinterbasdb.create_connect_argsc                 C   s   |j }|j}|  |¡S )zÆGet the version of the Firebird server used by a connection.

        Returns a tuple of (`major`, `minor`, `build`), three integers
        representing the version of the attached server.
        )Ú
connectionZserver_versionÚ_parse_version_info)r   rJ   ZfbconnÚversionr   r   r   Ú_get_server_version_infoš   s    z.FBDialect_kinterbasdb._get_server_version_infoc                 C   sp   t d|ƒ}|std| ƒ‚| d¡d krJtdd„ | ddd¡D ƒd	g ƒS td
d„ | ddd¡D ƒdg ƒS d S )Nz3\w+-V(\d+)\.(\d+)\.(\d+)\.(\d+)( \w+ (\d+)\.(\d+))?z,Could not determine version from string '%s'é   c                 S   s   g | ]}t |ƒ‘qS r   ©rC   ©Ú.0Úxr   r   r   Ú
<listcomp>¶   s     z=FBDialect_kinterbasdb._parse_version_info.<locals>.<listcomp>é   é   é   Zfirebirdc                 S   s   g | ]}t |ƒ‘qS r   rO   rP   r   r   r   rS   ¸   s     r   é   r   Z	interbase)r   ÚAssertionErrorÚgroupÚtuple)r   rL   Úmr   r   r   rK   ¬   s     ÿÿ"z)FBDialect_kinterbasdb._parse_version_infoc                 C   sN   t || jj| jjfƒrFt|ƒ}d|kpDd|kpDd|kpDd|kpDd|kS dS d S )Nz$Error writing data to the connectionz*Unable to complete network request to hostzInvalid connection statezInvalid cursor statezconnection shutdownF)r   r1   ZOperationalErrorZProgrammingErrorr   )r   ÚerJ   r    Úmsgr   r   r   Úis_disconnectº   s     ÿÿþýûz#FBDialect_kinterbasdb.is_disconnect)r%   r   TF)N)r   r   r   ZdriverZsupports_statement_cacher+   Zsupports_sane_multi_rowcountr   Zexecution_ctx_clsZsupports_native_decimalr   Zupdate_copyr   ZcolspecsÚsqltypesÚNumericr   ÚFloatr   r'   Úclassmethodr1   r5   r8   r:   rI   rM   rK   r^   Ú__classcell__r   r   r-   r   r#   O   s8     þþ
    û

r#   )Ú__doc__r   Úrer   Úbaser   r   Ú r   r_   r   Úobjectr	   r`   r   ra   r   r   r#   r   r   r   r   r   Ú<module>   s    {