
    S?Nj                       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     ]/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/psycopg/rows.py__call__zRowMaker.__call__1   s        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   s    r$   N)r/   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   s    Sr$   N)r4   zAsyncCursor[Any]r&   r0   r'   r   r$   r"   r2   r2   E   s     Ir$   r2   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   s    r$   N)r8   BaseCursor[Any, Any]r&   r0   r'   r   r$   r"   r6   r6   M   s     Mr$   r6   .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$   )rD   r%   r&   zdict[str, Any]
_get_names	no_result)r=   rF   rE   s     @r"   dict_rowrJ   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     )O1#))A,s   )pgresult_get_nfields_make_nt	_encodingrange_makerI   )r=   nfieldsntrP   s      @r"   namedtuple_rowrZ      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)rN   nencs     r"   rQ   z_make_nt.<locals>.<genexpr>   s      GA(#7s   $'r   )r<   r
   )r_   rE   snamess   `  r"   rT   rT      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   rA   )rD   clsrE   s    r"   class_row__z2class_row.<locals>.class_row_.<locals>.class_row__   s    6T#eV"4566r$   rD   r%   r&   r   rG   )r=   rf   rE   re   s     @r"   
class_row_zclass_row.<locals>.class_row_   s(    ''E47  r$   )r=   r9   r&   RowMaker[T]r   )re   rh   s   ` r"   	class_rowrj      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   )rD   funcs    r"   
args_row__z/args_row.<locals>.args_row_.<locals>.args_row__   s    = r$   rg   r   )curro   rn   s     r"   	args_row_zargs_row.<locals>.args_row_   s    	! r$   )rp   BaseCursor[Any, T]r&   ri   r   )rn   rq   s   ` r"   args_rowrs      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 rd   rA   )rD   rn   rE   s    r"   kwargs_row__z5kwargs_row.<locals>.kwargs_row_.<locals>.kwargs_row__   s    7d3uf#5677r$   rg   rG   )r=   rw   rE   rn   s     @r"   kwargs_row_zkwargs_row.<locals>.kwargs_row_   s(    ''E48   r$   )r=   rr   r&   ri   r   )rn   rx   s   ` r"   
kwargs_rowry      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   rD   s    r"   scalar_row_zscalar_row.<locals>.scalar_row_   s    !9r$   )rD   r%   r&   r   )rR   rS   eProgrammingErrorrI   )r=   rP   rX   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"   rI   rI      s     

=
>>r$   c                    | j                   x}rPt        |      x}C| j                  }t        |      D cg c]"  }|j	                  |      j                  |      $ c}S y c c}w r   )rR   rS   rU   rV   rM   r]   )r=   rP   rX   r_   rO   s        r"   rH   rH      sh    S0A%AW$N.3Gn
.<CIIaL$n
 	
 	
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)rX   status	TUPLES_OKSINGLE_TUPLETUPLES_CHUNK
COMMAND_OK)rP   rX   s     r"   rS   rS      sI     kkG 	

i::%::%JJ*$r$   )r=   r9   r&   zRowMaker[TupleRow])r=   r9   r&   zRowMaker[DictRow])r=   r9   r&   zRowMaker[NamedTuple])r_   strrE   bytesr&   ztype[NamedTuple])re   ztype[T]r&   BaseRowFactory[T])rn   zCallable[..., T]r&   r   )r=   r9   r&   zRowMaker[Any])rD   r%   r&   r   )r=   r9   r&   zlist[str] | None)rP   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-   r2   r6   r<   r   __annotations__rB   r   r:   r>   rJ   rZ   	lru_cacherT   rj   rs   ry   r   rI   rH   rS   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$   