o
    FDi                     @   sf   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	Z	d
S )    )Optional)CursorNotSupportedError)	constants)
Connection)ConnectionStatus)	parse_dsnc                   @   s~  e Zd Zdejejejejeje	ddf	de
e dededededed	ed
efddZd6ddZd7ddZdefddZede
e fddZedefddZedefddZedefddZedefddZedefddZedefd d!Zedefd"d#Zedefd$d%Zd7d&d'Zd(d) Zd7d*d+Zd8dd,d-e
ee	  d	ede	fd.d/Z d7d0d1Z!d7d2d3Z"d4d5 Z#dS )9r   NFdsnuserpasswordhostportdatabaseechostack_trackc
              	   K   s   |r=t |}td	i |d|	i|
| _|dd p|}|dd p#|}|dd p+|}|dd p3|}|dd p;|}ntd	||||||	d|
| _|| _|| _|| _|| _|| _|| _	d| _
d| _|| _|
| _|| _d S )
Nr   r   r   r   r   r   )r   r   r   r   r   r   F )r	   ProtoConnection_connectionget_dsn_user	_password_host_port	_database_opened_closed_cursor_cls_connection_kwargs_echo)selfr
   r   r   r   r   r   
cursor_clsr   r   kwargsconfigr   r   J/var/www/Datamplify/venv/lib/python3.10/site-packages/asynch/connection.py__init__   s:   	
zConnection.__init__returnc                    s   |   I d H  | S N)connectr!   r   r   r%   
__aenter__9   s   zConnection.__aenter__c                    s   |   I d H  d S r(   )close)r!   exc_typeexc_valexc_tbr   r   r%   	__aexit__=   s   zConnection.__aexit__c                 C   s.   t | j}| j}d| dt| dd| dS )N<z object at 0xxz
; status: >)type__name__statusid)r!   cls_namer6   r   r   r%   __repr__@   s   
zConnection.__repr__c                 C      | j S )ztReturn True if the connection is opened.

        :returns: the connection open status
        :rtype: bool
        )r   r*   r   r   r%   openedE      zConnection.openedc                 C   r:   )zuReturn True if the connection is closed.

        :returns: the connection close status
        :rtype: bool
        )r   r*   r   r   r%   closedO   r<   zConnection.closedc                 C   sD   | j s	| js	tjS | j r| jstjS | jr| j stjS t|  d)zReturn the status of the connection.

        :raise ConnectionError: an unresolved connection state
        :return: the Connection object status
        :rtype: str (ConnectionStatus StrEnum)
        z is in an unknown state)r   r   r   createdr;   r=   ConnectionErrorr*   r   r   r%   r6   Y   s   	zConnection.statusc                 C   r:   r(   )r   r*   r   r   r%   r   j      zConnection.hostc                 C   r:   r(   )r   r*   r   r   r%   r   n   r@   zConnection.portc                 C   r:   r(   )r   r*   r   r   r%   r   r   r@   zConnection.userc                 C   r:   r(   )r   r*   r   r   r%   r   v   r@   zConnection.passwordc                 C   r:   r(   )r   r*   r   r   r%   r   z   r@   zConnection.databasec                 C   r:   r(   )r    r*   r   r   r%   r   ~   r@   zConnection.echoc                    s2   | j rdS | jr| j I dH  d| _d| _ dS )zClose the connection.NFT)r   r   r   
disconnectr*   r   r   r%   r,      s   
zConnection.closec                       t r(   r   r*   r   r   r%   commit      zConnection.commitc                    s6   | j rd S | j I d H  d| _ | jrd| _d S d S )NTF)r   r   r)   r   r*   r   r   r%   r)      s   
zConnection.connect)r   cursorc                C   s   |p| j }|| |p| jS )a=  Return the cursor object for the connection.

        When a parameter is interpreted as True,
        it takes precedence over the corresponding default value.
        If the `cursor` is None, but the `echo` is True,
        then a default Cursor instance will be created
        with echoing even if the `self.echo` returns False.

        :param cursor Optional[type[Cursor]]: Cursor factory class
        :param echo bool: to override the `Connection.echo` parameter for a cursor

        :return: a cursor object of the given connection
        :rtype: Cursor
        )r   r   )r!   rE   r   r"   r   r   r%   rE      s   
zConnection.cursorc                    s(   | j  I dH sd|  }t|dS )zvCheck the connection liveliness.

        :raises ConnectionError: if ping() has failed
        :return: None
        NzPing has failed for )r   pingr?   r!   msgr   r   r%   rF      s
   
zConnection.pingc                    sz   | j tjkrd|  d}t|| j tjkr!d|  d}t|z
|  I dH  W dS  ty<   |  I dH  Y dS w )al  Refresh the connection.

        Attempting to ping and if failed,
        then trying to connect again.
        If the reconnection does not work,
        an Exception is propagated.

        :raises ConnectionError:
            1. refreshing created, i.e., not opened connection
            2. refreshing already closed connection

        :return: None
        zthe z is not opened to be refreshedz is already closedN)r6   r   r>   r?   r=   rF   r)   rG   r   r   r%   _refresh   s   zConnection._refreshc                    rB   r(   r   r*   r   r   r%   rollback   rD   zConnection.rollback)r'   r   )r'   Nr(   )$r5   
__module____qualname__r   DEFAULT_USERDEFAULT_PASSWORDDEFAULT_HOSTDEFAULT_PORTDEFAULT_DATABASEr   r   strintboolr&   r+   r0   r9   propertyr;   r=   r6   r   r   r   r   r   r   r,   rC   r)   r4   rE   rF   rI   rJ   r   r   r   r%   r      sn    	


-
		


&

r   N)typingr   asynch.cursorsr   asynch.errorsr   asynch.protor   asynch.proto.connectionr   r   asynch.proto.models.enumsr   asynch.proto.utils.dsnr	   r   r   r   r%   <module>   s    