U
    d*                     @  s4  d Z ddlmZ ddlmZ ddlmZ ddlZddl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 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 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ses6ddlmZ ddlmZ ddlm Z  n$ddl!mZ ddl!mZ ddl!m Z  erddl"m#Z# ddl"m$Z$ ddl%m&Z& ede	dZ'ed ee	d!f dZ(G d"d# d#eee	 ee( Z)eZ*e)Z+G d$d% d%eZ,G d&d' d'e,ej-e. ej/e	 Z0G d(d) d)e,ej1e.e	f Z2G d*d+ d+eeje.e	f Z3dS ),z.Define row constructs including :class:`.Row`.    )annotations)ABCN)Any)Callable)Dict)Generic)Iterator)List)Mapping)NoReturn)Optional)overload)Sequence)Tuple)TYPE_CHECKING)TypeVar)Union   )util)HAS_CYEXTENSION   )BaseRow)KEY_INTEGER_ONLY)KEY_OBJECTS_ONLY)_KeyType)	RMKeyView)_ResultProcessorType_T)bound_TP.c                   @  s  e Zd ZdZdZeZddddddZddd	d
dZddddZ	e
ddddZe
ddddZdddddZesddd	ddZedZedZdddd d!Zdd"dd#d$d%ZejZered&dd'd(d)Zed*d+d'd,d)Zd-d.d'd/d)Zddd0d1d2Zddd0d3d4Zddd0d5d6Zddd0d7d8Zddd0d9d:Zddd0d;d<Zddd=d>Ze
d?dd@dAZdBddCdDZ dES )FRowa  Represent a single result row.

    The :class:`.Row` object represents a row of a database result.  It is
    typically associated in the 1.x series of SQLAlchemy with the
    :class:`_engine.CursorResult` object, however is also used by the ORM for
    tuple-like results as of SQLAlchemy 1.4.

    The :class:`.Row` object seeks to act as much like a Python named
    tuple as possible.   For mapping (i.e. dictionary) behavior on a row,
    such as testing for containment of keys, refer to the :attr:`.Row._mapping`
    attribute.

    .. seealso::

        :ref:`tutorial_selecting_data` - includes examples of selecting
        rows from SELECT statements.

    .. versionchanged:: 1.4

        Renamed ``RowProxy`` to :class:`.Row`. :class:`.Row` is no longer a
        "proxy" object in that it contains the final form of data within it,
        and now acts mostly like a named tuple. Mapping-like functionality is
        moved to the :attr:`.Row._mapping` attribute. See
        :ref:`change_4710_core` for background on this change.

     strr   r   )namevaluereturnc                 C  s   t dd S )Nzcan't set attributeAttributeError)selfr#   r$   r!   r!   9/tmp/pip-unpacked-wheel-wbr0uges/sqlalchemy/engine/row.py__setattr__U   s    zRow.__setattr__)r#   r%   c                 C  s   t dd S )Nzcan't delete attributer&   )r(   r#   r!   r!   r)   __delattr__X   s    zRow.__delattr__r   r%   c                 C  s   | S )a  Return a 'tuple' form of this :class:`.Row`.

        At runtime, this method returns "self"; the :class:`.Row` object is
        already a named tuple. However, at the typing level, if this
        :class:`.Row` is typed, the "tuple" return type will be a :pep:`484`
        ``Tuple`` datatype that contains typing information about individual
        elements, supporting typed unpacking and attribute access.

        .. versionadded:: 2.0

        .. seealso::

            :meth:`.Result.tuples`

        r!   r(   r!   r!   r)   tuple[   s    z	Row.tuplec                 C  s   | S )z~a synonym for :attr:`.Row.tuple`

        .. versionadded:: 2.0

        .. seealso::

            :meth:`.Result.t`

        r!   r-   r!   r!   r)   tm   s    zRow.t
RowMappingc                 C  s   t | jd| jt j| jS )a]  Return a :class:`.RowMapping` for this :class:`.Row`.

        This object provides a consistent Python mapping (i.e. dictionary)
        interface for the data contained within the row.   The :class:`.Row`
        by itself behaves like a named tuple.

        .. seealso::

            :attr:`.Row._fields`

        .. versionadded:: 1.4

        N)r0   _parent_keymap_default_key_style_datar-   r!   r!   r)   _mappingz   s    zRow._mappingz7Optional[Sequence[Optional[_ResultProcessorType[Any]]]]zRow[Any])filtersr%   c                 C  s   t | j|| j| j| jS N)r    r1   r2   Z
_key_styler4   )r(   r6   r!   r!   r)   _filter_on_values   s    zRow._filter_on_valuesc                   s   t ddd fdd}|S )z3Handle ambiguous names such as "count" and "index" r    r   )r(   r%   c                   s6    j r S dddd fdd}|S d S )Nr   )argkwr%   c                    s   t tj f| |S r7   )getattrcollections_abcr   )r9   r:   )r#   r(   r!   r)   meth   s    
z4Row._special_name_accessor.<locals>.go.<locals>.meth)r1   _has_key__getattr__)r(   r=   r#   r-   r)   go   s    
z&Row._special_name_accessor.<locals>.go)property)r#   rA   r!   r@   r)   _special_name_accessor   s    zRow._special_name_accessorcountindexboolkeyr%   c                 C  s
   || j kS r7   )r4   r(   rH   r!   r!   r)   __contains__   s    zRow.__contains__zCallable[[Any, Any], bool])otheropr%   c                 C  s*   t |tr|t| t|S |t| |S r7   )
isinstancer    r.   )r(   rK   rL   r!   r!   r)   _op   s    zRow._opint)rE   r%   c                 C  s   d S r7   r!   r(   rE   r!   r!   r)   __getitem__   s    zRow.__getitem__sliceSequence[Any]c                 C  s   d S r7   r!   rP   r!   r!   r)   rQ      s    zUnion[int, slice]zUnion[Any, Sequence[Any]]c                 C  s   d S r7   r!   rP   r!   r!   r)   rQ      s    rK   r%   c                 C  s   |  |tjS r7   )rN   operatorltr(   rK   r!   r!   r)   __lt__   s    z
Row.__lt__c                 C  s   |  |tjS r7   )rN   rU   lerW   r!   r!   r)   __le__   s    z
Row.__le__c                 C  s   |  |tjS r7   )rN   rU   gerW   r!   r!   r)   __ge__   s    z
Row.__ge__c                 C  s   |  |tjS r7   )rN   rU   gtrW   r!   r!   r)   __gt__   s    z
Row.__gt__c                 C  s   |  |tjS r7   )rN   rU   eqrW   r!   r!   r)   __eq__   s    z
Row.__eq__c                 C  s   |  |tjS r7   )rN   rU   nerW   r!   r!   r)   __ne__   s    z
Row.__ne__c                 C  s   t t| S r7   )reprsql_utilZ	_repr_rowr-   r!   r!   r)   __repr__   s    zRow.__repr__zTuple[str, ...]c                 C  s   t dd | jjD S )a  Return a tuple of string keys as represented by this
        :class:`.Row`.

        The keys can represent the labels of the columns returned by a core
        statement or the names of the orm classes returned by an orm
        execution.

        This attribute is analogous to the Python named tuple ``._fields``
        attribute.

        .. versionadded:: 1.4

        .. seealso::

            :attr:`.Row._mapping`

        c                 S  s   g | ]}|d k	r|qS r7   r!   .0kr!   r!   r)   
<listcomp>   s      zRow._fields.<locals>.<listcomp>)r.   r1   keysr-   r!   r!   r)   _fields   s    zRow._fieldszDict[str, Any]c                 C  s
   t | jS )am  Return a new dict which maps field names to their corresponding
        values.

        This method is analogous to the Python named tuple ``._asdict()``
        method, and works by applying the ``dict()`` constructor to the
        :attr:`.Row._mapping` attribute.

        .. versionadded:: 1.4

        .. seealso::

            :attr:`.Row._mapping`

        )dictr5   r-   r!   r!   r)   _asdict   s    zRow._asdictN)!__name__
__module____qualname____doc__	__slots__r   r3   r*   r+   r.   rB   r/   r5   r8   r   rC   rD   rE   rJ   rN   r   __hash__r   rQ   rX   rZ   r\   r^   r`   rb   re   rk   rm   r!   r!   r!   r)   r    5   sD   r    c                   @  s   e Zd ZU dZded< ded< dddddZd	d
ddZdd
ddZdd
ddZdddddZ	dddddZ
dddddZdS )ROMappingViewr!   rS   _itemszMapping[str, Any]r5   )mappingitemsc                 C  s   || _ || _d S r7   )r5   ru   )r(   rv   rw   r!   r!   r)   __init__  s    zROMappingView.__init__rO   r,   c                 C  s
   t | jS r7   )lenru   r-   r!   r!   r)   __len__  s    zROMappingView.__len__r"   c                 C  s
   d | S )Nz&{0.__class__.__name__}({0._mapping!r}))formatr-   r!   r!   r)   re     s    zROMappingView.__repr__zIterator[Any]c                 C  s
   t | jS r7   )iterru   r-   r!   r!   r)   __iter__  s    zROMappingView.__iter__r   rF   )itemr%   c                 C  s
   || j kS r7   ru   )r(   r~   r!   r!   r)   rJ   !  s    zROMappingView.__contains__rT   c                 C  s   t |t | kS r7   listrW   r!   r!   r)   r`   $  s    zROMappingView.__eq__c                 C  s   t |t | kS r7   r   rW   r!   r!   r)   rb   '  s    zROMappingView.__ne__N)rn   ro   rp   rr   __annotations__rx   rz   re   r}   rJ   r`   rb   r!   r!   r!   r)   rt     s   
rt   c                   @  s   e Zd ZdZdS )ROMappingKeysValuesViewr   Nrn   ro   rp   rr   r!   r!   r!   r)   r   +  s   r   c                   @  s   e Zd ZdZdS )ROMappingItemsViewr   Nr   r!   r!   r!   r)   r   1  s   r   c                   @  s   e Zd ZdZdZeZer*dddddZne	j
Zdd	d
dZdd	ddZdd	ddZdddddZdd	ddZdd	ddZdd	ddZdd	d d!Zd"S )#r0   a  A ``Mapping`` that maps column names and objects to :class:`.Row`
    values.

    The :class:`.RowMapping` is available from a :class:`.Row` via the
    :attr:`.Row._mapping` attribute, as well as from the iterable interface
    provided by the :class:`.MappingResult` object returned by the
    :meth:`_engine.Result.mappings` method.

    :class:`.RowMapping` supplies Python mapping (i.e. dictionary) access to
    the  contents of the row.   This includes support for testing of
    containment of specific keys (string column names or objects), as well
    as iteration of keys, values, and items::

        for row in result:
            if 'a' in row._mapping:
                print("Column 'a': %s" % row._mapping['a'])

            print("Column b: %s" % row._mapping[table.c.b])


    .. versionadded:: 1.4 The :class:`.RowMapping` object replaces the
       mapping-like access previously provided by a database result row,
       which now seeks to behave mostly like a named tuple.

    r!   r   r   rG   c                 C  s   d S r7   r!   rI   r!   r!   r)   rQ   V  s    zRowMapping.__getitem__z	List[Any]r,   c                 C  s
   t | jS r7   )r   r4   r-   r!   r!   r)   _values_impl\  s    zRowMapping._values_implzIterator[str]c                 C  s   dd | j jD S )Nc                 s  s   | ]}|d k	r|V  qd S r7   r!   rf   r!   r!   r)   	<genexpr>`  s      z&RowMapping.__iter__.<locals>.<genexpr>r1   rj   r-   r!   r!   r)   r}   _  s    zRowMapping.__iter__rO   c                 C  s
   t | jS r7   )ry   r4   r-   r!   r!   r)   rz   b  s    zRowMapping.__len__objectrF   c                 C  s   | j |S r7   )r1   r>   rI   r!   r!   r)   rJ   e  s    zRowMapping.__contains__r"   c                 C  s   t t| S r7   )rc   rl   r-   r!   r!   r)   re   h  s    zRowMapping.__repr__r   c                   s   t   fdd  D S )zeReturn a view of key/value tuples for the elements in the
        underlying :class:`.Row`.

        c                   s   g | ]}| | fqS r!   r!   )rg   rH   r-   r!   r)   ri   q  s     z$RowMapping.items.<locals>.<listcomp>)r   rj   r-   r!   r-   r)   rw   k  s     zRowMapping.itemsr   c                 C  s   | j jS )znReturn a view of 'keys' for string column names represented
        by the underlying :class:`.Row`.

        r   r-   r!   r!   r)   rj   t  s    zRowMapping.keysr   c                 C  s   t | |  S )zeReturn a view of values for the values represented in the
        underlying :class:`.Row`.

        )r   r   r-   r!   r!   r)   values|  s    zRowMapping.valuesN)rn   ro   rp   rq   rr   r   r3   r   rQ   r   Z_get_by_key_impl_mappingr   r}   rz   rJ   re   rw   rj   r   r!   r!   r!   r)   r0   5  s   	r0   )4rq   
__future__r   abcr   collections.abcr<   rU   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Zsqlr   rd   Zutil._has_cyr   Z_py_rowr   r   r   Z"sqlalchemy.cyextension.resultproxyresultr   r   Zsql.type_apir   r   r   r    ZBaseRowProxyZRowProxyrt   KeysViewr"   
ValuesViewr   	ItemsViewr   r0   r!   r!   r!   r)   <module>   s^    V
  