î
ªÍ X-&  ã               @   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 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)ÚFieldDoesNotExist)Ú
LOOKUP_SEP)ÚtreeÚ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   úJ/home/ubuntu/projects/ifolica/build/django/django/db/models/query_utils.pyr      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_sql(   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   6   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   Ú_combine9   s    	z
Q._combinec             C   s   |  j  | |  j ƒ S)N)r(   r   )r   r%   r   r   r   Ú__or__B   s    zQ.__or__c             C   s   |  j  | |  j ƒ S)N)r(   r   )r   r%   r   r   r   Ú__and__E   s    z	Q.__and__c             C   s0   t  |  ƒ ƒ  } | j |  |  j ƒ | j ƒ  | S)N)r#   r$   r   Únegate)r   r'   r   r   r   Ú
__invert__H   s    
zQ.__invert__NTFc             C   s5   | j  |  | d | d d ƒ\ } } | j | ƒ | S)NÚallow_joinsZ
split_subqF)Z_add_qZpromote_joins)r   Úqueryr-   ZreuseZ	summarizeZfor_saveZclauseZjoinsr   r   r   Úresolve_expressionN   s    $zQ.resolve_expression)r	   r
   r   r   r   r   Údefaultr   r(   r)   r*   r,   r/   r   r   )r    r   r   ,   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   r2   Úmodelr   r   r   r   [   s    zDeferredAttribute.__init__Nc                sõ   | d k r ˆ  S| j  } | j } | j ˆ  j ˆ  ƒ ˆ  k rê y | j ˆ  j ƒ } Wn2 t k
 r‡ ‡  f d d †  | j Dƒ d } Yn X| 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.
        Nc                s(   g  |  ] } | j  ˆ  j k r | ‘ q Sr   )Úattnamer2   )Ú.0Úf)r   r   r   ú
<listcomp>m   s   	 z-DeferredAttribute.__get__.<locals>.<listcomp>r   Úfields)Ú_metaÚ__dict__Úgetr2   Ú	get_fieldr   r8   ÚnameÚ_check_parent_chainZrefresh_from_dbÚgetattr)r   ÚinstanceÚclsÚoptsr   r6   r=   Úvalr   )r   r   Ú__get__^   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)r9   r<   Zget_ancestor_linkr3   Úprimary_keyr?   r4   )r   r@   r=   rB   r6   Z
link_fieldr   r   r   r>   x   s    	z%DeferredAttribute._check_parent_chain)r	   r
   r   r   r   rD   r>   r   r   r   r   r1   V   s   r1   c               @   s^   e  Z d  Z d d „  Z d d „  Z d d „  Z e d d d	 „ ƒ Z e d d
 d „ ƒ Z d S)ÚRegisterLookupMixinc             C   s…   y |  j  | SWno t k
 ro xI t j |  j ƒ D]5 } d | j k rM q2 n  | | j  k r2 | j  | Sq2 WYn t k
 r€ Yn Xd  S)NÚclass_lookups)rG   ÚKeyErrorÚinspectZgetmror    r:   ÚAttributeError)r   Úlookup_nameÚparentr   r   r   Ú_get_lookup‡   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   )ÚLookupÚoutput_field)Údjango.db.models.lookupsrN   rM   ÚhasattrrO   Ú
get_lookupÚ
issubclass)r   rK   rN   Úfoundr   r   r   rR   –   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   )Ú	TransformrO   )rP   rU   rM   rQ   rO   Úget_transformrS   )r   rK   rU   rT   r   r   r   rV   Ÿ   s    z!RegisterLookupMixin.get_transformNc             C   sD   | d  k r | j  } n  d |  j k r3 i  |  _ n  | |  j | <| S)NrG   )rK   r:   rG   )rA   ÚlookuprK   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)rK   rG   )rA   rW   rK   r   r   r   Ú_unregister_lookup±   s    z&RegisterLookupMixin._unregister_lookup)	r	   r
   r   rM   rR   rV   ÚclassmethodrX   rY   r   r   r   r   rF   †   s   		rF   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_namer=   Únullr4   r   r3   r9   Zobject_name)ÚfieldZ
restrictedÚ	requestedZload_fieldsÚreverser   r   r   Úselect_related_descend¼   s"    	%r_   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
    re   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)r9   Zconcrete_modelZget_parent_list)rB   )r3   r   r   Úcheckõ   s    z-check_rel_lookup_compatibility.<locals>.checkrE   F)r?   r3   r9   )r3   Ztarget_optsr\   rf   r   )r3   r   Úcheck_rel_lookup_compatibilityî   s    rg   )r   Ú
__future__r   rI   Úcollectionsr   Zdjango.core.exceptionsr   Zdjango.db.models.constantsr   Zdjango.utilsr   r   Ú	Exceptionr   Úobjectr   ÚNoder   r1   rF   r_   re   rg   r   r   r   r   Ú<module>   s   *06$