î

_ÝV$5  ã               @   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 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 e _ 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)Úutils)Ú
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-build-ghmbqnp_/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   Ú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 „  Z d d d d d d d „ Z e d d „  ƒ Z d d „  Z ‡  S)ÚQzb
    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   7   s    z
Q.__init__c             C   sZ   t  | t ƒ s t | ƒ ‚ n  t |  ƒ ƒ  } | | _ | j |  | ƒ | j | | ƒ | S)N)Ú
isinstancer   Ú	TypeErrorÚtypeÚ	connectorÚadd)r   ÚotherÚconnÚobjr   r   r   Ú_combine:   s    	z
Q._combinec             C   s   |  j  | |  j ƒ S)N)r+   r   )r   r(   r   r   r   Ú__or__C   s    zQ.__or__c             C   s   |  j  | |  j ƒ S)N)r+   r   )r   r(   r   r   r   Ú__and__F   s    z	Q.__and__c             C   s0   t  |  ƒ ƒ  } | j |  |  j ƒ | j ƒ  | S)N)r%   r'   r   Únegate)r   r*   r   r   r   Ú
__invert__I   s    
zQ.__invert__c             C   sw   |  j  j d g  d |  j d |  j ƒ } xI |  j D]> } t | d ƒ r_ | j j | j ƒ  ƒ q1 | j j | ƒ q1 W| S)Nr   r&   ÚnegatedÚclone)r"   Z_new_instancer&   r0   r   ÚhasattrÚappendr1   )r   r1   Úchildr   r   r   r1   O   s    zQ.cloneNTFc             C   s5   | j  |  | d | d d ƒ\ } } | j | ƒ | S)NÚallow_joinsZ
split_subqF)Z_add_qZpromote_joins)r   Úqueryr5   ZreuseZ	summarizeZfor_saveZclauseZjoinsr   r   r   Úresolve_expressionY   s    $zQ.resolve_expressionc             C   s¯   t  | t j ƒ si t | d j t ƒ | ƒ \ } } | r_ t | d d ƒ r_ | d j | ƒ S| | f Sx9 | j D]. } |  j | | ƒ \ } } | rs | | f Sqs Wd f  f S)Nr   é   Úrefs_aggregateF)	r#   r   ÚNoder9   Úsplitr   r2   r   Ú_refs_aggregate)Úclsr*   Úexisting_aggregatesZ	aggregateZaggregate_lookupsÚcr   r   r   r<   `   s    "
zQ._refs_aggregatec             C   s   | s
 d S|  j  |  | ƒ S)NF)r<   )r   r>   r   r   r   r9   m   s    zQ.refs_aggregate)r
   r   r   r   r   r   Údefaultr   r+   r,   r-   r/   r1   r7   Úclassmethodr<   r9   r   r   )r"   r   r   -   s   	
r   c               @   sF   e  Z d  Z d Z d d „  Z d d „  Z 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   rC   Úmodelr   r   r   r   y   s    zDeferredAttribute.__init__c       	         s  | j  j } | j  } | d k	 s' t ‚ | 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   )ÚattnamerC   )Ú.0Úf)r   r   r   ú
<listcomp>Œ   s   	 z-DeferredAttribute.__get__.<locals>.<listcomp>r   Úfields)Ú_metaÚproxy_for_modelÚAssertionErrorÚ__dict__ÚgetrC   Ú	get_fieldr   rI   ÚnameÚ_check_parent_chainZrefresh_from_dbÚgetattr)	r   ÚinstanceÚownerZnon_deferred_modelÚoptsr   rG   rP   Úvalr   )r   r   Ú__get__|   s     		%	zDeferredAttribute.__get__c             C   s   | | j  |  j <d S)z†
        Deferred loading attributes can be set normally (which means there will
        never be a database lookup involved.
        N)rM   rC   )r   rS   Úvaluer   r   r   Ú__set__—   s    zDeferredAttribute.__set__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)rJ   rO   Zget_ancestor_linkrD   Úprimary_keyrR   rE   )r   rS   rP   rU   rG   Z
link_fieldr   r   r   rQ   ž   s    	z%DeferredAttribute._check_parent_chainN)r
   r   r   r   r   rW   rY   rQ   r   r   r   r   rB   t   s
   rB   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)r\   ÚKeyErrorÚinspectZgetmror"   rM   Ú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.lookupsrc   rb   r2   rd   Ú
get_lookupÚ
issubclass)r   r`   rc   Úfoundr   r   r   rf   ¼   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   )Ú	Transformrd   )re   ri   rb   r2   rd   Úget_transformrg   )r   r`   ri   rh   r   r   r   rj   Å   s    z!RegisterLookupMixin.get_transformNc             C   sD   | d  k r | j  } n  d |  j k r3 i  |  _ n  | |  j | <| S)Nr\   )r`   rM   r\   )r=   Úlookupr`   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)r`   r\   )r=   rk   r`   r   r   r   Ú_unregister_lookup×   s    z&RegisterLookupMixin._unregister_lookup)	r
   r   r   rb   rf   rj   rA   rl   rm   r   r   r   r   r[   ¬   s   		r[   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 ƒ ‚ n  d Sn  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_namerP   ÚnullrE   r	   rD   rJ   Zobject_name)ÚfieldZ
restrictedÚ	requestedZload_fieldsÚreverser   r   r   Úselect_related_descendâ   s$    	rr   c                s  | s
 ˆ  Sˆ  j  ‰ ˆ  j r( ˆ j ‰  n  d ˆ  j d j t | ƒ ƒ f } t j | d d ƒ } y ˆ j j	 ˆ  j  j
 | ƒ SWn~ t k
 rü G‡ f d d †  d ƒ } ‡  f d d †  | Dƒ } | | d <ˆ  j | d	 <d
 | d <t t | ƒ ˆ  f | ƒ SYn Xd S)zß
    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.
    z%s_Deferred_%sÚ_éP   é    c                   s(   e  Z d  Z d Z ”  j Z ”  j Z d S)z$deferred_class_factory.<locals>.MetaTN)r
   r   r   ÚproxyÚappsÚ	app_labelr   )rU   r   r   ÚMeta%  s   	ry   c                s"   i  |  ] } t  | ˆ  ƒ | “ q Sr   )rB   )rF   Úattr)rD   r   r   ú
<dictcomp>*  s   	 z*deferred_class_factory.<locals>.<dictcomp>r   TÚ	_deferredN)rJ   r|   rK   r
   ÚjoinÚsortedr   Ztruncate_namerw   Z	get_modelrx   ÚLookupErrorr   r%   Ústr)rD   ÚattrsrP   ry   Z	overridesr   )rD   rU   r   Údeferred_class_factory
  s     		"

r‚   Tc             C   su   xh t  t |  ƒ d ƒ D]P } t j |  d | … ƒ } | | k r | | j r | | |  | d … f Sq Wd f  f S)zò
    A helper method to check if the lookup_parts contains references
    to the given aggregates set. Because the LOOKUP_SEP is contained in the
    default annotation names we must check each prefix of the lookup_parts
    for a match.
    r8   r   NF)ÚrangeÚlenr   r}   r   )Úlookup_partsZ
aggregatesÚnÚlevel_n_lookupr   r   r   r9   6  s
    r9   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.
    r8   r   NF)rƒ   r„   r   r}   )r…   Zannotationsr†   r‡   r   r   r   Úrefs_expressionD  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  j ƒ  k p< ˆ  |  j ƒ  k S)N)rJ   Zconcrete_modelZget_parent_list)rU   )rD   r   r   ÚcheckY  s    z-check_rel_lookup_compatibility.<locals>.checkrZ   F)rR   rD   rJ   )rD   Ztarget_optsro   r‰   r   )rD   r   Úcheck_rel_lookup_compatibilityR  s    rŠ   )r   Ú
__future__r   r^   Úcollectionsr   Zdjango.core.exceptionsr   Zdjango.db.backendsr   Zdjango.db.models.constantsr   Zdjango.utilsr   r   Ú	Exceptionr	   Úobjectr   r:   r   rB   r[   rr   r‚   Z__safe_for_unpickling__r9   rˆ   rŠ   r   r   r   r   Ú<module>   s&   G86()	