
    j{                     0    d Z ddlmZ  G d d          ZdS )a  
QueryChain is a wrapper for sequence of queries.


Features:

    * Easy iteration for sequence of queries
    * Limit, offset and count which are applied to all queries in the chain
    * Smart __getitem__ support


Initialization
^^^^^^^^^^^^^^

QueryChain takes iterable of queries as first argument. Additionally limit and
offset parameters can be given

::

    chain = QueryChain([session.query(User), session.query(Article)])

    chain = QueryChain(
        [session.query(User), session.query(Article)],
        limit=4
    )


Simple iteration
^^^^^^^^^^^^^^^^
::

    chain = QueryChain([session.query(User), session.query(Article)])

    for obj in chain:
        print obj


Limit and offset
^^^^^^^^^^^^^^^^

Lets say you have 5 blog posts, 5 articles and 5 news items in your
database.

::

    chain = QueryChain(
        [
            session.query(BlogPost),
            session.query(Article),
            session.query(NewsItem)
        ],
        limit=5
    )

    list(chain)  # all blog posts but not articles and news items


    chain = chain.offset(4)
    list(chain)  # last blog post, and first four articles


Just like with original query object the limit and offset can be chained to
return a new QueryChain.

::

    chain = chain.limit(5).offset(7)


Chain slicing
^^^^^^^^^^^^^

::

    chain = QueryChain(
        [
            session.query(BlogPost),
            session.query(Article),
            session.query(NewsItem)
        ]
    )

    chain[3:6]   # New QueryChain with offset=3 and limit=6


Count
^^^^^

Let's assume that there are five blog posts, five articles and five news
items in the database, and you have the following query chain::

    chain = QueryChain(
        [
            session.query(BlogPost),
            session.query(Article),
            session.query(NewsItem)
        ]
    )

You can then get the total number rows returned by the query chain
with :meth:`~QueryChain.count`::

    >>> chain.count()
    15


    )copyc                   >    e Zd ZdZd
dZd Zd Zd Zd Zd Z	d	 Z
dS )
QueryChaina  
    QueryChain can be used as a wrapper for sequence of queries.

    :param queries: A sequence of SQLAlchemy Query objects
    :param limit: Similar to normal query limit this parameter can be used for
        limiting the number of results for the whole query chain.
    :param offset: Similar to normal query offset this parameter can be used
        for offsetting the query chain as a whole.

    .. versionadded: 0.26.0
    Nc                 0    || _         || _        || _        d S N)queries_limit_offset)selfr   limitoffsets       f/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/sqlalchemy_utils/query_chain.py__init__zQueryChain.__init__{   s        c              #   <  K   d}d}| j         D ]}t          |          }| j        r|                    | j        |z
            }| j        r|                    | j        |z
            }d}|D ]}|dz  }|dz  }|V  |s||                                z  }||z  }d S )Nr      )r   r   r	   r   r
   r   count)r   consumedskippedquery
query_copy	obj_countobjs          r   __iter__zQueryChain.__iter__   s      \ 	% 	%EeJ{ <DK($:;;| =T\G%;<<I  AQ					 %:++---9$!	% 	%r   c                     | d |         S r    r   values     r   r   zQueryChain.limit   s    FUF|r   c                     | |d          S r   r   r   s     r   r   zQueryChain.offset   s    EFF|r   c                 >    t          d | j        D                       S )zY
        Return the total number of rows this QueryChain's queries would return.
        c              3   >   K   | ]}|                                 V  d S r   )r   ).0qs     r   	<genexpr>z#QueryChain.count.<locals>.<genexpr>   s*      3317799333333r   )sumr   r   s    r   r   zQueryChain.count   s#     33dl333333r   c                     t          |t                    rC|                     | j        |j        |j        n| j        |j        |j        n| j                  S | |d         D ]}|c S d S )N)r   r   r   r   )
isinstanceslice	__class__r   stopr	   startr
   )r   keyr   s      r   __getitem__zQueryChain.__getitem__   s    c5!! 	>>"%("6chhDK$'I$9syyt| "    CE{  


 r   c                 &    dt          |           z  S )Nz<QueryChain at 0x%x>)idr&   s    r   __repr__zQueryChain.__repr__   s    %400r   )NN)__name__
__module____qualname____doc__r   r   r   r   r   r.   r1   r   r   r   r   r   o   s        
 
   
% % %*    4 4 4	 	 	1 1 1 1 1r   r   N)r5   r   r   r   r   r   <module>r6      sX   j jV      >1 >1 >1 >1 >1 >1 >1 >1 >1 >1r   