o
    "4g                     @  s  U d Z ddlmZ ddlZddlmZmZ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 dd
lmZ ddlmZmZ ddlmZ e	red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dZ&ed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,d e-d< 	 ee.ef Z/d e-d!< 	 dQd&d'Z0dRd)d*Z1dSd,d-Z2e3d.dTd4d5Z4dUd9d:Z5dVd=d>Z6dVd?d@Z7dWdBdCZ8dXdGdHZ9dYdJdKZ:dZdOdPZ;dS )[z
psycopg row factories
    )annotationsN)AnyCallable
NamedTupleNoReturn)TYPE_CHECKINGProtocolSequence)DictTuple)
namedtuple   )pq)errors)	TypeAliasTypeVar)_as_python_identifier)Cursor)
BaseCursor)AsyncCursor)PGresultTT)	covariantRowTupleRow)r   defaultc                   @     e Zd ZdZd	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                 C     d S N )selfr   r#   r#   U/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/rows.py__call__2       zRowMaker.__call__N)r   r   r    r   __name__
__module____qualname____doc__r&   r#   r#   r#   r%   r   &   s    r   c                   @  r   )

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                 C  r!   r"   r#   )r$   r.   r#   r#   r%   r&   C   r'   zRowFactory.__call__N)r.   r/   r    r0   r(   r#   r#   r#   r%   r-   5   s    r-   c                   @  r   )
AsyncRowFactoryz@
    Like `RowFactory`, taking an async cursor as argument.
    _AsyncRowFactory__cursorAsyncCursor[Any]r    r0   c                 C  r!   r"   r#   )r$   r2   r#   r#   r%   r&   K   r'   zAsyncRowFactory.__call__N)r2   r3   r    r0   r(   r#   r#   r#   r%   r1   F       r1   c                   @  r   )
BaseRowFactoryzF
    Like `RowFactory`, taking either type of cursor as argument.
    _BaseRowFactory__cursorBaseCursor[Any, Any]r    r0   c                 C  r!   r"   r#   )r$   r6   r#   r#   r%   r&   S   r'   zBaseRowFactory.__call__N)r6   r7   r    r0   r(   r#   r#   r#   r%   r5   N   r4   r5   .r   DictRowcursorr7   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)r9   r#   r#   r%   	tuple_rowe   s   
r<   RowMaker[DictRow]c                   s&   t |   du r
tS d 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   r    dict[str, Any]c                   s   t t | S r"   dictzipr>   namesr#   r%   	dict_row_{   s   zdict_row.<locals>.dict_row_)r>   r   r    r?   
_get_names	no_result)r9   rF   r#   rD   r%   dict_rowr   s
   rJ   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>)pgresultrI   _get_nfields_make_nt	_encodingrange_make)r9   nfieldsntr#   rP   r%   namedtuple_row   s   $r[   i   encstrrE   bytestype[NamedTuple]c                   s    t  fdd|D }td|S )Nc                 3  s    | ]
}t | V  qd S r"   )r   decode)rN   nr\   r#   r%   rR      s    z_make_nt.<locals>.<genexpr>r   )r;   r   )r\   rE   snamesr#   rb   r%   rU      s   
rU   clstype[T]BaseRowFactory[T]c                      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]]
    r9   r7   r    RowMaker[T]c                   (   t |   d u r
tS d fdd}|S )Nr>   r   r    r   c                       di t t| S Nr#   r@   rC   )rd   rE   r#   r%   class_row__      z2class_row.<locals>.class_row_.<locals>.class_row__r>   r   r    r   rG   )r9   rl   rd   rD   r%   
class_row_   
   zclass_row.<locals>.class_row_N)r9   r7   r    rh   r#   )rd   rp   r#   ro   r%   	class_row   s   

rr   funcCallable[..., T]c                   rg   )	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    rh   c                   s   d fdd}|S )Nr>   r   r    r   c                   s    |  S r"   r#   rC   rs   r#   r%   
args_row__      z/args_row.<locals>.args_row_.<locals>.args_row__rn   r#   )ru   rx   rw   r#   r%   	args_row_   s   zargs_row.<locals>.args_row_N)ru   rv   r    rh   r#   )rs   rz   r#   rw   r%   args_row   s   r{   c                   rg   )	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.
    r9   rv   r    rh   c                   ri   )Nr>   r   r    r   c                   rj   rk   r@   rC   )rs   rE   r#   r%   kwargs_row__   rm   z5kwargs_row.<locals>.kwargs_row_.<locals>.kwargs_row__rn   rG   )r9   r|   rw   rD   r%   kwargs_row_   rq   zkwargs_row.<locals>.kwargs_row_N)r9   rv   r    rh   r#   )rs   r}   r#   rw   r%   
kwargs_row   s   
r~   RowMaker[Any]c                 C  sB   | j }|stS t|}|du rtS |dk rtdd
dd	}|S )zR
    Generate a row factory returning the first column
    as a scalar value.
    Nr   zat least one column expectedr>   r   r    r   c                 S  s   | d S )Nr   r#   rC   r#   r#   r%   scalar_row_   ry   zscalar_row.<locals>.scalar_row_)r>   r   r    r   )rS   rI   rT   eProgrammingError)r9   rQ   rY   r   r#   r#   r%   
scalar_row   s   

r   r>   r   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)r   InterfaceErrorrC   r#   r#   r%   rI      s   
rI   list[str] | Nonec                   s@   | j sd S t}|d u rd S | j  fddt|D S )Nc                   s   g | ]
} | qS r#   )rL   r`   rM   r\   rQ   r#   r%   
<listcomp>   s    z_get_names.<locals>.<listcomp>)rS   rT   rV   rW   )r9   rY   r#   r   r%   rH      s   rH   rQ   r   
int | Nonec                 C  s:   | j }| jtks| 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)rY   status	TUPLES_OKSINGLE_TUPLETUPLES_CHUNK
COMMAND_OK)rQ   rY   r#   r#   r%   rT     s   


rT   )r9   r7   r    r:   )r9   r7   r    r=   )r9   r7   r    rK   )r\   r]   rE   r^   r    r_   )rd   re   r    rf   )rs   rt   r    rf   )r9   r7   r    r   )r>   r   r    r   )r9   r7   r    r   )rQ   r   r    r   )<r,   
__future__r   	functoolstypingr   r   r   r   r   r   r	   r
   r   collectionsr    r   r   r   _compatr   r   
_encodingsr   r9   r   _cursor_baser   cursor_asyncr   psycopg.pq.abcr   
ExecStatusr   r   r   r   r   r   r   r-   r1   r5   r   __annotations__r]   r8   r<   rJ   r[   	lru_cacherU   rr   r{   r~   r   rI   rH   rT   r#   r#   r#   r%   <module>   sT    









