3
]ð]‘6  ã               @   s¼   d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
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G dd„ deƒZdS )zOperation class definitions.é    )Úvalidate_booleanÚvalidate_is_mappingÚvalidate_list)Úvalidate_collation_or_none)Ú_gen_index_nameÚ_index_documentÚ_index_listc               @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )Ú	InsertOnez#Represents an insert_one operation.Ú_docc             C   s
   || _ dS )züCreate 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.
        N)r
   )ÚselfÚdocument© r   ú5/tmp/pip-build-20mum3z4/pymongo/pymongo/operations.pyÚ__init__   s    	zInsertOne.__init__c             C   s   |j | jƒ dS )z3Add this operation to the _Bulk instance `bulkobj`.N)Z
add_insertr
   )r   Úbulkobjr   r   r   Ú_add_to_bulk&   s    zInsertOne._add_to_bulkc             C   s   d| j f S )NzInsertOne(%r))r
   )r   r   r   r   Ú__repr__*   s    zInsertOne.__repr__c             C   s    t |ƒt | ƒkr|j| jkS tS )N)Útyper
   ÚNotImplemented)r   Úotherr   r   r   Ú__eq__-   s    zInsertOne.__eq__c             C   s
   | |k S )Nr   )r   r   r   r   r   Ú__ne__2   s    zInsertOne.__ne__N)r
   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r   r   r   r   r   r   r	      s   r	   c               @   s>   e Zd ZdZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )Ú	DeleteOnez"Represents a delete_one operation.Ú_filterÚ
_collationNc             C   s"   |dk	rt d|ƒ || _|| _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`. This option is only
            supported on MongoDB 3.4 and above.

        .. versionchanged:: 3.5
           Added the `collation` option.
        NÚfilter)r   r   r   )r   r    Ú	collationr   r   r   r   ;   s    
zDeleteOne.__init__c             C   s   |j | jd| jd� dS )z3Add this operation to the _Bulk instance `bulkobj`.é   )r!   N)Ú
add_deleter   r   )r   r   r   r   r   r   N   s    zDeleteOne._add_to_bulkc             C   s   d| j | jf S )NzDeleteOne(%r, %r))r   r   )r   r   r   r   r   R   s    zDeleteOne.__repr__c             C   s,   t |ƒt | ƒkr(|j|jf| j| jfkS tS )N)r   r   r   r   )r   r   r   r   r   r   U   s    
zDeleteOne.__eq__c             C   s
   | |k S )Nr   )r   r   r   r   r   r   [   s    zDeleteOne.__ne__)r   r   )N)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   6   s   
r   c               @   s>   e Zd ZdZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )Ú
DeleteManyz#Represents a delete_many operation.r   r   Nc             C   s"   |dk	rt d|ƒ || _|| _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`. This option is only
            supported on MongoDB 3.4 and above.

        .. versionchanged:: 3.5
           Added the `collation` option.
        Nr    )r   r   r   )r   r    r!   r   r   r   r   d   s    
zDeleteMany.__init__c             C   s   |j | jd| jd� dS )z3Add this operation to the _Bulk instance `bulkobj`.r   )r!   N)r#   r   r   )r   r   r   r   r   r   w   s    zDeleteMany._add_to_bulkc             C   s   d| j | jf S )NzDeleteMany(%r, %r))r   r   )r   r   r   r   r   {   s    zDeleteMany.__repr__c             C   s,   t |ƒt | ƒkr(|j|jf| j| jfkS tS )N)r   r   r   r   )r   r   r   r   r   r   ~   s    
zDeleteMany.__eq__c             C   s
   | |k S )Nr   )r   r   r   r   r   r   „   s    zDeleteMany.__ne__)r   r   )N)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   _   s   
r$   c               @   s>   e Zd ZdZdZddd	„Zd
d„ Zdd„ Zdd„ Zdd„ Z	dS )Ú
ReplaceOnez#Represents a replace_one operation.r   r
   Ú_upsertr   FNc             C   s@   |dk	rt d|ƒ |dk	r$td|ƒ || _|| _|| _|| _dS )aX  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`. This option is only
            supported on MongoDB 3.4 and above.

        .. versionchanged:: 3.5
           Added the `collation` option.
        Nr    Úupsert)r   r   r   r
   r&   r   )r   r    Úreplacementr'   r!   r   r   r   r   �   s    

zReplaceOne.__init__c             C   s   |j | j| j| j| jd� dS )z3Add this operation to the _Bulk instance `bulkobj`.)r!   N)Zadd_replacer   r
   r&   r   )r   r   r   r   r   r   §   s    zReplaceOne._add_to_bulkc             C   s<   t |ƒt | ƒkr8|j|j|j|jf| j| j| j| jfkS tS )N)r   r   r
   r&   r   r   )r   r   r   r   r   r   ¬   s    zReplaceOne.__eq__c             C   s
   | |k S )Nr   )r   r   r   r   r   r   ³   s    zReplaceOne.__ne__c             C   s   d| j j| j| j| j| jf S )Nz%s(%r, %r, %r, %r))Ú	__class__r   r   r
   r&   r   )r   r   r   r   r   ¶   s    zReplaceOne.__repr__)r   r
   r&   r   )FN)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   ˆ   s   
r%   c               @   s4   e Zd ZdZdZdd„ Zd	d
„ Zdd„ Zdd„ ZdS )Ú	_UpdateOpz)Private base class for update operations.r   r
   r&   r   Ú_array_filtersc             C   sX   |d k	rt d|ƒ |d k	r$td|ƒ |d k	r6td|ƒ || _|| _|| _|| _|| _d S )Nr    r'   Úarray_filters)r   r   r   r   r
   r&   r   r+   )r   r    Údocr'   r!   r,   r   r   r   r   Á   s    


z_UpdateOp.__init__c             C   sD   t |ƒt | ƒkr@|j|j|j|j|jf| j| j| j| j| jfkS tS )N)r   r   r
   r&   r   r+   r   )r   r   r   r   r   r   Î   s    
z_UpdateOp.__eq__c             C   s
   | |k S )Nr   )r   r   r   r   r   r   ×   s    z_UpdateOp.__ne__c             C   s"   d| j j| j| j| j| j| jf S )Nz%s(%r, %r, %r, %r, %r))r)   r   r   r
   r&   r   r+   )r   r   r   r   r   Ú   s    z_UpdateOp.__repr__N)r   r
   r&   r   r+   )	r   r   r   r   r   r   r   r   r   r   r   r   r   r*   ¼   s   	r*   c                   s.   e Zd ZdZf Zd‡ fdd„	Zdd„ Z‡  ZS )	Ú	UpdateOnez#Represents an update_one operation.FNc                s   t t| ƒj|||||ƒ 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`. This option is only
            supported on MongoDB 3.4 and above.
          - `array_filters` (optional): A list of filters specifying which
            array elements an update should apply. Requires MongoDB 3.6+.

        .. 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   )r   r    Úupdater'   r!   r,   )r)   r   r   r   å   s    zUpdateOne.__init__c             C   s$   |j | j| jd| j| j| jd� dS )z3Add this operation to the _Bulk instance `bulkobj`.F)r!   r,   N)Ú
add_updater   r
   r&   r   r+   )r   r   r   r   r   r      s    zUpdateOne._add_to_bulk)FNN)r   r   r   r   r   r   r   Ú__classcell__r   r   )r)   r   r.   à   s
    r.   c                   s.   e Zd ZdZf Zd‡ fdd„	Zdd„ Z‡  ZS )	Ú
UpdateManyz$Represents an update_many operation.FNc                s   t t| ƒj|||||ƒ 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`. This option is only
            supported on MongoDB 3.4 and above.
          - `array_filters` (optional): A list of filters specifying which
            array elements an update should apply. Requires MongoDB 3.6+.

        .. 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)r/   r3   r   )r   r    r0   r'   r!   r,   )r)   r   r   r     s    zUpdateMany.__init__c             C   s$   |j | j| jd| j| j| jd� dS )z3Add this operation to the _Bulk instance `bulkobj`.T)r!   r,   N)r1   r   r
   r&   r   r+   )r   r   r   r   r   r   '  s    zUpdateMany._add_to_bulk)FNN)r   r   r   r   r   r   r   r2   r   r   )r)   r   r3     s
    r3   c               @   s(   e Zd ZdZdZdd„ Zedd„ ƒZdS )	Ú
IndexModelzRepresents an index to create.Ú
__documentc             K   sT   t |ƒ}d|krt|ƒ|d< t|ƒ|d< t|jddƒƒ}|| _|dk	rP|| 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 of (key, direction) pairs.
        The key(s) must be an instance of :class:`basestring`
        (:class:`str` in python 3), and the direction(s) must be one of
        (:data:`~pymongo.ASCENDING`, :data:`~pymongo.DESCENDING`,
        :data:`~pymongo.GEO2D`, :data:`~pymongo.GEOHAYSTACK`,
        :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. Requires server version >= 3.2.
          - `collation`: An instance of :class:`~pymongo.collation.Collation`
            that specifies the collation to use in MongoDB >= 3.4.
          - `wildcardProjection`: Allows users to include or exclude specific
            field paths from a `wildcard index`_ using the { "$**" : 1} key
            pattern. Requires server version >= 4.2.

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

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

        .. versionchanged:: 3.2
            Added partialFilterExpression to support partial indexes.

        .. _wildcard index: https://docs.mongodb.com/master/core/index-wildcard/#wildcard-index-core
        ÚnameÚkeyr!   N)r   r   r   r   ÚpopÚ_IndexModel__document)r   ÚkeysÚkwargsr!   r   r   r   r   3  s    8zIndexModel.__init__c             C   s   | j S )zUAn index document suitable for passing to the createIndexes
        command.
        )r9   )r   r   r   r   r   t  s    zIndexModel.documentN)r5   )r   r   r   r   r   r   Úpropertyr   r   r   r   r   r4   .  s   Ar4   N)r   Zpymongo.commonr   r   r   Zpymongo.collationr   Zpymongo.helpersr   r   r   Úobjectr	   r   r$   r%   r*   r.   r3   r4   r   r   r   r   Ú<module>   s    ))4$''