a
    !f;@                     @  s   d 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mZmZ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mZmZmZmZ G dd deZG dd deeZ dS )a+  Classes for representing queries for the Google Cloud Firestore API.

A :class:`~google.cloud.firestore_v1.query.Query` can be created directly from
a :class:`~google.cloud.firestore_v1.collection.Collection` and that can be
a more common way to create a query than direct usage of the constructor.
    )annotations)firestore_v1)DocumentSnapshot)
exceptions)gapic_v1)retry)BaseCollectionGroup	BaseQueryQueryPartition_query_response_to_snapshot,_collection_group_query_response_to_snapshot_enum_from_direction)aggregation)document)Watch)AnyCallable	GeneratorListOptionalTypec                
      s   e Zd ZdZd(dd fddZdejjdfd	d
ddddZdddddZ	dd Z
dd Zd)dddddZdejjdfd	d
ddddZd d!d"d#d$Zed%dd&d'Z  ZS )*Querya
  Represents a query to the Firestore API.

    Instances of this class are considered immutable: all methods that
    would modify an instance instead return a new instance.

    Args:
        parent (:class:`~google.cloud.firestore_v1.collection.CollectionReference`):
            The collection that this query applies to.
        projection (Optional[:class:`google.cloud.proto.firestore.v1.            query.StructuredQuery.Projection`]):
            A projection of document fields to limit the query results to.
        field_filters (Optional[Tuple[:class:`google.cloud.proto.firestore.v1.            query.StructuredQuery.FieldFilter`, ...]]):
            The filters to be applied in the query.
        orders (Optional[Tuple[:class:`google.cloud.proto.firestore.v1.            query.StructuredQuery.Order`, ...]]):
            The "order by" entries to use in the query.
        limit (Optional[int]):
            The maximum number of documents the query is allowed to return.
        offset (Optional[int]):
            The number of results to skip.
        start_at (Optional[Tuple[dict, bool]]):
            Two-tuple of :

            * a mapping of fields. Any field that is present in this mapping
              must also be present in ``orders``
            * an ``after`` flag

            The fields and the flag combine to form a cursor used as
            a starting point in a query result set. If the ``after``
            flag is :data:`True`, the results will start just after any
            documents which have fields matching the cursor, otherwise
            any matching documents will be included in the result set.
            When the query is formed, the document values
            will be used in the order given by ``orders``.
        end_at (Optional[Tuple[dict, bool]]):
            Two-tuple of:

            * a mapping of fields. Any field that is present in this mapping
              must also be present in ``orders``
            * a ``before`` flag

            The fields and the flag combine to form a cursor used as
            an ending point in a query result set. If the ``before``
            flag is :data:`True`, the results will end just before any
            documents which have fields matching the cursor, otherwise
            any matching documents will be included in the result set.
            When the query is formed, the document values
            will be used in the order given by ``orders``.
        all_descendants (Optional[bool]):
            When false, selects only collections that are immediate children
            of the `parent` specified in the containing `RunQueryRequest`.
            When true, selects all descendant collections.
    N FNonereturnc                   s*   t t| j|||||||||	|
|d d S N)parent
projectionfield_filtersorderslimitlimit_to_lastoffsetstart_atend_atall_descendants	recursive)superr   __init__selfr   r   r   r    r!   r"   r#   r$   r%   r&   r'   	__class__r   `/var/www/html/python-backend/venv/lib/python3.9/site-packages/google/cloud/firestore_v1/query.pyr)   d   s    
zQuery.__init__retries.RetryfloatzList[DocumentSnapshot]r   timeoutr   c                 C  sf   | j }| j r>| jD ]$}t|jj| jkr,| jn| j|_qd| _ | j|||d}|r^tt	|}t	|S )a  Read the documents in the collection that match this query.

        This sends a ``RunQuery`` RPC and returns a list of documents
        returned in the stream of ``RunQueryResponse`` messages.

        Args:
            transaction
                (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
                An existing transaction that this query will run in.
                If a ``transaction`` is used and it already has write operations
                added, this method cannot be used (i.e. read-after-write is not
                allowed).
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.

        Returns:
            list: The documents in the collection that match this query.
        F)transactionr   r2   )
Z_limit_to_lastZ_ordersr   	directionnameZ	ASCENDINGZ
DESCENDINGstreamreversedlist)r+   r3   r   r2   Zis_limited_to_lastorderresultr   r   r.   get   s    
z	Query.getintz-Generator[List[DocumentSnapshot], None, None])
chunk_sizer   c           	      c  s   | j }d}|  }d }|||}||}|r:||}| }|rN|d }|t|7 }|V  t||k sx|r||krd S qd S )Nr   )_limitZ_copyZ_resolve_chunk_sizer!   start_afterr;   len)	r+   r=   Zmax_to_returnZnum_returnedoriginalZlast_document_chunk_sizeZ_qZ	snapshotsr   r   r.   	_chunkify   s&    

zQuery._chunkifyc                 C  s:   |  |||\}}}| jjjf || jjd|}||fS )!Helper method for :meth:`stream`.requestmetadata)Z_prep_stream_client_firestore_api	run_query_rpc_metadata)r+   r3   r   r2   rG   expected_prefixkwargsresponse_iteratorr   r   r.   _get_stream_iterator   s    

zQuery._get_stream_iteratorc                 C  s8   |du r4|t jju r*| jjj}|j}|j}||S dS )rE   NF)	r   methodDEFAULTrI   rJ   
_transportrK   Z_retryZ
_predicate)r+   excr   r3   	transportZgapic_callabler   r   r.   _retry_query_after_exception   s    

z"Query._retry_query_after_exceptionz
str | Nonez1Type['firestore_v1.aggregation.AggregationQuery'])aliasr   c                 C  s   t | j|dS )z
        Adds a count over the query.

        :type alias: str
        :param alias: (Optional) The alias for the count
        )rW   )r   ZAggregationQuerycount)r+   rW   r   r   r.   rX      s    	zQuery.countz/Generator[document.DocumentSnapshot, Any, None]c              
   c  s   |  |||\}}d}zt|d}W n` tjy } zF| |||rn| |}	|	 |||\}}
W Y d}~qn W Y d}~n
d}~0 0 |du rq| jrt|| j}nt	|| j|}|dur|}|V  qdS )a"  Read the documents in the collection that match this query.

        This sends a ``RunQuery`` RPC and then returns an iterator which
        consumes each document returned in the stream of ``RunQueryResponse``
        messages.

        .. note::

           The underlying stream of responses will time out after
           the ``max_rpc_timeout_millis`` value set in the GAPIC
           client configuration for the ``RunQuery`` API.  Snapshots
           not consumed from the iterator before that point will be lost.

        If a ``transaction`` is used and it already has write operations
        added, this method cannot be used (i.e. read-after-write is not
        allowed).

        Args:
            transaction
                (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
                An existing transaction that this query will run in.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.

        Yields:
            :class:`~google.cloud.firestore_v1.document.DocumentSnapshot`:
            The next document that fulfills the query.
        N)
rP   nextr   ZGoogleAPICallErrorrV   r@   Z_all_descendantsr   _parentr   )r+   r3   r   r2   rO   rM   Zlast_snapshotresponserT   Z	new_query_Zsnapshotr   r   r.   r6      s<    $
zQuery.streamr   r   )callbackr   c                 C  s   t | |tjS )a  Monitor the documents in this collection that match this query.

        This starts a watch on this query using a background thread. The
        provided callback is run on the snapshot of the documents.

        Args:
            callback(Callable[[:class:`~google.cloud.firestore.query.QuerySnapshot`], NoneType]):
                a callback to run when a change occurs.

        Example:

        .. code-block:: python

            from google.cloud import firestore_v1

            db = firestore_v1.Client()
            query_ref = db.collection(u'users').where("user", "==", u'Ada')

            def on_snapshot(docs, changes, read_time):
                for doc in docs:
                    print(u'{} => {}'.format(doc.id, doc.to_dict()))

            # Watch this query
            query_watch = query_ref.on_snapshot(on_snapshot)

            # Terminate this watch
            query_watch.unsubscribe()
        )r   Z	for_queryr   r   )r+   r]   r   r   r.   on_snapshotE  s    zQuery.on_snapshotz3Type['firestore_v1.collection.CollectionReference']c                  C  s   ddl m}  | S )Nr   CollectionReference)Z$google.cloud.firestore_v1.collectionr`   r_   r   r   r.   _get_collection_reference_classd  s    z%Query._get_collection_reference_class)
Nr   r   NFNNNFF)N)__name__
__module____qualname____doc__r)   r   rQ   rR   r;   rD   rP   rV   rX   r6   r^   staticmethodra   __classcell__r   r   r,   r.   r   ,   s8   :          .' Jr   c                
      sP   e Zd ZdZddd fdd	Zed
d Zejj	dfddddddZ
  ZS )CollectionGroupa}  Represents a Collection Group in the Firestore API.

    This is a specialization of :class:`.Query` that includes all documents in the
    database that are contained in a collection or subcollection of the given
    parent.

    Args:
        parent (:class:`~google.cloud.firestore_v1.collection.CollectionReference`):
            The collection that this query applies to.
    Nr   FTr   r   c                   s*   t t| j|||||||||	|
|d d S r   )r(   rh   r)   r*   r,   r   r.   r)   y  s    
zCollectionGroup.__init__c                   C  s   t S )N)r   r   r   r   r.   _get_query_class  s    z CollectionGroup._get_query_classr/   r0   z%Generator[QueryPartition, None, None]r1   c           
      c  sv   |  |||\}}| jjjf || jjd|}d}|D ]*}| j|jd j}	t| ||	V  |	}q8t| |dV  dS )a	  Partition a query for parallelization.

        Partitions a query by returning partition cursors that can be used to run the
        query in parallel. The returned partition cursors are split points that can be
        used as starting/end points for the query results.

        Args:
            partition_count (int): The desired maximum number of partition points. The
                number must be strictly positive. The actual number of partitions
                returned may be fewer.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.
        rF   Nr   )	Z_prep_get_partitionsrI   rJ   Zpartition_queryrL   r   valuesZreference_valuer
   )
r+   Zpartition_countr   r2   rG   rN   pagerr$   Z	cursor_pbcursorr   r   r.   get_partitions  s    
zCollectionGroup.get_partitions)
Nr   r   NFNNNTF)rb   rc   rd   re   r)   rf   ri   r   rQ   rR   rm   rg   r   r   r,   r.   rh   m  s"             
rh   N)!re   
__future__r   Zgoogle.cloudr   Z'google.cloud.firestore_v1.base_documentr   Zgoogle.api_corer   r   r   retriesZ$google.cloud.firestore_v1.base_queryr   r	   r
   r   r   r   Zgoogle.cloud.firestore_v1r   r   Zgoogle.cloud.firestore_v1.watchr   typingr   r   r   r   r   r   r   rh   r   r   r   r.   <module>   s       C