
     hH5                     4   d Z ddl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j        d          Z ed	d
          Zd Z G d dej                  Z G d d          Z G d d          ZddZd Zd Z G 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              #   j   K   | V  |                                  D ]}t          |          E d {V  d S N)__subclasses__
subclassesclssubclasss     X/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/django/db/models/query_utils.pyr   r      sW      
III&&(( ( (h''''''''''( (    c                   |     e Zd ZdZdZdZdZeZdZddd fd	
Z	d
 Z
d Zd Zd Zd Z	 ddZd ZefdZd Z xZS )Qze
    Encapsulate filters as objects that can then be combined logically (using
    `&` and `|`).
    ANDORXORTNF)
_connector_negatedc                    t                                          g |t          |                                          ||           d S )N)children	connectornegated)super__init__sorteditems)selfr   r   argskwargs	__class__s        r   r!   z
Q.__init__1   sQ    5t5fV\\^^445  	 	
 	
 	
 	
 	
r   c                    t          |t                    s"t          |dd          du st          |          | s8t	          |d          r|                                nt          j        |          S t          |t                    r/|s-|                                 \  }}} t          |           |i |S  t          |                       }||_        |	                    | |           |	                    ||           |S )NconditionalFTcopy)

isinstancer   getattr	TypeErrorhasattrr*   deconstructtyper   add)r$   otherconn_r%   r&   objs          r   _combinez
Q._combine8   s    5!$$ 	#}e(L(LPT(T(TE""" 	/#*5&#9#9O5::<<<ty?O?OOq!! 	/% 	/"..00OAtV4::t.v...d4jjlldt
r   c                 8    |                      || j                  S r   )r6   r   r$   r2   s     r   __or__zQ.__or__H   s    }}UDG,,,r   c                 8    |                      || j                  S r   )r6   r   r8   s     r   __and__z	Q.__and__K       }}UDH---r   c                 8    |                      || j                  S r   )r6   r   r8   s     r   __xor__z	Q.__xor__N   r<   r   c                      t          |                       }|                    | | j                   |                                 |S r   )r0   r1   r   negate)r$   r5   s     r   
__invert__zQ.__invert__Q   s;    d4jjlldh


r   c                 j    |                     | ||dd          \  }}|                    |           |S )NF)allow_joins
split_subqcheck_filterable)_add_qpromote_joins)r$   queryrC   reuse	summarizefor_saveclausejoinss           r   resolve_expressionzQ.resolve_expressionW   sJ    
 #" % 
 
 	E"""r   c              #      K   | V  | j         D ]N}t          |t                    r|d         }t          |d          r|                                E d{V  J|V  OdS )zg
        Recursively yield this Q object and all subexpressions, in depth-first
        order.
           flattenN)r   r+   tupler.   rQ   )r$   childs     r   rQ   z	Q.flattenf   s      
 


] 	 	E%'' !aui((  ==??********	 	r   c           
         ddl m}m} ddlm} ddlm} ddlm}  |d          }|	                                D ]8\  }	}
t          |
d          s ||
          }
|                    |
|	d	           9|                     |d
          d           t          |         j        j        r7|                    t!           || d |                                           n|                    |            |                    |          }	 |                    |          duS # t&          $ r'}t(                              d| |           Y d}~dS d}~ww xY w)z|
        Do a database query to check if the expressions of the Q instance
        matches against the expressions.
        r   )BooleanFieldValue)Coalesce)Query)SINGLENrN   F)selectrP   _checkT)output_field)usingz.Got a database error calling check() on %r: %s)django.db.modelsrU   rV   django.db.models.functionsrW   django.db.models.sqlrX   django.db.models.sql.constantsrY   r#   r.   add_annotationr   featuressupports_comparing_boolean_expradd_qr   get_compilerexecute_sqlr   loggerwarning)r$   againstr]   rU   rV   rW   rX   rY   rH   namevaluecompileres                r   checkzQ.checku   s    	98888888777777......999999d"==?? 	< 	<KD%5"677 %e  U ;;;;UU1XXx000u&F 	KK((4LLNNKKKLLMMMMKK%%E%22	''//t;; 	 	 	NNKTSTUUU44444	s   D# #
E-EEc                    | j         j        d| j         j        }|                    d          r|                    dd          }t          | j                  }i }| j        | j        k    r
| j        |d<   | j	        rd|d<   |||fS )N.zdjango.db.models.query_utilsr
   r   Tr   )
r'   
__module____name__
startswithreplacerR   r   r   defaultr   )r$   pathr%   r&   s       r   r/   zQ.deconstruct   s    .333T^5L5LM??9:: 	T<< >@RSSDT]##>T\))#'>F< < 	&!%F:T6!!r   )NTNFF)rs   rr   __qualname____doc__r   r   r   rv   r)   r!   r6   r9   r;   r>   rA   rN   rQ   r   ro   r/   __classcell__)r'   s   @r   r   r   $   s	         C	B
CGK)- 
 
 
 
 
 
 
   - - -. . .. . .   SX      $4    :
" 
" 
" 
" 
" 
" 
"r   r   c                   &    e Zd ZdZd ZddZ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                     || _         d S r   )field)r$   r~   s     r   r!   zDeferredAttribute.__init__   s    


r   Nc                     || S |j         }| j        j        }||vr4|                     |          }||                    |g           n|||<   ||         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_chainrefresh_from_db)r$   instancer   data
field_namevals         r   __get__zDeferredAttribute.__get__   sw    
 K Z'
T!! **844C{(((====#&Z Jr   c                     |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)_metaget_ancestor_linkr~   modelprimary_keyr,   r   )r$   r   opts
link_fields       r   r   z%DeferredAttribute._check_parent_chain   sT     ~++DJ,<==
:! 	9djJ&>&>8Z%7888tr   r   )rs   rr   rx   ry   r!   r   r    r   r   r|   r|      sP         
         &
 
 
 
 
r   r|   c                       e Zd Zed             Ze ej        d          d                         Zd Zd Z	e
d             Zed             Zedd	            Zedd
            ZdS )RegisterLookupMixinc                 R    |                                                      |d           S r   )get_lookupsget)r   lookup_names     r   _get_lookupzRegisterLookupMixin._get_lookup   s"      $$[$777r   N)maxsizec                 h    d t          j        |           D             }|                     |          S )Nc                 D    g | ]}|j                             d i           S )class_lookups)r   r   ).0parents     r   
<listcomp>z3RegisterLookupMixin.get_lookups.<locals>.<listcomp>   s7     
 
 
9?FO44
 
 
r   )inspectgetmromerge_dicts)r   r   s     r   r   zRegisterLookupMixin.get_lookups   s?    
 
CJ>RUCVCV
 
 
 }---r   c                     ddl m} |                     |          }|*t          | d          r| j                            |          S |t          ||          sd S |S )Nr   )Lookupr\   )django.db.models.lookupsr   r   r.   r\   
get_lookup
issubclass)r$   r   r   founds       r   r   zRegisterLookupMixin.get_lookup   sp    333333  --=WT>::=$//<<<Zv%>%>4r   c                     ddl m} |                     |          }|*t          | d          r| j                            |          S |t          ||          sd S |S )Nr   )	Transformr\   )r   r   r   r.   r\   get_transformr   )r$   r   r   r   s       r   r   z!RegisterLookupMixin.get_transform   sp    666666  --=WT>::=$22;???Zy%A%A4r   c                 X    i }t          |           D ]}|                    |           |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)dictsmergedds      r   r   zRegisterLookupMixin.merge_dicts   s8     % 	 	AMM!r   c                 \    t          |           D ]}|j                                         d S r   )r   r   cache_clearr   s     r   _clear_cached_lookupsz)RegisterLookupMixin._clear_cached_lookups   s9    "3 	/ 	/H ,,....	/ 	/r   c                 t    ||j         }d| j        vri | _        || j        |<   |                                  |S )Nr   )r   r   r   r   r   lookupr   s      r   register_lookupz#RegisterLookupMixin.register_lookup   sI     ,K#,.. "C)/+&!!###r   c                 P    ||j         }| 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   r   s      r   _unregister_lookupz&RegisterLookupMixin._unregister_lookup  s5      ,Kk*!!#####r   r   )rs   rr   rx   classmethodr   	functools	lru_cacher   r   r   staticmethodr   r   r   r   r   r   r   r   r      s        8 8 [8 Y&&&. . '& [.       \ / / [/    [ $ $ $ [$ $ $r   r   Fc                 $   | 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 |r@| j        |vr7|r5| j        |v r,d| j        j        j        d| j        d}t          |          dS )ap  
    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.
     * load_fields - the set of fields to be loaded on this model
     * reverse - boolean, True if we are checking a reverse select related
    FzField rq   zM cannot be both deferred and traversed using select_related at the same time.T)
remote_fieldparent_linkrelated_query_namerk   nullr   r   r   object_namer   )r~   
restricted	requestedload_fieldsreversemsgs         r   select_related_descendr     s      u% g u  	u//11BB5 	5:Y665 %* u &=++ &ejI555 [&222EJJJ@ !oo%4r   c                     t          dt          |           dz             D ]>}t          j        | d|                   }||v r||         r||         | |d         fc S ?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.
    rP   r   N)Fr   )rangelenr   join)lookup_partsannotationsnlevel_n_lookups       r   refs_expressionr   4  s~     1c,''!+,, A A#ac):;;[(([-H(~.QRR0@@@@@9r   c                 p      f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                     j         j        | j        k    p5| j        j                                         v p|                                 v S r   )r   concrete_modelget_parent_list)r   r   s    r   ro   z-check_rel_lookup_compatibility.<locals>.checkI  sO    K&$*== /"ek&A&A&C&CC/,,...	
r   r   F)r,   r   r   )r   target_optsr~   ro   s   `   r   check_rel_lookup_compatibilityr   A  sX    
 
 
 
 
  5 }e,,Iu{7H1I1Ir   c                   F    e Zd ZdZ e            ddZd Zd Zd Zd Z	dS )	FilteredRelationz7Specify custom filtering in the ON clause of SQL joins.	conditionc                    |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   rw   )r$   r   r   s      r   r!   zFilteredRelation.__init__a  s\     	?=>>>*
)Q'' 	KIJJJ"			r   c                     t          || j                  st          S | j        |j        k    o| j        |j        k    o| j        |j        k    S r   )r+   r'   NotImplementedr   r   r   r8   s     r   __eq__zFilteredRelation.__eq__k  sQ    %00 	"!!%"55 2
ek)2%/1	
r   c                 |    t          | j        | j                  }| j        |_        | j        d d          |_        |S )Nr   )r   r   r   r   rw   )r$   clones     r   r   zFilteredRelation.clonet  s8     !3t~NNNjYqqq\
r   c                      t          d          )zz
        QuerySet.annotate() only accepts expression-like arguments
        (with a resolve_expression() method).
        z0FilteredRelation.resolve_expression() is unused.)NotImplementedError)r$   r%   r&   s      r   rN   z#FilteredRelation.resolve_expressionz  s    
 ""TUUUr   c                     |j         }|                    | j        t          | j                            }|                    |          S )N)rI   )rH   build_filtered_relation_qr   setrw   compile)r$   rm   
connectionrH   wheres        r   as_sqlzFilteredRelation.as_sql  s?    //c$)nn/UU&&&r   N)
rs   rr   rx   ry   r   r!   r   r   rN   r   r   r   r   r   r   ^  s{        AA34133     
 
 
  V V V' ' ' ' 'r   r   )F)ry   r*   r   r   loggingcollectionsr   django.core.exceptionsr   	django.dbr   r   r   django.db.models.constantsr   django.utilsr	   	getLoggerrh   r   r   Noder   r|   r   r   r   r   r   r   r   r   <module>r      s           " " " " " " - - - - - - B B B B B B B B B B 1 1 1 1 1 1      		-	.	.
 :M ( ( (x" x" x" x" x"	 x" x" x"v& & & & & & & &RD$ D$ D$ D$ D$ D$ D$ D$N" " " "J
 
 
  :'' '' '' '' '' '' '' '' '' ''r   