B
    \                 @   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 )aK  
.. dialect:: firebird+kinterbasdb
    :name: kinterbasdb
    :dbapi: kinterbasdb
    :connectstring: firebird+kinterbasdb://user:password@host:port/path/to/db[?key=value&key=value...]
    :url: http://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::

    http://sourceforge.net/projects/kinterbasdb

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

    http://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decimalZDecimalstr)value r   Glib/python3.7/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_kinterbasdbN)r   r   r   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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kinterbasdbFT   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   dbapio   s    zFBDialect_kinterbasdb.dbapiNc             C   s   | ||pg  d S )N)Zexecute)r   r   Z	statementZ
parameterscontextr   r   r   
do_executes   s    z FBDialect_kinterbasdb.do_executec             C   s   | | j d S )N)Zrollbackr'   )r   dbapi_connectionr   r   r   do_rollbackx   s    z!FBDialect_kinterbasdb.do_rollbackc             C   s   | | j d S )N)Zcommitr'   )r   r0   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)ZusernameZportz%s/%sZhostr%   r&   initializedZ_initializedF)r%   r&   )Ztranslate_connect_argsr   updateZqueryr   Zcoerce_kw_typeintpopr%   r&   r-   getattrZinit)r   ZurlZoptsr%   r&   r4   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   r:   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   )r6   ).0xr   r   r   
<listcomp>   s    z=FBDialect_kinterbasdb._parse_version_info.<locals>.<listcomp>         Zfirebirdc             S   s   g | ]}t |qS r   )r6   )r?   r@   r   r   r   rA      s    r      r   Z	interbase)r   AssertionErrorgrouptuple)r   r<   mr   r   r   r;      s    
"z)FBDialect_kinterbasdb._parse_version_infoc             C   sF   t || jj| jjfr>t|}d|kp<d|kp<d|kp<d|kS dS d S )Nz*Unable to complete network request to hostzInvalid connection statezInvalid cursor statezconnection shutdownF)r
   r-   ZOperationalErrorZProgrammingErrorr   )r   er:   r   msgr   r   r   is_disconnect   s    z#FBDialect_kinterbasdb.is_disconnect)r"   r   TF)N)r   r   r   Zdriverr(   Zsupports_sane_multi_rowcountr   Zexecution_ctx_clsZsupports_native_decimalr   Zupdate_copyr   ZcolspecssqltypesNumericr   Floatr   r$   classmethodr-   r/   r1   r2   r9   r=   r;   rL   __classcell__r   r   )r*   r   r    O   s*      
r    )__doc__r   rer   baser   r    r   rM   r   objectr	   rN   r   rO   r   r   r    r   r   r   r   r   <module>&   s   y