3
(h-&                 @   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G d
d deZG dd deZG dd dejZG dd deZG d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   @/tmp/pip-install-q3hcpn_q/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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)NN)r	   r
   r   r   Zcontains_aggregater   r   r   r   r   r   r      s   r   c                   sV   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Z  ZS )Qzf
    Encapsulates filters as objects that can then be combined logically (using
    `&` and `|`).
    ANDORc                s&   t t| jt|t|j  d d S )N)children)superr   r   r   items)r   argskwargs)	__class__r   r   r   6   s    z
Q.__init__c             C   s>   t |tst|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   s$   t |  }|j| | j |j  |S )N)r#   r$   r   negate)r   r'   r   r   r   
__invert__H   s    
zQ.__invert__NTFc             C   s$   |j | ||dd\}}|j| |S )NF)allow_joinsZ
split_subq)Z_add_qZpromote_joins)r   queryr-   ZreuseZ	summarizeZfor_saveZclauseZjoinsr   r   r   resolve_expressionN   s    
zQ.resolve_expression)NTNFF)r	   r
   r   r   r   r   defaultr   r(   r)   r*   r,   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 )N)
field_name)r   r3   modelr   r   r   r   [   s    zDeferredAttribute.__init__Nc                s   |dkr S |j }|j}|j j  kry|j j}W n, tk
rf    fdd|jD d }Y nX |j} j||}|dkr|j	 jgd t
| j}|| j< | 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S r   )attnamer3   ).0f)r   r   r   
<listcomp>m   s    z-DeferredAttribute.__get__.<locals>.<listcomp>r   )fields)_meta__dict__getr3   	get_fieldr   r9   name_check_parent_chainZrefresh_from_dbgetattr)r   instanceclsoptsr   r7   r>   valr   )r   r   __get__^   s     
zDeferredAttribute.__get__c             C   s:   |j }|j|}|j|j}|jr6||kr6t||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)r:   r=   Zget_ancestor_linkr4   primary_keyr@   r5   )r   rA   r>   rC   r7   Z
link_fieldr   r   r   r?   x   s    
z%DeferredAttribute._check_parent_chain)N)r	   r
   r   r   r   rE   r?   r   r   r   r   r2   V   s   
r2   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   sl   y
| j | S  tk
rT   x4tj| jD ]$}d|jkr8q(||j kr(|j | S q(W Y n tk
rf   Y nX d S )Nclass_lookups)rH   KeyErrorinspectgetmror    r;   AttributeError)r   lookup_nameparentr   r   r   _get_lookup   s    


zRegisterLookupMixin._get_lookupc             C   sP   ddl m} | j|}|d kr4t| dr4| jj|S |d k	rLt|| rLd S |S )Nr   )Lookupoutput_field)django.db.models.lookupsrP   rO   hasattrrQ   
get_lookup
issubclass)r   rM   rP   foundr   r   r   rT      s    
zRegisterLookupMixin.get_lookupc             C   sP   ddl m} | j|}|d kr4t| dr4| jj|S |d k	rLt|| rLd S |S )Nr   )	TransformrQ   )rR   rW   rO   rS   rQ   get_transformrU   )r   rM   rW   rV   r   r   r   rX      s    
z!RegisterLookupMixin.get_transformNc             C   s,   |d kr|j }d| jkri | _|| j|< |S )NrH   )rM   r;   rH   )rB   lookuprM   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)rM   rH   )rB   rY   rM   r   r   r   _unregister_lookup   s    z&RegisterLookupMixin._unregister_lookup)N)N)	r	   r
   r   rO   rT   rX   classmethodrZ   r[   r   r   r   r   rG      s   		rG   Fc             C   s   | j s
dS | j jr| rdS |rH|r4| j |kr4dS | rH| j|krHdS | rX| jrXdS |r| j|kr|r| j|krtd| jjj	| jf 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>   nullr5   r   r4   r:   Zobject_name)fieldZ
restricted	requestedZload_fieldsreverser   r   r   select_related_descend   s"    
ra   c             C   sX   xNt t| d D ]:}tj| d| }||kr|| r|| | |d fS qW d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_partsannotationsnZlevel_n_lookupr   r   r   refs_expression   s
    rh   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)r:   Zconcrete_modelZget_parent_list)rC   )r4   r   r   check   s    z-check_rel_lookup_compatibility.<locals>.checkrF   F)r@   r4   r:   )r4   Ztarget_optsr^   ri   r   )r4   r   check_rel_lookup_compatibility   s    rj   )F)r   
__future__r   rJ   collectionsr   Zdjango.core.exceptionsr   Zdjango.db.models.constantsr   Zdjango.utilsr   r   	Exceptionr   objectr   Noder   r2   rG   ra   rh   rj   r   r   r   r   <module>   s   
*06
$