o
    Ti;                     @   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	m
Z
mZ ddlmZ ddlmZ ed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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)DEFAULT_DB_ALIASDatabaseErrorconnections)
LOOKUP_SEP)treedjango.db.models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   \/var/www/html/evchargy.com/venv/lib/python3.10/site-packages/django/db/models/query_utils.pyr      s
   r   c                       s   e Zd 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 Z	dddZdd ZefddZdd Z  ZS )Qze
    Encapsulate filters as objects that can then be combined logically (using
    `&` and `|`).
    ANDORXORTNF)
_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   0   s
   
z
Q.__init__c                 C   sb   t |dddu rt|| s| S |st|tr|  S | j|d}|| | ||| |S )NconditionalF)r   )getattr	TypeErrorcopy
isinstancer   createadd)r    otherconnobjr   r   r   _combine7   s   z
Q._combinec                 C      |  || jS r   )r/   r   r    r,   r   r   r   __or__D      zQ.__or__c                 C   r0   r   )r/   r   r1   r   r   r   __and__G   r3   z	Q.__and__c                 C   r0   r   )r/   r   r1   r   r   r   __xor__J   r3   z	Q.__xor__c                 C   s   |   }|  |S r   )r(   negate)r    r.   r   r   r   
__invert__M   s   zQ.__invert__c                 C   s(   |j | ||dd|d\}}|| |S )NF)allow_joinsZ
split_subqZcheck_filterable	summarize)Z_add_qZpromote_joins)r    queryr8   reuser9   Zfor_saveZclauseZjoinsr   r   r   resolve_expressionR   s   

zQ.resolve_expressionc                 c   sJ    | V  | j D ]}t|tr|d }t|dr| E dH  q|V  qdS )zg
        Recursively yield this Q object and all subexpressions, in depth-first
        order.
           flattenN)r   r)   tuplehasattrr>   )r    childr   r   r   r>   b   s   


z	Q.flattenc              
   C   s  ddl m}m} ddlm} ddlm} ddlm} |d}|	 D ]\}	}
t
|
ds/||
}
|j|
|	dd	 q"||d
d t| jjrT|t|| d| d n||  |j|d}z||duW S  ty } ztd| | W Y d}~dS d}~ww )z|
        Do a database query to check if the expressions of the Q instance
        matches against the expressions.
        r   )BooleanFieldValue)Coalesce)Query)SINGLENr<   F)selectr=   _checkT)output_field)usingz.Got a database error calling check() on %r: %s)Zdjango.db.modelsrB   rC   Zdjango.db.models.functionsrD   Zdjango.db.models.sqlrE   Zdjango.db.models.sql.constantsrF   r   r@   Zadd_annotationr   featuresZsupports_comparing_boolean_exprZadd_qr   Zget_compilerZexecute_sqlr   loggerwarning)r    ZagainstrJ   rB   rC   rD   rE   rF   r:   namevaluecompilerer   r   r   checkq   s*   

zQ.checkc                 C   sf   d| j j| j jf }|dr|dd}t| j}i }| j| jkr'| j|d< | j	r.d|d< |||fS )Nz%s.%szdjango.db.models.query_utilsr	   r   Tr   )
r$   
__module____name__
startswithreplacer?   r   r   defaultr   )r    pathr!   r"   r   r   r   deconstruct   s   



zQ.deconstruct)NTNFF)rT   rS   __qualname____doc__r   r   r   rW   r%   r   r/   r2   r4   r5   r7   r<   r>   r   rR   rY   __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    r^   r   r   r   r      s   
zDeferredAttribute.__init__Nc                 C   sZ   |du r| S |j }| jj}||vr)| |}|du r%|j|gd || S |||< || S )zx
        Retrieve and caches the value from the datastore on the first lookup.
        Return the cached value.
        N)fields)__dict__r^   attname_check_parent_chainZrefresh_from_db)r    instancer   data
field_namevalr   r   r   __get__   s   
zDeferredAttribute.__get__c                 C   s6   |j }|| jj}| jjr| j|krt||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_linkr^   modelprimary_keyr&   ra   )r    rc   optsZ
link_fieldr   r   r   rb      s
   z%DeferredAttribute._check_parent_chainr   )rT   rS   rZ   r[   r   rg   rb   r   r   r   r   r]      s
    
r]   c                   @   s    e Zd ZdZdd Zdd ZdS )class_or_instance_methodz
    Hook used in RegisterLookupMixin to return partial functions depending on
    the caller type (instance or class of models.Field).
    c                 C   s   || _ || _d S r   )class_methodinstance_method)r    rm   rn   r   r   r   r      s   
z!class_or_instance_method.__init__c                 C   s$   |d u rt | j|S t | j|S r   )	functoolspartialrm   rn   )r    rc   ownerr   r   r   rg      s   z class_or_instance_method.__get__N)rT   rS   rZ   r[   r   rg   r   r   r   r   rl      s    rl   c                   @   s   e Zd Zdd Zejdddd Zdd ZeeeZ	e
eZd	d
 Zdd Zedd Ze
dd ZdddZdddZeeeZe
eZdddZdddZeeeZe
e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)r`   rt   ).0parentr   r   r   
<listcomp>   s    z9RegisterLookupMixin.get_class_lookups.<locals>.<listcomp>)inspectgetmromerge_dicts)r   rx   r   r   r   get_class_lookups   s   
z%RegisterLookupMixin.get_class_lookupsc                 C   s(   |   }t| dd  }ri ||S |S Ninstance_lookups)r   r&   )r    rx   r   r   r   r   get_instance_lookups   s   z(RegisterLookupMixin.get_instance_lookupsc                 C   N   ddl m} | |}|d u rt| dr| j|S |d ur%t||s%d S |S )Nr   )LookuprI   )django.db.models.lookupsr   rv   r@   rI   
get_lookup
issubclass)r    ru   r   foundr   r   r   r         
zRegisterLookupMixin.get_lookupc                 C   r   )Nr   )	TransformrI   )r   r   rv   r@   rI   get_transformr   )r    ru   r   r   r   r   r   r      r   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dictsZmergeddr   r   r   r~      s   zRegisterLookupMixin.merge_dictsc                 C   s   t | D ]}|j  qd S r   )r   r   cache_clearr   r   r   r   _clear_cached_class_lookups  s   z/RegisterLookupMixin._clear_cached_class_lookupsc                 C   s4   |d u r|j }d| jvri | _|| j|< |   |S )Nrx   )ru   r`   rx   r   r   lookupru   r   r   r   register_class_lookup  s   

z)RegisterLookupMixin.register_class_lookupc                 C   s,   |d u r|j }d| jvri | _|| j|< |S r   )ru   r`   r   r    r   ru   r   r   r   register_instance_lookup  s   

z,RegisterLookupMixin.register_instance_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)ru   rx   r   r   r   r   r   _unregister_class_lookup"  s   z,RegisterLookupMixin._unregister_class_lookupc                 C   s   |du r|j }| j|= dS )zs
        Remove given lookup from instance lookups. For use in tests only as
        it's not thread-safe.
        N)ru   r   r   r   r   r   _unregister_instance_lookup,  s   z/RegisterLookupMixin._unregister_instance_lookupr   )rT   rS   rZ   rv   ro   	lru_cacher   r   rl   rs   classmethodr   r   staticmethodr~   r   r   r   Zregister_lookupr   r   Z_unregister_lookupr   r   r   r   rr      s2    









	


	rr   Fc                 C   s   | j sdS | j jr|sdS |r"|r|  |vrdS |s"| j|vr"dS |s)| jr)dS |rE|rE| j|v rE| |vrEtd| jjj d| j ddS )af  
    Return True if this field should be used to descend deeper for
    select_related() purposes. Used by both the query construction code
    (compiler.get_related_selections()) and the model instance creation code
    (compiler.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.
     * select_mask - the dictionary of selected fields.
     * reverse - boolean, True if we are checking a reverse select related
    FzField .zM cannot be both deferred and traversed using select_related at the same time.T)	Zremote_fieldZparent_linkZrelated_query_namerN   nullr   ri   rh   Zobject_name)r^   Z
restricted	requestedZselect_maskreverser   r   r   select_related_descend;  s*   

r   c                 C   sL   t dt| d D ]}t| d| }||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=   r   N)Nr   )rangelenr   joinrt   )Zlookup_partsannotationsnZlevel_n_lookupr   r   r   refs_expressionb  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   )rh   Zconcrete_modelZget_parent_list)rk   ri   r   r   rR   w  s
   
z-check_rel_lookup_compatibility.<locals>.checkrj   F)r&   ri   rh   )ri   Ztarget_optsr^   rR   r   r   r   check_rel_lookup_compatibilityo  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   rX   )r    r   r   r   r   r   r     s   

zFilteredRelation.__init__c                 C   s4   t || jstS | j|jko| j|jko| j|jkS r   )r)   r$   NotImplementedr   r   r   r1   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   rX   )r    cloner   r   r   r     s   zFilteredRelation.clonec                 O   s   t d)zz
        QuerySet.annotate() only accepts expression-like arguments
        (with a resolve_expression() method).
        z0FilteredRelation.resolve_expression() is unused.)NotImplementedError)r    r!   r"   r   r   r   r<     s   z#FilteredRelation.resolve_expressionc                 C   s&   |j }|j| jt| jd}||S )N)r;   )r:   Zbuild_filtered_relation_qr   setrX   compile)r    rP   
connectionr:   wherer   r   r   as_sql  s   
zFilteredRelation.as_sqlN)
rT   rS   rZ   r[   r   r   r   r   r<   r   r   r   r   r   r     s    
	r   )F)r[   ro   r|   loggingcollectionsr   Zdjango.core.exceptionsr   Z	django.dbr   r   r   Zdjango.db.models.constantsr   Zdjango.utilsr   	getLoggerrL   r
   r   Noder   r]   rl   rr   r   r   r   r   r   r   r   r   <module>   s.    
x)
g'