ó
ø¢TQc           @  sÃ   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d e f d „  ƒ  YZ	 d e
 f d	 „  ƒ  YZ d
 e j f d „  ƒ  YZ d e
 f d „  ƒ  YZ e d „ Z d „  Z e e _ d S(   uî   
Various data structures used in query construction.

Factored out from django.db.models.query to avoid making the main module very
large and/or so that they can be used by other modules without getting into
circular import difficulties.
iÿÿÿÿ(   t   unicode_literals(   t   util(   t   six(   t   treet   InvalidQueryc           B  s   e  Z d  Z RS(   uE   
    The query passed to raw isn't a safe query to use with raw.
    (   t   __name__t
   __module__t   __doc__(    (    (    s5   ../Django//lib/python/django/db/models/query_utils.pyR      s   t   QueryWrapperc           B  s&   e  Z d  Z d „  Z d d d „ Z RS(   u¢   
    A type that indicates the contents are an SQL fragment and the associate
    parameters. Can be used to pass opaque data to a where-clause, for example.
    c         C  s   | | f |  _  d  S(   N(   t   data(   t   selft   sqlt   params(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   __init__   s    c         C  s   |  j  S(   N(   R	   (   R
   t   qnt
   connection(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   as_sql   s    N(   R   R   R   R   t   NoneR   (    (    (    s5   ../Django//lib/python/django/db/models/query_utils.pyR      s   	t   Qc           B  sM   e  Z d  Z d Z d Z e Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   ub   
    Encapsulates filters as objects that can then be combined logically (using
    & and |).
    u   ANDu   ORc         O  s6   t  t |  ƒ j d t | ƒ t t j | ƒ ƒ ƒ d  S(   Nt   children(   t   superR   R   t   listR   t	   iteritems(   R
   t   argst   kwargs(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyR   +   s    c         C  sQ   t  | t ƒ s t | ƒ ‚ n  t |  ƒ ƒ  } | j |  | ƒ | j | | ƒ | S(   N(   t
   isinstanceR   t	   TypeErrort   typet   add(   R
   t   othert   connt   obj(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   _combine.   s    c         C  s   |  j  | |  j ƒ S(   N(   R    t   OR(   R
   R   (    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   __or__6   s    c         C  s   |  j  | |  j ƒ S(   N(   R    t   AND(   R
   R   (    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   __and__9   s    c         C  s0   t  |  ƒ ƒ  } | j |  |  j ƒ | j ƒ  | S(   N(   R   R   R#   t   negate(   R
   R   (    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt
   __invert__<   s    
(   R   R   R   R#   R!   t   defaultR   R    R"   R$   R&   (    (    (    s5   ../Django//lib/python/django/db/models/query_utils.pyR   !   s   				t   DeferredAttributec           B  s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   u„   
    A wrapper for a deferred-loading field. When the value is read from this
    object the first time, the query is executed.
    c         C  s   | |  _  d  S(   N(   t
   field_name(   R
   R)   t   model(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyR   G   s    c   
      C  s<  d d l  m } | j j } | j } | d k	 s7 t ‚ | j } | j |  j |  ƒ |  k r1y | j	 |  j ƒ d } WnC | k
 rº g  | j
 D] } | j |  j k r | ^ q d } n X| j } |  j | | ƒ }	 |	 d k r!t | j j | ƒ j | j j ƒ j d | j ƒ |  j ƒ }	 n  |	 | |  j <n  | |  j S(   uz   
        Retrieves and caches the value from the datastore on the first lookup.
        Returns the cached value.
        iÿÿÿÿ(   t   FieldDoesNotExisti    t   pkN(   t   django.db.models.fieldsR+   t   _metat   proxy_for_modelR   t   AssertionErrort   __dict__t   getR)   t   get_field_by_namet   fieldst   attnamet   namet   _check_parent_chaint   getattrt   _base_managert   onlyt   usingt   _statet   dbR,   (
   R
   t   instancet   ownerR+   t   non_deferred_modelt   optsR	   t   fR6   t   val(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   __get__J   s(    		&	c         C  s   | | j  |  j <d S(   u†   
        Deferred loading attributes can be set normally (which means there will
        never be a database lookup involved.
        N(   R1   R)   (   R
   R>   t   value(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   __set__m   s    c         C  sW   | j  } | j | ƒ d } | j | j ƒ } | j rS | | k rS t | | j ƒ Sd S(   uÁ   
        Check if the field value can be fetched from a parent field already
        loaded in the instance. This can be done if the to-be fetched
        field is a primary key field.
        i    N(   R.   R3   t   get_ancestor_linkR*   t   primary_keyR8   R5   R   (   R
   R>   R6   RA   RB   t
   link_field(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyR7   t   s    	(   R   R   R   R   RD   RF   R7   (    (    (    s5   ../Django//lib/python/django/db/models/query_utils.pyR(   B   s
   		#	c      	   C  sÑ   |  j  s t S|  j  j r$ | r$ t S| rc | rF |  j ƒ  | k rF t S| rc |  j | k rc t Sn  | rw |  j rw t S| rÍ |  j | k rÍ | rÆ |  j | k rÆ t d |  j j j	 |  j f ƒ ‚ n  t Sn  t
 S(   uv  
    Returns True if this field should be used to descend deeper for
    select_related() purposes. Used by both the query construction code
    (sql.query.fill_related_selections()) and the model instance creation code
    (query.get_klass_info()).

    Arguments:
     * field - the field to be checked
     * restricted - a boolean field, indicating if the field list has been
       manually restricted using a requested clause)
     * requested - The select_related() dictionary.
     * load_fields - the set of fields to be loaded on this model
     * reverse - boolean, True if we are checking a reverse select related
    uX   Field %s.%s cannot be both deferred and traversed using select_related at the same time.(   t   relt   Falset   parent_linkt   related_query_nameR6   t   nullR   R*   R.   t   object_namet   True(   t   fieldt
   restrictedt	   requestedt   load_fieldst   reverse(    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   select_related_descend‚   s$    	c           s¾   d d
 ‡  f d †  ƒ  Y} d ˆ  j  d j t t | ƒ ƒ ƒ f } t j | d d ƒ } t g  | D] } | t | ˆ  ƒ f ^ q` ƒ } | | d <ˆ  j | d <t	 | d	 <t
 t | ƒ ˆ  f | ƒ S(   uß   
    Returns a class object that is a copy of "model" with the specified "attrs"
    being replaced with DeferredAttribute objects. The "pk_value" ties the
    deferred attributes to a particular instance of the model.
    t   Metac             s   e  Z e Z ˆ  j j Z RS(    (   R   R   RP   t   proxyR.   t	   app_label(    (   R*   (    s5   ../Django//lib/python/django/db/models/query_utils.pyRW   ¯   s   u   %s_Deferred_%su   _iP   i    u   Metau
   __module__u	   _deferred(    (   R   t   joint   sortedR   R   t   truncate_namet   dictR(   R   RP   R   t   str(   R*   t   attrsRW   R6   t   attrt	   overrides(    (   R*   s5   ../Django//lib/python/django/db/models/query_utils.pyt   deferred_class_factory©   s    ((

N(   R   t
   __future__R    t   django.db.backendsR   t   django.utilsR   R   t	   ExceptionR   t   objectR   t   NodeR   R(   RK   RV   Rb   RP   t   __safe_for_unpickling__(    (    (    s5   ../Django//lib/python/django/db/models/query_utils.pyt   <module>   s   !@'	