
    (ph2                       S r SSKJr  SSKJrJrJrJrJr  SSK	J
r
Jr  SSK	Jr  SSKJrJr  SSKJrJrJrJrJrJrJrJr  SSKJr  SS	KJr  \(       a  SS
KJr  SSK J!r!  SSK"J#r#   " S S\RH                  \5      r% " S S\5      r&SS jr'g)zEHelpers for applying Google Cloud Firestore changes in a transaction.    )annotations)TYPE_CHECKINGAnyCallable	GeneratorOptional)
exceptionsgapic_v1)retry)_helpersbatch)_CANT_BEGIN_CANT_COMMIT_CANT_ROLLBACK_EXCEED_ATTEMPTS_TEMPLATE_WRITE_READ_ONLYMAX_ATTEMPTSBaseTransaction_BaseTransactional)DocumentReference)Query)DocumentSnapshot)ExplainOptions)StreamGeneratorc                    ^  \ rS rSrSr\S4SU 4S jjjrSU 4S jjrSSS jjrSS jr	SS	 jr
\R                  R                  S4       SS
 jjr\R                  R                  S4SS.         SS jjjrSrU =r$ )Transaction,   a  Accumulate read-and-write operations to be sent in a transaction.

Args:
    client (:class:`~google.cloud.firestore_v1.client.Client`):
        The client that created this transaction.
    max_attempts (Optional[int]): The maximum number of attempts for
        the transaction (i.e. allowing retries). Defaults to
        :attr:`~google.cloud.firestore_v1.transaction.MAX_ATTEMPTS`.
    read_only (Optional[bool]): Flag indicating if the transaction
        should be read-only or should allow writes. Defaults to
        :data:`False`.
Fc                Z   > [         [        U ]  U5        [        R                  " XU5        g N)superr   __init__r   )selfclientmax_attempts	read_only	__class__s       X/var/www/html/venv/lib/python3.13/site-packages/google/cloud/firestore_v1/transaction.pyr!   Transaction.__init__:   s"    k4)&1  Y?    c                l   > U R                   (       a  [        [        5      e[        [        U ]  U5        g)zAdd `Write`` protobufs to this transaction.

Args:
    write_pbs (List[google.cloud.firestore_v1.                write.Write]): A list of write protobufs to be added.

Raises:
    ValueError: If this transaction is read-only.
N)
_read_only
ValueErrorr   r    r   _add_write_pbs)r"   	write_pbsr&   s     r'   r-   Transaction._add_write_pbs>   s'     ??-..k4/	:r)   Nc                Z   U R                   (       a+  [        R                  " U R                  5      n[	        U5      eU R
                  R                  R                  U R
                  R                  U R                  U5      S.U R
                  R                  S9nUR                  U l        g)zBegin the transaction.

Args:
    retry_id (Optional[bytes]): Transaction ID of a transaction to be
        retried.

Raises:
    ValueError: If the current transaction has already begun.
)databaseoptionsrequestmetadataN)in_progressr   format_idr,   _client_firestore_apibegin_transaction_database_string_options_protobuf_rpc_metadatatransaction)r"   retry_idmsgtransaction_responses       r'   _beginTransaction._beginM   s     $$TXX.CS/!#||::LL LL9911(; \\//  M  
 (33r)   c                B   U R                   (       d  [        [        5      e U R                  R                  R                  U R                  R                  U R                  S.U R                  R                  S9  U R                  5         g! U R                  5         f = f)zRoll back the transaction.

Raises:
    ValueError: If no transaction is in progress.
    google.api_core.exceptions.GoogleAPICallError: If the rollback fails.
)r1   r?   r3   N)
r6   r,   r   r9   r:   rollbackr<   r8   r>   	_clean_up)r"   s    r'   	_rollbackTransaction._rollbackd   sy     ^,,	LL''00 $ = =#'88 33 1  NNDNNs   AB Bc                   U R                   (       d  [        [        5      eU R                  R                  R                  U R                  R                  U R                  U R                  S.U R                  R                  S9nU R                  5         [        UR                  5      U l        UR                  U l        U R                  $ )a~  Transactionally commit the changes accumulated.

Returns:
    List[:class:`google.cloud.firestore_v1.write.WriteResult`, ...]:
    The write results corresponding to the changes committed, returned
    in the same order as the changes were applied to this transaction.
    A write result contains an ``update_time`` field.

Raises:
    ValueError: If no transaction is in progress.
)r1   writesr?   r3   )r6   r,   r   r9   r:   commitr<   
_write_pbsr8   r>   rG   listwrite_resultscommit_time)r"   commit_responses     r'   _commitTransaction._commit{   s     \**,,55<< LL99//#xx
 \\// = 
 	!/"?"?@*66!!!r)   c                l    [         R                  " X#5      nU R                  R                  " U4SU 0UD6$ )a  Retrieves multiple documents from Firestore.

Args:
    references (List[.DocumentReference, ...]): Iterable of document
        references to be retrieved.
    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:
    .DocumentSnapshot: The next document snapshot that fulfills the
    query, or :data:`None` if the document does not exist.
r?   )r   make_retry_timeout_kwargsr9   get_all)r"   
referencesr   timeoutkwargss        r'   rV   Transaction.get_all   s3    ( 33EC||##JKDKFKKr)   )explain_optionsc               *   [         R                  " X#5      n[        U[        5      (       a.  Ub  [	        S5      eU R
                  R                  " U/4SU 0UD6$ [        U[        5      (       a  Ub  XES'   UR                  " SSU 0UD6$ [	        S5      e)a  Retrieve a document or a query result from the database.

Args:
    ref_or_query (DocumentReference | Query):
        The document references or query object to return.
    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.
        Can only be used when running a query, not a document reference.

Yields:
    .DocumentSnapshot: The next document snapshot that fulfills the
    query, or :data:`None` if the document does not exist.

Raises:
    ValueError: if `ref_or_query` is not one of the supported types, or
    explain_options is provided when `ref_or_query` is a document
    reference.
z^When type of `ref_or_query` is `AsyncDocumentReference`, `explain_options` cannot be provided.r?   r[   zIValue for argument "ref_or_query" must be a DocumentReference or a Query. )	r   rU   
isinstancer   r,   r9   rV   r   stream)r"   ref_or_queryr   rX   r[   rY   s         r'   getTransaction.get   s    @ 33ECl$566* <  <<''SDSFSSe,,*,;()&&B4B6BB[ r)   )r8   rP   rO   returnNone)r.   rN   rd   re   r   )r@   zbytes | Nonerd   re   )rd   rN   )rW   rN   r   retries.Retry | object | NonerX   zfloat | Nonerd   z&Generator[DocumentSnapshot, Any, None])
r`   zDocumentReference | Queryr   rf   rX   zOptional[float]r[   zOptional[ExplainOptions]rd   zJStreamGenerator[DocumentSnapshot] | Generator[DocumentSnapshot, Any, None])__name__
__module____qualname____firstlineno____doc__r   r!   r-   rC   rH   rR   r
   methodDEFAULTrV   ra   __static_attributes____classcell__r&   s   @r'   r   r   ,   s     -9E @ @;4.."@ 08/F/F $	LL -L 	L
 
0L4 08/F/F#'	/ 59/// -/ !	/ 2/ 
T/ /r)   r   c                  D   ^  \ rS rSrSrSU 4S jjrSS jrS	S jrSrU =r	$ )
_Transactional   aA  Provide a callable object to use as a transactional decorater.

This is surfaced via
:func:`~google.cloud.firestore_v1.transaction.transactional`.

Args:
    to_wrap (Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]):
        A callable that should be run (and retried) in a transaction.
c                ,   > [         [        U ]  U5        g r   )r    rr   r!   )r"   to_wrapr&   s     r'   r!   _Transactional.__init__   s    nd,W5r)   c                    UR                  5         UR                  U R                  S9  UR                  U l        U R                  c  U R                  U l        U R
                  " U/UQ70 UD6$ )a  Begin transaction and call the wrapped callable.

Args:
    transaction
        (:class:`~google.cloud.firestore_v1.transaction.Transaction`):
        A transaction to execute the callable within.
    args (Tuple[Any, ...]): The extra positional arguments to pass
        along to the wrapped callable.
    kwargs (Dict[str, Any]): The extra keyword arguments to pass
        along to the wrapped callable.

Returns:
    Any: result of the wrapped callable.

Raises:
    Exception: Any failure caused by ``to_wrap``.
)r@   )rG   rC   r@   r8   
current_idru   )r"   r?   argsrY   s       r'   _pre_commit_Transactional._pre_commit   s`    & 	DMM2 &//==  OODM||K9$9&99r)   c                   U R                  5         UR                  (       d  [        R                  OSnSn [	        UR
                  5       H,  nU R                  " U/UQ70 UD6n UR                  5         Us  $    [        R                  " UR
                  5      n	[        U	5      Ue! U a  nUn SnAMj  SnAff = f! [         a    UR                  5         e f = f)a*  Execute the wrapped callable within a transaction.

Args:
    transaction
        (:class:`~google.cloud.firestore_v1.transaction.Transaction`):
        A transaction to execute the callable within.
    args (Tuple[Any, ...]): The extra positional arguments to pass
        along to the wrapped callable.
    kwargs (Dict[str, Any]): The extra keyword arguments to pass
        along to the wrapped callable.

Returns:
    Any: The result of the wrapped callable.

Raises:
    ValueError: If the transaction does not succeed in
        ``max_attempts``.
r]   N)_resetr+   r	   Abortedrange_max_attemptsrz   rR   r   r7   r,   BaseExceptionrH   )
r"   r?   ry   rY   retryable_exceptionslast_excattemptresultexcrA   s
             r'   __call___Transactional.__call__  s    & 	(3(>(>ZB 	 	 !:!:;))+GGG#'')!M	 < ,22;3L3LMCS/x/ , #"H#  	 !!#		s;   .B> &B(7B> :.B> (B;.B60B> 6B;;B> >C)rx   r@   rc   )r?   r   rd   r   )r?   r   )
rg   rh   ri   rj   rk   r!   rz   r   rn   ro   rp   s   @r'   rr   rr      s    6:8/ /r)   rr   c                    [        U 5      $ )ag  Decorate a callable so that it runs in a transaction.

Args:
    to_wrap
        (Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]):
        A callable that should be run (and retried) in a transaction.

Returns:
    Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]:
    the wrapped callable.
)rr   )ru   s    r'   transactionalr   =  s     '""r)   N)ru   r   rd   rr   )(rk   
__future__r   typingr   r   r   r   r   google.api_corer	   r
   r   retriesgoogle.cloud.firestore_v1r   r   *google.cloud.firestore_v1.base_transactionr   r   r   r   r   r   r   r   "google.cloud.firestore_v1.documentr   google.cloud.firestore_v1.queryr   'google.cloud.firestore_v1.base_documentr   'google.cloud.firestore_v1.query_profiler   *google.cloud.firestore_v1.stream_generatorr   
WriteBatchr   rr   r   r]   r)   r'   <module>r      so    L " D D 0 , 5	 	 	 A 1 HFJr%""O rjY' Yx#r)   