U
    dª/eØ  ã                   @   sD   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	dS )	é   )ÚClient)Údefinesé   )ÚCursor)ÚInterfaceErrorc                       sx   e Zd ZdZddejejejejf‡ fdd„	Z	dd„ Z
dd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zddd„Z‡  ZS )Ú
Connectiona)  
    Creates new Connection for accessing ClickHouse database.

    Connection is just wrapper for handling multiple cursors (clients) and
    do not initiate actual connections to the ClickHouse server.

    See parameters description in
    :data:`~clickhouse_driver.connection.Connection`.
    Nc                    sN   g | _ || _|| _|| _|| _|| _|| _|| _d| _d | _	t
t| ƒ ¡  d S )NF)ÚcursorsÚdsnÚuserÚpasswordÚhostÚportÚdatabaseÚconnection_kwargsÚ	is_closedÚ_hostsÚsuperr   Ú__init__)Úselfr	   r   r
   r   r   r   Úkwargs©Ú	__class__© úF/tmp/pip-unpacked-wheel-mds_o03w/clickhouse_driver/dbapi/connection.pyr      s    zConnection.__init__c                 C   s   d  t| ƒ| j¡S )Nz,<connection object at 0x{0:x}; closed: {1:}>)ÚformatÚidr   ©r   r   r   r   Ú__repr__"   s     ÿzConnection.__repr__c                 C   s   | S ©Nr   r   r   r   r   Ú	__enter__(   s    zConnection.__enter__c                 C   s   |   ¡  d S r   )Úclose)r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__+   s    zConnection.__exit__c                 C   s<   | j dk	rt | j ¡S t| jf| j| j| j| jdœ| j—ŽS )z1
        :return: a new Client instance.
        N)r   r
   r   r   )	r	   r   Zfrom_urlr   r   r
   r   r   r   r   r   r   r   Ú_make_client.   s    
 þþzConnection._make_clientc                 C   s   | j D ]}| ¡  qd| _dS )aM  
        Close the connection now. The connection will be unusable from this
        point forward; an :data:`~clickhouse_driver.dbapi.Error` (or subclass)
        exception will be raised if any operation is attempted with the
        connection. The same applies to all cursor objects trying to use the
        connection.
        TN)r   r    r   )r   Úcursorr   r   r   r    9   s    

zConnection.closec                 C   s   dS ©zB
        Do nothing since ClickHouse has no transactions.
        Nr   r   r   r   r   ÚcommitF   s    zConnection.commitc                 C   s   dS r'   r   r   r   r   r   ÚrollbackL   s    zConnection.rollbackc                 C   sX   | j rtdƒ‚|  ¡ }| jdkr,|jj| _n
| j|j_|p<t}||| ƒ}| j |¡ |S )z·
        :param cursor_factory: Argument can be used to create non-standard
                               cursors.
        :return: a new cursor object using the connection.
        zconnection already closedN)	r   r   r%   r   Ú
connectionÚhostsr   r   Úappend)r   Zcursor_factoryÚclientr&   r   r   r   r&   R   s    


zConnection.cursor)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZDEFAULT_USERZDEFAULT_PASSWORDZDEFAULT_PORTZDEFAULT_DATABASEr   r   r   r$   r%   r    r(   r)   r&   Ú__classcell__r   r   r   r   r      s   	  þr   N)
r-   r   Ú r   r&   r   Úerrorsr   Úobjectr   r   r   r   r   Ú<module>   s   