
    Ѯh%                        d 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mZ ddl
mZ ddlmZmZ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  G d de      Zy)aU  Classes for representing aggregation queries for the Google Cloud Firestore API.

A :class:`~google.cloud.firestore_v1.aggregation.AggregationQuery` can be created directly from
a :class:`~google.cloud.firestore_v1.collection.Collection` and that can be
a more common way to create an aggregation query than direct usage of the constructor.
    )annotations)TYPE_CHECKINGAny	GeneratorListOptionalUnion)
exceptionsgapic_v1)retry)AggregationResultBaseAggregationQuery_query_response_to_result)QueryResultsList)StreamGenerator)transaction)ExplainMetrics)ExplainOptionsc                      e Zd ZdZ	 	 d
 fdZdej                  j                  dfdd	 	 	 	 	 	 	 ddZddZ	d Z
dej                  j                  ddf	 	 	 	 	 	 	 	 	 ddZdej                  j                  dfdd	 	 	 	 	 	 	 	 	 dd	Z xZS )AggregationQueryz5Represents an aggregation query to the Firestore API.c                ,    t         t        |   |       y N)superr   __init__)selfnested_query	__class__s     w/home/www/academy-backend.kofcorporation.com/venv/lib/python3.12/site-packages/google/cloud/firestore_v1/aggregation.pyr   zAggregationQuery.__init__.   s     	.|<    N)explain_optionsc                   d}| j                  ||||      }t        |      }|d}n|j                         }t        |||      S )a  Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and returns a list of
        aggregation results in the stream of ``RunAggregationQueryResponse``
        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.
            explain_options
                (Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]):
                Options to enable query profiling for this query. When set,
                explain_metrics will be available on the returned generator.

        Returns:
            QueryResultsList[AggregationResult]: The aggregation query results.

        Nr   r   timeoutr    )streamlistget_explain_metricsr   )r   r   r   r#   r    explain_metricsresultresult_lists           r   getzAggregationQuery.get4   s\    D 26#+	  
 6l""O$88:O_oNNr   c                    | j                  ||||      \  }} | j                  j                  j                  d|| j                  j                  d|S )!Helper method for :meth:`stream`.)requestmetadata )_prep_stream_client_firestore_apirun_aggregation_query_rpc_metadata)r   r   r   r#   r    r-   kwargss          r   _get_stream_iteratorz%AggregationQuery._get_stream_iteratorg   sc    ++	
 At||**@@ 
\\//
 
 	
r   c                    |e|t         j                  j                  u r8| j                  j                  j
                  }|j                  }|j                  }|j                  |      S y)r,   F)	r   methodDEFAULTr1   r2   
_transportr3   _retry
_predicate)r   excr   r   	transportgapic_callables         r   _retry_query_after_exceptionz-AggregationQuery._retry_query_after_exceptionv   sY    /// LL77BB	!*!@!@&--##C((r   c              #  B  K   d}| j                  ||||      }	 	 t        |d      }|	 |S ||j
                  r|j
                  }t        |      }	|	r|	 ># t        j                  $ r1}| j	                  |||      r| j                  |||      }Y d}~} d}~ww xY ww)a  Internal method for stream(). Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and then returns a generator
        which consumes each document returned in the stream of
        ``RunAggregationQueryResponse`` messages.

        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 (Optional[google.api_core.retry.Retry]): Designation of what
                errors, if any, should be retried.  Defaults to a
                system-specified policy.
            timeout (Optional[float]): The timeout for this request.  Defaults
                to a system-specified value.
            explain_options
                (Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]):
                Options to enable query profiling for this query. When set,
                explain_metrics will be available on the returned generator.

        Yields:
            List[AggregationResult]:
            The result of aggregations of this query.

        Returns:
            (Optional[google.cloud.firestore_v1.types.query_profile.ExplainMetrtics]):
            The results of query profiling, if received from the service.

        N)r6   nextr
   GoogleAPICallErrorr@   r'   r   )
r   r   r   r#   r    metricsresponse_iteratorresponser=   r(   s
             r   _make_streamzAggregationQuery._make_stream   s     N *. 55	
  148   8#;#;"22.x8F-  00 	44S%M(,(A(A#)%
 	s3   BA 1BB+&BBBBBc               D    | j                  ||||      }t        ||      S )a  Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and then returns a generator
        which consumes each document returned in the stream of
        ``RunAggregationQueryResponse`` messages.

        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 (Optional[google.api_core.retry.Retry]): Designation of what
                errors, if any, should be retried.  Defaults to a
                system-specified policy.
            timeout (Optinal[float]): The timeout for this request.  Defaults
            to a system-specified value.
            explain_options
                (Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]):
                Options to enable query profiling for this query. When set,
                explain_metrics will be available on the returned generator.

        Returns:
            `StreamGenerator[List[AggregationResult]]`:
            A generator of the query results.
        r"   )rG   r   )r   r   r   r#   r    inner_generators         r   r$   zAggregationQuery.stream   s5    F ++#+	 , 
 @@r   )returnNone)r   "Union[retries.Retry, None, object]r#   zfloat | Noner    Optional[ExplainOptions]rJ   z#QueryResultsList[AggregationResult]r   )
r   z!Optional[transaction.Transaction]r   rL   r#   Optional[float]r    rM   rJ   zAGenerator[List[AggregationResult], Any, Optional[ExplainMetrics]])
r   z#Optional['transaction.Transaction']r   rL   r#   rN   r    rM   rJ   z(StreamGenerator[List[AggregationResult]])__name__
__module____qualname____doc__r   r   r8   r9   r*   r6   r@   rG   r$   __classcell__)r   s   @r   r   r   +   s#   ?= 
= 4<OO4K4K $	1O 591O 21O 	1O 21O 
-1Of
	 :>4<OO4K4K#'48G6G 2G !	G
 2G 
KGV <@4<OO4K4K#'	)A 59)A8)A 2)A !	)A 2)A 
2)Ar   r   N)rR   
__future__r   typingr   r   r   r   r   r	   google.api_corer
   r   r   retries*google.cloud.firestore_v1.base_aggregationr   r   r   'google.cloud.firestore_v1.query_resultsr   *google.cloud.firestore_v1.stream_generatorr   google.cloud.firestore_v1r   'google.cloud.firestore_v1.query_profiler   r   r   r/   r   r   <module>r]      sM    # G G 0 , 
 E F 5FFHA+ HAr   