σ
ψ’TQc           @@  s)  d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z d d l m Z d d	 l m Z d d
 l m Z d Z d Z d e f d     YZ d e j f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   sG   
Code to manage the creation and SQL rendering of 'where' constraints.
i    (   t   absolute_importN(   t   repeat(   t   tree(   t   Field(   t   EmptyResultSet(   t	   Aggregate(   t   is_iterator(   t   xranget   ANDt   ORt   EmptyShortCircuitc           B@  s   e  Z d  Z RS(   st   
    Internal exception used to indicate that a "matches nothing" node should be
    added to the where-clause.
    (   t   __name__t
   __module__t   __doc__(    (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR
      s   t	   WhereNodec           B@  sD   e  Z d  Z e Z d   Z d   Z d   Z d   Z d d  Z
 RS(   s  
    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).

    The children in this tree are usually either Q-like objects or lists of
    [table_alias, field_name, db_type, lookup_type, value_annotation, params].
    However, a child could also be any class with as_sql() and relabel_aliases() methods.
    c         C@  sξ   t  | t t f  s2 t t |   j | |  d S| \ } } } t |  r\ t |  } n  t  | t j  rz t j } n' t | d  r | j	 } n t
 |  } t | d  rΕ | j | |  } n  t t |   j | | | | f |  d S(   s  
        Add a node to the where-tree. If the data is a list or tuple, it is
        expected to be of the form (obj, lookup_type, value), where obj is
        a Constraint object, and is then slightly munged before being stored
        (to avoid storing any reference to field objects). Otherwise, the 'data'
        is stored unchanged and can be any class with an 'as_sql()' method.
        Nt   value_annotationt   prepare(   t
   isinstancet   listt   tuplet   superR   t   addR   t   datetimet   hasattrR   t   boolR   (   t   selft   datat	   connectort   objt   lookup_typet   valueR   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR   )   s    c         C@  sς  g  } g  } d
 \ } } t  |  j  } xN|  j D]C} yO t | d  rj | j d | d |  \ }	 }
 n |  j | | |  \ }	 }
 Wn t k
 r£ | d 7} nJ X|	 rΗ | j |	  | j |
  n& |	 d k rγ | d 8} q1 n  | d 7} |  j	 t
 k r| d } } n d | } } | | d k rE|  j r<d g  f St  n  | | d k r1 |  j rgt  qtd g  f Sq1 q1 W| d k rd g  f Sd |  j	 } | j |  } | rθ|  j rΖd | } qθt  |  d k rθd	 | } qθn  | | f S(   s  
        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.
        i    t   as_sqlt   qnt
   connectioni   t    s    %s s   NOT (%s)s   (%s)(   i    i    N(   t   lent   childrenR   R   t	   make_atomR   t   appendt   extendt   NoneR   R   t   negatedt   join(   R   R    R!   t   resultt   result_paramst   everything_childst   nothing_childst   non_empty_childst   childt   sqlt   paramst   full_neededt   empty_neededt   connt
   sql_string(    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR   L   sN    !

	
			
	c         C@  sέ  | \ } } } } t  | t  r] y | j | | |  \ } } Wq t k
 rY t  q Xn6 t  | t  r | j j | | |  } n t d   t  | t	  rΊ |  j
 | | |  }	 n | j | |  }	 | t j k rν | j j   }
 n d }
 t | d  r#| j | |  \ } } d }
 n d } t |  d k rr| d d k rr| d k rr| j j rrd } t } n  | | j k rΌd	 | j j |  f } | |	 | j | |
 | f | f S| d
 k r| sΧt  n  | rρd |	 | f | f S| j j   } | rάt |  | k rάd g } x t d t |  |  D]~ } | d k r\| j d  n  | j d |	  t t |  | |  } d j t d |   } | j |  | j d  q:W| j d  d j |  | f Sd |	 d j t d t |    f | f SnΎ | d  k r%d |	 | f S| d! k rNd | j j | |	  | f S| d k r{d |	 | rmd ppd f d" f S| d k r| j j |	  | f S| d# k rΙ| j j |  |	 |
 f | f St d |   d S($   s'  
        Turn a tuple (Constraint(table_alias, column_name, db_type),
        lookup_type, value_annotation, params) into valid SQL.

        The first item of the tuple may also be an Aggregate.

        Returns the string for the SQL fragment and the parameters to use for
        it.
        s[   'make_atom' expects a Constraint or an Aggregate as the first item of its 'child' argument.s   %sR   R"   i   i    t   exactt   isnulls
   %s %%s %%st   ins   %s IN %st   (s    OR s   %s IN (s   , t   )s
   %s IN (%s)t   ranget   years   %s BETWEEN %%s and %%st   montht   dayt   week_days   %s = %%ss   %s IS %sNULLs   NOT t   searcht   regext   iregexs   Invalid lookup_type: %rN(   s   ranges   year(   s   months   dayR@   (    (   RB   RC   (   R   t
   Constraintt   processR
   R   R   t   fieldt   get_db_prep_lookupt	   TypeErrorR   t   sql_for_columnsR   R   t   opst   datetime_cast_sqlR   R#   t   featurest!   interprets_empty_strings_as_nullst   Truet	   operatorst   lookup_castt   max_in_list_sizeR   R&   t   minR*   R   t   date_extract_sqlt   fulltext_search_sqlt   regex_lookup(   R   R0   R    R!   t   lvalueR   R   t   params_or_valueR2   t	   field_sqlt   cast_sqlt   extrat   formatRQ   t   in_clause_elementst   offsett
   group_sizet   param_group(    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR%      s|    
	.			
 c         C@  sT   | \ } } } | r4 d | |  | |  f } n | |  } | j  j |  | S(   s·   
        Returns the SQL fragment used for the left-hand side of a column
        constraint (for example, the "T1.foo" portion in the clause
        "WHERE ... T1.foo = 6").
        s   %s.%s(   RJ   t   field_cast_sql(   R   R   R    R!   t   table_aliast   namet   db_typet   lhs(    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRI   ν   s
    c         C@  s6  | s |  } n  x t  | j  D]\ } } t | d  rJ | j |  q t | t j  ro |  j | |  q t | t t f  r t | d t t f  rσ t | d  } | d | k r| | d | d <t |  f | d | j | <qn | d j |  t | d d  r.| d j |  q.q q Wd S(   s   
        Relabels the alias values of any children. 'change_map' is a dictionary
        mapping old (current) alias values to the new values.
        t   relabel_aliasesi    i   i   N(	   t	   enumerateR$   R   Re   R   R   t   NodeR   R   (   R   t
   change_mapt   nodet   posR0   t   elt(    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRe   ϊ   s     	$N(   R   R   R   R   t   defaultR   R   R%   RI   R(   Re   (    (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR      s   
	#	H	Y	t   EverythingNodec           B@  s)   e  Z d  Z d d d  Z d d  Z RS(   s)   
    A node that matches everything.
    c         C@  s
   d g  f S(   NR"   (    (   R   R    R!   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR     s    c         C@  s   d  S(   N(    (   R   Rh   Ri   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRe     s    N(   R   R   R   R(   R   Re   (    (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRm     s   t   NothingNodec           B@  s)   e  Z d  Z d d d  Z d d  Z RS(   s&   
    A node that matches nothing.
    c         C@  s
   t   d  S(   N(   R   (   R   R    R!   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR   "  s    c         C@  s   d  S(   N(    (   R   Rh   Ri   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRe   %  s    N(   R   R   R   R(   R   Re   (    (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRn     s   t
   ExtraWherec           B@  s    e  Z d    Z d d d  Z RS(   c         C@  s   | |  _  | |  _ d  S(   N(   t   sqlsR2   (   R   Rp   R2   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyt   __init__)  s    	c         C@  sB   g  |  j  D] } d | ^ q
 } d j |  t |  j p; d  f S(   Ns   (%s)s    AND (    (   Rp   R*   R   R2   (   R   R    R!   R1   Rp   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR   -  s     N(   R   R   Rq   R(   R   (    (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRo   (  s   	RD   c           B@  sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s   
    An object that can be passed to WhereNode.add() and knows how to
    pre-process itself prior to including in the WhereNode.
    c         C@  s!   | | | |  _  |  _ |  _ d  S(   N(   t   aliast   colRF   (   R   Rr   Rs   RF   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRq   6  s    c         C@  sF   |  j  j   } |  j r; |  j j | d <|  j j | d <n  | d =| S(   sχ   Save the state of the Constraint for pickling.

        Fields aren't necessarily pickleable, because they can have
        callable default values. So, instead of pickling the field
        store a reference so we can restore it manually
        t   modelt
   field_nameRF   (   t   __dict__t   copyRF   Rt   Rb   (   R   t   obj_dict(    (    s3   ../Django//lib/python/django/db/models/sql/where.pyt   __getstate__9  s    	c         C@  se   | j  d d  } | j  d d  } |  j j |  | d k	 rX | j j |  |  _ n	 d |  _ d S(   s   Restore the constraint Rt   Ru   N(   t   popR(   Rv   t   updatet   _metat	   get_fieldRF   (   R   R   Rt   Ru   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyt   __setstate__G  s    c         C@  s    |  j  r |  j  j | |  S| S(   N(   RF   t   get_prep_lookup(   R   R   R   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyR   Q  s    	c         C@  s°   d d l  m } ym |  j rU |  j j | | d | d t } |  j j d |  } n' t   j | | d | d t } d } Wn | k
 r t  n X|  j	 |  j
 | f | f S(   sa   
        Returns a tuple of data suitable for inclusion in a WhereNode
        instance.
        i    (   t   ObjectDoesNotExistR!   t   preparedN(   t   django.db.models.baseR   RF   RG   RN   Rc   R   R(   R
   Rr   Rs   (   R   R   R   R!   R   R2   Rc   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRE   V  s    	

c         C@  s&   |  j  | k r" | |  j  |  _  n  d  S(   N(   Rr   (   R   Rh   (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRe   n  s    (	   R   R   R   Rq   Ry   R~   R   RE   Re   (    (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyRD   1  s   			
		(   R   t
   __future__R    R   t	   itertoolsR   t   django.utilsR   t   django.db.models.fieldsR   t#   django.db.models.sql.datastructuresR   t   django.db.models.sql.aggregatesR   t   django.utils.itercompatR   t   django.utils.six.movesR   R   R	   t	   ExceptionR
   Rg   R   t   objectRm   Rn   Ro   RD   (    (    (    s3   ../Django//lib/python/django/db/models/sql/where.pyt   <module>   s"   χ
	