o
    6	fc7                     @   s  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZmZmZ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mZmZmZmZmZ ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z.m/Z/m0Z0 ddl-m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZB erddlCmDZD ejEjFZFejEjGZGejHjIZIejHjJZJeKdZLG dd de8e" ZMdS )z"
psycopg async connection objects
    N)TracebackType)AnyAsyncGeneratorAsyncIteratorListOptional)TypeUnioncastoverloadTYPE_CHECKING)asynccontextmanager   )pq)errors)waiting)AdaptContextParamsPQGen	PQGenConnQueryRV)Xid)RowAsyncRowFactory	tuple_rowTupleRowargs_row)AdaptersMap)IsolationLevel)Self)ConnDictmake_conninfoconninfo_to_dict)conninfo_attempts_asynctimeout_from_conninfo)AsyncPipeline)pgconn_encoding)BaseConnection	CursorRowNotify)notifies)AsyncTransaction)AsyncCursor)AsyncServerCursor)PGconnpsycopgc                       s  e Zd ZU dZdZeee  ed< eee  ed< e	e ed< e
e ed< ee	e efddde	e f fd	d
Zee	dmddddddedede
e de	e de
eee   de
e dedeef ddfddZee	dmddddddedede
e de
eee   de
e dedeef ddfddZe	dmdddddddedede
e de
e de
e	e  de
eee   dedefddZdefddZde
ee  d e
e d!e
e ddfd"d#Zedededefd$d%Zdnd&d'Zedd(d)edee fd*d+Z edd(d)ede	e! dee! fd,d+Z edddd-d.ed)ed/e
e d0edee f
d1d+Z edddd-d.ed)ede	e! d/e
e d0edee! fd2d+Z 	dmddddd3d.ed)ede
e	e  d/e
e d0edeee ee f fd4d+Z 	doddd5d6e"d7e
e# d8e
e d)edee f
d9d:Z$dnd;d<Z%dnd=d>Z&e'		dpd?e
e d@ede(e) fdAdBZ*de+e,df fdCdDZ-e'de(e fdEdFZ.dqdHe/e0 dIe
e1 de0fdJdKZ2edHe3e0 dIe
e de0fdLdMZ4dNeddfdOdPZ5dNeddfdQdRZ6dNe
e7 ddfdSdTZ8dNe
e7 ddfdUdVZ9dNe
e ddfdWdXZ:dNe
e ddfdYdZZ;dNe
e ddfd[d\Z<dNe
e ddfd]d^Z=d_eddfd`daZ>dbee?ef ddfdcddZ@dndedfZAdodbee?edf ddfdgdhZBdodbee?edf ddfdidjZCdeDe? fdkdlZE  ZFS )rAsyncConnectionz@
    Asynchronous wrapper for a connection to the database.
    r0   cursor_factoryserver_cursor_factoryrow_factory	_pipelinepgconnr/   c                    s,   t  | || _t | _t| _t| _	d S N)
super__init__r4   asyncioLocklockr-   r2   r.   r3   )selfr6   r4   	__class__ Q/home/ertert/spirit/venv/lib/python3.10/site-packages/psycopg/connection_async.pyr9   :   s
   

zAsyncConnection.__init__ F   N)
autocommitprepare_thresholdr2   contextconninforD   rE   rF   kwargsreturnzAsyncConnection[Row]c                      d S r7   r@   )clsrG   rD   rE   r4   r2   rF   rH   r@   r@   rA   connectE   s   zAsyncConnection.connectzAsyncConnection[TupleRow]c                   rJ   r7   r@   )rK   rG   rD   rE   r2   rF   rH   r@   r@   rA   rL   W   s   )rD   rE   rF   r4   r2   c                   s@  t jdkrt }t|tjrtd| j|fi |I d H }	t	|	}
d }t
|	I d H }|D ]H}ztd	i |}| j| ||
dI d H }W  n/ tjyx } z"t|dkrltd|d|d|dt| |}W Y d }~q0d }~ww |s|sJ |d t||_|r||_|r||_|rt|j|_||_|S )
Nwin32zPsycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'timeoutr   zAconnection attempt failed on host: %r, port: %r, hostaddr: %r: %shostporthostaddrr@   )sysplatformr:   get_running_loop
isinstanceProactorEventLoopeInterfaceError_get_connection_paramsr%   r$   r"   
_wait_conn_connect_gen_NO_TRACEBACKlenloggerdebuggetstrwith_tracebackbool_autocommitr4   r2   r   adapters	_adaptersrE   )rK   rG   rD   rE   rF   r4   r2   rH   loopparamsrO   rvattemptsattemptexlast_exr@   r@   rA   rL   d   sP   


c                    s   | S r7   r@   r=   r@   r@   rA   
__aenter__   s   zAsyncConnection.__aenter__exc_typeexc_valexc_tbc              
      s   | j rd S |r+z	|  I d H  W n  ty* } ztd| | W Y d }~nd }~ww |  I d H  t| dd sA|  I d H  d S d S )Nz#error ignored in rollback on %s: %s_pool)closedrollback	Exceptionr_   warningcommitgetattrclose)r=   rq   rr   rs   exc2r@   r@   rA   	__aexit__   s$   zAsyncConnection.__aexit__c                    s   t |fi |S )z3Manipulate connection parameters before connecting.)r#   )rK   rG   rH   r@   r@   rA   rZ      s   z&AsyncConnection._get_connection_paramsc                    s    | j rd S d| _| j  d S )NT)ru   _closedr6   finishro   r@   r@   rA   r{      s
   zAsyncConnection.close)binaryr   c                C      d S r7   r@   )r=   r   r@   r@   rA   cursor   s   zAsyncConnection.cursorc                C   r   r7   r@   )r=   r   r4   r@   r@   rA   r      s   )r   
scrollablewithholdnamer   r   c                C   r   r7   r@   )r=   r   r   r   r   r@   r@   rA   r      s   c                C   r   r7   r@   )r=   r   r   r4   r   r   r@   r@   rA   r      s   	)r   r4   r   r   c                C   sH   |    |s	| j}|r| j| ||||d}n| j| |d}|r"t|_|S )z\
        Return a new `AsyncCursor` to send commands and queries to the connection.
        )r   r4   r   r   r4   )_check_connection_okr4   r3   r2   BINARYformat)r=   r   r   r4   r   r   curr@   r@   rA   r      s   )preparer   queryri   r   c             
      sR   z|   }|rt|_|j|||dI d H W S  tjy( } z|d d }~ww )N)r   )r   r   r   executerX   r]   rc   )r=   r   ri   r   r   r   rm   r@   r@   rA   r     s   
zAsyncConnection.executec              	      V   | j 4 I d H  | |  I d H  W d   I d H  d S 1 I d H s$w   Y  d S r7   )r<   wait_commit_genro   r@   r@   rA   ry        .zAsyncConnection.commitc              	      r   r7   )r<   r   _rollback_genro   r@   r@   rA   rv     r   zAsyncConnection.rollbacksavepoint_nameforce_rollbackc                 C  s  t | ||}| jrh|  4 I dH I |4 I dH , |  4 I dH  |V  W d  I dH  n1 I dH s6w   Y  W d  I dH  n1 I dH sKw   Y  W d  I dH  dS 1 I dH saw   Y  dS |4 I dH  |V  W d  I dH  dS 1 I dH sw   Y  dS )zw
        Start a context block with a new transaction or nested transaction.

        :rtype: AsyncTransaction
        N)r,   r5   pipeline)r=   r   r   txr@   r@   rA   transaction"  s   2.zAsyncConnection.transactionc                 C  s   	 | j 4 I d H + z| t| jI d H }W n tjy) } z|d d }~ww W d   I d H  n1 I d H s:w   Y  t| j}|D ]}t|j	
||j
||j}|V  qFqr7   )r<   r   r+   r6   rX   r]   rc   r'   r*   relnamedecodeextrabe_pid)r=   nsrm   encpgnnr@   r@   rA   r+   5  s    
(
zAsyncConnection.notifiesc                 C  s^  | j 4 I dH  |   | j}|du rt|  }| _W d  I dH  n1 I dH s+w   Y  zQ|4 I dH  |V  W d  I dH  n1 I dH sKw   Y  W |jdkr| j 4 I dH  || ju seJ d| _W d  I dH  dS 1 I dH syw   Y  dS dS |jdkr| j 4 I dH  || ju sJ d| _W d  I dH  w 1 I dH sw   Y  w )z<Context manager to switch the connection into pipeline mode.Nr   )r<   r   r5   r&   level)r=   r   r@   r@   rA   r   A  s,   ((
.*zAsyncConnection.pipeline皙?genrO   c                    st   zt j|| jj|dI d H W S  tjtfy9   | | j zt j|| jj|dI d H  W   tj	y8   Y  w w )NrN   )
r   
wait_asyncr6   socketr:   CancelledErrorKeyboardInterrupt_try_cancelrX   QueryCanceled)r=   r   rO   r@   r@   rA   r   U  s   zAsyncConnection.waitc                    s   t ||I d H S r7   )r   wait_conn_async)rK   r   rO   r@   r@   rA   r[   b  s   zAsyncConnection._wait_connvaluec                 C      |  d d S )NrD   _no_set_asyncr=   r   r@   r@   rA   _set_autocommitf     zAsyncConnection._set_autocommitc              	      X   | j 4 I dH  | | |I dH  W d  I dH  dS 1 I dH s%w   Y  dS )z5Async version of the `~Connection.autocommit` setter.N)r<   r   _set_autocommit_genr   r@   r@   rA   set_autocommiti     .zAsyncConnection.set_autocommitc                 C   r   )Nisolation_levelr   r   r@   r@   rA   _set_isolation_leveln  r   z$AsyncConnection._set_isolation_levelc              	      r   )z:Async version of the `~Connection.isolation_level` setter.N)r<   r   _set_isolation_level_genr   r@   r@   rA   set_isolation_levelq  r   z#AsyncConnection.set_isolation_levelc                 C   r   )N	read_onlyr   r   r@   r@   rA   _set_read_onlyv  r   zAsyncConnection._set_read_onlyc              	      r   )z4Async version of the `~Connection.read_only` setter.N)r<   r   _set_read_only_genr   r@   r@   rA   set_read_onlyy  r   zAsyncConnection.set_read_onlyc                 C   r   )N
deferrabler   r   r@   r@   rA   _set_deferrable~  r   zAsyncConnection._set_deferrablec              	      r   )z5Async version of the `~Connection.deferrable` setter.N)r<   r   _set_deferrable_genr   r@   r@   rA   set_deferrable  r   zAsyncConnection.set_deferrable	attributec                 C   s   t d|d| d)Nz'the zD property is read-only on async connections: please use 'await .set_z()' instead.)AttributeError)r=   r   r@   r@   rA   r     s
   zAsyncConnection._no_set_asyncxidc              	      sX   | j 4 I d H  | | |I d H  W d   I d H  d S 1 I d H s%w   Y  d S r7   )r<   r   _tpc_begin_genr=   r   r@   r@   rA   	tpc_begin  s   .zAsyncConnection.tpc_beginc              
      s   z,| j 4 I d H  | |  I d H  W d   I d H  W d S 1 I d H s&w   Y  W d S  tjyB } ztt|d d }~ww r7   )r<   r   _tpc_prepare_genrX   ObjectNotInPrerequisiteStateNotSupportedErrorrb   )r=   rm   r@   r@   rA   tpc_prepare  s   2zAsyncConnection.tpc_preparec              	      Z   | j 4 I d H  | | d|I d H  W d   I d H  d S 1 I d H s&w   Y  d S )Nry   r<   r   _tpc_finish_genr   r@   r@   rA   
tpc_commit     .zAsyncConnection.tpc_commitc              	      r   )Nrv   r   r   r@   r@   rA   tpc_rollback  r   zAsyncConnection.tpc_rollbackc              	      s   |    | jj}| jttjd4 I d H }|t I d H  |	 I d H }W d   I d H  n1 I d H s8w   Y  |t
krN| jjtkrN|  I d H  |S )Nr   )
_check_tpcinfotransaction_statusr   r   r   _from_recordr   _get_recover_queryfetchallIDLEINTRANSrv   )r=   statusr   resr@   r@   rA   tpc_recover  s   (zAsyncConnection.tpc_recover)rB   )rI   Nr7   )NF)r   )G__name__
__module____qualname____doc__r   r-   r   __annotations__r.   r   r   r&   r
   r   r9   r   classmethodrb   rd   intr   r	   rL   r   r    rp   BaseExceptionr   r}   r!   rZ   r{   r   r)   r   r   r   ry   rv   r   r   r,   r   r   r*   r+   r   r   r   floatr   r   r[   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r@   r@   r>   rA   r1   .   s  
 	
	
	
8


		

%


  
r1   )Nr   rS   r:   loggingtypesr   typingr   r   r   r   r   r   r	   r
   r   r   
contextlibr   rB   r   r   rX   r   abcr   r   r   r   r   r   _tpcr   rowsr   r   r   r   r   adaptr   _enumsr   _compatr    rG   r!   r"   r#   r$   r%   r5   r&   
_encodingsr'   
connectionr(   r)   r*   
generatorsr+   r   r,   cursor_asyncr-   server_cursorr.   pq.abcr/   FormatTEXTr   TransactionStatusr   r   	getLoggerr_   r1   r@   r@   r@   rA   <module>   sD     
