U
     5i,-                     @   s   d Z ddlZddl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d	d
 ZG dd dejZG dd dZG dd dZdddZdd Zdd ZG d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.
    N)
namedtuple)
FieldError)
LOOKUP_SEP)treePathInfozGfrom_opts to_opts target_fields join_field m2m direct filtered_relationc                 c   s&   | V  |   D ]}t|E d H  qd S N)__subclasses__
subclassesclssubclass r   @/tmp/pip-unpacked-wheel-gcyiijx6/django/db/models/query_utils.pyr	      s    r	   c                       sj   e Zd ZdZdZdZeZdZddd fdd	
Zd
d Z	dd Z
dd Zdd ZdddZdd Z  ZS )Qze
    Encapsulate filters as objects that can then be combined logically (using
    `&` and `|`).
    ANDORTNF)
_connector_negatedc                   s"   t  j|t| ||d d S )N)children	connectornegated)super__init__sorteditems)selfr   r   argskwargs	__class__r   r   r   (   s    z
Q.__init__c                 C   s   t |ts"t|dddks"t|| sBt|dr8| S t|S t |trl|sl|  \}}}t| ||S t|  }||_|	| | |	|| |S )NconditionalFTcopy)

isinstancer   getattr	TypeErrorhasattrr!   deconstructtyper   add)r   otherconn_r   r   objr   r   r   _combine+   s    
z
Q._combinec                 C   s   |  || jS r   )r-   r   r   r)   r   r   r   __or__;   s    zQ.__or__c                 C   s   |  || jS r   )r-   r   r.   r   r   r   __and__>   s    z	Q.__and__c                 C   s$   t |  }|| | j |  |S r   )r'   r(   r   negate)r   r,   r   r   r   
__invert__A   s    
zQ.__invert__c                 C   s&   |j | ||ddd\}}|| |S )NF)allow_joinsZ
split_subqZcheck_filterable)Z_add_qZpromote_joins)r   queryr3   reuseZ	summarizeZfor_saveZclauseZjoinsr   r   r   resolve_expressionG   s       

zQ.resolve_expressionc                 C   sf   d| j j| j jf }|dr*|dd}t| j}i }| j| jkrN| j|d< | j	r\d|d< |||fS )Nz%s.%szdjango.db.models.query_utilszdjango.db.modelsr   Tr   )
r   
__module____name__
startswithreplacetupler   r   defaultr   )r   pathr   r   r   r   r   r&   Q   s    


zQ.deconstruct)NTNFF)r8   r7   __qualname____doc__r   r   r<   r    r   r-   r/   r0   r2   r6   r&   __classcell__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 r   )field)r   rB   r   r   r   r   c   s    zDeferredAttribute.__init__Nc                 C   sT   |dkr| S |j }| jj}||krL| |}|dkrD|j|gd n|||< || S )zx
        Retrieve and caches the value from the datastore on the first lookup.
        Return the cached value.
        N)fields)__dict__rB   attname_check_parent_chainZrefresh_from_db)r   instancer   data
field_namevalr   r   r   __get__f   s    
zDeferredAttribute.__get__c                 C   s6   |j }|| jj}| jjr2| j|kr2t||jS d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)_metaZget_ancestor_linkrB   modelprimary_keyr#   rE   )r   rG   optsZ
link_fieldr   r   r   rF   y   s
    z%DeferredAttribute._check_parent_chain)N)r8   r7   r>   r?   r   rK   rF   r   r   r   r   rA   ^   s   
rA   c                   @   st   e Zd Zedd Zeej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   |   |d S r   )get_lookupsget)r   lookup_namer   r   r   _get_lookup   s    zRegisterLookupMixin._get_lookupN)maxsizec                 C   s   dd t | D }| |S )Nc                 S   s   g | ]}|j d i qS )class_lookups)rD   rR   ).0parentr   r   r   
<listcomp>   s     z3RegisterLookupMixin.get_lookups.<locals>.<listcomp>)inspectgetmromerge_dicts)r   rV   r   r   r   rQ      s    zRegisterLookupMixin.get_lookupsc                 C   sN   ddl m} | |}|d kr4t| dr4| j|S |d k	rJt||sJd S |S )Nr   )Lookupoutput_field)django.db.models.lookupsr]   rT   r%   r^   
get_lookup
issubclass)r   rS   r]   foundr   r   r   r`      s    
zRegisterLookupMixin.get_lookupc                 C   sN   ddl m} | |}|d kr4t| dr4| j|S |d k	rJt||sJd S |S )Nr   )	Transformr^   )r_   rc   rT   r%   r^   get_transformra   )r   rS   rc   rb   r   r   r   rd      s    
z!RegisterLookupMixin.get_transformc                 C   s    i }t | D ]}|| q|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dictsmergeddr   r   r   r\      s    zRegisterLookupMixin.merge_dictsc                 C   s   t | D ]}|j  qd S r   )r	   rQ   cache_clearr
   r   r   r   _clear_cached_lookups   s    z)RegisterLookupMixin._clear_cached_lookupsc                 C   s4   |d kr|j }d| jkri | _|| j|< |   |S )NrV   )rS   rD   rV   rj   r   lookuprS   r   r   r   register_lookup   s    

z#RegisterLookupMixin.register_lookupc                 C   s   |dkr|j }| j|= dS )zn
        Remove given lookup from cls lookups. For use in tests only as it's
        not thread-safe.
        N)rS   rV   rk   r   r   r   _unregister_lookup   s    z&RegisterLookupMixin._unregister_lookup)N)N)r8   r7   r>   classmethodrT   	functools	lru_cacherQ   r`   rd   staticmethodr\   rj   rm   rn   r   r   r   r   rP      s   

		


	rP   Fc                 C   s   | j s
dS | j jr|sdS |rD|r2|  |kr2dS |sD| j|krDdS |sR| jrRdS |r| j|kr|r| j|krd| jjj| jf }t	|dS )au  
    Return 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_namenamenullrE   rM   rL   Zobject_namer   )rB   Z
restricted	requestedZload_fieldsreversemsgr   r   r   select_related_descend   s&    

rx   c                 C   sV   t dt| d D ]>}t| d| }||kr|| r|| | |d f  S qdS )z
    Check if the lookup_parts contains references to the given annotations set.
    Because the LOOKUP_SEP is contained in the default annotation names, check
    each prefix of the lookup_parts for a match.
       r   N)Fr   )rangelenr   join)Zlookup_partsannotationsnZlevel_n_lookupr   r   r   refs_expression   s
    r   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  kp( |  kS r   )rL   Zconcrete_modelZget_parent_list)rO   rM   r   r   check  s
    
z-check_rel_lookup_compatibility.<locals>.checkrN   F)r#   rM   rL   )rM   Ztarget_optsrB   r   r   r   r   check_rel_lookup_compatibility   s    r   c                   @   s@   e Zd ZdZe dddZdd Zdd Zd	d
 Zdd Z	dS )FilteredRelationz7Specify custom filtering in the ON clause of SQL joins.	conditionc                C   s:   |st d|| _d | _t|ts*t d|| _g | _d S )Nzrelation_name cannot be empty.z*condition argument must be a Q() instance.)
ValueErrorrelation_namealiasr"   r   r   r=   )r   r   r   r   r   r   r     s    
zFilteredRelation.__init__c                 C   s4   t || jstS | j|jko2| j|jko2| j|jkS r   )r"   r   NotImplementedr   r   r   r.   r   r   r   __eq__$  s    

zFilteredRelation.__eq__c                 C   s,   t | j| jd}| j|_| jd d  |_|S )Nr   )r   r   r   r   r=   )r   cloner   r   r   r   -  s    zFilteredRelation.clonec                 O   s   t ddS )zz
        QuerySet.annotate() only accepts expression-like arguments
        (with a resolve_expression() method).
        z0FilteredRelation.resolve_expression() is unused.N)NotImplementedError)r   r   r   r   r   r   r6   3  s    z#FilteredRelation.resolve_expressionc                 C   s&   |j }|j| jt| jd}||S )N)r5   )r4   Zbuild_filtered_relation_qr   setr=   compile)r   compiler
connectionr4   wherer   r   r   as_sql:  s    zFilteredRelation.as_sqlN)
r8   r7   r>   r?   r   r   r   r   r6   r   r   r   r   r   r     s   
	r   )F)r?   r!   rp   rZ   collectionsr   Zdjango.core.exceptionsr   Zdjango.db.models.constantsr   Zdjango.utilsr   r   r	   Noder   rA   rP   rx   r   r   r   r   r   r   r   <module>   s    
A(C
%