Ë
    bõhiŒ<  ã                   ó.  — 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j                   d«      Z ed	d
«      Zd„ Z G d„ dej(                  «      Z G d„ d«      Z G d„ d«      Z G d„ d«      Zd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ú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     úR/var/www/alwasay/venv/lib/python3.12/site-packages/django/db/models/query_utils.pyr   r      s4   è ø€ Ø
‚IØ×&Ñ&Ó(ò (ˆÜ˜hÓ'×'Ñ'ñ(Ø'ús   ‚&2¨0©2c                   óˆ   ‡ — e Zd ZdZdZdZdZeZdZdeeef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                óä   •— || j                   vr4dj                  d„ | j                   dd  D «       «      }t        d|› d�«      ‚t        ‰| �  g |¢t        |j                  «       «      ¢||¬«       y )Nz, c              3   ó"   K  — | ]  }|›–— Œ	 y ­wr   © )Ú.0Úconns     r   ú	<genexpr>zQ.__init__.<locals>.<genexpr>3   s   è ø€ Ò'T¸¨4¨(«Ñ'Tùs   ‚é   z_connector must be one of z
, or None.)ÚchildrenÚ	connectorÚnegated)Ú
connectorsÚjoinÚ
ValueErrorÚsuperÚ__init__ÚsortedÚitems)Úselfr   r   ÚargsÚkwargsÚconnector_reprsÚ	__class__s         €r   r)   z
Q.__init__1   sv   ø€ Ø˜TŸ_™_Ñ,Ø"Ÿi™iÑ'TÀÇÁÐPQÐPRÐ@SÔ'TÓTˆOÜÐ9¸/Ð9JÈ*ÐUÓVÐVÜ‰ÑØ5�tÐ5œf V§\¡\£^Ó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,   Úotherr   Úobjs       r   Ú_combinez
Q._combine;   sv   € Ü�5˜-¨Ó/°5Ñ8Ü˜EÓ"Ð"ÙØ—:‘:“<ÐÙœ E¬1Ô-Ø—9‘9“;Ðà�k‰k DˆkÓ)ˆØ�‰��dÔØ�‰��tÔØˆ
r1   c                 ó:   — | j                  || j                  «      S r   )r<   r   ©r,   r:   s     r   Ú__or__zQ.__or__H   s   € Ø�}‰}˜U D§G¡GÓ,Ð,r1   c                 ó:   — | j                  || j                  «      S r   )r<   r   r>   s     r   Ú__and__z	Q.__and__K   ó   € Ø�}‰}˜U D§H¡HÓ-Ð-r1   c                 ó:   — | j                  || j                  «      S r   )r<   r   r>   s     r   Ú__xor__z	Q.__xor__N   rB   r1   c                 óF   — | j                  «       }|j                  «        |S r   )r6   Únegate)r,   r;   s     r   Ú
__invert__zQ.__invert__Q   s   € Ø�i‰i‹kˆØ�
‰
ŒØˆ
r1   c                 ó\   — |j                  | ||dd|¬«      \  }}|j                  |«       |S )NF)Úallow_joinsÚ
split_subqÚcheck_filterableÚ	summarize)Ú_add_qÚpromote_joins)r,   ÚqueryrI   ÚreuserL   Úfor_saveÚclauseÚjoinss           r   Úresolve_expressionzQ.resolve_expressionV   sB   € ð
 Ÿ™ØØØ#ØØ"Øð %ó 
‰ˆ�ð 	×Ñ˜EÔ"Øˆr1   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.
        r!   ÚflattenN)r"   r7   ÚtupleÚhasattrrV   )r,   Úchilds     r   rV   z	Q.flattenf   sV   è ø€ ð
 Š
Ø—]‘]ò 	ˆEÜ˜%¤Ô'à˜a™�Ü�u˜iÔ(Ø Ÿ=™=›?×*Ñ*à“ñ	ð
 +ú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)ÚSINGLENrT   F)Úselectr!   Ú_checkT)Úoutput_field)Úusingz.Got a database error calling check() on %r: %s)Údjango.db.modelsr[   r\   Údjango.db.models.functionsr]   Údjango.db.models.sqlr^   Údjango.db.models.sql.constantsr_   r+   rX   Úadd_annotationr   ÚfeaturesÚsupports_comparing_boolean_exprÚadd_qr   Úget_compilerÚexecute_sqlr   ÚloggerÚwarning)r,   Úagainstrc   r[   r\   r]   r^   r_   rO   ÚnameÚvalueÚcompilerÚes                r   ÚcheckzQ.checku   s   € ÷ 	9Ý7Ý.Ý9á�d“ˆØ"Ÿ=™=›?ò 	<‰KˆD�%Ü˜5Ð"6Ô7Ù˜e›�Ø× Ñ  ¨°UÐ Õ;ð	<ð 	×Ñ™U 1›X xÔ0ä�uÑ×&Ñ&×FÒFØ�K‰Kœ™( 4¨¹L»NÔKÓLÕMà�K‰K˜ÔØ×%Ñ%¨EÐ%Ó2ˆð	Ø×'Ñ'¨Ó/°tÐ;Ð;øÜò 	Ü�N‰NÐKÈTÐSTÔ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   )
r0   Ú
__module__Ú__name__Ú
startswithÚreplacerW   r"   r#   Údefaultr$   )r,   Úpathr-   r.   s       r   ÚdeconstructzQ.deconstruct’   sŽ   € ØŸ.™.×3Ó3°T·^±^×5LÒ5LÐMˆØ�?‰?Ð9Ô:Ø—<‘<Ð >Ð@RÓSˆDÜ�T—]‘]Ó#ˆØˆØ�>‰>˜TŸ\™\Ò)Ø#'§>¡>ˆF�<Ñ Ø�<Š<Ø!%ˆF�:ÑØ�T˜6Ð!Ð!r1   )NTNFF)ry   rx   Ú__qualname__Ú__doc__r   r   r   r|   r3   r%   r)   r<   r?   rA   rD   rG   rT   rV   r   ru   r~   Ú__classcell__)r0   s   @r   r   r   #   st   ø„ ñð €CØ	€BØ
€CØ€GØ€KØ˜˜R Ð%€Jà)-¸ö 
òò-ò.ò.òð SXóò ð $4ó ö:
"r1   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	   € Øˆ�
r1   Nc                 óÀ   — |€| S |j                   }| j                  j                  }||vr0| j                  |«      }|€|j	                  |g¬«       ||   S |||<   ||   S )zx
        Retrieve and caches the value from the datastore on the first lookup.
        Return the cached value.
        )Úfields)Ú__dict__r…   ÚattnameÚ_check_parent_chainÚrefresh_from_db)r,   Úinstancer   ÚdataÚ
field_nameÚvals         r   Ú__get__zDeferredAttribute.__get__¨   s   € ð
 ÐØˆKØ× Ñ ˆØ—Z‘Z×'Ñ'ˆ
Ø˜TÑ!ð ×*Ñ*¨8Ó4ˆCØˆ{Ø×(Ñ(°°Ð(Ô=ð �JÑÐð $'��ZÑ Ø�JÑÐr1   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_keyr4   r‰   )r,   rŒ   ÚoptsÚ
link_fields       r   rŠ   z%DeferredAttribute._check_parent_chain»   sU   € ð �~‰~ˆØ×+Ñ+¨D¯J©J×,<Ñ,<Ó=ˆ
Ø�:‰:×!Ò! d§j¡j°JÒ&>Ü˜8 Z×%7Ñ%7Ó8Ð8Ør1   r   )ry   rx   r   r€   r)   r�   rŠ   r   r1   r   rƒ   rƒ   Ÿ   s   „ ñò
ó ó&
r1   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   € Ø(ˆÔØ.ˆÕr1   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Ñ!5°xÓ@Ð@r1   N)ry   rx   r   r€   r)   r�   r   r1   r   r™   r™   È   s   „ ñò
/óAr1   r™   c                   óü   — e Zd Zd„ Z ej
                  d¬«      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   € Ø×ÑÓ!×%Ñ% k°4Ó8Ð8r1   N)Úmaxsizec                 ó¦   — 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Ü   sQ   € ô DKÇ>Á>ÐRUÓCVö
Ø9?ˆF�O‰O×Ñ °Õ4ð
ˆð 
ð �‰˜}Ó-Ð-ùò
s   ˜#Ac                 óP   — | j                  «       }t        | dd «      x}ri |¥|¥S |S ©NÚinstance_lookups)r°   r4   )r,   r«   r³   s      r   Úget_instance_lookupsz(RegisterLookupMixin.get_instance_lookupsã   s=   € Ø×.Ñ.Ó0ˆÜ& tÐ-?ÀÓFÐFÐÐFØ8�mÐ8Ð'7Ð8Ð8ØÐr1   c                 ó¤   — ddl m} | j                  |«      }|€'t        | d«      r| j                  j                  |«      S |�t        ||«      sy |S )Nr   )ÚLookuprb   )Údjango.db.models.lookupsr¶   r§   rX   rb   Ú
get_lookupÚ
issubclass)r,   r¦   r¶   Úfounds       r   r¸   zRegisterLookupMixin.get_lookupì   sQ   € Ý3à× Ñ  Ó-ˆØˆ=œW T¨>Ô:Ø×$Ñ$×/Ñ/°Ó<Ð<ØÐ¤Z°°vÔ%>ØØˆr1   c                 ó¤   — ddl m} | j                  |«      }|€'t        | d«      r| j                  j                  |«      S |�t        ||«      sy |S )Nr   )Ú	Transformrb   )r·   r¼   r§   rX   rb   Úget_transformr¹   )r,   r¦   r¼   rº   s       r   r½   z!RegisterLookupMixin.get_transformö   sQ   € Ý6à× Ñ  Ó-ˆØˆ=œW T¨>Ô:Ø×$Ñ$×2Ñ2°;Ó?Ð?ØÐ¤Z°°yÔ%AØØˆr1   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Ø�M‰M˜!Õð	àˆr1   c                 óX   — t        | «      D ]  }|j                  j                  «        Œ y r   )r   r°   Úcache_clearr   s     r   Ú_clear_cached_class_lookupsz/RegisterLookupMixin._clear_cached_class_lookups  s'   € ä" 3›ò 	5ˆHØ×&Ñ&×2Ñ2Õ4ñ	5r1   c                 óŠ   — |€|j                   }d| j                  vri | _        || j                  |<   | j                  «        |S rª   )r¦   rˆ   r«   rÆ   ©r   Úlookupr¦   s      r   Úregister_class_lookupz)RegisterLookupMixin.register_class_lookup  sH   € ØÐØ ×,Ñ,ˆKØ #§,¡,Ñ.Ø "ˆCÔØ)/ˆ×Ñ˜+Ñ&Ø×'Ñ'Ô)Øˆr1   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Ñ*Øˆr1   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_lookup&  s/   € ð
 ÐØ ×,Ñ,ˆKØ×Ñ˜kÐ*Ø×'Ñ'Õ)r1   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_lookup0  s#   € ð
 ÐØ ×,Ñ,ˆKØ×!Ñ! +Ñ.r1   r   )ry   rx   r   r§   rž   Ú	lru_cacher°   r´   r™   r¤   Úclassmethodr¸   r½   Ústaticmethodr®   rÆ   rÊ   rÍ   Úregister_lookuprÏ   rÑ   Ú_unregister_lookupr   r1   r   r¢   r¢   Ø   sÐ   „ ò9ð €Y×Ñ Ô&ñ.ó 'ð.òñ +Ð+<Ð>RÓS€KÙ#Ð$5Ó6Ðòòð ñó ðð ñ5ó ð5óóñ /ØÐ7ó€Oñ (Ð(=Ó>Ðó*ó/ñ 2Ø Ð"=óÐñ  +Ð+CÓDÑr1   r¢   c                 ó^  — | j                   sy| j                   j                  r|sy|r&|r| j                  «       |vry|s| j                  |vry|s| j                  ry|rN|rL| j                  |v r>| |vr:t        d| j                  j                  j                  › d| j                  › d�«      ‚y)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 rw   zM cannot be both deferred and traversed using select_related at the same time.T)	Úremote_fieldÚparent_linkÚrelated_query_namerq   Únullr   r”   r’   Úobject_name)r…   Ú
restrictedÚ	requestedÚselect_maskÚreverses        r   Úselect_related_descendrá   ?  sµ   € ð ×ÒØØ×Ñ×%Ò%©gØÙÙ�u×/Ñ/Ó1¸ÑBØÙ˜5Ÿ:™:¨YÑ6ØÙ˜%Ÿ*š*ØáÙØ�J‰J˜)Ñ#Ø˜Ñ$äØ�U—[‘[×&Ñ&×2Ñ2Ð3°1°U·Z±Z°Lð ALð Ló
ð 	
ð r1   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.
    r!   r   N)Nr   )ÚrangeÚlenr   r&   r¥   )Úlookup_partsÚannotationsÚnÚlevel_n_lookups       r   Úrefs_expressionré   f  s\   € ô �1”c˜,Ó'¨!Ñ+Ó,ò 4ˆÜ#Ÿ™¨°a¸Ð):Ó;ˆØ�?‰?˜>Õ*Ø! <°°Ð#3Ð3Ò3ð4ð r1   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s: | j                  ‰j                   j                  «       v xs ‰| j                  «       v S r   )r’   Úconcrete_modelÚget_parent_list)r–   r”   s    €r   ru   z-check_rel_lookup_compatibility.<locals>.check{  sV   ø€ à�K‰K×&Ñ&¨$×*=Ñ*=Ñ=ò /Ø×"Ñ" e§k¡k×&AÑ&AÓ&CÐCò/à˜×,Ñ,Ó.Ð.ð	
r1   r•   F)r4   r”   r’   )r”   Útarget_optsr…   ru   s   `   r   Úcheck_rel_lookup_compatibilityrï   s  s<   ø€ ô
ñ  �Óò Ü��} eÓ,ÒI±°u·{±{×7HÑ7HÓ1Iðr1   c                   ó>   — e Zd ZdZ e«       dœ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«      ‚|| _        g | _        y )Nzrelation_name cannot be empty.z*condition argument must be a Q() instance.)r'   Úrelation_nameÚaliasr7   r   ró   r}   )r,   rõ   ró   s      r   r)   zFilteredRelation.__init__“  sD   € ÙÜÐ=Ó>Ð>Ø*ˆÔØˆŒ
Ü˜)¤QÔ'ÜÐIÓJÐJØ"ˆŒØˆ�	r1   c                 óØ   — t        || j                  «      st        S | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S r   )r7   r0   ÚNotImplementedrõ   rö   ró   r>   s     r   Ú__eq__zFilteredRelation.__eq__�  sY   € Ü˜% §¡Ô0Ü!Ð!à×Ñ %×"5Ñ"5Ñ5ò 2Ø—
‘
˜eŸk™kÑ)ò2à—‘ %§/¡/Ñ1ð	
r1   c                 ó’   — t        | j                  | j                  ¬«      }| j                  |_        | j                  d d  |_        |S )Nrò   )rñ   rõ   ró   rö   r}   )r,   Úclones     r   rû   zFilteredRelation.clone¦  s7   € Ü  ×!3Ñ!3¸t¿~¹~ÔNˆØ—j‘jˆŒØ—Y‘Y™q�\ˆŒ
Øˆr1   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   rT   z#FilteredRelation.resolve_expression¬  s   € ô
 "Ð"TÓUÐUr1   c                 óœ   — |j                   }|j                  | j                  t        | j                  «      ¬«      }|j                  |«      S )N)rP   )rO   Úbuild_filtered_relation_qró   Úsetr}   Úcompile)r,   rs   Ú
connectionrO   Úwheres        r   Úas_sqlzFilteredRelation.as_sql³  s=   € à—‘ˆØ×/Ñ/°·±ÄcÈ$Ï)É)ÃnÐ/ÓUˆØ×Ñ Ó&Ð&r1   N)
ry   rx   r   r€   r   r)   rù   rû   rT   r  r   r1   r   rñ   rñ   �  s$   „ ÙAá34³3ô ò
òòVó'r1   rñ   )F)r€   rž   r¬   ÚloggingÚcollectionsr   Údjango.core.exceptionsr   Ú	django.dbr   r   r   Údjango.db.models.constantsr   Údjango.utilsr	   Ú	getLoggerrn   r   r   ÚNoder   rƒ   r™   r¢   rá   ré   rï   rñ   r   r1   r   ú<module>r     s¤   ðñó Û Û Ý "å -ß BÑ BÝ 1Ý à	ˆ×	Ñ	Ð-Ó	.€ñ
 ØØMó€ò(ôy"ˆ�	‰	ô y"÷x&ñ &÷RAñ A÷ dEñ dEóN$òN
ò÷:''ò ''r1   