U
    ccO                     @   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   L/tmp/pip-unpacked-wheel-8u86ls_i/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	rt| j
dd }|d krt| 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 )zGet 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    {