U
    d.P                     @  s  d Z ddlmZ ddlmZmZmZmZ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 ddlmZmZmZ dd	lmZmZmZ erdd
l m!Z! ddl"m#Z# eee$ee$ee%e$e	e$ef f f f  Z&ee$e&f Z'G dd dee Z(G dd dZ)G dd dZ*G dd dee Z+G dd dZ,G dd de,Z-G dd de,Z.G dd dZ/G dd dZ0dS )zOperation class definitions.    )annotations)
TYPE_CHECKINGAnyDictGenericListMappingOptionalSequenceTupleUnion)RawBSONDocument)helpers)validate_collation_or_none)validate_booleanvalidate_is_mappingvalidate_list)_gen_index_name_index_document_index_list)_CollationIn_DocumentType	_Pipeline)SON)_Bulkc                   @  sb   e Zd ZdZdZdddddZddd	d
dZddddZdddddZdddddZ	dS )	InsertOnez#Represents an insert_one operation._docr   None)documentreturnc                 C  s
   || _ dS )zCreate an InsertOne instance.

        For use with :meth:`~pymongo.collection.Collection.bulk_write`.

        :Parameters:
          - `document`: The document to insert. If the document is missing an
            _id field one will be added.
        Nr   )selfr    r"   6/tmp/pip-unpacked-wheel-oblwsawz/pymongo/operations.py__init__4   s    	zInsertOne.__init__r   bulkobjr    c                 C  s   | | j dS )3Add this operation to the _Bulk instance `bulkobj`.N)Z
add_insertr   r!   r&   r"   r"   r#   _add_to_bulk?   s    zInsertOne._add_to_bulkstrr    c                 C  s   d| j dS )Nz
InsertOne()r   r!   r"   r"   r#   __repr__C   s    zInsertOne.__repr__r   boolotherr    c                 C  s    t |t | kr|j| jkS tS N)typer   NotImplementedr!   r1   r"   r"   r#   __eq__F   s    zInsertOne.__eq__c                 C  s
   | |k S r2   r"   r5   r"   r"   r#   __ne__K   s    zInsertOne.__ne__N
__name__
__module____qualname____doc__	__slots__r$   r)   r.   r6   r7   r"   r"   r"   r#   r   /   s   r   c                   @  sh   e Zd ZdZdZddddddd	d
ZdddddZddddZdddddZdddddZ	dS )	DeleteOnez"Represents a delete_one operation._filter
_collation_hintNMapping[str, Any]Optional[_CollationIn]Optional[_IndexKeyHint]r   filter	collationhintr    c                 C  sH   |dk	rt d| |dk	r2t|ts2t|| _n|| _|| _|| _dS )a  Create a DeleteOne instance.

        For use with :meth:`~pymongo.collection.Collection.bulk_write`.

        :Parameters:
          - `filter`: A query that matches the document to delete.
          - `collation` (optional): An instance of
            :class:`~pymongo.collation.Collation`.
          - `hint` (optional): An index to use to support the query
            predicate specified either by its string name, or in the same
            format as passed to
            :meth:`~pymongo.collection.Collection.create_index` (e.g.
            ``[('field', ASCENDING)]``). This option is only supported on
            MongoDB 4.4 and above.

        .. versionchanged:: 3.11
           Added the ``hint`` option.
        .. versionchanged:: 3.5
           Added the `collation` option.
        NrG   r   
isinstancer*   r   r   rB   r@   rA   r!   rG   rH   rI   r"   r"   r#   r$   T   s    
zDeleteOne.__init__r   r%   c                 C  s    |j | jdt| j| jd dS )r'      rH   rI   NZ
add_deleter@   r   rA   rB   r(   r"   r"   r#   r)   w   s    zDeleteOne._add_to_bulkr*   r+   c                 C  s   d| j d| jdS )Nz
DeleteOne(, r,   r@   rA   r-   r"   r"   r#   r.      s    zDeleteOne.__repr__r   r/   r0   c                 C  s,   t |t | kr(|j|jf| j| jfkS tS r2   r3   r@   rA   r4   r5   r"   r"   r#   r6      s    zDeleteOne.__eq__c                 C  s
   | |k S r2   r"   r5   r"   r"   r#   r7      s    zDeleteOne.__ne__)NNr8   r"   r"   r"   r#   r>   O   s     #	r>   c                   @  sh   e Zd ZdZdZddddddd	d
ZdddddZddddZdddddZdddddZ	dS )
DeleteManyz#Represents a delete_many operation.r?   NrC   rD   rE   r   rF   c                 C  sH   |dk	rt d| |dk	r2t|ts2t|| _n|| _|| _|| _dS )a!  Create a DeleteMany instance.

        For use with :meth:`~pymongo.collection.Collection.bulk_write`.

        :Parameters:
          - `filter`: A query that matches the documents to delete.
          - `collation` (optional): An instance of
            :class:`~pymongo.collation.Collation`.
          - `hint` (optional): An index to use to support the query
            predicate specified either by its string name, or in the same
            format as passed to
            :meth:`~pymongo.collection.Collection.create_index` (e.g.
            ``[('field', ASCENDING)]``). This option is only supported on
            MongoDB 4.4 and above.

        .. versionchanged:: 3.11
           Added the ``hint`` option.
        .. versionchanged:: 3.5
           Added the `collation` option.
        NrG   rJ   rL   r"   r"   r#   r$      s    
zDeleteMany.__init__r   r%   c                 C  s    |j | jdt| j| jd dS )r'   r   rN   NrO   r(   r"   r"   r#   r)      s    zDeleteMany._add_to_bulkr*   r+   c                 C  s   d| j d| jdS )NzDeleteMany(rP   r,   rQ   r-   r"   r"   r#   r.      s    zDeleteMany.__repr__r   r/   r0   c                 C  s,   t |t | kr(|j|jf| j| jfkS tS r2   rR   r5   r"   r"   r#   r6      s    zDeleteMany.__eq__c                 C  s
   | |k S r2   r"   r5   r"   r"   r#   r7      s    zDeleteMany.__ne__)NNr8   r"   r"   r"   r#   rS      s     #	rS   c                   @  sl   e Zd ZdZdZdddddd	d
dddZdd
dddZdddddZdddddZddddZ	dS )
ReplaceOnez#Represents a replace_one operation.)r@   r   _upsertrA   rB   FNrC   z%Union[_DocumentType, RawBSONDocument]r/   rD   rE   r   )rG   replacementupsertrH   rI   r    c                 C  sf   |dk	rt d| |dk	r$td| |dk	rDt|tsDt|| _n|| _|| _|| _|| _	|| _
dS )a  Create a ReplaceOne instance.

        For use with :meth:`~pymongo.collection.Collection.bulk_write`.

        :Parameters:
          - `filter`: A query that matches the document to replace.
          - `replacement`: The new document.
          - `upsert` (optional): If ``True``, perform an insert if no documents
            match the filter.
          - `collation` (optional): An instance of
            :class:`~pymongo.collation.Collation`.
          - `hint` (optional): An index to use to support the query
            predicate specified either by its string name, or in the same
            format as passed to
            :meth:`~pymongo.collection.Collection.create_index` (e.g.
            ``[('field', ASCENDING)]``). This option is only supported on
            MongoDB 4.2 and above.

        .. versionchanged:: 3.11
           Added the ``hint`` option.
        .. versionchanged:: 3.5
           Added the ``collation`` option.
        NrG   rW   )r   r   rK   r*   r   r   rB   r@   r   rU   rA   )r!   rG   rV   rW   rH   rI   r"   r"   r#   r$      s    

zReplaceOne.__init__r   r%   c                 C  s&   |j | j| j| jt| j| jd dS )r'   rN   N)Zadd_replacer@   r   rU   r   rA   rB   r(   r"   r"   r#   r)      s    zReplaceOne._add_to_bulkr   r0   c                 C  sD   t |t | kr@|j|j|j|j|jf| j| j| j| j|jfkS tS r2   )r3   r@   r   rU   rA   rB   r4   r5   r"   r"   r#   r6     s    zReplaceOne.__eq__c                 C  s
   | |k S r2   r"   r5   r"   r"   r#   r7     s    zReplaceOne.__ne__r*   r+   c                 C  s"   d | jj| j| j| j| j| jS )Nz {}({!r}, {!r}, {!r}, {!r}, {!r}))format	__class__r9   r@   r   rU   rA   rB   r-   r"   r"   r#   r.     s    zReplaceOne.__repr__)FNN)
r9   r:   r;   r<   r=   r$   r)   r6   r7   r.   r"   r"   r"   r#   rT      s      ,
rT   c                   @  sJ   e Zd ZdZdZddddddd	d
dZdddddZddddZdS )	_UpdateOpz)Private base class for update operations.)r@   r   rU   rA   _array_filtersrB   rC   #Union[Mapping[str, Any], _Pipeline]r/   rD   !Optional[List[Mapping[str, Any]]]rE   )rG   docrW   rH   array_filtersrI   c                 C  s~   |d k	rt d| |d k	r$td| |d k	r6td| |d k	rVt|tsVt|| _n|| _|| _|| _	|| _
|| _|| _d S )NrG   rW   r_   )r   r   r   rK   r*   r   r   rB   r@   r   rU   rA   r[   )r!   rG   r^   rW   rH   r_   rI   r"   r"   r#   r$   "  s    	


z_UpdateOp.__init__objectr0   c                 C  sJ   t |t| rF|j|j|j|j|j|jf| j| j| j| j| j| jfkS tS r2   )	rK   r3   r@   r   rU   rA   r[   rB   r4   r5   r"   r"   r#   r6   <  s"    z_UpdateOp.__eq__r*   r+   c              	   C  s&   d | jj| j| j| j| j| j| jS )Nz&{}({!r}, {!r}, {!r}, {!r}, {!r}, {!r}))	rX   rY   r9   r@   r   rU   rA   r[   rB   r-   r"   r"   r#   r.   O  s    z_UpdateOp.__repr__N)r9   r:   r;   r<   r=   r$   r6   r.   r"   r"   r"   r#   rZ     s
   rZ   c                	      sH   e Zd ZdZdZdddddd	d
dd fddZdddddZ  ZS )	UpdateOnez#Represents an update_one operation.r"   FNrC   r\   r/   rD   r]   rE   r   rG   updaterW   rH   r_   rI   r    c                   s   t  |||||| dS )a  Represents an update_one operation.

        For use with :meth:`~pymongo.collection.Collection.bulk_write`.

        :Parameters:
          - `filter`: A query that matches the document to update.
          - `update`: The modifications to apply.
          - `upsert` (optional): If ``True``, perform an insert if no documents
            match the filter.
          - `collation` (optional): An instance of
            :class:`~pymongo.collation.Collation`.
          - `array_filters` (optional): A list of filters specifying which
            array elements an update should apply.
          - `hint` (optional): An index to use to support the query
            predicate specified either by its string name, or in the same
            format as passed to
            :meth:`~pymongo.collection.Collection.create_index` (e.g.
            ``[('field', ASCENDING)]``). This option is only supported on
            MongoDB 4.2 and above.

        .. versionchanged:: 3.11
           Added the `hint` option.
        .. versionchanged:: 3.9
           Added the ability to accept a pipeline as the `update`.
        .. versionchanged:: 3.6
           Added the `array_filters` option.
        .. versionchanged:: 3.5
           Added the `collation` option.
        Nsuperr$   r!   rG   rc   rW   rH   r_   rI   rY   r"   r#   r$   `  s    &zUpdateOne.__init__r   r%   c              	   C  s,   |j | j| jd| jt| j| j| jd dS )r'   FrH   r_   rI   NZ
add_updater@   r   rU   r   rA   r[   rB   r(   r"   r"   r#   r)     s    zUpdateOne._add_to_bulk)FNNNr9   r:   r;   r<   r=   r$   r)   __classcell__r"   r"   rg   r#   ra   [  s        (ra   c                	      sH   e Zd ZdZdZdddddd	d
dd fddZdddddZ  ZS )
UpdateManyz$Represents an update_many operation.r"   FNrC   r\   r/   rD   r]   rE   r   rb   c                   s   t  |||||| dS )a  Create an UpdateMany instance.

        For use with :meth:`~pymongo.collection.Collection.bulk_write`.

        :Parameters:
          - `filter`: A query that matches the documents to update.
          - `update`: The modifications to apply.
          - `upsert` (optional): If ``True``, perform an insert if no documents
            match the filter.
          - `collation` (optional): An instance of
            :class:`~pymongo.collation.Collation`.
          - `array_filters` (optional): A list of filters specifying which
            array elements an update should apply.
          - `hint` (optional): An index to use to support the query
            predicate specified either by its string name, or in the same
            format as passed to
            :meth:`~pymongo.collection.Collection.create_index` (e.g.
            ``[('field', ASCENDING)]``). This option is only supported on
            MongoDB 4.2 and above.

        .. versionchanged:: 3.11
           Added the `hint` option.
        .. versionchanged:: 3.9
           Added the ability to accept a pipeline as the `update`.
        .. versionchanged:: 3.6
           Added the `array_filters` option.
        .. versionchanged:: 3.5
           Added the `collation` option.
        Nrd   rf   rg   r"   r#   r$     s    &zUpdateMany.__init__r   r%   c              	   C  s,   |j | j| jd| jt| j| j| jd dS )r'   Trh   Nri   r(   r"   r"   r#   r)     s    zUpdateMany._add_to_bulk)FNNNrj   r"   r"   rg   r#   rl     s        (rl   c                   @  s8   e Zd ZdZdZddddddZed	d
ddZdS )
IndexModelzRepresents an index to create.)
__document_IndexKeyHintr   r   )keyskwargsr    c                 K  sZ   t |}|ddkr"t||d< t||d< t|dd}|| _|dk	rV|| jd< dS )a  Create an Index instance.

        For use with :meth:`~pymongo.collection.Collection.create_indexes`.

        Takes either a single key or a list containing (key, direction) pairs
        or keys.  If no direction is given, :data:`~pymongo.ASCENDING` will
        be assumed.
        The key(s) must be an instance of :class:`str`, and the direction(s) must
        be one of (:data:`~pymongo.ASCENDING`, :data:`~pymongo.DESCENDING`,
        :data:`~pymongo.GEO2D`, :data:`~pymongo.GEOSPHERE`,
        :data:`~pymongo.HASHED`, :data:`~pymongo.TEXT`).

        Valid options include, but are not limited to:

          - `name`: custom name to use for this index - if none is
            given, a name will be generated.
          - `unique`: if ``True``, creates a uniqueness constraint on the index.
          - `background`: if ``True``, this index should be created in the
            background.
          - `sparse`: if ``True``, omit from the index any documents that lack
            the indexed field.
          - `bucketSize`: for use with geoHaystack indexes.
            Number of documents to group together within a certain proximity
            to a given longitude and latitude.
          - `min`: minimum value for keys in a :data:`~pymongo.GEO2D`
            index.
          - `max`: maximum value for keys in a :data:`~pymongo.GEO2D`
            index.
          - `expireAfterSeconds`: <int> Used to create an expiring (TTL)
            collection. MongoDB will automatically delete documents from
            this collection after <int> seconds. The indexed field must
            be a UTC datetime or the data will not expire.
          - `partialFilterExpression`: A document that specifies a filter for
            a partial index.
          - `collation`: An instance of :class:`~pymongo.collation.Collation`
            that specifies the collation to use.
          - `wildcardProjection`: Allows users to include or exclude specific
            field paths from a `wildcard index`_ using the { "$**" : 1} key
            pattern. Requires MongoDB >= 4.2.
          - `hidden`: if ``True``, this index will be hidden from the query
            planner and will not be evaluated as part of query plan
            selection. Requires MongoDB >= 4.4.

        See the MongoDB documentation for a full list of supported options by
        server version.

        :Parameters:
          - `keys`: a single key or a list containing (key, direction) pairs
             or keys specifying the index to create.
          - `**kwargs` (optional): any additional index creation
            options (see the above list) should be passed as keyword
            arguments.

        .. versionchanged:: 3.11
           Added the ``hidden`` option.
        .. versionchanged:: 3.2
           Added the ``partialFilterExpression`` option to support partial
           indexes.

        .. _wildcard index: https://mongodb.com/docs/master/core/index-wildcard/
        nameNkeyrH   )r   getr   r   r   pop_IndexModel__document)r!   rp   rq   rH   r"   r"   r#   r$     s    >zIndexModel.__init__zDict[str, Any]r+   c                 C  s   | j S )zUAn index document suitable for passing to the createIndexes
        command.
        )rv   r-   r"   r"   r#   r     s    zIndexModel.documentNr9   r:   r;   r<   r=   r$   propertyr   r"   r"   r"   r#   rm     s
   Grm   c                   @  s:   e Zd ZdZdZddddddd	Zedd
ddZdS )SearchIndexModelz$Represents a search index to create.rn   NrC   zOptional[str]r   )
definitionrr   r    c                 C  s(   |dk	rt ||d| _nt |d| _dS )a  Create a Search Index instance.

        For use with :meth:`~pymongo.collection.Collection.create_search_index` and :meth:`~pymongo.collection.Collection.create_search_indexes`.

        :Parameters:
          - `definition` - The definition for this index.
          - `name` (optional) - The name for this index, if present.

        .. versionadded:: 4.5

        .. note:: Search indexes require a MongoDB server version 7.0+ Atlas cluster.
        N)rr   rz   )rz   )dict_SearchIndexModel__document)r!   rz   rr   r"   r"   r#   r$   (  s    zSearchIndexModel.__init__r+   c                 C  s   | j S )zThe document for this index.)r|   r-   r"   r"   r#   r   :  s    zSearchIndexModel.document)Nrw   r"   r"   r"   r#   ry   #  s
   ry   N)1r<   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r   Zbson.raw_bsonr   Zpymongor   Zpymongo.collationr   Zpymongo.commonr   r   r   Zpymongo.helpersr   r   r   Zpymongo.typingsr   r   r   Zbson.sonr   Zpymongo.bulkr   r*   intZ
_IndexListro   r   r>   rS   rT   rZ   ra   rl   rm   ry   r"   r"   r"   r#   <module>   s,   0* ==T>::T