a
    ÛEbª-  ã                   @   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   ú\/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/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g |¢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u s"t|ƒ‚| sBt|dƒr8| ¡ S t |¡S t |tƒrp|sp|  ¡ \}}}t| ƒ|i |¤ŽS t| ƒƒ }||_| 	| |¡ | 	||¡ |S )NÚconditionalFTÚcopy)
Ú
isinstancer   ÚgetattrÚ	TypeErrorÚhasattrr!   ÚdeconstructÚtyper   Úadd)r   ÚotherÚconnÚ_r   r   Úobjr   r   r   Ú_combine3   s    
z
Q._combinec                 C   s   |   || j¡S r   )r-   r   ©r   r)   r   r   r   Ú__or__C   s    zQ.__or__c                 C   s   |   || j¡S r   )r-   r   r.   r   r   r   Ú__and__F   s    z	Q.__and__c                 C   s$   t | ƒƒ }| | | j¡ | ¡  |S r   )r'   r(   r   Únegate)r   r,   r   r   r   Ú
__invert__I   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_expressionO   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&   ^   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   q   s    zDeferredAttribute.__init__Nc                 C   sT   |du r| S |j }| jj}||vrL|  |¡}|du 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__t   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   ‡   s
    z%DeferredAttribute._check_parent_chain)N)r8   r7   r>   r?   r   rK   rF   r   r   r   r   rA   k   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 u r4t| dƒr4| j |¡S |d u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 u r4t| dƒr4| j |¡S |d u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 u r|j }d| jvri | _|| j|< |  ¡  |S )NrV   )rS   rD   rV   rj   ©r   ÚlookuprS   r   r   r   Úregister_lookupÅ   s    

z#RegisterLookupMixin.register_lookupc                 C   s   |du 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|  ¡ |vr2dS |sD| j|vrDdS |sR| jrRdS |rŠ| j|vrŠ|rŠ| j|v rŠd| 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|… ¡}||v 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  ¡ v p(ˆ |  ¡ v 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__6  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   E  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_sqlL  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&   þK)F
%