o
    TPf                     @  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 ddlmZ dd	lmZ dd
lmZ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)dZ*de+d< 	 d Z,de+d!< 	 dQd&d'Z-dRd)d*Z.dSd,d-Z/e0d.dTd4d5Z1dUd9d:Z2dVd=d>Z3dVd?d@Z4dWdBdCZ5dXdGdHZ6dYdJdKZ7dZdOdPZ8dS )[z
psycopg row factories
    )annotationsN)AnyCallable
NamedTupleNoReturn)TYPE_CHECKINGProtocolSequence)
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!   X/var/www/html/Testing_prj/Navya-Bakers/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   ztuple[Any, ...]r   dict[str, Any]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)r8   r!   r!   r#   	tuple_rowd   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   r6   c                   s   t t | S r    dictzipr=   namesr!   r#   	dict_row_z   s   zdict_row.<locals>.dict_row_)r=   r   r   r6   
_get_names	no_result)r8   rD   r!   rB   r#   dict_rowq   s
   rH   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>)pgresultrG   _get_nfields_make_nt	_encodingrange_make)r8   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   )r:   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]]
    r8   r5   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>   rA   )rb   rC   r!   r#   class_row__      z2class_row.<locals>.class_row_.<locals>.class_row__r=   r   r   r   rE   )r8   rj   rb   rB   r#   
class_row_   
   zclass_row.<locals>.class_row_N)r8   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.
    r8   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   )r8   rz   ru   rB   r#   kwargs_row_   ro   zkwargs_row.<locals>.kwargs_row_N)r8   rt   r   rf   r!   )rq   r{   r!   ru   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!   rA   r!   r!   r#   scalar_row_   rw   zscalar_row.<locals>.scalar_row_)r=   r   r   r   )rQ   rG   rR   eProgrammingError)r8   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 sd S t}|d u rd S | j  fddt|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   )r8   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   )r8   r5   r   r9   )r8   r5   r   r<   )r8   r5   r   rI   )rZ   r[   rC   r\   r   r]   )rb   rc   r   rd   )rq   rr   r   rd   )r8   r5   r   r}   )r=   r   r   r   )r8   r5   r   r   )rO   r   r   r   )9r*   
__future__r   	functoolstypingr   r   r   r   r   r   r	   collectionsr
    r   r   r   _compatr   r   
_encodingsr   r8   r   _cursor_baser   cursor_asyncr   psycopg.pq.abcr   
ExecStatusr   r   r   r   r   r   r   r+   r/   r3   r   __annotations__r7   r;   rH   rY   	lru_cacherS   rp   ry   r|   r   rG   rF   rR   r!   r!   r!   r#   <module>   sR    









