U
    ~fhB3                     @  s   d 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ZG dd deZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZdS )z{Result class definitions.

.. seealso:: This module is compatible with both the synchronous and asynchronous PyMongo APIs.
    )annotations)AnyMappingMutableMappingOptionalcast)InvalidOperationc                   @  sT   e Zd ZdZdZdddddZdd	d
dZdddddZedd	ddZ	dS )_WriteResultz$Base class for write result classes.)Z__acknowledgedboolNone)acknowledgedreturnc                 C  s
   || _ d S N_WriteResult__acknowledged)selfr    r   3/tmp/pip-unpacked-wheel-36gvocj8/pymongo/results.py__init__   s    z_WriteResult.__init__strr   c                 C  s   | j j d| j dS )N())	__class____name__r   r   r   r   r   __repr__"   s    z_WriteResult.__repr__property_namer   c                 C  s   | j std| ddS )z8Raise an exception on property access if unacknowledged.A value for zi is not available when the write is unacknowledged. Check the acknowledged attribute to avoid this error.N)r   r   r   r   r   r   r   _raise_if_unacknowledged%   s    
z%_WriteResult._raise_if_unacknowledgedc                 C  s   | j S )aC  Is this the result of an acknowledged write operation?

        The :attr:`acknowledged` attribute will be ``False`` when using
        ``WriteConcern(w=0)``, otherwise ``True``.

        .. note::
          If the :attr:`acknowledged` attribute is ``False`` all other
          attributes of this class will raise
          :class:`~pymongo.errors.InvalidOperation` when accessed. Values for
          other attributes cannot be determined if the write operation was
          unacknowledged.

        .. seealso::
          :class:`~pymongo.write_concern.WriteConcern`
        r   r   r   r   r   r   /   s    z_WriteResult.acknowledgedN)
r   
__module____qualname____doc__	__slots__r   r   r!   propertyr   r   r   r   r   r	      s   
r	   c                      sN   e Zd ZdZdZdddd fddZd	d
ddZedd
ddZ  Z	S )InsertOneResultzThe return type for :meth:`~pymongo.collection.Collection.insert_one`
    and as part of :meth:`~pymongo.mongo_client.MongoClient.bulk_write`.
    )Z__inserted_idr   r
   r   )inserted_idr   r   c                   s   || _ t | d S r   )_InsertOneResult__inserted_idsuperr   )r   r(   r   r   r   r   r   J   s    zInsertOneResult.__init__r   r   c                 C  s   | j j d| jd| j dS Nr   z, acknowledged=r   )r   r   r)   r   r   r   r   r   r   N   s    zInsertOneResult.__repr__c                 C  s   | j S )zThe inserted document's _id.)r)   r   r   r   r   r(   S   s    zInsertOneResult.inserted_id)
r   r"   r#   r$   r%   r   r   r&   r(   __classcell__r   r   r+   r   r'   C   s   r'   c                      sN   e Zd ZdZdZdddd fddZd	d
ddZedd
ddZ  Z	S )InsertManyResultzGThe return type for :meth:`~pymongo.collection.Collection.insert_many`.)Z__inserted_idsz	list[Any]r
   r   )inserted_idsr   r   c                   s   || _ t | d S r   )_InsertManyResult__inserted_idsr*   r   )r   r/   r   r+   r   r   r   ^   s    zInsertManyResult.__init__r   r   c                 C  s   | j j d| jd| j dS r,   )r   r   r0   r   r   r   r   r   r   b   s    zInsertManyResult.__repr__c                 C  s   | j S )a@  A list of _ids of the inserted documents, in the order provided.

        .. note:: If ``False`` is passed for the `ordered` parameter to
          :meth:`~pymongo.collection.Collection.insert_many` the server
          may have inserted the documents in a different order than what
          is presented here.
        )r0   r   r   r   r   r/   g   s    	zInsertManyResult.inserted_ids)
r   r"   r#   r$   r%   r   r   r&   r/   r-   r   r   r+   r   r.   Y   s   r.   c                      s   e Zd ZdZdZddddd fddZd	d
ddZedd
ddZedd
ddZ	edd
ddZ
edd
ddZedd
ddZ  ZS )UpdateResulta  The return type for :meth:`~pymongo.collection.Collection.update_one`,
    :meth:`~pymongo.collection.Collection.update_many`, and
    :meth:`~pymongo.collection.Collection.replace_one`, and as part of
    :meth:`~pymongo.mongo_client.MongoClient.bulk_write`.
    )__raw_resultZ__in_client_bulkFzOptional[Mapping[str, Any]]r
   )
raw_resultr   in_client_bulkc                   s   || _ || _t | d S r   )_UpdateResult__raw_result_UpdateResult__in_client_bulkr*   r   )r   r3   r   r4   r+   r   r   r      s    zUpdateResult.__init__r   r   c                 C  s   | j j d| jd| j dS r,   )r   r   r5   r   r   r   r   r   r      s    zUpdateResult.__repr__c                 C  s   | j S z/The raw result document returned by the server.)r5   r   r   r   r   r3      s    zUpdateResult.raw_resultintc                 C  s:   |  d | jdk	st| js,| jdk	r,dS | jddS )z0The number of documents matched for this update.matched_countNr   n)r!   r5   AssertionErrorr6   upserted_idgetr   r   r   r   r9      s
    
zUpdateResult.matched_countc                 C  s*   |  d | jdk	sttt| jdS )!The number of documents modified.modified_countN	nModified)r!   r5   r;   r   r8   r=   r   r   r   r   r?      s    
zUpdateResult.modified_countr   c                 C  sF   |  d | jdk	st| jr8| jdr8| jd d S | jddS )z^The _id of the inserted document if an upsert took place. Otherwise
        ``None``.
        r<   Nupserted_id)r!   r5   r;   r6   r=   r   r   r   r   r<      s
    
zUpdateResult.upserted_idc                 C  s   | j dk	std| j kS )zEWhether an upsert took place.

        .. versionadded:: 4.9
        NrA   )r5   r;   r   r   r   r   
did_upsert   s    zUpdateResult.did_upsert)F)r   r"   r#   r$   r%   r   r   r&   r3   r9   r?   r<   rC   r-   r   r   r+   r   r1   s   s   	 

r1   c                      s`   e Zd ZdZdZdddd fddZd	d
ddZedd
ddZedd
ddZ	  Z
S )DeleteResultzThe return type for :meth:`~pymongo.collection.Collection.delete_one`
    and :meth:`~pymongo.collection.Collection.delete_many`
    and as part of :meth:`~pymongo.mongo_client.MongoClient.bulk_write`.
    )r2   zMapping[str, Any]r
   r   )r3   r   r   c                   s   || _ t | d S r   )_DeleteResult__raw_resultr*   r   )r   r3   r   r+   r   r   r      s    zDeleteResult.__init__r   r   c                 C  s   | j j d| jd| j dS r,   )r   r   rE   r   r   r   r   r   r      s    zDeleteResult.__repr__c                 C  s   | j S r7   )rE   r   r   r   r   r3      s    zDeleteResult.raw_resultr8   c                 C  s   |  d | jddS ) The number of documents deleted.deleted_countr:   r   )r!   rE   r=   r   r   r   r   rG      s    
zDeleteResult.deleted_count)r   r"   r#   r$   r%   r   r   r&   r3   rG   r-   r   r   r+   r   rD      s   rD   c                      s   e Zd ZdZdZdddd fddZd	d
ddZedd
ddZedd
ddZ	edd
ddZ
edd
ddZedd
ddZedd
ddZ  ZS )_BulkWriteResultBasez.Private base class for bulk write API results.)Z__bulk_api_resultdict[str, Any]r
   r   bulk_api_resultr   r   c                   s   || _ t | d S r   )%_BulkWriteResultBase__bulk_api_resultr*   r   r   rK   r   r+   r   r   r      s    z_BulkWriteResultBase.__init__r   r   c                 C  s   | j j d| jd| j dS r,   )r   r   rL   r   r   r   r   r   r      s    z_BulkWriteResultBase.__repr__c                 C  s   | j S )zThe raw bulk write API result.)rL   r   r   r   r   rK      s    z$_BulkWriteResultBase.bulk_api_resultr8   c                 C  s   |  d tt| jdS )z!The number of documents inserted.inserted_countZ	nInsertedr!   r   r8   rL   r=   r   r   r   r   rN      s    
z#_BulkWriteResultBase.inserted_countc                 C  s   |  d tt| jdS )z.The number of documents matched for an update.r9   ZnMatchedrO   r   r   r   r   r9      s    
z"_BulkWriteResultBase.matched_countc                 C  s   |  d tt| jdS )r>   r?   r@   rO   r   r   r   r   r?      s    
z#_BulkWriteResultBase.modified_countc                 C  s<   |  d d| jkr&tt| jdS tt| jdS dS )rF   rG   ZnRemovedZnDeletedN)r!   rL   r   r8   r=   r   r   r   r   rG      s    

z"_BulkWriteResultBase.deleted_countc                 C  s   |  d tt| jdS )z!The number of documents upserted.upserted_countZ	nUpsertedrO   r   r   r   r   rP      s    
z#_BulkWriteResultBase.upserted_count)r   r"   r#   r$   r%   r   r   r&   rK   rN   r9   r?   rG   rP   r-   r   r   r+   r   rH      s    rH   c                      sN   e Zd ZdZdZdddd fddZd	d
ddZedd
ddZ  Z	S )BulkWriteResultz>An object wrapper for collection-level bulk write API results.r   rI   r
   r   rJ   c                   s   t  || dS )a@  Create a BulkWriteResult instance.

        :param bulk_api_result: A result dict from the collection-level bulk write API
        :param acknowledged: Was this write result acknowledged? If ``False``
            then all properties of this object will raise
            :exc:`~pymongo.errors.InvalidOperation`.
        N)r*   r   rM   r+   r   r   r   	  s    zBulkWriteResult.__init__r   r   c                 C  s   | j j d| jd| j dS r,   )r   r   rK   r   r   r   r   r   r     s    zBulkWriteResult.__repr__zOptional[dict[int, Any]]c                 C  s(   |  d | jr$dd | jd D S dS )z=A map of operation index to the _id of the upserted document.upserted_idsc                 S  s   i | ]}|d  |d qS )indexrB   r   ).0Zupsertr   r   r   
<dictcomp>  s      z0BulkWriteResult.upserted_ids.<locals>.<dictcomp>rA   N)r!   rK   r   r   r   r   rR     s    
zBulkWriteResult.upserted_ids)
r   r"   r#   r$   r%   r   r   r&   rR   r-   r   r   r+   r   rQ     s   
rQ   c                      s   e Zd ZdZdZddddd fddZd	d
ddZd	ddddZedd
ddZ	edd
ddZ
edd
ddZedd
ddZ  ZS )ClientBulkWriteResultz:An object wrapper for client-level bulk write API results.)Z__has_verbose_resultszMutableMapping[str, Any]r
   r   )rK   r   has_verbose_resultsr   c                   s   || _ t || dS )aU  Create a ClientBulkWriteResult instance.

        :param bulk_api_result: A result dict from the client-level bulk write API
        :param acknowledged: Was this write result acknowledged? If ``False``
            then all properties of this object will raise
            :exc:`~pymongo.errors.InvalidOperation`.
        :param has_verbose_results: Should the returned result be verbose?
            If ``False``, then the ``insert_results``, ``update_results``, and
            ``delete_results`` properties of this object will raise
            :exc:`~pymongo.errors.InvalidOperation`.
        N)+_ClientBulkWriteResult__has_verbose_resultsr*   r   )r   rK   r   rW   r+   r   r   r   &  s
    zClientBulkWriteResult.__init__r   r   c                 C  s   d | jj| j| j| jS )Nz%{}({!r}, acknowledged={}, verbose={}))formatr   r   rK   r   rW   r   r   r   r   r   =  s    zClientBulkWriteResult.__repr__r   c                 C  s   | j std| ddS )zARaise an exception on property access if verbose results are off.r   zv is not available when the results are not set to be verbose. Check the verbose_results attribute to avoid this error.N)rX   r   r    r   r   r   _raise_if_not_verboseE  s    
z+ClientBulkWriteResult._raise_if_not_verbosec                 C  s   | j S )z/Whether the returned results should be verbose.)rX   r   r   r   r   rW   N  s    z)ClientBulkWriteResult.has_verbose_resultszMapping[int, InsertOneResult]c                 C  s.   |  d | d ttttf | jdS )z:A map of successful insertion operations to their results.insert_resultsZinsertResults)r!   rZ   r   r   r8   r'   rK   r=   r   r   r   r   r[   S  s    



z$ClientBulkWriteResult.insert_resultszMapping[int, UpdateResult]c                 C  s.   |  d | d ttttf | jdS )z7A map of successful update operations to their results.update_resultsZupdateResults)r!   rZ   r   r   r8   r1   rK   r=   r   r   r   r   r\   ]  s    



z$ClientBulkWriteResult.update_resultszMapping[int, DeleteResult]c                 C  s.   |  d | d ttttf | jdS )z7A map of successful delete operations to their results.delete_resultsZdeleteResults)r!   rZ   r   r   r8   rD   rK   r=   r   r   r   r   r]   g  s    



z$ClientBulkWriteResult.delete_results)r   r"   r#   r$   r%   r   r   rZ   r&   rW   r[   r\   r]   r-   r   r   r+   r   rV   !  s   			rV   N)r$   
__future__r   typingr   r   r   r   r   Zpymongo.errorsr   r	   r'   r.   r1   rD   rH   rQ   rV   r   r   r   r   <module>   s   )C3