U
    Lk7gZ                     @   s   d dl Z d dlZd dlmZ d dlmZ dZdd 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ZG dd deZG dd deZdS )    N)InvalidQueryError)	transform)QQNodec                  C   s   d} t j| tdd d S )NzD'empty' property is deprecated in favour of using 'not bool(filter)'   )
stacklevel)warningswarnDeprecationWarning)msg r   @/tmp/pip-unpacked-wheel-n1etwkgt/mongoengine/queryset/visitor.pywarn_empty_is_deprecated
   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )QNodeVisitorz?Base visitor class for visiting Q-object nodes in a query tree.c                 C   s   |S )zCalled by QCombination objects.r   )selfcombinationr   r   r   visit_combination   s    zQNodeVisitor.visit_combinationc                 C   s   |S )zCalled by (New)Q objects.r   r   queryr   r   r   visit_query   s    zQNodeVisitor.visit_queryN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )DuplicateQueryConditionsErrorN)r   r   r   r   r   r   r   r      s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )SimplificationVisitorzgSimplifies query trees by combining unnecessary 'and' connection nodes
    into a single Q-object.
    c                 C   s\   |j |jkrXtdd |jD rXdd |jD }ztf | |W S  tk
rV   Y nX |S )Nc                 s   s   | ]}t |tV  qd S N)
isinstancer   .0noder   r   r   	<genexpr>'   s     z:SimplificationVisitor.visit_combination.<locals>.<genexpr>c                 S   s   g | ]
}|j qS r   r   )r   nr   r   r   
<listcomp>(   s     z;SimplificationVisitor.visit_combination.<locals>.<listcomp>)	operationANDallchildrenr   _query_conjunctionr   )r   r   queriesr   r   r   r   $   s    z'SimplificationVisitor.visit_combinationc                 C   sR   t  }i }|D ]>}t | }||}|r2t || |t| q|S )z4Merges query dicts - effectively &ing them together.)setkeysintersectionr   updatecopydeepcopy)r   r*   Z	query_opsZcombined_queryr   opsr-   r   r   r   r)   0   s    

z(SimplificationVisitor._query_conjunctionN)r   r   r   r   r   r)   r   r   r   r   r      s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	QueryCompilerVisitorzUCompiles the nodes in a query tree to a PyMongo-compatible query
    dictionary.
    c                 C   s
   || _ d S r   )document)r   r3   r   r   r   __init__F   s    zQueryCompilerVisitor.__init__c                 C   s   d}|j |jkrd}||jiS )Nz$andz$or)r%   ORr(   )r   r   operatorr   r   r   r   I   s    z&QueryCompilerVisitor.visit_combinationc                 C   s   t j| jf|jS r   )r   r   r3   r   r   r   r   r   O   s    z QueryCompilerVisitor.visit_queryN)r   r   r   r   r4   r   r   r   r   r   r   r2   A   s   r2   c                   @   sL   e Zd ZdZdZdZdd Zdd Zdd	 Ze	d
d Z
dd Zdd ZdS )r   z$Base class for nodes in query trees.r      c                 C   s   |  t }| t|}|S r   )acceptr   r2   )r   r3   r   r   r   r   to_queryY   s    zQNode.to_queryc                 C   s   t d S r   )NotImplementedErrorr   visitorr   r   r   r8   ^   s    zQNode.acceptc                 C   s&   t |s| S t | s|S t|| |gS )zPCombine this node with another node into a QCombination
        object.
        )boolQCombination)r   otherr%   r   r   r   _combinea   s
    zQNode._combinec                 C   s
   t   dS )NF)r   r   r   r   r   emptyo   s    zQNode.emptyc                 C   s   |  || jS r   )r@   r5   r   r?   r   r   r   __or__t   s    zQNode.__or__c                 C   s   |  || jS r   )r@   r&   rC   r   r   r   __and__w   s    zQNode.__and__N)r   r   r   r   r&   r5   r9   r8   r@   propertyrB   rD   rE   r   r   r   r   r   S   s   
r   c                   @   sD   e Zd ZdZdd Zdd Zdd Zdd	 Zed
d Z	dd Z
dS )r>   zVRepresents the combination of several conditions by a given
    logical operator.
    c                 C   sL   || _ g | _|D ]6}t|tr:|j |kr:|  j|j7  _q| j| qd S r   )r%   r(   r   r>   append)r   r%   r(   r    r   r   r   r4      s    zQCombination.__init__c                 C   s.   | j | jkrdnd}d|dd | jD  S )Nz & z | z(%s)c                 S   s   g | ]}t |qS r   )reprr   r   r   r   r$      s     z)QCombination.__repr__.<locals>.<listcomp>)r%   r&   joinr(   )r   opr   r   r   __repr__   s    zQCombination.__repr__c                 C   s
   t | jS r   )r=   r(   rA   r   r   r   __bool__   s    zQCombination.__bool__c                 C   sD   t t| jD ]*}t| j| tr| j| || j|< q|| S r   )rangelenr(   r   r   r8   r   )r   r<   ir   r   r   r8      s    zQCombination.acceptc                 C   s   t   t| j S r   )r   r=   r(   rA   r   r   r   rB      s    zQCombination.emptyc                 C   s$   | j |j ko"| j|jko"| j|jkS r   )	__class__r%   r(   rC   r   r   r   __eq__   s
    

zQCombination.__eq__N)r   r   r   r   r4   rK   rL   r8   rF   rB   rQ   r   r   r   r   r>   {   s   
r>   c                   @   sD   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
dS )r   z_A simple query object, used in a query tree to build up more complex
    query structures.
    c                 K   s
   || _ d S r   r"   r   r   r   r   r4      s    z
Q.__init__c                 C   s   dt | j S )NzQ(**%s))rH   r   rA   r   r   r   rK      s    z
Q.__repr__c                 C   s
   t | jS r   )r=   r   rA   r   r   r   rL      s    z
Q.__bool__c                 C   s   | j |j ko| j|jkS r   )rP   r   rC   r   r   r   rQ      s    zQ.__eq__c                 C   s
   | | S r   )r   r;   r   r   r   r8      s    zQ.acceptc                 C   s   t   t| j S r   )r   r=   r   rA   r   r   r   rB      s    zQ.emptyN)r   r   r   r   r4   rK   rL   rQ   r8   rF   rB   r   r   r   r   r      s   r   )r/   r   Zmongoengine.errorsr   Zmongoengine.querysetr   __all__r   r   r   r   r2   r   r>   r   r   r   r   r   <module>   s   "(+