
^Q\&                 @   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 e d d	  Z Gd
 d   d e  Z d d   Z Gd d   d e  Z Gd d   d e	 j  Z Gd d   d e  Z Gd d   d e  Z d d d  Z d d   Z d d   Z d S)z
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.
    )unicode_literalsN)
namedtuple)
LOOKUP_SEP)tree)	lru_cachePathInfoz5from_opts to_opts target_fields join_field m2m directc               @   s   e  Z d  Z d Z d S)InvalidQueryzE
    The query passed to raw isn't a safe query to use with raw.
    N)__name__
__module____qualname____doc__ r   r   B/var/www/dbchiro/venv/build/Django/django/db/models/query_utils.pyr      s   r   c             c   s<   |  Vx0 |  j    D]" } x t |  D] } | Vq% Wq Wd  S)N)__subclasses__
subclasses)clssubclassitemr   r   r   r      s    r   c               @   s:   e  Z d  Z d Z d Z d d   Z d d d d  Z d S)QueryWrapperz
    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.
    Fc             C   s   | t  |  f |  _ d  S)N)listdata)selfZsqlparamsr   r   r   __init__.   s    zQueryWrapper.__init__Nc             C   s   |  j  S)N)r   )r   compiler
connectionr   r   r   as_sql1   s    zQueryWrapper.as_sql)r	   r
   r   r   Zcontains_aggregater   r   r   r   r   r   r   '   s   r   c                   s   e  Z d  Z d Z d Z d Z e Z   f d d   Z d d   Z d d	   Z	 d
 d   Z
 d d   Z d d d d d d d  Z   S)Qzf
    Encapsulates filters as objects that can then be combined logically (using
    `&` and `|`).
    ANDORc                s3   t  t |   j d t |  t | j     d  S)Nchildren)superr   r   r   items)r   argskwargs)	__class__r   r   r   ?   s    z
Q.__init__c             C   sZ   t  | t  s t |   n  t |     } | | _ | j |  |  | j | |  | S)N)
isinstancer   	TypeErrortypeZ	connectoradd)r   otherconnobjr   r   r   _combineB   s    	z
Q._combinec             C   s   |  j  | |  j  S)N)r-   r   )r   r*   r   r   r   __or__K   s    zQ.__or__c             C   s   |  j  | |  j  S)N)r-   r   )r   r*   r   r   r   __and__N   s    z	Q.__and__c             C   s0   t  |     } | j |  |  j  | j   | S)N)r(   r)   r   negate)r   r,   r   r   r   
__invert__Q   s    
zQ.__invert__NTFc             C   s5   | j  |  | d | d d \ } } | j |  | S)Nallow_joinsZ
split_subqF)Z_add_qZpromote_joins)r   queryr2   ZreuseZ	summarizeZfor_saveZclauseZjoinsr   r   r   resolve_expressionW   s    $zQ.resolve_expression)r	   r
   r   r   r   r   defaultr   r-   r.   r/   r1   r4   r   r   )r%   r   r   5   s   	r   c               @   s=   e  Z d  Z d Z d d   Z d d d  Z d d   Z d S)	DeferredAttributez
    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)
field_name)r   r7   modelr   r   r   r   d   s    zDeferredAttribute.__init__Nc             C   s   | d k r |  S| j  } | j |  j |   |  k r |  j | |  j  } | d k r | j d |  j g  t | |  j  } n  | | |  j <n  | |  j S)zz
        Retrieves and caches the value from the datastore on the first lookup.
        Returns the cached value.
        Nfields)__dict__getr7   _check_parent_chainZrefresh_from_dbgetattr)r   instancer   r   valr   r   r   __get__g   s    	zDeferredAttribute.__get__c             C   sS   | j  } | j |  } | j | j  } | j rO | | k rO t | | j  Sd S)z
        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.
        N)_meta	get_fieldZget_ancestor_linkr8   primary_keyr=   attname)r   r>   nameoptsfZ
link_fieldr   r   r   r<   y   s    	z%DeferredAttribute._check_parent_chain)r	   r
   r   r   r   r@   r<   r   r   r   r   r6   _   s   r6   c               @   s   e  Z d  Z e d d    Z e e d d  d d     Z d d   Z d	 d
   Z e	 d d    Z
 e d d    Z e d d d   Z e d d d   Z d S)RegisterLookupMixinc             C   s   |  j    j | d   S)N)get_lookupsr;   )r   lookup_namer   r   r   _get_lookup   s    zRegisterLookupMixin._get_lookupmaxsizeNc             C   s)   d d   t  j |   D } |  j |  S)Nc             S   s%   g  |  ] } | j  j d  i    q S)class_lookups)r:   r;   ).0parentr   r   r   
<listcomp>   s   	 z3RegisterLookupMixin.get_lookups.<locals>.<listcomp>)inspectZgetmromerge_dicts)r   rM   r   r   r   rI      s    zRegisterLookupMixin.get_lookupsc             C   sn   d d l  m } |  j |  } | d  k rJ t |  d  rJ |  j j |  S| d  k	 rj t | |  rj d  S| S)Nr   )Lookupoutput_field)django.db.models.lookupsrS   rK   hasattrrT   
get_lookup
issubclass)r   rJ   rS   foundr   r   r   rW      s    zRegisterLookupMixin.get_lookupc             C   sn   d d l  m } |  j |  } | d  k rJ t |  d  rJ |  j j |  S| d  k	 rj t | |  rj d  S| S)Nr   )	TransformrT   )rU   rZ   rK   rV   rT   get_transformrX   )r   rJ   rZ   rY   r   r   r   r[      s    z!RegisterLookupMixin.get_transformc             C   s.   i  } x! t  |   D] } | j |  q W| S)z
        Merge dicts in reverse to preference the order of the original list. e.g.,
        merge_dicts([a, b]) will preference the keys in 'a' over those in 'b'.
        )reversedupdate)ZdictsZmergeddr   r   r   rR      s    zRegisterLookupMixin.merge_dictsc             C   s(   x! t  |   D] } | j j   q Wd  S)N)r   rI   cache_clear)r   r   r   r   r   _clear_cached_lookups   s    z)RegisterLookupMixin._clear_cached_lookupsc             C   sN   | d  k r | j  } n  d |  j k r3 i  |  _ n  | |  j | <|  j   | S)NrM   )rJ   r:   rM   r`   )r   lookuprJ   r   r   r   register_lookup   s    
z#RegisterLookupMixin.register_lookupc             C   s&   | d k r | j  } n  |  j | =d S)zn
        Remove given lookup from cls lookups. For use in tests only as it's
        not thread-safe.
        N)rJ   rM   )r   ra   rJ   r   r   r   _unregister_lookup   s    z&RegisterLookupMixin._unregister_lookup)r	   r
   r   classmethodrK   r   rI   rW   r[   staticmethodrR   r`   rb   rc   r   r   r   r   rH      s   			rH   Fc             C   s   |  j  s d S|  j  j r$ | r$ d S| rc | rF |  j   | k rF d S| rc |  j | k rc d Sn  | rw |  j rw d S| r |  j | k r | r |  j | k r t d |  j j j	 |  j f   q q n  d S)av  
    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
    FzXField %s.%s cannot be both deferred and traversed using select_related at the same time.T)
Zremote_fieldZparent_linkZrelated_query_namerE   nullrD   r   r8   rA   Zobject_name)fieldZ
restricted	requestedZload_fieldsreverser   r   r   select_related_descend   s"    	%rj   c             C   sr   xe t  t |   d  D]M } t j |  d |   } | | k r | | r | | |  | d  f Sq Wd f  f S)z
    A helper method to check if the lookup_parts contains references
    to the given annotations set. Because the LOOKUP_SEP is contained in the
    default annotation names we must check each prefix of the lookup_parts
    for a match.
       r   NF)rangelenr   join)Zlookup_partsZannotationsnZlevel_n_lookupr   r   r   refs_expression   s
    rp   c                s@     f d d   } | |  p? t  | d d  o? | | j j  S)z
    Check that self.model is compatible with target_opts. Compatibility
    is OK if:
      1) model and opts match (where proxy inheritance is removed)
      2) model is parent of opts' model or the other way around
    c                s=     j  j |  j k p< |  j   j  j   k p<   |  j   k S)N)rA   Zconcrete_modelZget_parent_list)rF   )r8   r   r   check  s    z-check_rel_lookup_compatibility.<locals>.checkrC   F)r=   r8   rA   )r8   Ztarget_optsrg   rq   r   )r8   r   check_rel_lookup_compatibility   s    rr   )r   
__future__r   rQ   collectionsr   Zdjango.db.models.constantsr   Zdjango.utilsr   Zdjango.utils.lru_cacher   r   	Exceptionr   r   objectr   Noder   r6   rH   rj   rp   rr   r   r   r   r   <module>   s    	*(C$