
    grC                     D   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 ddlmZ dd	lmZ  ej(                  d
      Z edd      Zd Z G d dej0                        Z G d d      Z G d d      Z G d d      Zd Zd Zd Z G d d      Z y)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)cached_property)make_hashabledjango.db.modelsPathInfozGfrom_opts to_opts target_fields join_field m2m direct filtered_relationc              #   h   K   |  | j                         D ]  }t        |      E d {     y 7 wN)__subclasses__
subclassesclssubclasss     t/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/db/models/query_utils.pyr   r       s/     
I&&(h''' )'s   &202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ed        Zd Zd Ze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        |j                               ||       y )N)children	connectornegated)super__init__sorteditems)selfr   r   argskwargs	__class__s        r   r"   z
Q.__init__3   s3    5t5fV\\^45  	 	
    c                    t        |dd      du rt        |      | s|j                         S |s t        |t              r| j                         S | j                  |      }|j                  | |       |j                  ||       |S )NconditionalF)r   )getattr	TypeErrorcopy
isinstancer   createadd)r%   otherconnobjs       r   _combinez
Q._combine:   sv    5-/58E""::<E1-99;kkDk)dt
r)   c                 :    | j                  || j                        S r   )r5   r   r%   r2   s     r   __or__zQ.__or__G   s    }}UDGG,,r)   c                 :    | j                  || j                        S r   )r5   r   r7   s     r   __and__z	Q.__and__J       }}UDHH--r)   c                 :    | j                  || j                        S r   )r5   r   r7   s     r   __xor__z	Q.__xor__M   r;   r)   c                 F    | j                         }|j                          |S r   )r.   negate)r%   r4   s     r   
__invert__zQ.__invert__P   s    iik


r)   c                 \    |j                  | ||dd|      \  }}|j                  |       |S )NF)allow_joins
split_subqcheck_filterable	summarize)_add_qpromote_joins)r%   queryrB   reuserE   for_saveclausejoinss           r   resolve_expressionzQ.resolve_expressionU   sB    
 #" % 
 	E"r)   c              #      K   |  | j                   D ]@  }t        |t              r|d   }t        |d      r|j	                         E d{    =| B y7 w)zg
        Recursively yield this Q object and all subexpressions, in depth-first
        order.
           flattenN)r   r/   tuplehasattrrP   )r%   childs     r   rP   z	Q.flattene   sQ     
 
]]E%'aui( ==?** #
 +s   AA
AAc           
      J   ddl m}m} ddlm} ddlm} ddlm}  |d      }|j                         D ]-  \  }	}
t        |
d      s ||
      }
|j                  |
|	d	       / |j                   |d
      d       t        |   j                  j                  r)|j                  t!         || d |                          n|j                  |        |j#                  |      }	 |j%                  |      duS # t&        $ r!}t(        j+                  d| |       Y d}~y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)SINGLENrM   F)selectrO   _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$   rR   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.checkt   s     	97.9d"==?KD%5"67e  U ; + 	U1Xx0u&&FFKK(4LNKLMKK%%E%2	''/t;; 	NNKTSTU	s   %C8 8	D"DD"c                 P   | j                   j                  d| j                   j                  }|j                  d      r|j	                  dd      }t        | j                        }i }| j                  | j                  k7  r| j                  |d<   | j                  rd|d<   |||fS )N.zdjango.db.models.query_utilsr   r   Tr   )
r(   
__module____name__
startswithreplacerQ   r   r   defaultr    )r%   pathr&   r'   s       r   deconstructzQ.deconstruct   s    ..33T^^5L5LM??9:<< >@RSDT]]#>>T\\)#'>>F< <<!%F:T6!!r)   c                     | j                         \  }}}|g|j                         }|D ]G  }t        |t              r$|\  }}t	        |      }|j                  ||f       7|j                  |       I t        |      S r   )rx   r$   r/   rQ   r   append)r%   rw   r&   r'   identityrS   argrl   s           r   r{   z
Q.identity   sy    !--/dF*6<<>*E%'"
U%e,e-&  Xr)   c                 `    t        |t              st        S |j                  | j                  k(  S r   )r/   r   NotImplementedr{   r7   s     r   __eq__zQ.__eq__   s%    %#!!~~..r)   c                 ,    t        | j                        S r   )hashr{   )r%   s    r   __hash__z
Q.__hash__   s    DMM""r)   c                     ddl m} |j                  |       D ch c]  }|j                  t        d      d    c}S c c}w )z
        Retrieve all base fields referenced directly or through F expressions
        excluding any fields referenced through joins.
        r   )rH   rO   )r`   rH   get_children_from_qsplitr   )r%   rH   rS   s      r   referenced_base_fieldszQ.referenced_base_fields   sG     	/ 8=7P7PQU7V
7VeEKK
A&q)7V
 	
 
s    =)NTNFF)rs   rr   __qualname____doc__r   r   r   rv   r+   r"   r5   r8   r:   r=   r@   rM   rP   r   ro   rx   r
   r{   r   r   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y)DeferredAttributez
    A wrapper for a deferred-loading field. When the value is read from this
    object the first time, the query is executed.
    c                     || _         y r   )field)r%   r   s     r   r"   zDeferredAttribute.__init__   s	    
r)   Nc                    || S |j                   }| j                  j                  }||vr]| j                  |      }|E|j                  !| j                  j
                  rt        d      |j                  |g       ||   S |||<   ||   S )zx
        Retrieve and caches the value from the datastore on the first lookup.
        Return the cached value.
        z4Cannot read a generated field from an unsaved model.)fields)__dict__r   attname_check_parent_chainpk	generatedAttributeErrorrefresh_from_db)r%   instancer   data
field_namevals         r   __get__zDeferredAttribute.__get__   s    
 K  ZZ''
T! **84C{;;&4::+?+?(N  (((= J $'Z Jr)   c                     |j                   }|j                  | j                  j                        }| j                  j                  r%| j                  |k7  rt        ||j                        S y)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   sU     ~~++DJJ,<,<=
::!!djjJ&>8Z%7%788r)   r   )rs   rr   r   r   r"   r   r    r)   r   r   r      s    
 .
r)   r   c                       e Zd ZdZd Zd Zy)class_or_instance_methodz
    Hook used in RegisterLookupMixin to return partial functions depending on
    the caller type (instance or class of models.Field).
    c                      || _         || _        y r   )class_methodinstance_method)r%   r   r   s      r   r"   z!class_or_instance_method.__init__   s    (.r)   c                     | t        j                  | j                  |      S t        j                  | j                  |      S r   )	functoolspartialr   r   )r%   r   owners      r   r   z class_or_instance_method.__get__   s:    $$T%6%6>>  !5!5x@@r)   N)rs   rr   r   r   r"   r   r   r)   r   r   r      s    
/Ar)   r   c                       e Zd Zd Zej
                  d        Zd Z eee      Z	 e
e      Zd Zd Zed        Ze
d        Zdd	Zdd
Z eee      Z e
e      ZddZddZ eee      Z e
e      Zy)RegisterLookupMixinc                 B    | j                         j                  |d       S r   )get_lookupsget)r%   lookup_names     r   _get_lookupzRegisterLookupMixin._get_lookup   s    !%%k488r)   c                     t        j                  |       D cg c]  }|j                  j                  di         }}| j	                  |      S c c}w Nclass_lookups)inspectgetmror   r   merge_dicts)r   parentr   s      r   get_class_lookupsz%RegisterLookupMixin.get_class_lookups  sT     DK>>RUCV
CVFOO4CV 	 
 }--
s   #Ac                 P    | j                         }t        | dd       x}ri ||S |S Ninstance_lookups)r   r,   )r%   r   r   s      r   get_instance_lookupsz(RegisterLookupMixin.get_instance_lookups  s=    ..0&t-?FFF8m8'788r)   c                     ddl m} | j                  |      }|'t        | d      r| j                  j                  |      S |t        ||      sy |S )Nr   )Lookupr\   )django.db.models.lookupsr   r   rR   r\   
get_lookup
issubclass)r%   r   r   founds       r   r   zRegisterLookupMixin.get_lookup  sQ    3  -=WT>:$$//<<Zv%>r)   c                     ddl m} | j                  |      }|'t        | d      r| j                  j                  |      S |t        ||      sy |S )Nr   )	Transformr\   )r   r   r   rR   r\   get_transformr   )r%   r   r   r   s       r   r   z!RegisterLookupMixin.get_transform  sQ    6  -=WT>:$$22;??Zy%Ar)   c                 L    i }t        |       D ]  }|j                  |        |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%  s(     %AMM! !r)   c                 X    t        |       D ]  }|j                  j                           y r   )r   r   cache_clearr   s     r   _clear_cached_class_lookupsz/RegisterLookupMixin._clear_cached_class_lookups0  s"    "3H&&224 (r)   Nc                     ||j                   }d| j                  vri | _        || j                  |<   | j                          |S r   )r   r   r   r   r   lookupr   s      r   register_class_lookupz)RegisterLookupMixin.register_class_lookup5  sH     ,,K#,,. "C)/+&'')r)   c                 j    ||j                   }d| j                  vri | _        || j                  |<   |S r   )r   r   r   r%   r   r   s      r   register_instance_lookupz,RegisterLookupMixin.register_instance_lookup>  s<     ,,KT]]2$&D!-3k*r)   c                 Z    ||j                   }| j                  |= | j                          y)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_class_lookupz,RegisterLookupMixin._unregister_class_lookupK  s/    
  ,,Kk*'')r)   c                 :    ||j                   }| j                  |= y)zs
        Remove given lookup from instance lookups. For use in tests only as
        it's not thread-safe.
        N)r   r   r   s      r   _unregister_instance_lookupz/RegisterLookupMixin._unregister_instance_lookupU  s#    
  ,,K!!+.r)   r   )rs   rr   r   r   r   cacher   r   r   r   classmethodr   r   staticmethodr   r   r   r   register_lookupr   r   _unregister_lookupr   r)   r   r   r      s    9 __. . ++<>RSK#$56   5 5 /7O ((=>*/ 2 "=  ++CDr)   r   c                 
   | j                   syt        | j                   dd      ry|s| j                   S | j                  |vry|r>| |vr:t	        d| j
                  j                  j                   d| j                   d      y)a  
    Return whether `field` should be used to descend deeper for
    `select_related()` purposes.

    Arguments:
     * `field` - the field to be checked. Can be either a `Field` or
       `ForeignObjectRel` instance.
     * `restricted` - a boolean field, indicating if the field list has been
       manually restricted using a select_related() clause.
     * `requested` - the select_related() dictionary.
     * `select_mask` - the dictionary of selected fields.
    Fparent_linkzField rq   zM cannot be both deferred and traversed using select_related at the same time.T)remote_fieldr,   nullrk   r   r   r   object_name)r   
restricted	requestedselect_masks       r   select_related_descendr   d  s      u!!=%8 ::~ zz" uK/U[[&&2231UZZL AL L
 	
 r)   c                     t        dt        |       dz         D ]4  }t        j                  | d|       }|j	                  |      s-|| |d fc S  y)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.
    rO   r   N)Nr   )rangelenr   joinr   )lookup_partsannotationsnlevel_n_lookups       r   refs_expressionr     sW     1c,'!+,#a):;??>*!<#333 - r)   c                 x      fd} ||      xs+ t        |dd      xr  ||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(  xs2 | j                  j                   j                  v xs | j                  v S r   )r   concrete_modelall_parents)r   r   s    r   ro   z-check_rel_lookup_compatibility.<locals>.check  sP    KK&&$*=*== )""ekk&=&==)(((	
r)   r   F)r,   r   r   )r   target_optsr   ro   s   `   r   check_rel_lookup_compatibilityr     s<    
   }e,Iu{{7H7H1Ir)   c                   D    e Zd ZdZ e       ddZd Zd Zd Zd Z	d Z
y	)
FilteredRelationz7Specify custom filtering in the ON clause of SQL joins.	conditionc                    |st        d      || _        d | _        t        |t              st        d      || _        d | _        y )Nzrelation_name cannot be empty.z*condition argument must be a Q() instance.)
ValueErrorrelation_namealiasr/   r   r   resolved_condition)r%   r   r   s      r   r"   zFilteredRelation.__init__  sG    =>>*
)Q'IJJ #"&r)   c                     t        || j                        st        S | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S r   )r/   r(   r~   r   r  r   r7   s     r   r   zFilteredRelation.__eq__  sY    %0!!%"5"55 2

ekk)2%//1	
r)   c                     t        | j                  | j                        }| j                  |_        | j                  x}|j                         |_        |S )Nr   )r   r   r   r  r  clone)r%   r  r  s      r   r  zFilteredRelation.clone  sK     !3!3t~~Njj"&"9"99F'9'?'?'AE$r)   c                 n    | j                         }|j                  x}r|j                  |      |_        |S r   )r  r  relabeled_clone)r%   
change_mapr  r  s       r   r  z FilteredRelation.relabeled_clone  s7    

!&!9!999'9'I'I*'UE$r)   c                 v    | j                         }|j                  | j                  |ddd      d   |_        |S )NTF)	can_reuserB   rC   update_join_typesr   )r  build_filterr   r  )r%   rH   rI   r&   r'   r  s         r   rM   z#FilteredRelation.resolve_expression  sI    

#(#5#5NN# $6 $
 $  r)   c                 8    |j                  | j                        S r   )compiler  )r%   rm   
connections      r   as_sqlzFilteredRelation.as_sql  s     7 788r)   N)rs   rr   r   r   r   r"   r   r  r  rM   r  r   r)   r   r   r     s(    A343 '
	9r)   r   )!r   r   r   loggingcollectionsr   django.core.exceptionsr   	django.dbr   r   r   django.db.models.constantsr   django.utilsr	   django.utils.functionalr
   django.utils.hashabler   	getLoggerrh   r   r   Noder   r   r   r   r   r   r   r   r   r)   r   <module>r     s       " - B B 1  3 /			-	.
 M(W
		 W
t* *ZA A dE dEN#L
:29 29r)   