o
    DDil+                     @  s   d Z ddlmZ ddlmZ ddlmZmZm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mZ ddlmZmZ ddlmZmZmZ ddlmZmZ ddl m!Z! ddl"m#Z# erjd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)TYPE_CHECKINGAnyoverload)contextmanager)IterableIterator   )errors)pq)ParamsQueryQueryNoTemplate)CopyWriter)Row
RowFactoryRowMaker)SelfTemplate)Pipeline)
BaseCursor)
Connectionc                      sp  e Zd ZdZdZeddddZeded
dZdddf fddZdgddZdhddZdiddZe	djddZ
e
jdkd dZ
dld"d#Ze	dmddd$dnd,d-Zeddd$dod/d-Z	dmddd$dpd1d-Zd2d3dqd8d9Z	dmdd:d;drd?d@ZdsdBdCZdtdEdFZdudHdIZdvdwdLdMZdxdNdOZdgdPdQZdydSdTZdzd{dYdZZe	dmdd[d|d`daZdidbdcZ  ZS )}Cursorpsycopg 
connectionConnection[Row]c                 C     d S Nr   )selfr   r   r   G/var/www/Datamplify/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 r    r   r!   r   r   r"   	__enter__2   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!   r2   r4   r6   r   r   r"   __exit__5   s   zCursor.__exit__c                 C  s   |    dS )zI
        Close the current cursor and free associated resources.
        N)_closer0   r   r   r"   r9   =   s   zCursor.closec                 C  s   | j S )z9Writable attribute to control how result rows are formed.r+   r0   r   r   r"   r&   C   s   zCursor.row_factoryc                 C  s   || _ | jr|| | _d S d S r    )r+   pgresult	_make_row)r!   r&   r   r   r"   r&   H   s   RowMaker[Row]c                 C  s
   |  | S r    r<   r0   r   r   r"   _make_row_makerN   s   
zCursor._make_row_makerpreparebinaryqueryr   paramsParams | NonerB   bool | NonerC   c                C  r   r    r   )r!   rD   rE   rB   rC   r   r   r"   executeQ   s   zCursor.executer   c                C  r   r    r   )r!   rD   rB   rC   r   r   r"   rH   [   s   r   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.
        rA   N)_connlockwait_execute_gene_NO_TRACEBACKwith_traceback)r!   rD   rE   rB   rC   exr   r   r"   rH   d   s   

F)	returning
params_seqIterable[Params]rQ   boolc             
   C  s   zk| j j\ t r;| j jr| j | ||| n>| j   | j | ||| W d   n1 s5w   Y  n| j | ||| W d   W dS W d   W dS W d   W dS 1 sdw   Y  W dS  t	j
y} } z|dd}~ww )zI
        Execute the same command with a sequence of input data.
        N)rI   rJ   r   is_supported	_pipelinerK   _executemany_gen_pipeline_pipeline_nolock_executemany_gen_no_pipelinerM   rN   rO   )r!   rD   rR   rQ   rP   r   r   r"   executemanyx   s6   
 &
zCursor.executemanyr
   rC   sizer\   intIterator[Row]c          	      c  s   | j jr
td| jj zz:| j| j||||d d}| j| | }rIt	|j
D ]}| j|| jV  q0d}| 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stW 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 sw   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 moder[   TFN)first)_pgconnpipeline_statusrM   ProgrammingErrorrI   rJ   rK   _stream_send_gen_stream_fetchone_genrangentuples_txload_rowr>   rN   rO   transaction_statusACTIVE_try_cancel	Exception)	r!   rD   rE   rC   r\   r_   resposrP   r   r   r"   stream   sp   




"zCursor.streamIterator[Self]c                 c  s"    | j r	 | V  |  sdS qdS )z
        Iterate across multiple record sets received by the cursor.

        Multiple record sets are received after using `executemany()` with
        `!returning=True` or using `execute()` with more than one query in the
        command.
        TN)r=   nextsetr0   r   r   r"   results   s   zCursor.resultsindexc                 C  sd   t | j |  krt | jk s n td| dt | j d|dk r+t | j| }| | | S )a  
        Move to a specific result set.

        :arg index: index of the result to go to
        :type index: `!int`

        More than one result will be available after executing calling
        `executemany()` or `execute()` with more than one query.

        `!index` is 0-based and supports negative values, counting from the end,
        the same way you can index items in a list.

        The function returns self, so that the result may be followed by a
        fetch operation. See `results()` for details.
        zindex z out of range: z result(s) availabler   )len_results
IndexError_select_current_result)r!   rs   r   r   r"   
set_result   s   $
zCursor.set_result
Row | Nonec                 C  sD   |    |  }| j|jk r | j| j| j}|  jd7  _|S dS )z
        Return the next record from the current result set.

        Return `!None` the result set is finished.

        :rtype: Row | None, with Row defined by `row_factory`
        r
   N)_fetch_pipeline_check_result_for_fetch_posrf   rg   rh   r>   r!   rm   recordr   r   r"   fetchone   s   zCursor.fetchoner   	list[Row]c                 C  sR   |    |  }|s| j}| j| jt| j| |j| j}|  jt	|7  _|S )z
        Return the next `!size` records from the current result set.

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

        :rtype: Sequence[Row], with Row defined by `row_factory`
        )
rz   r{   	arraysizerg   	load_rowsr|   minrf   r>   rt   )r!   r\   rm   recordsr   r   r"   	fetchmany   s   zCursor.fetchmanyc                 C  s2   |    |  }| j| j|j| j}|j| _|S )z
        Return all the remaining records from the current result set.

        :rtype: Sequence[Row], with Row defined by `row_factory`
        )rz   r{   rg   r   r|   rf   r>   )r!   rm   r   r   r   r"   fetchall  s
   zCursor.fetchallc                 C  r/   r    r   r0   r   r   r"   __iter__  r$   zCursor.__iter__r   c                 C  sH   |    |  }| j|jk r | j| j| j}|  jd7  _|S td)Nr
   zno more records to return)rz   r{   r|   rf   rg   rh   r>   StopIterationr}   r   r   r"   __next__!  s   zCursor.__next__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)rz   _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 | || t| |d}|V  W d   n1 s%w   Y  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.
        r   Nr   )	rI   rJ   rK   _start_copy_genr   rM   rN   rO   rw   )r!   r   rE   r   copyrP   r   r   r"   r   8  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=   rI   rV   rJ   rK   
_fetch_genr0   r   r   r"   rz   P  s   

"zCursor._fetch_pipeline)r   r   )r   r%   r&   r'   )r   r%   r&   r)   )r.   r   )r2   r3   r4   r5   r6   r7   r.   r8   )r.   r8   )r.   r'   )r&   r'   r.   r8   )r.   r?   r    )
rD   r   rE   rF   rB   rG   rC   rG   r.   r   )rD   r   rB   rG   rC   rG   r.   r   )
rD   r   rE   rF   rB   rG   rC   rG   r.   r   )rD   r   rR   rS   rQ   rT   r.   r8   )
rD   r   rE   rF   rC   rG   r\   r]   r.   r^   )r.   rp   )rs   r]   r.   r   )r.   ry   )r   )r\   r]   r.   r   )r.   r   )r.   r   )r   )r   r]   r   r   r.   r8   )r   r   rE   rF   r   r   r.   r   )__name__
__module____qualname__	__slots__r   r#   r1   r:   r9   propertyr&   setterr@   rH   rZ   ro   rr   rx   r   r   r   r   r   r   r   r   rz   __classcell__r   r   r,   r"   r       sj    



	!
0




	r   r%   N))__doc__
__future__r   typesr   typingr   r   r   
contextlibr   collections.abcr   r	    r   rM   r   abcr   r   r   r   r   r   rowsr   r   r   _compatr   r   rV   r   _cursor_baser   r   r   TransactionStatusrj   r   r   r   r   r"   <module>   s$   