o
    6	fv                     @   sX  U d Z ddlZddlmZmZmZmZmZmZm	Z	 ddlm
Z
mZmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ e
raddlmZ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dddZ&edddZ'G dd dee' Z(G dd dee' Z)G dd dee' Z*G dd dee' Z+eedf Z,ee-d< 	 ee.ef Z/ee-d< 	 d@d"d#Z0dAd%d&Z1			 	'dBd(d)Z2e3d*d+e.d,e4d ee fd-d.Z5d/ee& d e+e& fd0d1Z6d2ede&f d e+e& fd3d4Z7d2ede&f d e+e& fd5d6Z8d7ee d e	fd8d9Z9ddd eee.  fd:d;Z:d<d=d ee; fd>d?Z<dS )Cz
psycopg row factories
    N)AnyCallableDictListOptional
NamedTupleNoReturn)TYPE_CHECKINGSequenceTupleTypeTypeVar)
namedtuple)	TypeAlias   )pq)errors)Protocol)_as_python_identifier)
BaseCursorCursor)AsyncCursor)PGresultTT)	covariantRowc                   @   s&   e Zd ZdZdee def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returnc                 C      d S N )selfr   r!   r!   E/home/ertert/spirit/venv/lib/python3.10/site-packages/psycopg/rows.py__call__.       zRowMaker.__call__N)__name__
__module____qualname____doc__r
   r   r   r$   r!   r!   r!   r#   r   "   s    r   c                   @   &   e Zd ZdZdddee f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__cursorzCursor[Any]r   c                 C   r   r    r!   )r"   r,   r!   r!   r#   r$   ?   r%   zRowFactory.__call__Nr&   r'   r(   r)   r   r   r$   r!   r!   r!   r#   r+   1   s    r+   c                   @   r*   )AsyncRowFactoryz@
    Like `RowFactory`, taking an async cursor as argument.
    _AsyncRowFactory__cursorzAsyncCursor[Any]r   c                 C   r   r    r!   )r"   r/   r!   r!   r#   r$   G   r%   zAsyncRowFactory.__call__Nr-   r!   r!   r!   r#   r.   B       r.   c                   @   r*   )BaseRowFactoryzF
    Like `RowFactory`, taking either type of cursor as argument.
    _BaseRowFactory__cursorBaseCursor[Any, Any]r   c                 C   r   r    r!   )r"   r2   r!   r!   r#   r$   O   r%   zBaseRowFactory.__call__Nr-   r!   r!   r!   r#   r1   J   r0   r1   .TupleRowDictRowcursorr3   r   RowMaker[TupleRow]c                 C   s   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)r6   r!   r!   r#   	tuple_rowa   s   
r9   RowMaker[DictRow]c                    s:   t |   du r
tS dtt dtttf f fdd}|S )zRow factory to represent rows as dictionaries.

    The dictionary keys are taken from the column names of the returned columns.
    Nvaluesr   c                    s   t t | S r    dictzipr;   namesr!   r#   	dict_row_w   s   zdict_row.<locals>.dict_row_)
_get_names	no_resultr
   r   r   str)r6   rB   r!   r@   r#   dict_rown   s
   "rF   RowMaker[NamedTuple]c                    sL   | j   stS t }|du rtS t| jg fddt|D R  }|j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   s    | ]}  |V  qd S r    )fname.0iresr!   r#   	<genexpr>   s    z!namedtuple_row.<locals>.<genexpr>)pgresultrD   _get_nfields_make_nt	_encodingrange_make)r6   nfieldsntr!   rL   r#   namedtuple_row}   s   $rW   i   encrA   c                    s    t  fdd|D }td|S )Nc                 3   s    | ]
}t | V  qd S r    )r   decode)rJ   nrX   r!   r#   rN      s    z_make_nt.<locals>.<genexpr>r   )r8   r   )rX   rA   snamesr!   r[   r#   rQ      s   
rQ   clsc                       d fd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]]
    r6   r3   r   RowMaker[T]c                    4   t |   d u r
tS dtt dtf fdd}|S )Nr;   r   c                        di t t| S Nr!   r<   r?   )r]   rA   r!   r#   class_row__      z2class_row.<locals>.class_row_.<locals>.class_row__rC   rD   r
   r   r   )r6   rc   r]   r@   r#   
class_row_   
   zclass_row.<locals>.class_row_N)r6   r3   r   r_   r!   )r]   rg   r!   rf   r#   	class_row   s   

ri   funcc                    r^   )	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   r_   c                    s   dt t dtf fdd}|S )Nr;   r   c                    s    |  S r    r!   r?   rj   r!   r#   
args_row__   s   z/args_row.<locals>.args_row_.<locals>.args_row__)r
   r   r   )rk   rn   rm   r!   r#   	args_row_   s   zargs_row.<locals>.args_row_N)rk   rl   r   r_   r!   )rj   ro   r!   rm   r#   args_row   s   rp   c                    r^   )	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.
    r6   rl   r   r_   c                    r`   )Nr;   r   c                    ra   rb   r<   r?   )rj   rA   r!   r#   kwargs_row__   rd   z5kwargs_row.<locals>.kwargs_row_.<locals>.kwargs_row__re   )r6   rq   rm   r@   r#   kwargs_row_   rh   zkwargs_row.<locals>.kwargs_row_N)r6   rl   r   r_   r!   )rj   rr   r!   rm   r#   
kwargs_row   s   
rs   r;   c                 C   s
   t 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)eInterfaceErrorr?   r!   r!   r#   rD      s   
rD   c                    s@   | j sd S t}|d u rd S | j  fddt|D S )Nc                    s   g | ]
} | qS r!   )rH   rY   rI   rX   rM   r!   r#   
<listcomp>   s    z_get_names.<locals>.<listcomp>)rO   rP   rR   rS   )r6   rU   r!   rv   r#   rC      s   rC   rM   r   c                 C   s0   | j }| 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)rU   status	TUPLES_OKSINGLE_TUPLE
COMMAND_OK)rM   rU   r!   r!   r#   rP      s   

rP   )r6   r3   r   r7   )r6   r3   r   r:   )r6   r3   r   rG   )=r)   	functoolstypingr   r   r   r   r   r   r   r	   r
   r   r   r   collectionsr   typing_extensionsr    r   r   rt   _compatr   
_encodingsr   r6   r   r   cursor_asyncr   psycopg.pq.abcr   
ExecStatusr{   ry   rz   r   r   r   r+   r.   r1   r4   __annotations__rE   r5   r9   rF   rW   	lru_cachebytesrQ   ri   rp   rs   rD   rC   intrP   r!   r!   r!   r#   <module>   sT    $



