o
    DDi                     @  s  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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 Z0de-d < 	 dPd%d&Z1dQd(d)Z2dRd+d,Z3e4d-dSd3d4Z5dTd8d9Z6dUd<d=Z7dUd>d?Z8dVdAdBZ9dWdFdGZ:dXdIdJZ;dYdNdOZ<dS )Zz
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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!   E/var/www/Datamplify/venv/lib/python3.10/site-packages/psycopg/rows.py__call__1       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$   B   r%   zRowFactory.__call__N)r,   r-   r   r.   r&   r!   r!   r!   r#   r+   4   s    r+   c                   @  r   )
AsyncRowFactoryz@
    Like `RowFactory`, taking an async cursor as argument.
    _AsyncRowFactory__cursorAsyncCursor[Any]r   r.   c                 C  r   r    r!   )r"   r0   r!   r!   r#   r$   J   r%   zAsyncRowFactory.__call__N)r0   r1   r   r.   r&   r!   r!   r!   r#   r/   E       r/   c                   @  r   )
BaseRowFactoryzF
    Like `RowFactory`, taking either type of cursor as argument.
    _BaseRowFactory__cursorBaseCursor[Any, Any]r   r.   c                 C  r   r    r!   )r"   r4   r!   r!   r#   r$   R   r%   zBaseRowFactory.__call__N)r4   r5   r   r.   r&   r!   r!   r!   r#   r3   M   r2   r3   .r   DictRowcursorr5   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)r7   r!   r!   r#   	tuple_rowd   s   
r:   RowMaker[DictRow]c                   s&   t |   durd fd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                   s   t t | S r    dictzipr<   namesr!   r#   	dict_row_x   s   zdict_row.<locals>.dict_row_)r<   r   r   r=   
_get_names	no_result)r7   rD   r!   rB   r#   dict_rowq   s   rH   RowMaker[NamedTuple]c                   sH   | j   r"t  }dur"t| jg fd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  s    | ]}  |V  qd S r    )fname.0iresr!   r#   	<genexpr>   s    z!namedtuple_row.<locals>.<genexpr>)pgresult_get_nfields_make_nt	_encodingrange_makerG   )r7   nfieldsntr!   rN   r#   namedtuple_row   s   $rY   i   encstrrC   bytestype[NamedTuple]c                   s    t  fdd|D }td|S )Nc                 3  s    | ]
}t | V  qd S r    )r   decode)rL   nrZ   r!   r#   rP      s    z_make_nt.<locals>.<genexpr>r   )r9   r	   )rZ   rC   snamesr!   r`   r#   rS      s   
rS   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]]
    r7   r5   r   RowMaker[T]c                   (   t |   d urd fdd}|S tS )Nr<   r   r   r   c                       di t t| S Nr!   r>   rA   )rb   rC   r!   r#   class_row__      z2class_row.<locals>.class_row_.<locals>.class_row__r<   r   r   r   rE   )r7   rj   rb   rB   r#   
class_row_      zclass_row.<locals>.class_row_N)r7   r5   r   rf   r!   )rb   rn   r!   rm   r#   	class_row   s   
rp   funcCallable[..., T]c                   re   )	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   rf   c                   s   d fdd}|S )Nr<   r   r   r   c                   s    |  S r    r!   rA   rq   r!   r#   
args_row__      z/args_row.<locals>.args_row_.<locals>.args_row__rl   r!   )rs   rv   ru   r!   r#   	args_row_   s   zargs_row.<locals>.args_row_N)rs   rt   r   rf   r!   )rq   rx   r!   ru   r#   args_row   s   ry   c                   re   )	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.
    r7   rt   r   rf   c                   rg   )Nr<   r   r   r   c                   rh   ri   r>   rA   )rq   rC   r!   r#   kwargs_row__   rk   z5kwargs_row.<locals>.kwargs_row_.<locals>.kwargs_row__rl   rE   )r7   rz   ru   rB   r#   kwargs_row_   ro   zkwargs_row.<locals>.kwargs_row_N)r7   rt   r   rf   r!   )rq   r{   r!   ru   r#   
kwargs_row   s   r|   RowMaker[Any]c                 C  s>   | j  }rt| }dur|dk rtd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 expectedr<   r   r   r   c                 S  s   | d S )Nr   r!   rA   r!   r!   r#   scalar_row_   rw   zscalar_row.<locals>.scalar_row_)r<   r   r   r   )rQ   rR   eProgrammingErrorrG   )r7   rO   rW   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   InterfaceErrorrA   r!   r!   r#   rG      s   
rG   list[str] | Nonec                   s<   | j  rt }d ur| j  fddt|D S d S )Nc                   s   g | ]
} | qS r!   )rJ   r^   rK   rZ   rO   r!   r#   
<listcomp>   s    z_get_names.<locals>.<listcomp>)rQ   rR   rT   rU   )r7   rW   r!   r   r#   rF      s   rF   rO   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)rW   status	TUPLES_OKSINGLE_TUPLETUPLES_CHUNK
COMMAND_OK)rO   rW   r!   r!   r#   rR      s   


rR   )r7   r5   r   r8   )r7   r5   r   r;   )r7   r5   r   rI   )rZ   r[   rC   r\   r   r]   )rb   rc   r   rd   )rq   rr   r   rd   )r7   r5   r   r}   )r<   r   r   r   )r7   r5   r   r   )rO   r   r   r   )=r*   
__future__r   	functoolstypingr   r   r   r   r   r   collectionsr	   collections.abcr
   r    r   r   r   _compatr   
_encodingsr   r7   r   pq.abcr   _cursor_baser   cursor_asyncr   
ExecStatusr   r   r   r   r   r   r   r+   r/   r3   r9   r   __annotations__r?   r[   r6   r:   rH   rY   	lru_cacherS   rp   ry   r|   r   rG   rF   rR   r!   r!   r!   r#   <module>   sR     










