
     hZ$                       U d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z ddl	mZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlmZmZmZ ddlmZ er&ddlmZ ddl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Z)de*d<   ejV                  jX                  Z,ejV                  jZ                  Z-ej\                  j^                  Z/ej`                  jb                  Z1 ejd                  d      Z3 G d d      Z4 G d de4      Z5 G d de4      Z6y)z
commands pipeline management
    )annotationsN)TracebackType)TYPE_CHECKINGAny   )errors)pq)PipelineCommandPQGen)DequeSelf	TypeAlias)connection_summary)
fetch_manypipeline_communicatesend)capabilities)PGresult)KeyPrepare)
Connection)
BaseCursor)BaseConnection)AsyncConnectionzEtuple[BaseCursor[Any, Any], tuple[Key, Prepare, bytes] | None] | Noner   PendingResultpsycopgc                      e Zd ZU ded<   ded<   ddZddZedd       Ze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Zy)BasePipelinezDeque[PipelineCommand]command_queuezDeque[PendingResult]result_queuec                    || _         |j                  | _        t        t                  | _        t        t
                  | _        d| _        y )Nr   )_connpgconnr   r
   r   r   r    level)selfconns     K/var/www/tech_portal/venv/lib/python3.12/site-packages/psycopg/_pipeline.py__init__zBasePipeline.__init__/   s;    
kk"?35!-02
    c                    | j                   j                   d| j                   j                   }t        | j                  j
                        }d| d| dt        |       ddS )N.< z at 0xx>)	__class__
__module____qualname__r   r"   r#   id)r%   clsinfos      r'   __repr__zBasePipeline.__repr__6   s]    **+1T^^-H-H,IJ!$**"3"343%qfRXaL22r)   c                T    t        j                  | j                  j                        S N)r	   PipelineStatusr#   pipeline_statusr%   s    r'   statuszBasePipeline.status;   s      !<!<==r)   c                *    t        j                         S )zCReturn `!True` if the psycopg libpq wrapper supports pipeline mode.)r   has_pipeline)r4   s    r'   is_supportedzBasePipeline.is_supported?   s     ((**r)   c              #  <  K   t        j                  d       | j                  dk(  r| j                  j	                          nA| j
                  s| j                  j                  t        k(  r| j                         E d {    | xj                  dz  c_        y 7 w)NT)checkr   r   )	r   r>   r$   r#   enter_pipeline_moder   transaction_statusACTIVE	_sync_genr;   s    r'   
_enter_genzBasePipeline._enter_genD   sm     !!-::?KK++-4;;#A#AV#K ~~'''

a
 (s   A=B?B Bc                V   | xj                   dz  c_         | j                   dk(  r:| j                  j                  t        k7  r	 | j                  j	                          y y y # t
        j                  $ r5}|rt        j                  d| |       n|j                  d       Y d }~y d }~ww xY w)Nr   r   zerror ignored exiting %r: %s)
r$   r#   r<   BADexit_pipeline_modeeOperationalErrorloggerwarningwith_traceback)r%   excexc2s      r'   _exitzBasePipeline._exitR   s    

a
::?t{{11S8	4..0  9? %% 4 NN#A4N--d33 O4s   A   B(3+B##B(c              #     K   | j                          | j                         E d {    | j                  d      E d {    y 7 7 w)NFflush_enqueue_sync_communicate_gen
_fetch_genr;   s    r'   rE   zBasePipeline._sync_gen`   sA     ((***???/// 	+/s!   $A
AA
 AA
A
c              #     K   	 | j                          | j                         E d{    | j                  d      E d{    y7 7 # | j                  d      E d{  7   w xY ww)z_
        Exit current pipeline by sending a Sync and fetch back all remaining results.
        NTrS   rU   r;   s    r'   	_exit_genzBasePipeline._exit_gene   sZ     	3 ,,...T222 /2tT222sI   A+#A AA A+A	A+A 	A+A(!A$"A((A+c              #    K   t        | j                  | j                        E d{   }d}|D ]/  }| j                  j	                         }	 | j                  ||       1 ||y7 ?# t        j                  $ r}||}Y d}~Ud}~ww xY ww)zqCommunicate with pipeline to send commands and possibly fetch
        results, which are then processed.
        N)r   r#   r   r    popleft_process_resultsrJ   Error)r%   fetched	exceptionresultsqueuedrO   s         r'   rW   zBasePipeline._communicate_geno   s      2$++t?Q?QRR	 	$G&&..0F$%%fg6	$  O ! S 77 $$ #I$s9   $BA&%BA(B(B	;B?BB		Bc             #    K   | j                   sy|r7| j                  j                          t        | j                        E d{    d}| j                   rZt	        | j                        E d{   x}sn:| j                   j                         }	 | j                  ||       | j                   rZ||y7 q7 H# t        j                  $ r}||}Y d}~2d}~ww xY ww)a^  Fetch available results from the connection and process them with
        pipeline queued items.

        If 'flush' is True, a PQsendFlushRequest() is issued in order to make
        sure results can be fetched. Otherwise, the caller may emit a
        PQpipelineSync() call to ensure the output buffer gets flushed before
        fetching.
        N)	r    r#   send_flush_requestr   r   r\   r]   rJ   r^   )r%   rT   r`   ra   rb   rO   s         r'   rX   zBasePipeline._fetch_gen   s        KK**,DKK(((	+5dkk+B BCGC &&..0F$%%fg6   O ! ) !C 77 $$ #I$sT   ACB6*C/B80!CB: $C1C8C:CCCCCc                   |k|\  }|j                   t        k(  r+t        j                  || j                  j
                        |j                   t        k(  rt        j                  d      y|\  }}|r.|\  }}}|j                  j                  j                  ||||       |j                  |       |j                  |       y)ar  Process a results set fetched from the current pipeline.

        This matches 'results' with its respective element in the pipeline
        queue. For commands (None value in the pipeline queue), results are
        checked directly. For prepare statement creation requests, update the
        cache. Otherwise, results are attached to their respective cursor.
        N)encodingzpipeline aborted)r<   FATAL_ERRORrJ   error_from_resultr#   	_encodingPIPELINE_ABORTEDPipelineAbortedr"   	_preparedvalidate_check_results_set_results)	r%   rb   ra   resultcursorprepinfokeyprepnames	            r'   r]   zBasePipeline._process_results   s     >IV}}+))&4;;;P;PQQ"22''(:;; 3  &FH"*T4&&//T4I!!'*(r)   c                    | j                   j                  | j                  j                         | j                  j                  d       y)z#Enqueue a PQpipelineSync() command.N)r   appendr#   pipeline_syncr    r;   s    r'   rV   zBasePipeline._enqueue_sync   s4    !!$++";";<  &r)   N)r&   zBaseConnection[Any]returnNone)ry   str)ry   zpq.PipelineStatus)ry   bool)ry   PQGen[None])rO   BaseException | Nonery   rz   )rT   r|   ry   r}   )rb   r   ra   zlist[PGresult]ry   rz   ry   rz   )__name__r1   r2   __annotations__r(   r6   propertyr<   classmethodr?   rF   rQ   rE   rZ   rW   rX   r]   rV    r)   r'   r   r   +   sh    ))&&3
 > > + +40
3 >).'r)   r   c                  \     e Zd ZU dZdZded<   d	 fdZd
dZddZ	 	 	 	 	 	 	 	 ddZ xZ	S )Pipelinez(Handler for connection in pipeline mode.r   Connection[Any]r"   c                $    t         |   |       y r8   superr(   r%   r&   r0   s     r'   r(   zPipeline.__init__       r)   c                   	 | j                   j                  5  | j                   j                  | j                                ddd       y# 1 sw Y   yxY w# t        j
                  $ r}|j                  d      d}~ww xY w)zkSync the pipeline, send any pending command and receive and process
        all available results.
        Nr"   lockwaitrE   rJ   _NO_TRACEBACKrN   r%   exs     r'   synczPipeline.sync   sb    	* 2

 012 2 2 	*##D))	*s4   A *AA AA A B *A;;B c                    | j                   j                  5  | j                   j                  | j                                d d d        | S # 1 sw Y   | S xY wr8   r"   r   r   rF   r;   s    r'   	__enter__zPipeline.__enter__   s>    ZZ__ 	/JJOODOO-.	/	/s   *AAc                ~   	 | j                   j                  5  | j                   j                  | j                                d d d        | j                  |       y # 1 sw Y   xY w# t        $ r5}|rt
        j                  d| |       n|j                  d       Y d }~Wd }~ww xY w# | j                  |       w xY wNz error ignored terminating %r: %s	r"   r   r   rZ   	ExceptionrL   rM   rN   rQ   r%   exc_typeexc_valexc_tbrP   s        r'   __exit__zPipeline.__exit__   s    
	  2

 012 JJw2 2 	0A4N))$// O	0 JJws@   A( *AA( A%!A( (	B&1+B!B) !B&&B) )B<)r&   r   ry   rz   r   ry   r   r   ztype[BaseException] | Noner   r~   r   zTracebackType | Nonery   rz   )
r   r1   r2   __doc__r   r(   r   r   r   __classcell__r0   s   @r'   r   r      sK    2J*
 ,  &  %	 
 
 r)   r   c                  \     e Zd ZU dZdZded<   d	 fdZd
dZddZ	 	 	 	 	 	 	 	 ddZ xZ	S )AsyncPipelinez.Handler for async connection in pipeline mode.r   AsyncConnection[Any]r"   c                $    t         |   |       y r8   r   r   s     r'   r(   zAsyncPipeline.__init__   r   r)   c                \  K   	 | j                   j                  4 d {    | j                   j                  | j                                d {    d d d       d {    y 7 F7 7 	# 1 d {  7  sw Y   y xY w# t        j
                  $ r}|j                  d       d }~ww xY wwr8   r   r   s     r'   r   zAsyncPipeline.sync   s     	*zz 8 8jjoodnn&67778 8 878 8 8 8 	*##D))	*s   B,B  A%B  -A+A'A+B  A) B  $B,%B  'A+)B  +A=1A42A=9B  <B,=B   B)B$$B))B,c                  K   | j                   j                  4 d {    | j                   j                  | j                                d {    d d d       d {    | S 7 G7 7 
# 1 d {  7  sw Y   | S xY wwr8   r   r;   s    r'   
__aenter__zAsyncPipeline.__aenter__   sk     ::?? 	5 	5**//$//"3444	5 	5	54	5 	5 	5 	5sV   BA%B-A+A'A+BA)B'A+)B+A>1A42A>9Bc                  K   	 | j                   j                  4 d {    | j                   j                  | j                                d {    d d d       d {    | j                  |       y 7 W7 (7 # 1 d {  7  sw Y   *xY w# t        $ r5}|rt
        j                  d| |       n|j                  d       Y d }~fd }~ww xY w# | j                  |       w xY wwr   r   r   s        r'   	__aexit__zAsyncPipeline.__aexit__   s     
	 zz 8 8jjoodnn&67778 8 JJw878 8 8 8 	0A4N))$// O	0 JJws   C(B A6B -A<A8A<B A: B $C(6B 8A<:B <BBB
B 	C+C
C 
CC C%%C()r&   r   ry   rz   r   r   r   )
r   r1   r2   r   r   r(   r   r   r   r   r   s   @r'   r   r      sK    8J*
 ,  &  %	 
 
 r)   r   )7r   
__future__r   loggingtypesr   typingr   r    r   rJ   r	   abcr
   r   _compatr   r   r   pq.miscr   
generatorsr   r   r   _capabilitiesr   pq.abcr   
_preparingr   r   
connectionr   _cursor_baser   _connection_baser   connection_asyncr   r   r   
ExecStatusrg   rj   
ConnStatusrH   TransactionStatusrD   	getLoggerrL   r   r   r   r   r)   r'   <module>r      s    #   %   ' + + ' > > ' (&(01 L y  mm''==11 mm				$	$			9	%M' M'`( | ( V% L % r)   