
    iI                       d Z ddlmZ ddlZddl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 d	d
lmZ d	dlmZmZ d	dlmZ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' d	dl(m)Z) d	dl*m+Z+ d	dl,m-Z- d	dl.m/Z/ d	dl0m1Z1m2Z2m3Z3 d	dl0m4Z4 d	dl5m6Z6 d	dl7m8Z8 d	dl9m:Z: d	dl;m<Z< d	dl=m>Z> d	dl?m@Z@ d	dlAmBZBmCZCmDZD e
rd	dlEmFZF dZGej                  j                  ZIej                  j                  ZJej                  j                  ZLej                  j                  ZMej                  j                  ZNeOZP ej                  d      ZR G d d eBe$         ZSy)!z*
Psycopg connection object (sync version)
    )annotationsN)	monotonic)TracebackType)TYPE_CHECKINGAny	GeneratorIteratorcastoverload)Lock)contextmanager   )errors)pqwaiting)RVAdaptContextConnDict	ConnParamParamsPQGenQuery)Xid)Row
RowFactoryargs_row	tuple_row)AdaptersMap)IsolationLevel)Cursor)Self)conninfo_attemptsconninfo_to_dictmake_conninfo)timeout_from_conninfo)Pipeline)notifies)Transaction)capabilities)ServerCursor)gssapi_requested)BaseConnection	CursorRowNotify)PGconng?psycopgc                      e Zd ZU dZdZded<   ded<   ded<   d	ed
<    eee   e      f	 	 	 d9 fdZ	e
	 d:dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d;d       Zd<dZ	 	 	 	 	 	 	 	 d=dZe
d>d       Zd?dZeddd@d       Zedd	 	 	 	 	 dAd       Zedddd	 	 	 	 	 	 	 	 	 dBd       Zedddd	 	 	 	 	 	 	 	 	 	 	 dCd       Z	 d:ddddd	 	 	 	 	 	 	 	 	 	 	 dDdZ	 dEddd	 	 	 	 	 	 	 	 	 dFdZd?dZd?d Zd!d"dGd#Zd$d"dGd%Ze	 dH	 	 	 	 	 dId&       Zddd'	 	 	 	 	 dJd(ZedKd)       ZedKd*       ZefdLd+ZdMd,ZdMd-ZdNd.Z dNd/Z!dOd0Z"dOd1Z#dOd2Z$dOd3Z%dPd4Z&d?d5Z'dEdQd6Z(dEdQd7Z)dRd8Z* xZ+S )S
Connectionz3
    Wrapper for a connection to the database.
    r0   ztype[Cursor[Row]]cursor_factoryztype[ServerCursor[Row]]server_cursor_factoryRowFactory[Row]row_factoryzPipeline | None	_pipelinec                |    t         |   |       || _        t               | _        t
        | _        t        | _        y N)	super__init__r6   r   lockr    r3   r*   r4   )selfpgconnr6   	__class__s      r/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/psycopg/connection.pyr;   zConnection.__init__D   s3    
 	 &F	$%1"    F   N)
autocommitprepare_thresholdcontextr6   r3   c          	         | j                   |fi |}t        |      }	d}
t        |      }g }|D ]  }|j                  d      |j                  d      |j                  d      f}d|z  }t        j                  d|       	 t        di |}| j                  ||	      }t        j                  |t              }
t        j                  d	|        n |
s|d   d   }t#        |      dk(  r|j!                  d      t        |      g}|j                  d       |j%                  d |D                t'        |      dj)                  |            j!                  d      t+        j,                         dk\  r;|
j.                  j0                  r%t3        |      st5        j6                  dt8               t;        |      |
_        |r||
_        |r||
_         |rtC        |jD                        |
_#        ||
_$        |
S # t        j                  $ r>}t        j                  d
|t        |             |j                  ||f       Y d}~d}~wt        j                  $ r}|j!                  d      d}~ww xY w)zV
        Connect to a database server and return a new `Connection` instance.
        Nhostporthostaddrz host: %r, port: %r, hostaddr: %rzconnection attempt: %stimeoutintervalzconnection succeeded: %szconnection failed: %s: %sr   r   z7Multiple connection attempts failed. All failures were:c              3  2   K   | ]  \  }}d | d|   yw)z- z: N ).0errordescrs      r@   	<genexpr>z%Connection.connect.<locals>.<genexpr>~   s!     PLE5BugRw/Ps   
i q aa  the connection was obtained using the GSSAPI relying on the 'gssencmode=prefer' libpq default. In a future psycopg[binary] version this default will be changed to 'disable'. If you wish to interact with the GSSAPI reliably please set the 'gssencmode' parameter in the connection string or the 'PGGSSENCMODE' environment variable to 'prefer' or 'require' )%_get_connection_paramsr%   r"   getloggerdebugr$   _connect_genr   	wait_conn_WAIT_INTERVALeErrorstrappend_NO_TRACEBACKwith_tracebacklenextendtypejoinr   versionr>   used_gssapir+   warningswarnDeprecationWarningbool_autocommitr6   r3   r   adapters	_adaptersrD   )clsconninforC   rD   rE   r6   r3   kwargsparamsrK   rvattemptsconn_errorsattempttdescrrS   genexlast_exliness                       r@   connectzConnection.connectO   s5     ,++H??'/$V,13 	Gkk&)7;;v+>J@WXF6?ELL159(7w7&&x&A&&s^D 7?	" !"oa(G;1$,,T22 \NELLRSLLPKPQ$w-		% 01@@FF JJLF"		%%%f-MM t"
 j)(BN .B&w'7'78BL0	O 77 08%RI""B;//?? .''--.s$   9G99I13II1I,,I1c                    | S r9   rP   r=   s    r@   	__enter__zConnection.__enter__   s    rA   c                    | j                   ry |r	 | j                          n| j                          t        | dd       s| j                          y y # t        $ r!}t        j	                  d| |       Y d }~Dd }~ww xY w)Nz#error ignored in rollback on %s: %s_pool)closedrollback	ExceptionrZ   warningcommitgetattrclose)r=   exc_typeexc_valexc_tbexc2s        r@   __exit__zConnection.__exit__   so     ;;R KKM tWd+JJL ,  RDdDQQRs   A 	A;A66A;c                    t        |fi |S )z3Manipulate connection parameters before connecting.)r#   )rr   rs   rt   s      r@   rX   z!Connection._get_connection_params   s      3F33rA   c                `    | j                   ryd| _        | j                  j                          y)zClose the database connection.NT)r   _closedr>   finishr   s    r@   r   zConnection.close   s%    ;; 	rA   )binaryc                    y r9   rP   )r=   r   s     r@   cursorzConnection.cursor   s    >ArA   c                    y r9   rP   )r=   r   r6   s      r@   r   zConnection.cursor   s      rA   )r   
scrollablewithholdc                    y r9   rP   )r=   namer   r   r   s        r@   r   zConnection.cursor   s      rA   c                    y r9   rP   )r=   r   r   r6   r   r   s         r@   r   zConnection.cursor   s     #&rA   )r   r6   r   r   c                   | j                          |s| j                  }|r| j                  | ||||      }n| j                  | |      }|rt        |_        |S )zW
        Return a new `Cursor` to send commands and queries to the connection.
        )r   r6   r   r   r6   )_check_connection_okr6   r4   r3   BINARYformat)r=   r   r   r6   r   r   curs          r@   r   zConnection.cursor   sm     	!!#**K ,,'%! - C %%d%DCCJ
rA   )preparer   c                   	 | j                         }|rt        |_        |j                  |||      S # t        j
                  $ r}|j                  d      d}~ww xY w)z8Execute a query and return a cursor to read its results.)r   N)r   r   r   executer_   rc   rd   )r=   queryru   r   r   r   r|   s          r@   r   zConnection.execute   sV    	*++-C#
;;ufg;>> 	*##D))	*s   03 AAAc                    | j                   5  | j                  | j                                ddd       y# 1 sw Y   yxY w)z/Commit any pending transaction to the database.N)r<   wait_commit_genr   s    r@   r   zConnection.commit  s3    YY 	*IId&&()	* 	* 	*    6?c                    | j                   5  | j                  | j                                ddd       y# 1 sw Y   yxY w)z2Roll back to the start of any pending transaction.N)r<   r   _rollback_genr   s    r@   r   zConnection.rollback  s3    YY 	,IId((*+	, 	, 	,r   g      >@rJ   c                   | j                         syt        j                         r,t        j                  | j                  |      t               y| j                          y)a  Cancel the current operation on the connection.

        :param timeout: raise a `~errors.CancellationTimeout` if the
            cancellation request does not succeed within `timeout` seconds.

        Note that a successful cancel attempt on the client is not a guarantee
        that the server will successfully manage to cancel the operation.

        This is a non-blocking version of `~Connection.cancel()` which
        leverages a more secure and improved cancellation feature of the libpq,
        which is only available from version 17.

        If the underlying libpq is older than version 17, the method will fall
        back to using the same implementation of `!cancel()`.
        NrJ   rL   )_should_cancelr)   has_cancel_safer   r]   _cancel_genr^   cancel)r=   rK   s     r@   cancel_safezConnection.cancel_safe  sK      ""$'')   1N KKMrA         @c                   	 | j                  |       y # t        $ r }t        j                  d|       Y d }~y d }~ww xY w)NrJ   zquery cancellation failed: %s)r   r   rZ   r   )r=   rK   r|   s      r@   _try_cancelzConnection._try_cancel3  s=    	@W- 	@NN:B??	@s    	>9>c              #  @  K   t        | ||      }| j                  rB| j                         5  |5  | j                         5  | ddd       ddd       ddd       y|5  | ddd       y# 1 sw Y   *xY w# 1 sw Y   .xY w# 1 sw Y   yxY w# 1 sw Y   yxY ww)a  
        Start a context block with a new transaction or nested transaction.

        :param savepoint_name: Name of the savepoint used to manage a nested
            transaction. If `!None`, one will be chosen automatically.
        :param force_rollback: Roll back the transaction at the end of the
            block even if there were no error (e.g. to try a no-op process).
        :rtype: Transaction
        N)r(   r7   pipeline)r=   savepoint_nameforce_rollbacktxs       r@   transactionzConnection.transaction9  s      ~~>>> " dmmo             sc   *BBA: A.A:BB B%	B.A73A::B	?BBBBB)rK   
stop_afterc             #  X  K   |t               |z   }t        |t              }nd}t        }d}| j                  rt	        j
                  dt               | j                  5  | j                  j                  }d| j                  c| _
        }	 	 |r|r|j                          |dz  }|rn~	 | j                  t        | j                        |      }|D ]R  }
t#        |
j$                  j'                  |      |
j(                  j'                  |      |
j*                         |dz  }T |||k\  rn$|r!t        t        |t               z
        }|dk  rn|| _
        	 ddd       y# t        j                  $ r}	|	j!                  d      d}	~	ww xY w# || _
        w xY w# 1 sw Y   yxY ww)a  
        Yield `Notify` objects as soon as they are received from the database.

        :param timeout: maximum amount of time to wait for notifications.
            `!None` means no timeout.
        :param stop_after: stop after receiving this number of notifications.
            You might actually receive more than this number if more than one
            notifications arrives in the same packet.
        Nr   zusing 'notifies()' together with notifies handlers on the same connection is not reliable. Please use only one of thees methodsr   rL   g        )r   minr^   _notify_handlersrk   rl   RuntimeWarningr<   r>   	_encoding_notifies_backlogpopleftr   r'   r_   rc   rd   r.   relnamedecodeextrabe_pid)r=   rK   r   deadlinerM   	nreceivedencdpgnsr|   pgns              r@   r'   zConnection.notifiesN  s      {W,H7N3HH%H	  MM R
 YY (	+++''C *.t/E/E%D"A!+ "#))+-%NI  :#'99Xdkk-BX9#VD $( +C"( # 2 23 7 #		 0 0 5 #

# 
 &NI+ "-)z2I  #&~x)+7M#N#c>!= @ *+&Q(	+ (	+$  ! :"$"3"3D"99:, *+&Q(	+ (	+sa   AF*+FF*F,&E&BFF	F*&F9F

FF	FFF'#F*c              #    K   | j                   5  | j                          | j                  x}t        |       x}| _        ddd       	 5  | ddd       |j                  dk(  r-| j                   5  || j                  u sJ d| _        ddd       yy# 1 sw Y   VxY w# 1 sw Y   RxY w# 1 sw Y   yxY w# j                  dk(  r9| j                   5  || j                  u sJ d| _        ddd       w # 1 sw Y   w xY ww xY ww)z<Context manager to switch the connection into pipeline mode.Nr   )r<   r   r7   r&   levelr=   r   s     r@   r   zConnection.pipeline  s     YY 	;%%' NN*3,4TN:4>	;	*  ~~"YY *#t~~555%)DN* * #	; 	; * * ~~"YY *#t~~555%)DN* * * #s|   D1B D	B9 B!B9 D3B-
DBD!B*&B9 -B62D9DC6-	D6C?;DDc              #  2  K   | j                   rJ t        | d      x}| _         	 |5  | ddd       |j                  dk(  sJ || j                   u sJ d| _         y# 1 sw Y   2xY w# |j                  dk(  sJ || j                   u sJ d| _         w xY ww)zglike pipeline() but don't acquire a lock.

        Assume that the caller is holding the lock.
        T)_no_lockNr   )r7   r&   r   r   s     r@   _pipeline_nolockzConnection._pipeline_nolock  s      >>!!$,TD$AA4>	"  >>Q&&&t~~---!DN  >>Q&&&t~~---!DNs1   #BA* AA* )BA'#A* **BBc                d   	 t        j                  || j                  j                  |      S # t        $ rx | j                  j
                  t        k(  rY| j                  d       	 t        j                  || j                  j                  |        # t        j                  $ r Y  w xY w w xY w)z
        Consume a generator operating on the connection.

        The function must be used on generators that don't change connection
        fd (i.e. not on connect and reset).
        rL   r   rJ   )
r   r   r>   socket_INTERRUPTEDtransaction_statusACTIVEr   r_   QueryCanceled)r=   r{   rM   s      r@   r   zConnection.wait  s    	<<T[[%7%7(KK 		{{--7    -LLdkk&8&88L   		s-   +. 8B/',BB/B*'B/)B**B/c                &    | j                  |       y r9   )set_autocommitr=   values     r@   _set_autocommitzConnection._set_autocommit      E"rA   c                    | j                   5  | j                  | j                  |             ddd       y# 1 sw Y   yxY w)z6Method version of the `~Connection.autocommit` setter.N)r<   r   _set_autocommit_genr   s     r@   r   zConnection.set_autocommit  5    YY 	7IId..u56	7 	7 	7	   !7A c                &    | j                  |       y r9   )set_isolation_levelr   s     r@   _set_isolation_levelzConnection._set_isolation_level  s      'rA   c                    | j                   5  | j                  | j                  |             ddd       y# 1 sw Y   yxY w)z;Method version of the `~Connection.isolation_level` setter.N)r<   r   _set_isolation_level_genr   s     r@   r   zConnection.set_isolation_level  s5    YY 	<IId33E:;	< 	< 	<r   c                &    | j                  |       y r9   )set_read_onlyr   s     r@   _set_read_onlyzConnection._set_read_only  s    5!rA   c                    | j                   5  | j                  | j                  |             ddd       y# 1 sw Y   yxY w)z5Method version of the `~Connection.read_only` setter.N)r<   r   _set_read_only_genr   s     r@   r   zConnection.set_read_only  s5    YY 	6IId--e45	6 	6 	6r   c                &    | j                  |       y r9   )set_deferrabler   s     r@   _set_deferrablezConnection._set_deferrable  r   rA   c                    | j                   5  | j                  | j                  |             ddd       y# 1 sw Y   yxY w)z6Method version of the `~Connection.deferrable` setter.N)r<   r   _set_deferrable_genr   s     r@   r   zConnection.set_deferrable  r   r   c                    | j                   5  | j                  | j                  |             ddd       y# 1 sw Y   yxY w)zO
        Begin a TPC transaction with the given transaction ID `!xid`.
        N)r<   r   _tpc_begin_genr=   xids     r@   	tpc_beginzConnection.tpc_begin  s7     YY 	0IId))#./	0 	0 	0r   c                    	 | j                   5  | j                  | j                                ddd       y# 1 sw Y   yxY w# t        j                  $ r$}t        j
                  t        |            dd}~ww xY w)zV
        Perform the first phase of a transaction started with `tpc_begin()`.
        N)r<   r   _tpc_prepare_genr_   ObjectNotInPrerequisiteStateNotSupportedErrorra   )r=   r|   s     r@   tpc_preparezConnection.tpc_prepare  sd    	9 3		$//123 3 3-- 	9%%c"g.D8	9s0   A  7A A A  A A:A55A:c                    | j                   5  | j                  | j                  d|             ddd       y# 1 sw Y   yxY w)z:
        Commit a prepared two-phase transaction.
        COMMITNr<   r   _tpc_finish_genr   s     r@   
tpc_commitzConnection.tpc_commit  s9     YY 	;IId**8S9:	; 	; 	;	   "8Ac                    | j                   5  | j                  | j                  d|             ddd       y# 1 sw Y   yxY w)z=
        Roll back a prepared two-phase transaction.
        ROLLBACKNr   r   s     r@   tpc_rollbackzConnection.tpc_rollback	  s9     YY 	=IId**:s;<	= 	= 	=r  c                   | j                          | j                  j                  }| j                  t	        t
        j                              5 }|j                  t        j                                |j                         }d d d        |t        k(  r-| j                  j                  t        k(  r| j                          S # 1 sw Y   AxY w)Nr   )
_check_tpcinfor   r   r   r   _from_recordr   _get_recover_queryfetchallIDLEINTRANSr   )r=   statusr   ress       r@   tpc_recoverzConnection.tpc_recover  s    --[[Xc.>.>%?[@ 	!CKK..01,,.C	! T>dii::gEMMO
	! 	!s   4CC)r>   r/   r6   r5   rV   )rs   ra   rC   rn   rD   
int | NonerE   zAdaptContext | Noner6   zRowFactory[Row] | Noner3   ztype[Cursor[Row]] | Nonert   r   returnr!   )r  r!   )r   ztype[BaseException] | Noner   zBaseException | Noner   zTracebackType | Noner  None)rs   ra   rt   r   r  r   )r  r  )r   rn   r  Cursor[Row])r   rn   r6   RowFactory[CursorRow]r  zCursor[CursorRow])
r   ra   r   rn   r   bool | Noner   rn   r  zServerCursor[Row])r   ra   r   rn   r6   r  r   r  r   rn   r  zServerCursor[CursorRow])r   ra   r   rn   r6   zRowFactory[Any] | Noner   r  r   rn   r  zCursor[Any] | ServerCursor[Any]r9   )
r   r   ru   zParams | Noner   r  r   rn   r  r  )rK   floatr  r  )NF)r   z
str | Noner   rn   r  zIterator[Transaction])rK   float | Noner   r  r  zGenerator[Notify, None, None])r  zIterator[Pipeline])r{   z	PQGen[RV]rM   r  r  r   )r   rn   r  r  )r   zIsolationLevel | Noner  r  )r   r  r  r  )r   z	Xid | strr  r  )r   zXid | str | Noner  r  )r  z	list[Xid]),__name__
__module____qualname____doc____annotations__r
   r   r   r   r;   classmethodr   r   r   rX   r   r   r   r   r   r   r   r   r   r   r'   r   r   r^   r   r   r   r   r   r   r   r   r   r   r   r  r  r  __classcell__)r?   s   @r@   r2   r2   8   s    J%%22  
 (,JsOY'G	2	2 %	2  C !()'+.237CC 	C
 &C %C ,C 1C C 
C CJ, & %	
 
. 4 4 ',A A %  4I 	    
 "&   	 
      
    
 "&&& 	&
 +&  & & 
!& &   .2"&   	 
 ,       
) J !%*
  $** *
 * * 
*$*
,
 /3 4 /2 @ HM(AE	 * *.E+&E+;EE+	&E+N * *$ " "( =K (#7
(<
"6
#7
09;=
rA   r2   )Tr  
__future__r   loggingrk   timer   typesr   typingr   r   r   r	   r
   r   	threadingr   
contextlibr   rW   r   r_   r   r   abcr   r   r   r   r   r   r   _tpcr   rowsr   r   r   r   adaptr   _enumsr   r   r    _compatr!   rs   r"   r#   r$   r%   r7   r&   
generatorsr'   r   r(   _capabilitiesr)   _server_cursorr*   _conninfo_utilsr+   _connection_baser,   r-   r.   pq.abcr/   r^   FormatTEXTr   TransactionStatusr  r   r  KeyboardInterruptr   	getLoggerrZ   r2   rP   rA   r@   <module>r9     s    #     J J  %   L L L  6 6  "   H H +    $ ' ( - ? ?	yy~~				  				$	$



&
& 			9	%b$ brA   