
v^!                 @   s   d  Z  d d l m Z d d l m Z d d l m Z d Z d Z Gd d   d e j	  Z
 Gd	 d
   d
  Z Gd d   d  Z Gd d   d  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 Z d Z d d d  Z d d   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 e d d    Z e d d    Z e d d    Z e d d     Z e d! d"    Z d# d$   Z d S)%	WhereNodea  
    An 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.
    FTc             C   s[  |  j  s |  d f S| |  j A} | r5 |  j t k pH | oH |  j t k } | rd |  j  rd d |  f Sg  } g  } x |  j D] } t | d  r | j |  \ } } | d k	 r | j |  | d k	 r | j |  qz | j  r | j |  qz | j |  qz W| r$|  j	 | |  j |  j  n d }	 | rK|  j	 | |  j |  j  n d }
 |
 |	 f S)z
        Return 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-build-8lau8j11/django/django/db/models/sql/where.pyr       s,    	

	''zWhereNode.split_havingc             C   sw  g  } g  } |  j  t k r4 t |  j  d } } n d t |  j  } } x |  j D] } y | j |  \ } }	 Wn t k
 r | d 8} Yn. X| r | j |  | j |	  n
 | d 8} | d k r |  j r d g  f St  | d k rT |  j rt  qT d g  f SqT Wd |  j  }
 |
 j	 |  } | rm|  j rHd | } n% t |  d k sc|  j
 rmd | } | | f S)a  
        Return the SQL version of the where clause and the value to be
        substituted in. Return '', [] if this node matches everything,
        None, [] if this node is empty, and raise EmptyResultSet if this
        node can't match anything.
           r    z %s zNOT (%s)z(%s))r   r   lenr   compiler   r   extendr
   joinresolved)r   compiler
connectionresultZresult_paramsZfull_neededZempty_neededchildsqlparamsconnZ
sql_stringr   r   r   as_sqlA   s<    
	
			
zWhereNode.as_sqlNc             C   s1   g  } x$ |  j  D] } | j | j    q W| S)N)r   r   get_group_by_cols)r   aliascolsr   r   r   r   r#   u   s    zWhereNode.get_group_by_colsc             C   s   |  j  d  d   S)N)r   )r   r   r   r   get_source_expressions{   s    z WhereNode.get_source_expressionsc             C   s.   t  |  t  |  j  k s! t  | |  _ d  S)N)r   r   AssertionError)r   r   r   r   r   set_source_expressions~   s    !z WhereNode.set_source_expressionsc             C   sh   xa t  |  j  D]P \ } } t | d  r; | j |  q t | d  r | j |  |  j | <q Wd S)z
        Relabel 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)      s
    zWhereNode.relabel_aliasesc             C   sw   |  j  j d g  d |  j d |  j  } xI |  j D]> } t | d  r_ | j j | j    q1 | j j |  q1 W| S)z
        Create a clone of the tree. Must only be called on root nodes (nodes
        with empty subtree_parents). Childs must be either (Constraint, 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                s9   t  | t j  r2 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	   )r1   objr   )r1   r   r/      s     zWhereNode._contains_aggregatec             C   s   |  j  |   S)N)r/   )r   r   r   r   r	      s    zWhereNode.contains_aggregatec                s9   t  | t j  r2 t   f d d   | j D  S| j S)Nc             3   s   |  ] }   j  |  Vq d  S)N)_contains_over_clause)r0   r   )r1   r   r   r2      s    z2WhereNode._contains_over_clause.<locals>.<genexpr>)r3   r   r4   r5   r   contains_over_clause)r1   r6   r   )r1   r   r7      s     zWhereNode._contains_over_clausec             C   s   |  j  |   S)N)r7   )r   r   r   r   r8      s    zWhereNode.contains_over_clausec             C   s   t  d d   |  j D  S)Nc             s   s   |  ] } | j  Vq d  S)N)
is_summary)r0   r   r   r   r   r2      s    z'WhereNode.is_summary.<locals>.<genexpr>)r5   r   )r   r   r   r   r9      s    zWhereNode.is_summaryc             O   s(   t  |  d  r$ |  j | | |  }  |  S)Nresolve_expression)r   r:   )exprqueryargskwargsr   r   r   _resolve_leaf   s    zWhereNode._resolve_leafc             O   s   t  | d  r9 x' | j D] } |  j | | | |  q Wt  | d  rf |  j | j | | |  | _ t  | d  r |  j | j | | |  | _ d  S)Nr   lhsrhs)r   r   _resolve_noder?   r@   rA   )r1   noder<   r=   r>   r   r   r   r   rB      s    zWhereNode._resolve_nodec             O   s,   |  j    } | j | | |  d | _ | S)NT)r.   rB   r   )r   r=   r>   r.   r   r   r   r:      s    	zWhereNode.resolve_expression)__name__
__module____qualname____doc__r   defaultr   Zconditionalr   r"   r#   r&   r(   r)   r.   r*   classmethodr/   r   r	   r7   r8   propertyr9   staticmethodr?   rB   r:   r   r   r   r   r      s(   !4
r   c               @   s.   e  Z d  Z d Z d Z d 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)rD   rE   rF   rG   r	   r"   r   r   r   r   rL      s   rL   c               @   s4   e  Z d  Z d Z d d   Z d d d d  Z d S)
ExtraWhereFc             C   s   | |  _  | |  _ d  S)N)sqlsr    )r   rN   r    r   r   r   __init__   s    	zExtraWhere.__init__Nc             C   s8   d d   |  j  D } d j |  t |  j p1 f   f S)Nc             S   s   g  |  ] } d  |  q S)z(%s)r   )r0   r   r   r   r   
<listcomp>   s   	 z%ExtraWhere.as_sql.<locals>.<listcomp>z AND )rN   r   listr    )r   r   r   rN   r   r   r   r"      s    zExtraWhere.as_sql)rD   rE   rF   r	   rO   r"   r   r   r   r   rM      s   rM   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)SubqueryConstraintFc             C   s(   | |  _  | |  _ | |  _ | |  _ d  S)N)r$   columnstargetsquery_object)r   r$   rS   rT   rU   r   r   r   rO      s    			zSubqueryConstraint.__init__c             C   sD   |  j  } | j |  j  | j d |  } | j |  j |  j |  S)Nr   )rU   Z
set_valuesrT   Zget_compilerZas_subquery_conditionr$   rS   )r   r   r   r<   Zquery_compilerr   r   r   r"      s    	zSubqueryConstraint.as_sqlN)rD   rE   rF   r	   rO   r"   r   r   r   r   rR      s   rR   N)rG   Zdjango.core.exceptionsr   Zdjango.utilsr   Zdjango.utils.functionalr   r   r   r4   r   rL   rM   rR   r   r   r   r   <module>   s   