Ë
    q^ëiŒ;  ã                   ó.  — 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     úM/var/www/eb/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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        |j                  «       «      ¢||¬«       y )N)ÚchildrenÚ	connectorÚnegated)ÚsuperÚ__init__ÚsortedÚitems)Úselfr   r   ÚargsÚkwargsÚ	__class__s        €r   r    z
Q.__init__0   s3   ø€ Ü‰ÑØ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#   ÚotherÚconnÚobjs       r   Ú_combinez
Q._combine7   sv   € Ü�5˜-¨Ó/°5Ñ8Ü˜EÓ"Ð"ÙØ—:‘:“<ÐÙœ E¬1Ô-Ø—9‘9“;Ðà�k‰k DˆkÓ)ˆØ�‰��dÔØ�‰��tÔØˆ
r'   c                 ó:   — | j                  || j                  «      S r   )r3   r   ©r#   r0   s     r   Ú__or__zQ.__or__D   s   € Ø�}‰}˜U D§G¡GÓ,Ð,r'   c                 ó:   — | j                  || j                  «      S r   )r3   r   r5   s     r   Ú__and__z	Q.__and__G   ó   € Ø�}‰}˜U D§H¡HÓ-Ð-r'   c                 ó:   — | j                  || j                  «      S r   )r3   r   r5   s     r   Ú__xor__z	Q.__xor__J   r9   r'   c                 óF   — | j                  «       }|j                  «        |S r   )r,   Únegate)r#   r2   s     r   Ú
__invert__zQ.__invert__M   s   € Ø�i‰i‹kˆØ�
‰
ŒØˆ
r'   c                 ó\   — |j                  | ||dd|¬«      \  }}|j                  |«       |S )NF)Úallow_joinsÚ
split_subqÚcheck_filterableÚ	summarize)Ú_add_qÚpromote_joins)r#   Úqueryr@   ÚreuserC   Úfor_saveÚclauseÚjoinss           r   Úresolve_expressionzQ.resolve_expressionR   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ÚhasattrrN   )r#   Úchilds     r   rN   z	Q.flattenb   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)ÚSINGLENrK   F)ÚselectrM   Ú_checkT)Úoutput_field)Úusingz.Got a database error calling check() on %r: %s)Údjango.db.modelsrS   rT   Údjango.db.models.functionsrU   Údjango.db.models.sqlrV   Údjango.db.models.sql.constantsrW   r"   rP   Úadd_annotationr   ÚfeaturesÚsupports_comparing_boolean_exprÚadd_qr   Úget_compilerÚexecute_sqlr   ÚloggerÚwarning)r#   Úagainstr[   rS   rT   rU   rV   rW   rF   ÚnameÚvalueÚcompilerÚes                r   ÚcheckzQ.checkq   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   )
r&   Ú
__module__Ú__name__Ú
startswithÚreplacerO   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Ð!Ð!r'   )NTNFF)rq   rp   Ú__qualname__Ú__doc__r   r   r   rt   r)   r    r3   r6   r8   r;   r>   rK   rN   r   rm   rv   Ú__classcell__)r&   s   @r   r   r   #   sf   ø„ ñð €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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ÑÐ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   € ð �~‰~ˆØ×+Ñ+¨D¯J©J×,<Ñ,<Ó=ˆ
Ø�:‰:×!Ò! d§j¡j°JÒ&>Ü˜8 Z×%7Ñ%7Ó8Ð8Ør'   r   )rq   rp   rw   rx   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Ñ!5°xÓ@Ð@r'   N)rq   rp   rw   rx   r    rˆ   r�   r'   r   r’   r’   Ä   s   „ ñò
/óAr'   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Ð8r'   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©   r*   )r#   r¤   r¬   s      r   Úget_instance_lookupsz(RegisterLookupMixin.get_instance_lookupsß   s=   € Ø×.Ñ.Ó0ˆÜ& tÐ-?ÀÓFÐFÐÐFØ8�mÐ8Ð'7Ð8Ð8ØÐr'   c                 ó¤   — ddl m} | j                  |«      }|€'t        | d«      r| j                  j                  |«      S |�t        ||«      sy |S )Nr   )ÚLookuprZ   )Údjango.db.models.lookupsr¯   r    rP   rZ   Ú
get_lookupÚ
issubclass)r#   rŸ   r¯   Úfounds       r   r±   zRegisterLookupMixin.get_lookupè   sQ   € Ý3à× Ñ  Ó-ˆØˆ=œW T¨>Ô:Ø×$Ñ$×/Ñ/°Ó<Ð<ØÐ¤Z°°vÔ%>ØØˆr'   c                 ó¤   — ddl m} | j                  |«      }|€'t        | d«      r| j                  j                  |«      S |�t        ||«      sy |S )Nr   )Ú	TransformrZ   )r°   rµ   r    rP   rZ   Úget_transformr²   )r#   rŸ   rµ   r³   s       r   r¶   z!RegisterLookupMixin.get_transformò   sQ   € Ý6à× Ñ  Ó-ˆØˆ=œW T¨>Ô:Ø×$Ñ$×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Ø�M‰M˜!Õð	àˆr'   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ñ	5r'   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ÔØ)/ˆ×Ñ˜+Ñ&Ø×'Ñ'Ô)Øˆ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_lookup"  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_lookup,  s#   € ð
 ÐØ ×,Ñ,ˆKØ×!Ñ! +Ñ.r'   r   )rq   rp   rw   r    r—   Ú	lru_cacher©   r­   r’   r�   Úclassmethodr±   r¶   Ústaticmethodr§   r¿   rÃ   rÆ   Úregister_lookuprÈ   rÊ   Ú_unregister_lookupr�   r'   r   r›   r›   Ô   sÐ   „ ò9ð €Y×Ñ Ô&ñ.ó 'ð.òñ +Ð+<Ð>RÓS€KÙ#Ð$5Ó6Ðòòð ñó ðð ñ5ó ð5óóñ /ØÐ7ó€Oñ (Ð(=Ó>Ðó*ó/ñ 2Ø Ð"=óÐñ  +Ð+CÓDÑr'   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 ro   zM cannot be both deferred and traversed using select_related at the same time.T)	Úremote_fieldÚparent_linkÚrelated_query_nameri   Ú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ó
ð 	
ð 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.
    rM   r   N)Nr�   )ÚrangeÚlenr   Újoinrž   )Úlookup_partsÚannotationsÚnÚlevel_n_lookups       r   Úrefs_expressionrã   b  s\   € ô �1”c˜,Ó'¨!Ñ+Ó,ò 4ˆÜ#Ÿ™¨°a¸Ð):Ó;ˆØ�?‰?˜>Õ*Ø! <°°Ð#3Ð3Ò3ð4ð 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s: | j                  ‰j                   j                  «       v xs ‰| j                  «       v S r   )rŠ   Úconcrete_modelÚget_parent_list)rŽ   rŒ   s    €r   rm   z-check_rel_lookup_compatibility.<locals>.checkw  sV   ø€ à�K‰K×&Ñ&¨$×*=Ñ*=Ñ=ò /Ø×"Ñ" e§k¡k×&AÑ&AÓ&CÐCò/à˜×,Ñ,Ó.Ð.ð	
r'   r�   F)r*   rŒ   rŠ   )rŒ   Útarget_optsr}   rm   s   `   r   Úcheck_rel_lookup_compatibilityré   o  s<   ø€ ô
ñ  �Óò Ü��} eÓ,ÒI±°u·{±{×7HÑ7HÓ1Iðr'   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.)Ú
ValueErrorÚrelation_nameÚaliasr-   r   rí   ru   )r#   rð   rí   s      r   r    zFilteredRelation.__init__�  sD   € ÙÜÐ=Ó>Ð>Ø*ˆÔØˆŒ
Ü˜)¤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&   ÚNotImplementedrð   rñ   rí   r5   s     r   Ú__eq__zFilteredRelation.__eq__™  sY   € Ü˜% §¡Ô0Ü!Ð!à×Ñ %×"5Ñ"5Ñ5ò 2Ø—
‘
˜eŸk™kÑ)ò2à—‘ %§/¡/Ñ1ð	
r'   c                 ó’   — t        | j                  | j                  ¬«      }| j                  |_        | j                  d d  |_        |S )Nrì   )rë   rð   rí   rñ   ru   )r#   Úclones     r   rö   zFilteredRelation.clone¢  s7   € Ü  ×!3Ñ!3¸t¿~¹~ÔNˆØ—j‘jˆŒØ—Y‘Y™q�\ˆŒ
Øˆ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   rK   z#FilteredRelation.resolve_expression¨  s   € ô
 "Ð"TÓUÐUr'   c                 óœ   — |j                   }|j                  | j                  t        | j                  «      ¬«      }|j                  |«      S )N)rG   )rF   Úbuild_filtered_relation_qrí   Úsetru   Úcompile)r#   rk   Ú
connectionrF   Úwheres        r   Úas_sqlzFilteredRelation.as_sql¯  s=   € à—‘ˆØ×/Ñ/°·±ÄcÈ$Ï)É)ÃnÐ/ÓUˆØ×Ñ Ó&Ð&r'   N)
rq   rp   rw   rx   r   r    rô   rö   rK   rÿ   r�   r'   r   rë   rë   Œ  s$   „ ÙAá34³3ô ò
òòVó'r'   rë   )F)rx   r—   r¥   ÚloggingÚcollectionsr   Údjango.core.exceptionsr   Ú	django.dbr   r   r   Údjango.db.models.constantsr   Údjango.utilsr	   Ú	getLoggerrf   r   r   ÚNoder   r{   r’   r›   rÚ   rã   ré   rë   r�   r'   r   ú<module>r     s¤   ðñó Û Û Ý "å -ß BÑ BÝ 1Ý à	ˆ×	Ñ	Ð-Ó	.€ñ
 ØØMó€ò(ôu"ˆ�	‰	ô u"÷p&ñ &÷RAñ A÷ dEñ dEóN$òN
ò÷:''ò ''r'   