o
    ×èFhc)  ã                   @   sª   d Z ddlmZ ddlmZ ddlmZ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 ddlmZ G dd„ dee
j ƒZdS )zHClasses for representing collections for the Google Cloud Firestore API.é    )Úgapic_v1)Úretry)ÚBaseCollectionReferenceÚ_item_to_document_ref)Úquery)Úaggregation)Úvector_query)ÚWatch)Údocument)ÚAnyÚCallableÚ	GeneratorÚTupleÚUnion)ÚTransactionc                       st  e Zd ZdZd ‡ fdd„Zdejfdd„Zdej	fdd	„Z
dejfd
d„Zdejjdfdedeedf dejdeedf deeef f
dd„Zdejjdfdeedf dejdeedf deeedf fdd„Zdefdd„Zdejjdfdeedf dejdeedf defdd„Z dejjdfdeedf dejdeedf dee!j"edf fdd„Z#de$de%fdd„Z&‡  Z'S )!ÚCollectionReferencea  A reference to a collection in a Firestore database.

    The collection may already exist or this class can facilitate creation
    of documents within the collection.

    Args:
        path (Tuple[str, ...]): The components in the collection path.
            This is a series of strings representing each collection and
            sub-collection ID, as well as the document IDs for any documents
            that contain a sub-collection.
        kwargs (dict): The keyword arguments for the constructor. The only
            supported keyword is ``client`` and it must be a
            :class:`~google.cloud.firestore_v1.client.Client` if provided. It
            represents the client that created this collection reference.

    Raises:
        ValueError: if

            * the ``path`` is empty
            * there are an even number of elements
            * a collection ID in ``path`` is not a string
            * a document ID in ``path`` is not a string
        TypeError: If a keyword other than ``client`` is used.
    ÚreturnNc                    s   t t| ƒj|i |¤Ž d S ©N)Úsuperr   Ú__init__)ÚselfÚpathÚkwargs©Ú	__class__© ú]/var/www/html/loop/nvenv/lib/python3.10/site-packages/google/cloud/firestore_v1/collection.pyr   =   s   zCollectionReference.__init__c                 C   s
   t  | ¡S )zeQuery factory.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`
        )Ú	query_modÚQuery©r   r   r   r   Ú_query@   s   
zCollectionReference._queryc                 C   ó   t  |  ¡ ¡S )z‡AggregationQuery factory.

        Returns:
            :class:`~google.cloud.firestore_v1.aggregation_query.AggregationQuery`
        )r   ÚAggregationQueryr    r   r   r   r   Ú_aggregation_queryH   ó   z&CollectionReference._aggregation_queryc                 C   r!   )zxVectorQuery factory.

        Returns:
            :class:`~google.cloud.firestore_v1.vector_query.VectorQuery`
        )r   ÚVectorQueryr    r   r   r   r   Ú_vector_queryP   r$   z!CollectionReference._vector_queryÚdocument_dataÚdocument_idr   Útimeoutc                 C   s0   |   ||||¡\}}|j|fi |¤Ž}|j|fS )a  Create a document in the Firestore database with the provided data.

        Args:
            document_data (dict): Property names and values to use for
                creating the document.
            document_id (Optional[str]): The document identifier within the
                current collection. If not provided, an ID will be
                automatically assigned by the server (the assigned ID will be
                a random 20 character string composed of digits,
                uppercase and lowercase letters).
            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:
            Tuple[:class:`google.protobuf.timestamp_pb2.Timestamp`,                 :class:`~google.cloud.firestore_v1.document.DocumentReference`]:
                Pair of

                * The ``update_time`` when the document was created/overwritten.
                * A document reference for the created document.

        Raises:
            :class:`google.cloud.exceptions.Conflict`:
                If ``document_id`` is provided and the document already exists.
        )Ú	_prep_addÚcreateÚupdate_time)r   r'   r(   r   r)   Údocument_refr   Úwrite_resultr   r   r   ÚaddX   s   "ü
zCollectionReference.addÚ	page_sizec                    sB   ˆ   |||¡\}}ˆ jjjd|ˆ jjdœ|¤Ž}‡ fdd„|D ƒS )a`  List all subdocuments of the current collection.

        Args:
            page_size (Optional[int]]): The maximum number of documents
                in each page of results from this request. Non-positive values
                are ignored. Defaults to a sensible value set by the API.
            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:
            Sequence[:class:`~google.cloud.firestore_v1.collection.DocumentReference`]:
                iterator of subdocuments of the current collection. If the
                collection does not exist at the time of `snapshot`, the
                iterator will be empty
        )ÚrequestÚmetadatac                 3   s   � | ]}t ˆ |ƒV  qd S r   )r   )Ú.0Úir   r   r   Ú	<genexpr>¡   s   € z5CollectionReference.list_documents.<locals>.<genexpr>Nr   )Ú_prep_list_documentsÚ_clientÚ_firestore_apiÚlist_documentsÚ_rpc_metadata)r   r0   r   r)   r1   r   Úiteratorr   r   r   r9   ƒ   s   
þýz"CollectionReference.list_documentsÚ
chunk_sizec                 C   s   |   ¡  |¡S r   )r    Ú	_chunkify)r   r<   r   r   r   r=   £   s   zCollectionReference._chunkifyÚtransactionc                 C   ó$   |   ||¡\}}|jdd|i|¤ŽS )a�  Read the documents in this collection.

        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.
            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.

        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).

        Returns:
            list: The documents in this collection that match the query.
        r>   Nr   )Ú_prep_get_or_streamÚget©r   r>   r   r)   r   r   r   r   r   rA   ¦   s   zCollectionReference.getc                 C   r?   )a  Read the documents in this collection.

        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 the 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.
        r>   Nr   )r@   ÚstreamrB   r   r   r   rC   Å   s   $zCollectionReference.streamÚcallbackc                 C   s   |   ¡ }t ||tj¡S )aš  Monitor the documents in this collection.

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

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

        Example:
            from google.cloud import firestore_v1

            db = firestore_v1.Client()
            collection_ref = db.collection(u'users')

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

            # Watch this collection
            collection_watch = collection_ref.on_snapshot(on_snapshot)

            # Terminate this watch
            collection_watch.unsubscribe()
        )r    r	   Ú	for_queryr
   ÚDocumentSnapshot)r   rD   r   r   r   r   Úon_snapshotí   s   zCollectionReference.on_snapshot)r   N)(Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r   r"   r#   r   r%   r&   r   ÚmethodÚDEFAULTÚdictr   ÚstrÚretriesÚRetryÚfloatr   r   r/   Úintr   r9   r=   r   ÚlistrA   r
   rF   rC   r   r	   rG   Ú__classcell__r   r   r   r   r   #   st    ûþ
ýü
û

ú-ü
þý
ü
û ü
þý
ü
û!ü
þý
ü
û(r   N)rK   Úgoogle.api_corer   r   rP   Ú)google.cloud.firestore_v1.base_collectionr   r   Úgoogle.cloud.firestore_v1r   r   r   r   Úgoogle.cloud.firestore_v1.watchr	   r
   Útypingr   r   r   r   r   Ú%google.cloud.firestore_v1.transactionr   r   r   r   r   r   r   Ú<module>   s   