
    Hwg;                     .   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     S/var/www/horilla/myenv/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    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._combine7   sv    5-/58E""::<E1-99;kkDk)dt
r'   c                 :    | j                  || j                        S r   )r3   r   r#   r0   s     r   __or__zQ.__or__D   s    }}UDGG,,r'   c                 :    | j                  || j                        S r   )r3   r   r5   s     r   __and__z	Q.__and__G       }}UDHH--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    iik


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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)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"==? 	<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replacerO   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'   )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  ZZ''
T! **84C{(((= 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   )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!5x@@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    !%%k4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>>RUCV
9?FOO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8m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  -=WT>:$$//<<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  -=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_lookups  s'    "3 	5H&&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SK#$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%**JJ)#$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.
    rM   r   N)Nr   )rangelenr   joinr   )lookup_partsannotationsnlevel_n_lookups       r   refs_expressionr   b  s\     1c,'!+, 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    KK&&$*=*== /""ekk&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{{7H7H1I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

ekk)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!3t~~NjjYYq\
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$    A343 
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'   