3
(hv                 @   sv   d Z ddlmZ ddlmZ ddlmZ dZdZG dd dej	Z
G d	d
 d
eZG dd deZG dd deZdS )zG
Code to manage the creation and SQL rendering of 'where' constraints.
    )EmptyResultSet)tree)cached_propertyANDORc               @   s^   e Zd ZdZeZdddZdd Zdd Zd	d
 Z	dd Z
dd Zedd Zedd ZdS )	WhereNodea  
    Used to represent the SQL where-clause.

    The class is tied to the Query class that created it (in order to create
    the correct SQL).

    A child is usually an expression producing boolean values. Most likely the
    expression is a Lookup instance.

    However, a child could also be any class with as_sql() and either
    relabeled_clone() method or relabel_aliases() and clone() methods and
    contains_aggregate attribute.
    Fc             C   s   | j s| dfS || jA }|r&| jtkp4| o4| jtk}|rH| j rHd| fS g }g }xh| jD ]^}t|dr|j|\}}|dk	r|j| |dk	r|j| qX|j r|j| qX|j| qXW |r| j	|| j| jnd}	|r| j	|| j| jnd}
|
|	fS )z
        Returns two possibly None nodes: one for those parts of self that
        should be included in the WHERE clause and one for those parts of
        self that must be included in the HAVING clause.
        Nsplit_having)
contains_aggregatenegated	connectorr   r   childrenhasattrr   append	__class__)selfr
   Z
in_negatedZmay_need_splitZwhere_partsZhaving_partscZ
where_partZhaving_partZhaving_nodeZ
where_node r   >/tmp/pip-install-q3hcpn_q/Django/django/db/models/sql/where.pyr      s,    



zWhereNode.split_havingc             C   s  g }g }| j tkr$t| jd }}ndt| j }}x| jD ]}y|j|\}}	W n tk
rn   |d8 }Y n$X |r|j| |j|	 n|d8 }|dkr| jrdg fS t|dkr<| jrtq<dg fS q<W d| j  }
|
j	|}|r| jrd| }nt|dkrd| }||fS )a  
        Returns the SQL version of the where clause and the value to be
        substituted in. Returns '', [] if this node matches everything,
        None, [] if this node is empty, and raises EmptyResultSet if this
        node can't match anything.
           r    z %s zNOT (%s)z(%s))
r   r   lenr   compiler   r   extendr
   join)r   compiler
connectionresultZresult_paramsZfull_neededZempty_neededchildsqlparamsconnZ
sql_stringr   r   r   as_sql?   s<    




zWhereNode.as_sqlc             C   s&   g }x| j D ]}|j|j  qW |S )N)r   r   get_group_by_cols)r   colsr   r   r   r   r"   s   s    zWhereNode.get_group_by_colsc             C   sL   xFt | jD ]8\}}t|dr*|j| qt|dr|j|| j|< qW dS )z
        Relabels the alias values of any children. 'change_map' is a dictionary
        mapping old (current) alias values to the new values.
        relabel_aliasesrelabeled_cloneN)	enumerater   r   r$   r%   )r   
change_mapposr   r   r   r   r$   y   s
    

zWhereNode.relabel_aliasesc             C   sR   | j jg | j| jd}x6| jD ],}t|dr>|jj|j  q|jj| qW |S )z
        Creates a clone of the tree. Must only be called on root nodes (nodes
        with empty subtree_parents). Childs must be either (Contraint, lookup,
        value) tuples, or objects supporting .clone().
        )r   r   r
   clone)r   Z_new_instancer   r
   r   r   r   r)   )r   r)   r   r   r   r   r)      s    
zWhereNode.clonec             C   s   | j  }|j| |S )N)r)   r$   )r   r'   r)   r   r   r   r%      s    
zWhereNode.relabeled_clonec                s*   t |tjr$t fdd|jD S |jS )Nc             3   s   | ]} j |V  qd S )N)_contains_aggregate).0r   )clsr   r   	<genexpr>   s    z0WhereNode._contains_aggregate.<locals>.<genexpr>)
isinstancer   Nodeanyr   r	   )r,   objr   )r,   r   r*      s    zWhereNode._contains_aggregatec             C   s
   | j | S )N)r*   )r   r   r   r   r	      s    zWhereNode.contains_aggregateN)F)__name__
__module____qualname____doc__r   defaultr   r!   r"   r$   r)   r%   classmethodr*   r   r	   r   r   r   r   r      s   
!4r   c               @   s   e Zd ZdZdZdddZdS )NothingNodez&
    A node that matches nothing.
    FNc             C   s   t d S )N)r   )r   r   r   r   r   r   r!      s    zNothingNode.as_sql)NN)r2   r3   r4   r5   r	   r!   r   r   r   r   r8      s   r8   c               @   s"   e Zd ZdZdd ZdddZdS )
ExtraWhereFc             C   s   || _ || _d S )N)sqlsr   )r   r:   r   r   r   r   __init__   s    zExtraWhere.__init__Nc             C   s(   dd | j D }dj|t| jp"f fS )Nc             S   s   g | ]}d | qS )z(%s)r   )r+   r   r   r   r   
<listcomp>   s    z%ExtraWhere.as_sql.<locals>.<listcomp>z AND )r:   r   listr   )r   r   r   r:   r   r   r   r!      s    zExtraWhere.as_sql)NN)r2   r3   r4   r	   r;   r!   r   r   r   r   r9      s   r9   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )SubqueryConstraintFc             C   s   || _ || _|| _|| _d S )N)aliascolumnstargetsquery_object)r   r?   r@   rA   rB   r   r   r   r;      s    zSubqueryConstraint.__init__c             C   s   | j }t|drb|jr*|j|jkr*td|jd krB|j| j }n|j }|j	}|j
 rb|jd |j|d}|j| j| j|S )Nvaluesz2Can't do subqueries with queries on different DBs.T)r   )rB   r   _dbr?   
ValueError_fieldsrC   rA   Z_clonequeryZ
can_filterZclear_orderingZget_compilerZas_subquery_conditionr@   )r   r   r   rG   Zquery_compilerr   r   r   r!      s    


zSubqueryConstraint.as_sqlc             C   s   |j | j| j| _d S )N)getr?   )r   r'   r   r   r   r$      s    z"SubqueryConstraint.relabel_aliasesc             C   s   | j | j| j| j| jS )N)r   r?   r@   rA   rB   )r   r   r   r   r)      s    zSubqueryConstraint.cloneN)r2   r3   r4   r	   r;   r!   r$   r)   r   r   r   r   r>      s
   r>   N)r5   Z#django.db.models.sql.datastructuresr   Zdjango.utilsr   Zdjango.utils.functionalr   r   r   r/   r   objectr8   r9   r>   r   r   r   r   <module>   s    
