
    jl+                       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rddl$m%Z% ej&        j'        Z' G 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                      e Zd ZdZdZedQd            ZedRd	            Zd
ddS fdZdTdZdUdZdVdZe	dWd            Z
e
j        dXd            Z
dYdZe	 dZd
d
dd[d&            Zed
d
dd\d(            Z	 dZd
d
dd]d*Zd+d,d^d1Z	 dZd
d2d3d_d7Zd`d9Zdad;Zdbd=Zdcddd@ZdedAZdTdBZdfdDZdgdhdIZe	 dZd
dJdidO            ZdVdPZ xZS )jCursorpsycopg 
connectionConnection[Row]c                    d S Nr   )selfr   s     Y/DATA/AppData/hermes/projects/honcho/.venv/lib/python3.11/site-packages/psycopg/cursor.py__init__zCursor.__init__$   s    58S    Connection[Any]row_factoryRowFactory[Row]c                   d S r"   r   )r#   r   r(   s      r$   r%   zCursor.__init__'   s	     sr&   N)r(   RowFactory[Row] | Nonec               f    t                                          |           |p|j        | _        d S r"   )superr%   r(   _row_factory)r#   r   r(   	__class__s      r$   r%   zCursor.__init__,   s3     	$$$'A:+Ar&   returnr   c                    | S r"   r   r#   s    r$   	__enter__zCursor.__enter__2       r&   exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NoneNonec                .    |                                   d S r"   )close)r#   r5   r7   r9   s       r$   __exit__zCursor.__exit__5   s     	

r&   c                .    |                                   dS )zI
        Close the current cursor and free associated resources.
        N)_closer2   s    r$   r=   zCursor.close=   s     	r&   c                    | j         S )z9Writable attribute to control how result rows are formed.r.   r2   s    r$   r(   zCursor.row_factoryC   s       r&   c                F    || _         | j        r ||           | _        d S d S r"   )r.   pgresult	_make_row)r#   r(   s     r$   r(   zCursor.row_factoryH   s3    '= 	/([..DNNN	/ 	/r&   RowMaker[Row]c                ,    |                      |           S r"   rB   r2   s    r$   _make_row_makerzCursor._make_row_makerN   s      &&&r&   preparebinaryqueryr   paramsParams | NonerJ   bool | NonerK   c                   d S r"   r   )r#   rL   rM   rJ   rK   s        r$   executezCursor.executeQ   s	     sr&   r   c                   d S r"   r   )r#   rL   rJ   rK   s       r$   rQ   zCursor.execute[   s	     sr&   r   c          	        	 | j         j        5  | j                             |                     ||||                     ddd           n# 1 swxY w Y   n,# t          j        $ r}|                    d          d}~ww xY w| S )z=
        Execute a query or command to the database.
        rI   N)_connlockwait_execute_gene_NO_TRACEBACKwith_traceback)r#   rL   rM   rJ   rK   exs         r$   rQ   zCursor.executed   s    	*  
%%eVWV%TT                  	* 	* 	*##D)))	*s:   A 2A A AA AA B'A<<BF)	returning
params_seqIterable[Params]r\   boolc          	     h   	 | j         j        5  t          j                    r| j         j        r0| j                             |                     |||                     n| j                                         5  | j                             |                     |||                     ddd           n# 1 swxY w Y   n/| j                             |                     |||                     ddd           dS # 1 swxY w Y   dS # t          j
        $ r}|                    d          d}~ww xY w)zI
        Execute the same command with a sequence of input data.
        N)rT   rU   r   is_supported	_pipelinerV   _executemany_gen_pipeline_pipeline_nolock_executemany_gen_no_pipelinerX   rY   rZ   )r#   rL   r]   r\   r[   s        r$   executemanyzCursor.executemanyx   s   	*  (**  z+ 
 ::5*iXX   
 "Z88::   JOO $ > >$):y!" !"                 JOO99%YWW  #                 (  	* 	* 	*##D)))	*s_   D A)C;70B3'C;3B7	7C;:B7	;3C;.D ;C??D C?D D1D,,D1r   rK   sizerh   intIterator[Row]c          	   #    K   | j         j        rt          j        d          | j        j        5  	 | j                            |                     ||||                     d}| j                            |                     |                    x}rjt          |j
                  D ]$}| j                            || j                  V  %d}| j                            |                     |                    x}jn,# t          j        $ r}|                    d          d}~ww xY w| j         j        t"          k    r| j                                         	 | j                            |                     d                    r/	 | j                            |                     d                    /n# t&          $ r Y nw xY w	 | j                            |                     d                     n# t&          $ r Y nw xY wn# | j         j        t"          k    r| j                                         	 | j                            |                     d                    r/	 | j                            |                     d                    /n# t&          $ r Y nw xY w	 | j                            |                     d                     w # t&          $ r Y w w xY ww xY wddd           dS # 1 swxY 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 moderg   TFN)first)_pgconnpipeline_statusrX   ProgrammingErrorrT   rU   rV   _stream_send_gen_stream_fetchone_genrangentuples_txload_rowrE   rY   rZ   transaction_statusACTIVE_try_cancel	Exception)	r#   rL   rM   rK   rh   rl   resposr[   s	            r$   streamzCursor.stream   s      <' 	Q$%OPPPZ_ 	 	
))%T)RR   !Z__T-F-Fu-M-MNNNc "$S[11 E E"h//T^DDDDDD!E "Z__T-F-Fu-M-MNNNc " ? . . .''---. <2f<< J**,,,"jood.G.Ge.G.T.TUU !  #jood.G.Ge.G.T.TUU !$   

(A(A(A(N(NOOOO$    =4<2f<< J**,,,"jood.G.Ge.G.T.TUU !  #jood.G.Ge.G.T.TUU !$   

(A(A(A(N(NOOOO$    =	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   K7CC>=H>D'D""D''H*.K7AF76K77
GK7GK7.G76K77
HK7HK7/K'8AJK'
J#	 K'"J#	#K''.KK'
K#	 K'"K#	#K''K77K;>K;Iterator[Self]c              #  P   K   | j         r	 | V  |                                 sdS 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)rD   nextsetr2   s    r$   resultszCursor.results   sD       = 	


||~~ E		 	r&   indexc                "   t          | j                   |cxk    rt          | j                  k     s*n t          d| dt          | j                   d          |dk     rt          | j                  |z   }|                     |           | 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#   r   s     r$   
set_resultzCursor.set_result   s      DM"""e@@@@c$-.@.@@@@@WWWs4=/A/AWWW   199&&.E##E***r&   
Row | Nonec                    |                                   |                                 }| j        |j        k     r7| j                            | j        | j                  }| xj        dz  c_        |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_posrs   rt   ru   rE   r#   rz   records      r$   fetchonezCursor.fetchone   sj     	**,,9s{""X&&ty$.AAFIINIIMtr&   r   	list[Row]c                $   |                                   |                                 }|s| j        }| j                            | j        t          | j        |z   |j                  | j                  }| xj        t          |          z  c_        |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`
        )
r   r   	arraysizert   	load_rowsr   minrs   rE   r   )r#   rh   rz   recordss       r$   	fetchmanyzCursor.fetchmany   s     	**,, 	">D($$Is49t+S[994>
 
 			S\\!		r&   c                    |                                   |                                 }| 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`
        )r   r   rt   r   r   rs   rE   )r#   rz   r   s      r$   fetchallzCursor.fetchall  sT     	**,,($$TYT^LLK	r&   c                    | S r"   r   r2   s    r$   __iter__zCursor.__iter__  r4   r&   r   c                    |                                   |                                 }| j        |j        k     r7| j                            | j        | j                  }| xj        dz  c_        |S t          d          )Nr   zno more records to return)r   r   r   rs   rt   ru   rE   StopIterationr   s      r$   __next__zCursor.__next__!  sr    **,,9s{""X&&ty$.AAFIINIIM7888r&   relativevaluemodestrc                Z    |                                   |                     ||           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)r   _scroll)r#   r   r   s      r$   scrollzCursor.scroll*  s0     	UD!!!!!r&   writer	statementr   Writer | NoneIterator[Copy]c             #    K   	 | j         j        5  | j                             |                     ||                     t	          | |          5 }|V  ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   n,# t
          j        $ r}|                    d          d}~ww xY w|                     d           dS )zU
        Initiate a :sql:`COPY` operation and return an object to manage it.
        r   Nr   )	rT   rU   rV   _start_copy_genr   rX   rY   rZ   r   )r#   r   rM   r   copyr[   s         r$   r   zCursor.copy8  sP     	*  
 4 4Y G GHHH$v... $JJJ                            
  	* 	* 	*##D)))	*
 	##A&&&&&s_   B A A8A!A8!A%	%A8(A%	)A8,B 8A<<B ?A< B B-B((B-c                   | j         durr| j        sm| j        j        rc| j        j        5  | j                            | j        j                            d                     d d d            d S # 1 swxY w Y   d S d S d S d S )NFT)flush)_execmany_returningrD   rT   rb   rU   rV   
_fetch_genr2   s    r$   r   zCursor._fetch_pipelineP  s    $E11] 2
$ 2  M M

 4 ? ?d ? K KLLLM M M M M M M M M M M M M M M M M M	 211111s   9A//A36A3)r   r    )r   r'   r(   r)   )r   r'   r(   r+   )r0   r   )r5   r6   r7   r8   r9   r:   r0   r;   )r0   r;   )r0   r)   )r(   r)   r0   r;   )r0   rF   r"   )
rL   r   rM   rN   rJ   rO   rK   rO   r0   r   )rL   r   rJ   rO   rK   rO   r0   r   )
rL   r   rM   rN   rJ   rO   rK   rO   r0   r   )rL   r   r]   r^   r\   r_   r0   r;   )
rL   r   rM   rN   rK   rO   rh   ri   r0   rj   )r0   r}   )r   ri   r0   r   )r0   r   )r   )rh   ri   r0   r   )r0   r   )r0   r   )r   )r   ri   r   r   r0   r;   )r   r   rM   rN   r   r   r0   r   )__name__
__module____qualname__	__slots__r   r%   r3   r>   r=   propertyr(   setterrH   rQ   rf   r|   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r/   s   @r$   r   r       s       JI888 X8   X
 UYB B B B B B B B          ! ! ! X! / / / /
' ' ' '  !%
  $"     X 
  $"     X !%
  $"     * PU* * * * * *B !%.
 #. . . . . .`      4        &
 
 
 
   9 9 9 9" " " " "  !%'
 !%' ' ' ' ' ^'.M M M M M M M Mr&   r   r'   N))__doc__
__future__r   typesr   typingr   r   r   
contextlibr   collections.abcr	   r
    r   rX   r   abcr   r   r   r   r   r   rowsr   r   r   _compatr   r   rb   r   _cursor_baser   r   r   TransactionStatusrw   r   r   r&   r$   <module>r      s    # " " " " "       / / / / / / / / / / % % % % % % . . . . . . . .             / / / / / / / / / /         + + + + + + + + + + # # # # # # # #       $ $ $ $ $ $ '&&&&&&			$wM wM wM wM wMZ)3./ wM wM wM wM wMr&   