
    2i                       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@                  jB                  Z!ej@                  jD                  Z"ej@                  jF                  Z#ej@                  jH                  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 <   	 d-d!Z1d.d"Z2d/d#Z3 ejh                  d$      d0d%       Z5d1d&Z6d2d'Z7d2d(Z8d3d)Z9d4d*Z:d5d+Z;d6d,Z<y)7z
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y)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`.
    c                     y N )self_RowMaker__valuess     K/var/www/html/secretsanta/venv/lib/python3.12/site-packages/psycopg/rows.py__call__zRowMaker.__call__1           N)r!   Sequence[Any]returnr   __name__
__module____qualname____doc__r#   r   r%   r"   r   r   %   s    	 <r%   r   c                      e Zd ZdZddZy)
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.
    c                     y r   r   )r    _RowFactory__cursors     r"   r#   zRowFactory.__call__B   r$   r%   N)r0   zCursor[Any]r'   RowMaker[Row]r(   r   r%   r"   r.   r.   4   s     Dr%   r.   c                      e Zd ZdZddZy)AsyncRowFactoryz@
    Like `RowFactory`, taking an async cursor as argument.
    c                     y r   r   )r    _AsyncRowFactory__cursors     r"   r#   zAsyncRowFactory.__call__J   r$   r%   N)r5   zAsyncCursor[Any]r'   r1   r(   r   r%   r"   r3   r3   E   s     Ir%   r3   c                      e Zd ZdZddZy)BaseRowFactoryzF
    Like `RowFactory`, taking either type of cursor as argument.
    c                     y r   r   )r    _BaseRowFactory__cursors     r"   r#   zBaseRowFactory.__call__R   r$   r%   N)r9   BaseCursor[Any, Any]r'   r1   r(   r   r%   r"   r7   r7   M   s     Mr%   r7   .r	   DictRowc                    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)cursors    r"   	tuple_rowr?   d   s	     Lr%   c                :    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.
    c                .    t        t        |             S r   dictzip)valuesnamess    r"   	dict_row_zdict_row.<locals>.dict_row_x   s    E6*++r%   )rE   r&   r'   zdict[str, Any]
_get_names	no_result)r>   rG   rF   s     @r"   dict_rowrK   q   s*    
 F##0	,  r%   c                    | j                   xrAt              x}4t        | j                  gfdt	        |      D         }|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.
    c              3  @   K   | ]  }j                  |        y wr   )fname).0iress     r"   	<genexpr>z!namedtuple_row.<locals>.<genexpr>   s     )O1#))A,)Os   )pgresult_get_nfields_make_nt	_encodingrange_makerJ   )r>   nfieldsntrQ   s      @r"   namedtuple_rowr[      sQ     S0A%AW$Nf&&P)Og)OPxxr%   i   c                D     t         fd|D              }t        d|      S )Nc              3  R   K   | ]  }t        |j                                 y wr   )r   decode)rO   nencs     r"   rR   z_make_nt.<locals>.<genexpr>   s     GA(#7Gs   $'r   )r=   r
   )r`   rF   snamess   `  r"   rU   rU      s     GGGFeV$$r%   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]]
    c                >    t        |       x	dfd}|S t        S )Nc           
     :     di t        t        |             S Nr   rB   )rE   clsrF   s    r"   class_row__z2class_row.<locals>.class_row_.<locals>.class_row__   s    6T#eV"4566r%   rE   r&   r'   r   rH   )r>   rg   rF   rf   s     @r"   
class_row_zclass_row.<locals>.class_row_   s(    ''E47  r%   )r>   r:   r'   RowMaker[T]r   )rf   ri   s   ` r"   	class_rowrk      s    	 r%   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.
    c                    dfd}|S )Nc                     |  S r   r   )rE   funcs    r"   
args_row__z/args_row.<locals>.args_row_.<locals>.args_row__   s    = r%   rh   r   )currp   ro   s     r"   	args_row_zargs_row.<locals>.args_row_   s    	! r%   )rq   BaseCursor[Any, T]r'   rj   r   )ro   rr   s   ` r"   args_rowrt      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.
    c                >    t        |       x	dfd}|S t        S )Nc           
     :     di t        t        |             S re   rB   )rE   ro   rF   s    r"   kwargs_row__z5kwargs_row.<locals>.kwargs_row_.<locals>.kwargs_row__   s    7d3uf#5677r%   rh   rH   )r>   rx   rF   ro   s     @r"   kwargs_row_zkwargs_row.<locals>.kwargs_row_   s(    ''E48   r%   )r>   rs   r'   rj   r   )ro   ry   s   ` r"   
kwargs_rowrz      s    	 r%   c                    | j                   x}r-t        |      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.
    r   zat least one column expectedc                    | d   S )Nr   r   rE   s    r"   scalar_row_zscalar_row.<locals>.scalar_row_   s    !9r%   )rE   r&   r'   r   )rS   rT   eProgrammingErrorrJ   )r>   rQ   rY   r~   s       r"   
scalar_rowr      sP    
 S0A%AW$NQ;$$%CDD	  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"   rJ   rJ      s     

=
>>r%   c                    | j                   x}rPt        |      x}C| j                  }t        |      D cg c]"  }|j	                  |      j                  |      $ c}S y c c}w r   )rS   rT   rV   rW   rN   r^   )r>   rQ   rY   r`   rP   s        r"   rI   rI      sd    S0A%AW$N.3Gn
)*CIIaL$
 	
 	
s   'A c                    | j                   }| j                  t        k(  s;| j                  t        k(  s(| j                  t        k(  s| j                  t
        k(  r|r|S y)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)rY   status	TUPLES_OKSINGLE_TUPLETUPLES_CHUNK
COMMAND_OK)rQ   rY   s     r"   rT   rT      sI     kkG 	

i::%::%JJ*$r%   )r>   r:   r'   zRowMaker[TupleRow])r>   r:   r'   zRowMaker[DictRow])r>   r:   r'   zRowMaker[NamedTuple])r`   strrF   bytesr'   ztype[NamedTuple])rf   ztype[T]r'   BaseRowFactory[T])ro   zCallable[..., T]r'   r   )r>   r:   r'   zRowMaker[Any])rE   r&   r'   r   )r>   r:   r'   zlist[str] | None)rQ   r   r'   z
int | None)=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.   r3   r7   r=   r   __annotations__rC   r   r;   r?   rK   r[   	lru_cacherU   rk   rt   rz   r   rJ   rI   rT   r   r%   r"   <module>r      s\   #  P P " .    - ()]]%%
MM##	}}))}}))C4  etZ8<x} <D# D"Ihsm IMXc] M CHo) %
 #s(^ #
 
 S% %
0 *$?r%   