o
    "4g$                     @  s   d Z ddl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 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 ddlm Z  e	rbddl!m"Z" ej#j$Z$G dd de def Z%dS )z
Psycopg Cursor object.
    )annotations)TracebackType)AnyIteratorIterableTYPE_CHECKINGoverload)contextmanager   )pq)errors)QueryParams)CopyWriter)RowRowMaker
RowFactory)Self)Pipeline)
BaseCursor)
Connectionc                      s"  e Zd ZdZdZedWddZedXd
dZdddY fddZdZddZd[ddZd\ddZe	d]ddZ
e
jd^d dZ
d_d"d#Z	d`ddd$dad,d-Zd.d/dbd4d5Z	d`dd6d7dcd;d<Zddd>d?ZdedfdBdCZdgdDdEZdhdFdGZdidjdLdMZe	d`ddNdkdSdTZd\dUdVZ  ZS )lCursorpsycopg 
connectionConnection[Row]c                 C     d S Nr   )selfr   r   r   W/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/cursor.py__init__#      zCursor.__init__Connection[Any]row_factoryRowFactory[Row]c                C  r   r   r   r   r   r$   r   r   r    r!   &   s   N)r$   RowFactory[Row] | Nonec                  s   t  | |p
|j| _d S r   )superr!   r$   _row_factoryr&   	__class__r   r    r!   +   s   returnr   c                 C  s   | S r   r   r   r   r   r    	__enter__1   r"   zCursor.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NoneNonec                 C  s   |    d S r   )close)r   r/   r1   r3   r   r   r    __exit__4   s   zCursor.__exit__c                 C  s   |    dS )zI
        Close the current cursor and free associated resources.
        N)_closer-   r   r   r    r6   <   s   zCursor.closec                 C  s   | j S )z9Writable attribute to control how result rows are formed.r)   r-   r   r   r    r$   B   s   zCursor.row_factoryc                 C  s   || _ | jr|| | _d S d S r   )r)   pgresult	_make_row)r   r$   r   r   r    r$   G   s   RowMaker[Row]c                 C  s
   |  | S r   r9   r-   r   r   r    _make_row_makerM   s   
zCursor._make_row_makerpreparebinaryqueryr   paramsParams | Noner?   bool | Noner@   c             
   C  sr   z&| j j | j | j||||d W d   W | S 1 sw   Y  W | S  tjy8 } z|dd}~ww )z=
        Execute a query or command to the database.
        r>   N)_connlockwait_execute_gene_NO_TRACEBACKwith_traceback)r   rA   rB   r?   r@   exr   r   r    executeP   s   

zCursor.executeF)	returning
params_seqIterable[Params]rN   boolc             
   C  s>  zt  ri| jj | jj}|r| j| ||| W d   n1 s%w   Y  |sc| j ' | jj | j| ||| W d   n1 sLw   Y  W d   n1 s[w   Y  W dS W dS W dS | jj | j| ||| W d   W dS 1 sw   Y  W dS  t	j
y } z|dd}~ww )zI
        Execute the same command with a sequence of input data.
        N)r   is_supportedrE   rF   	_pipelinerG   _executemany_gen_pipelinepipeline_executemany_gen_no_pipelinerI   rJ   rK   )r   rA   rO   rN   prL   r   r   r    executemanyd   s:   
" 
&
zCursor.executemanyr
   r@   sizerZ   intIterator[Row]c          	      c  s   | j jr
td| jj zz=| j| j||||d d}| j| |rLt	|D ]}| j
|| j}|du r= n|V  q-d}| j| |s)W n tjy_ } z|dd}~ww W | j jtkr| j  z| j| jddr	 | j| jddswW n	 ty   Y nw z| j| jdd W nU ty   Y nUw nC| j jtkr| j  z| j| jddr	 | j| jddsW n	 ty   Y nw z| j| jdd W w  ty   Y w w w W d   dS W d   dS W d   dS 1 sw   Y  dS )a  
        Iterate row-by-row on a result from the database.

        :param size: if greater than 1, results will be retrieved by chunks of
            this size from the server (but still yielded row-by-row); this is only
            available from version 17 of the libpq.
        z(stream() cannot be used in pipeline moderY   TNF)first)_pgconnpipeline_statusrI   ProgrammingErrorrE   rF   rG   _stream_send_gen_stream_fetchone_genrange_txload_rowr;   rJ   rK   transaction_statusACTIVE_try_cancel	Exception)	r   rA   rB   r@   rZ   r]   posrecrL   r   r   r    stream   sv   




$zCursor.stream
Row | Nonec                 C  s<   |    |   | j| j| j}|dur|  jd7  _|S )z
        Return the next record from the current recordset.

        Return `!None` the recordset is finished.

        :rtype: Row | None, with Row defined by `row_factory`
        Nr
   )_fetch_pipeline_check_result_for_fetchrd   re   _posr;   )r   recordr   r   r    fetchone   s   zCursor.fetchoner   	list[Row]c                 C  s^   |    |   | jsJ |s| j}| j| jt| j| | jj| j	}|  jt
|7  _|S )z
        Return the next `!size` records from the current recordset.

        `!size` default to `!self.arraysize` if not specified.

        :rtype: Sequence[Row], with Row defined by `row_factory`
        )rn   ro   r:   	arraysizerd   	load_rowsrp   minntuplesr;   len)r   rZ   recordsr   r   r    	fetchmany   s   
zCursor.fetchmanyc                 C  s@   |    |   | jsJ | j| j| jj| j}| jj| _|S )z
        Return all the remaining records from the current recordset.

        :rtype: Sequence[Row], with Row defined by `row_factory`
        )rn   ro   r:   rd   ru   rp   rw   r;   )r   ry   r   r   r    fetchall   s   

zCursor.fetchallc                 #  sN           d	 fdd}	 | j}|d u rd S   jd7  _|V  q)
Nrj   r[   r,   rm   c                   s    j |  jS r   )rd   re   r;   )rj   r-   r   r    load   s   zCursor.__iter__.<locals>.loadTr
   )rj   r[   r,   rm   )rn   ro   rp   )r   r|   rowr   r-   r    __iter__   s   
zCursor.__iter__relativevaluemodestrc                 C  s   |    | || dS )a  
        Move the cursor in the result set to a new position according to mode.

        If `!mode` is ``'relative'`` (default), `!value` is taken as offset to
        the current position in the result set; if set to ``'absolute'``,
        `!value` states an absolute target position.

        Raise `!IndexError` in case a scroll operation would leave the result
        set. In this case the position will not change.
        N)rn   _scroll)r   r   r   r   r   r    scroll   s   zCursor.scrollwriter	statementr   Writer | NoneIterator[Copy]c             
   c  s    z9| j j | j | || W d   n1 sw   Y  t| |d}|V  W d   n1 s4w   Y  W n tjyL } z|dd}~ww | d dS )zU
        Initiate a :sql:`COPY` operation and return an object to manage it.
        Nr   r   )	rE   rF   rG   _start_copy_genr   rI   rJ   rK   _select_current_result)r   r   rB   r   copyrL   r   r   r    r     s   

zCursor.copyc                 C  sj   | j dur/| js1| jjr3| jj | j| jjjdd W d    d S 1 s(w   Y  d S d S d S d S )NFT)flush)_execmany_returningr:   rE   rS   rF   rG   
_fetch_genr-   r   r   r    rn     s   

"zCursor._fetch_pipeline)r   r   )r   r#   r$   r%   )r   r#   r$   r'   )r,   r   )r/   r0   r1   r2   r3   r4   r,   r5   )r,   r5   )r,   r%   )r$   r%   r,   r5   )r,   r<   r   )
rA   r   rB   rC   r?   rD   r@   rD   r,   r   )rA   r   rO   rP   rN   rQ   r,   r5   )
rA   r   rB   rC   r@   rD   rZ   r[   r,   r\   )r,   rm   )r   )rZ   r[   r,   rs   )r,   rs   )r,   r\   )r   )r   r[   r   r   r,   r5   )r   r   rB   rC   r   r   r,   r   )__name__
__module____qualname__	__slots__r   r!   r.   r7   r6   propertyr$   setterr=   rM   rX   rl   rr   rz   r{   r~   r   r	   r   rn   __classcell__r   r   r*   r    r      sP    



!
3

r   r#   N)&__doc__
__future__r   typesr   typingr   r   r   r   r   
contextlibr	    r   r   rI   abcr   r   r   r   r   rowsr   r   r   _compatr   rS   r   _cursor_baser   r   r   TransactionStatusrg   r   r   r   r   r    <module>   s"   