
    j                       U d Z ddlmZ ddl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mZ ddlmZ dd	lmZ dd
lmZ ddlmZ erddlmZ ddlmZ ddlmZ ddlmZ ej         j!        Z!ej         j"        Z"ej         j#        Z#ej         j$        Z$ edd          Z% eddd          Z& G d de	e&                   Z' G d de	e&                   Z( G d de	e&                   Z) G d de	e&                   Z*e+edf         Z,de-d<   	 e.e/ef         Z0de-d <   	 dEd%Z1dFd'Z2dGd)Z3 ej4        d*          dHd0            Z5dId4Z6dJd7Z7dJd8Z8dKd:Z9dLd>Z:dMd@Z;dNdDZ<dS )Oz
psycopg row factories
    )annotationsN)TYPE_CHECKINGAny
NamedTupleNoReturnProtocol	TypeAlias)
namedtuple)CallableSequence   )errors)pq)TypeVar)_as_python_identifier)Cursor)PGresult)
BaseCursor)AsyncCursorTT)	covariantRowTupleRow)r   defaultc                      e Zd ZdZddZdS )	RowMakera  
    Callable protocol taking a sequence of value and returning an object.

    The sequence of value is what is returned from a database query, already
    adapted to the right Python types. The return value is the object that your
    program would like to receive: by default (`tuple_row()`) it is a simple
    tuple, but it may be any type of object.

    Typically, `!RowMaker` functions are returned by `RowFactory`.
    _RowMaker__valuesSequence[Any]returnr   c                    d S N )selfr   s     W/DATA/AppData/hermes/projects/honcho/.venv/lib/python3.11/site-packages/psycopg/rows.py__call__zRowMaker.__call__1             N)r   r   r   r   __name__
__module____qualname____doc__r%   r"   r'   r$   r   r   %   s(        	 	 <;;;;;r'   r   c                      e Zd ZdZddZdS )	
RowFactorya  
    Callable protocol taking a `~psycopg.Cursor` and returning a `RowMaker`.

    A `!RowFactory` is typically called when a `!Cursor` receives a result.
    This way it can inspect the cursor state (for instance the
    `~psycopg.Cursor.description` attribute) and help a `!RowMaker` to create
    a complete object.

    For instance the `dict_row()` `!RowFactory` uses the names of the column to
    define the dictionary key and returns a `!RowMaker` function which would
    use the values to create a dictionary for each record.
    _RowFactory__cursorCursor[Any]r   RowMaker[Row]c                    d S r!   r"   )r#   r/   s     r$   r%   zRowFactory.__call__B   r&   r'   N)r/   r0   r   r1   r(   r"   r'   r$   r.   r.   4   s)          DCCCCCr'   r.   c                      e Zd ZdZddZdS )	AsyncRowFactoryz@
    Like `RowFactory`, taking an async cursor as argument.
    _AsyncRowFactory__cursorAsyncCursor[Any]r   r1   c                    d S r!   r"   )r#   r5   s     r$   r%   zAsyncRowFactory.__call__J   r&   r'   N)r5   r6   r   r1   r(   r"   r'   r$   r4   r4   E   s)          IHHHHHr'   r4   c                      e Zd ZdZddZdS )	BaseRowFactoryzF
    Like `RowFactory`, taking either type of cursor as argument.
    _BaseRowFactory__cursorBaseCursor[Any, Any]r   r1   c                    d S r!   r"   )r#   r:   s     r$   r%   zBaseRowFactory.__call__R   r&   r'   N)r:   r;   r   r1   r(   r"   r'   r$   r9   r9   M   s)          MLLLLLr'   r9   .r	   DictRowcursorr;   r   RowMaker[TupleRow]c                    t           S )zRow factory to represent rows as simple tuples.

    This is the default factory, used when `~psycopg.Connection.connect()` or
    `~psycopg.Connection.cursor()` are called without a `!row_factory`
    parameter.

    )tuple)r>   s    r$   	tuple_rowrB   d   s	     Lr'   RowMaker[DictRow]c                D    t          |           xdfd}|S t          S )zRow factory to represent rows as dictionaries.

    The dictionary keys are taken from the column names of the returned columns.
    Nvaluesr   r   dict[str, Any]c                >    t          t          |                     S r!   dictzip)rE   namess    r$   	dict_row_zdict_row.<locals>.dict_row_x   s    E6**+++r'   )rE   r   r   rF   
_get_names	no_result)r>   rL   rK   s     @r$   dict_rowrP   q   sF    
 F###0	, 	, 	, 	, 	, 	,  r'   RowMaker[NamedTuple]c                    | j         xrCt                    x}2t          | j        gfdt	          |          D             R  }|j        S t          S )zRow factory to represent rows as `~collections.namedtuple`.

    The field names are taken from the column names of the returned columns,
    with some mangling to deal with invalid names.
    Nc              3  B   K   | ]}                     |          V  d S r!   )fname).0iress     r$   	<genexpr>z!namedtuple_row.<locals>.<genexpr>   s-      )O)O1#))A,,)O)O)O)O)O)Or'   )pgresult_get_nfields_make_nt	_encodingrange_makerO   )r>   nfieldsntrW   s      @r$   namedtuple_rowra      sf      S0A0A%AW$Nf&P)O)O)O)Og)O)O)OPPPxr'   i   encstrrK   bytestype[NamedTuple]c                Z     t           fd|D                       }t          d|          S )Nc              3  \   K   | ]&}t          |                                        V  'd S r!   )r   decode)rU   nrb   s     r$   rX   z_make_nt.<locals>.<genexpr>   s6      GGA(#77GGGGGGr'   r   )rA   r
   )rb   rK   snamess   `  r$   r[   r[      s5    GGGGGGGGGFeV$$$r'   clstype[T]BaseRowFactory[T]c                     d fd}|S )aQ  Generate a row factory to represent rows as instances of the class `!cls`.

    The class must support every output column name as a keyword parameter.

    :param cls: The class to return for each row. It must support the fields
        returned by the query as keyword arguments.
    :rtype: `!Callable[[Cursor],` `RowMaker`\[~T]]
    r>   r;   r   RowMaker[T]c                H    t          |           x	dfd}|S t          S )NrE   r   r   r   c           
     J     di t          t          |                     S Nr"   rH   )rE   rk   rK   s    r$   class_row__z2class_row.<locals>.class_row_.<locals>.class_row__   s+    s66T#eV"4"455666r'   rE   r   r   r   rM   )r>   rs   rK   rk   s     @r$   
class_row_zclass_row.<locals>.class_row_   sJ    '''E47 7 7 7 7 7 7  r'   )r>   r;   r   ro   r"   )rk   ru   s   ` r$   	class_rowrv      s)    	 	 	 	 	 	 r'   funcCallable[..., T]c                     d fd}|S )zGenerate a row factory calling `!func` with positional parameters for every row.

    :param func: The function to call for each row. It must support the fields
        returned by the query as positional arguments.
    curBaseCursor[Any, T]r   ro   c                    dfd}|S )NrE   r   r   r   c                     |  S r!   r"   )rE   rw   s    r$   
args_row__z/args_row.<locals>.args_row_.<locals>.args_row__   s    4= r'   rt   r"   )rz   r~   rw   s     r$   	args_row_zargs_row.<locals>.args_row_   s)    	! 	! 	! 	! 	! 	! r'   )rz   r{   r   ro   r"   )rw   r   s   ` r$   args_rowr      s)          r'   c                     d fd}|S )zGenerate a row factory calling `!func` with keyword parameters for every row.

    :param func: The function to call for each row. It must support the fields
        returned by the query as keyword arguments.
    r>   r{   r   ro   c                H    t          |           x	dfd}|S t          S )NrE   r   r   r   c           
     J     di t          t          |                     S rr   rH   )rE   rw   rK   s    r$   kwargs_row__z5kwargs_row.<locals>.kwargs_row_.<locals>.kwargs_row__   s+    t77d3uf#5#566777r'   rt   rM   )r>   r   rK   rw   s     @r$   kwargs_row_zkwargs_row.<locals>.kwargs_row_   sJ    '''E48 8 8 8 8 8 8   r'   )r>   r{   r   ro   r"   )rw   r   s   ` r$   
kwargs_rowr      s)    	 	 	 	 	 	 r'   RowMaker[Any]c                    | j         x}r1t          |          x} |dk     rt          j        d          d	d}|S t          S )
zR
    Generate a row factory returning the first column
    as a scalar value.
    Nr   zat least one column expectedrE   r   r   r   c                    | d         S )Nr   r"   rE   s    r$   scalar_row_zscalar_row.<locals>.scalar_row_   s    !9r'   )rE   r   r   r   )rY   rZ   eProgrammingErrorrO   )r>   rW   r_   r   s       r$   
scalar_rowr      sd    
  
S0A0A%AW$NQ;;$%CDDD	 	 	 	  r'   rE   r   r   c                *    t          j        d          )zA `RowMaker` that always fail.

    It can be used as return value for a `RowFactory` called with no result.
    Note that the `!RowFactory` *will* be called with no result, but the
    resulting `!RowMaker` never should.
    z the cursor doesn't have a result)r   InterfaceErrorr   s    r$   rO   rO      s     
=
>
>>r'   list[str] | Nonec                    | j         xr4t                    x}#| j        fdt          |          D             S d S )Nc                `    g | ]*}                     |                                        +S r"   )rT   rh   )rU   rV   rb   rW   s     r$   
<listcomp>z_get_names.<locals>.<listcomp>   s>     
 
 
)*CIIaLL$$
 
 
r'   )rY   rZ   r\   r]   )r>   r_   rb   rW   s     @@r$   rN   rN      sj     S0A0A%AW$N
 
 
 
 
.3Gnn
 
 
 	
 tr'   rW   r   
int | Nonec                    | j         }| j        t          k    s2| j        t          k    s"| j        t          k    s| j        t
          k    r|r|S dS )z
    Return the number of columns in a result, if it returns tuples else None

    Take into account the special case of results with zero columns.
    N)r_   status	TUPLES_OKSINGLE_TUPLETUPLES_CHUNK
COMMAND_OK)rW   r_   s     r$   rZ   rZ      sP     kG 	
i:%%:%%J*$$$tr'   )r>   r;   r   r?   )r>   r;   r   rC   )r>   r;   r   rQ   )rb   rc   rK   rd   r   re   )rk   rl   r   rm   )rw   rx   r   rm   )r>   r;   r   r   )rE   r   r   r   )r>   r;   r   r   )rW   r   r   r   )=r,   
__future__r   	functoolstypingr   r   r   r   r   r	   collectionsr
   collections.abcr   r    r   r   r   _compatr   
_encodingsr   r>   r   pq.abcr   _cursor_baser   cursor_asyncr   
ExecStatusr   r   r   r   r   r   r   r.   r4   r9   rA   r   __annotations__rI   rc   r=   rB   rP   ra   	lru_cacher[   rv   r   r   r   rO   rN   rZ   r"   r'   r$   <module>r      s     # " " " " "     P P P P P P P P P P P P P P P P " " " " " " . . . . . . . .                   - - - - - - *      (((((())))))]%
M#	})})GC4    getZ888< < < < <x} < < <D D D D D# D D D"I I I I Ihsm I I IM M M M MXc] M M M CHo % % % %
 #s(^ # # # #
 
 
 
    
 
 
 
 S% % % %
   0       *   $? ? ? ?        r'   