
    hQ                    &   S r SSKrSSKrSSKrSSKrSSKJrJr  SSKJ	r	J
r
  SSKJrJrJr  SSKJr  SSKJrJr  SSKJrJrJr  SS	KJr  SS
KJr  SSKJrJrJrJ r J!r!J"r"J#r#J$r$  SSK%J&r&  SSK'J(r(  SSK)J*r*  SSK+J,r,J-r-J.r.  SSK/J0r0J1r1J2r2J3r3  SSK4J5r5J6r6J7r7J8r8  SSK9J:r:J;r;J<r<J=r=J>r>  SSK?J@r@  SSKAJBrB  SSKCJDrD  SS/rE\B" S5      rF\B" S5      rGS rHS rIS rJS rKS rL\" SS 5      rM " S! S5      rN\" S"S#5      rO " S$ S\5      rPS(S% jrQ " S& S'5      rRg))aW  
Create SQL statements for QuerySets.

The code in here encapsulates all of the SQL construction so that QuerySets
themselves do not have to (and could be backed by things other than SQL
databases). The abstraction barrier only works one way: this module has to know
all about the internals of models in order to get the information it needs.
    N)Counter
namedtuple)IteratorMapping)chaincountproduct)ascii_uppercase)FieldDoesNotExist
FieldError)DEFAULT_DB_ALIASNotSupportedErrorconnections)Count)
LOOKUP_SEP)BaseExpressionColExistsFOuterRefRefResolvedOuterRefValue)Field)MultiColSource)Lookup)Qcheck_rel_lookup_compatibilityrefs_expression)INNERLOUTER	ORDER_DIRSINGLE)	BaseTableEmptyJoin	MultiJoin)ANDOR
ExtraWhereNothingNode	WhereNode)cached_property)_lazy_re_compile)NodeQueryRawQueryz['`\"\]\[;\s]|--|/\*|\*/z[\w-]+c                     U c
  [        5       $ [        [        R                  " S U R                  5        5       5      5      $ )Nc              3      #    U  H:  oR                   (       a  UR                  UR                  4OUR                  4v   M<     g 7fN)concretenameattname.0fs     L/var/www/html/env/lib/python3.13/site-packages/django/db/models/sql/query.py	<genexpr>,get_field_names_from_opts.<locals>.<genexpr>?   s0      
FW::QVVQYYAFF9<FWs   AA)setr   from_iterable
get_fields)optss    r;   get_field_names_from_optsrB   ;   s=    |u 
FJooFW
 	
     c              #   H  #    [        U [        5      (       a  U R                  v   g [        U S5      (       ag  U R	                  5        HR  n[        U[        5      (       a  UR                  v   M(  [        U[
        5      (       d  M?  [        U5       S h  vN   MT     g g  N
7f)Nflatten)
isinstancer   r6   hasattrrE   r   get_children_from_q)exprchilds     r;   get_paths_from_expressionrK   E   st     $ii	y	!	!\\^E%##jj E1%%.u555	 $ 
"
 6s   BB"B"B B"c              #   h  #    U R                    H  n[        U[        5      (       a  [        U5       S h  vN   M-  [        U[        5      (       a0  Uu  p#Uv   [        US5      (       a  [        U5       S h  vN   Mp  Mr  [        US5      (       d  M  [        U5       S h  vN   M     g  Nv N5 N7f)Nresolve_expression)childrenrF   r/   rH   tuplerG   rK   )qrJ   lhsrhss       r;   rH   rH   P   s     eT""*5111u%%HCIs0114S999 2U0110777 1
 :7s:   3B2B,AB28B.9B2B2"B0#
B2.B20B2c           
      "   SSK Jn  [        U[        5      (       a  [	        XU5      $ [        U[
        5      (       ah  Uu  pEUR                  U [        -   5      (       a  UR                  XS5      n[        U[        5      (       d  [        US5      (       a  [        XU5      nXE4$ [        U[        5      (       aZ  UR                  5       nUR                  R                  U [        -   5      (       a!  UR                  R                  XS5      Ul        U$ [        X#5      (       a  [        S5      e[        US5      (       aJ  UR                  5       nUR                  UR!                  5        Vs/ s H  n[        XU5      PM     sn5        U$ s  snf )Nr   )QuerySet   rM   z>Passing a QuerySet within a FilteredRelation is not supported.)django.db.models.queryrT   rF   r/   rename_prefix_from_qrO   
startswithr   replacer   rG   get_child_with_renamed_prefixcopyr6   
ValueErrorset_source_expressionsget_source_expressions)prefixreplacementrJ   rT   rQ   rR   grand_childs          r;   rZ   rZ   ]   sV   /%#F??%>>&:-..++f15C#q!!gc3G&H&H/SICx%

::  *!455++FCEJ" L! 
E	$	$
 L
 	
 
,	-	-

$$ $)#?#?#A#AK .f;O#A	
 Ls   -Fc           
          [         R                  " UR                   Vs/ s H  n[        XU5      PM     snUR                  UR
                  5      $ s  snf r4   )r   createrN   rZ   	connectornegated)r_   r`   rP   cs       r;   rW   rW      sB    88HI

S
1	&vA	>
S				 Ss   AJoinInfo)final_fieldtargetsrA   joinspathtransform_functionc                   \    \ rS rSrSrSS jrS rS rS rS r	S r
\S	 5       rS
 rS rSrg)r1      zA single raw SQL query.c                 p    X0l         Xl        X l        S U l        Su  U l        U l        0 U l        0 U l        g )Nr   N)paramssqlusingcursorlow_mark	high_markextra_selectannotation_select)selfrr   rs   rq   s       r;   __init__RawQuery.__init__   s8    
 )0%t~!#rC   c                 $    U R                  U5      $ r4   )clonery   rs   s     r;   r   RawQuery.chain   s    zz%  rC   c                 >    [        U R                  XR                  S9$ )N)rq   )r1   rr   rq   r~   s     r;   r}   RawQuery.clone   s    %<<rC   c                     U R                   c  U R                  5         [        U R                     R                  R
                  nU R                   R                   Vs/ s H  o!" US   5      PM     sn$ s  snf Nr   )rt   _execute_queryr   rs   introspectionidentifier_converterdescription)ry   	convertercolumn_metas      r;   get_columnsRawQuery.get_columns   s_    ;;!

+99NN	=A[[=T=TU=Tk	+a.)=TUUUs   A4c                     U R                  5         [        U R                     R                  R                  (       d  [        U R                  5      nOU R                  n[        U5      $ r4   )r   r   rs   featurescan_use_chunked_readslistrt   iter)ry   results     r;   __iter__RawQuery.__iter__   sK     	4::&//EE $++&F[[FF|rC   c                 @    SU R                   R                  < SU < S3$ )N<z: >)	__class____name__ry   s    r;   __repr__RawQuery.__repr__   s    !^^44d;;rC   c                 t    U R                   c  g [        U R                   [        5      (       a  [        $ [        $ r4   )rq   rF   r   dictrO   r   s    r;   params_typeRawQuery.params_type   s*    ;;!$++w77tBUBrC   c                     U R                   c  U R                  $ U R                  U R                  U R                  5      -  $ r4   )r   rr   rq   r   s    r;   __str__RawQuery.__str__   s5    #88Oxx$**4;;777rC   c                   ^ [         U R                     nU R                  nUR                  R                  mU[
        L a   [        U4S jU R                   5       5      nOSU[        L a6  U R                  R                  5        VVs0 s H  u  pEUT" U5      _M     nnnOUc  S nO[        SU-  5      eUR                  5       U l
        U R                  R                  U R                  U5        g s  snnf )Nc              3   4   >#    U  H  nT" U5      v   M     g 7fr4    )r9   valadapters     r;   r<   *RawQuery._execute_query.<locals>.<genexpr>   s     ?;C73<<;s   zUnexpected params type: %s)r   rs   r   opsadapt_unknown_valuerO   rq   r   itemsRuntimeErrorrt   executerr   )ry   
connectionr   rq   keyr   r   s         @r;   r   RawQuery._execute_query   s     ,
 &&..44%?4;;??FD 8<8I8I8KL8KHCc73<'8KFLF F;kIJJ '')DHHf- Ms   C/)rx   rt   rw   rv   ru   rq   rr   rs   N)r   )r   
__module____qualname____firstlineno____doc__rz   r   r}   r   r   r   propertyr   r   r   __static_attributes__r   rC   r;   r1   r1      sD    !
$!=V
< C C
8
.rC   ExplainInfo)formatoptionsc                      \ rS rSrSrSrSr\" \/5      rSr	\
r\rSrSrSrSrSrSrSrSrS	rSrSrSrSrSrSrSrSrSrSr S
r!Sr"Sr#Sr$Sr%Sr&Sr'Sr(Sr)Sr*Sr+\" 5       S4r,Sr-SfS jr.\/S 5       r0\1S 5       r2S r3S r4S r5SgS jr6S r7S r8ShS jr9S r:S r;S r<S r=S r>SfS jr?S r@ShS jrAS rBShS jrCShS jrDS  rESiS! jrFS" rGSjS# jrHS$ rIS% rJS& rKS' rLShS( jrMS) rNS* rOShS+ jrPS, rQS- rRSfS. jrSS/ rTS0 rUShS1 jrVS2 rWSkS3 jrXSkS4 jrYS5 rZS6 r[S7 r\S8 r]S9 r^        SlS: jr_S; r`S< raS= rbS> rc       SmS? jrdS@ reSnSA jrf  SoSB jrgSC rh\iSpSD j5       rj\iSE 5       rkSqSF jrlSG rmSH rnSI roSrSJ jrpSK rq\/SL 5       rrSM rsSN rtSO ruSP rvSQ rwSR rxSS rySfST jrzSU r{SpSV jr|SfSW jr}SX r~SY rSZ rS[ rS\ rS] rS^ rS_ rS` r\/Sa 5       r\/Sb 5       rSc rSd rSerg)sr0      zA single SQL query.TNSQLCompilerTFr   r      c                     Xl         0 U l        0 U l        X l        0 U l        0 U l        [        5       U l        [        5       U l	        0 U l
        0 U l        0 U l        g r4   )modelalias_refcount	alias_map
alias_colsexternal_aliases	table_mapr>   used_aliasesr,   whereannotationsextra_filtered_relations)ry   r   r   s      r;   rz   Query.__init__$  sZ    
  $
 !#E[
 
#% rC   c                 2   [        U R                  5      S:X  a/  U R                  S   n[        USS 5      =(       d    UR                  $ [        U R                  5      S:X  a6  [        [        U R                  R                  5       5      5      R                  $ g )NrU   r   target)	lenselectgetattrfieldrx   nextr   valuesoutput_field)ry   r   s     r;   r   Query.output_field@  su    t{{q [[^F68T2BfllB''(A-T33::<=>KKK .rC   c                 .    U R                    H  nUs  $    g r4   )r   ry   aliass     r;   
base_tableQuery.base_tableH  s    ^^EL $rC   c                 .    U R                  5       u  pX-  $ )a   
Return the query as a string of SQL with the parameter values
substituted in (use sql_with_params() to see the unsubstituted string).

Parameter values won't necessarily be quoted correctly, since that is
done by the database interface at execution time.
)sql_with_params)ry   rr   rq   s      r;   r   Query.__str__M  s     **,|rC   c                 H    U R                  [        5      R                  5       $ )z_
Return the query as an SQL string and the parameters that will be
substituted into the query.
)get_compilerr   as_sqlr   s    r;   r   Query.sql_with_paramsX  s    
   !1299;;rC   c                 @    U R                  5       nX![        U 5      '   U$ )z4Limit the amount of work when a Query is deepcopied.)r}   id)ry   memor   s      r;   __deepcopy__Query.__deepcopy___  s    RXrC   c                     Uc  Uc  [        S5      eU(       a	  [        U   nUR                  R                  U R                  5      " XX5      $ )NzNeed either using or connection)r\   r   r   compiler)ry   rs   r   elide_emptys       r;   r   Query.get_compilere  sJ    =Z/>??$U+J~~&&t}}5e
 	
rC   c                 R    U R                   (       a  U R                   R                  $ g)z
Return the Options instance (the model._meta) from which to start
processing. Normally, this is self.model._meta, but it can be changed
by subclasses.
N)r   _metar   s    r;   get_metaQuery.get_metan  s      ::::### rC   c                 X   [        5       nU R                  Ul        U R                  R                  5       Ul        U R                  R                  5       Ul        U R
                  R                  5       Ul        U R                  R                  5       Ul        U R                  R                  5       Ul        U R                  R                  5       Ul        U R                  R                  5       Ul
        U R                  b  U R                  R                  5       Ul        U R                  (       a8  [        U R                   Vs/ s H  o"R                  5       PM     sn5      Ul        SUl        U R                  R                  5       Ul        U R                   b  U R                   R                  5       Ul        U R"                  b  U R"                  R                  5       Ul        U R$                  SLa%  [        R&                  " UR$                  5      Ul        SU R                  ;   a  U R(                  R                  5       Ul        U R*                  R                  5       Ul        U R,                  R                  5       Ul        UR                  R/                  SS5        U$ s  snf )zN
Return a copy of the current Query. A lightweight alternative to
deepcopy().
NFsubq_aliasesr   )r%   r   __dict__r[   r   r   r   r   r   r}   r   annotation_select_maskcombined_queriesrO   _annotation_select_cacher   extra_select_mask_extra_select_cacheselect_relateddeepcopyr   r   r   pop)ry   objquerys      r;   r}   Query.clonew  s   
 g}}))+!00557++-#4499;++-JJ$$&	**//1&&2)-)D)D)I)I)KC&  #(,0,A,AB,A5,AB$C  (,$JJOO%	!!-$($:$:$?$?$AC!##/&*&>&>&C&C&EC#e+ "&s/A/A!BCT]]*#00557C,,113"&":":"?"?"At,
/ Cs   J'c                     U R                  5       nU(       a  UR                  U:w  a  Xl        UR                  (       d  [        5       Ul        SUl        [        US5      (       a  UR                  5         U$ )z
Return a copy of the current Query that's ready for another operation.
The klass argument changes the type of the Query, e.g. UpdateQuery.
F_setup_query)r}   r   filter_is_stickyr>   r   rG   r   )ry   klassr   s      r;   r   Query.chain  s[    
 jjlS]]e+!M##"uC$3''
rC   c                 H    U R                  5       nUR                  U5        U$ r4   )r}   change_aliases)ry   
change_mapr}   s      r;   relabeled_cloneQuery.relabeled_clone  s     

Z(rC   c                 J    U R                   (       d  S nUR                  X25      $ r4   r   get_col)ry   r   r   r   s       r;   _get_colQuery._get_col  s    E~~e++rC   c                 p  ^  U(       d  0 $ SnSn0 nT R                   nUR                  5        H  u  pxT R                  U5        UR                  T SSSS9n	U	R                  (       d  [        SU-  5      eT R                  U/5        U	R                  5       n
U[        U 4S jU
 5       5      -  nU[        U 4S jU
 5       5      -  nU	R                  U5      n	U	T R                  U'   X[        Xy5      '   M     U Vs0 s H  owT R                  R                  U5      _M      nnT R                  U5        T R                  R                  5       u  pn[        S T R                  R!                  5        5       5      =(       d    Un[#        T R$                  [&        5      (       dP  T R(                  (       d?  U(       d8  U(       d1  U(       d*  U(       d#  T R*                  (       d  T R,                  (       Ga  S	S
KJn  T R3                  5       nSUl        U" T R6                  U5      nSUl        SUl        UR                  T R<                  5        UR?                  SS9  UR*                  (       GdP  UR@                  (       aJ  U(       aC  T R6                  RB                  RD                  RG                  URI                  5       5      4Ul        SUl         U(       d  T R,                  (       d  [K        5       n[#        T R$                  [&        5      (       a&  T R$                   H  nUUR                  5       -  nM     UR!                  5        H  n	UU	R                  5       -  nM     T R<                  R                  5        H.  u  nnURM                  5       (       d  M  URO                  U5        M0     UR                  U5        0 nUR                  5        H  u  py0 nT RQ                  U	/SS9 He  nURS                  U5      =n(       dE  [U        U5      S-   nSU 3n[        UU5      nUUU'   UUR                  U'   UR                  U/5        UUU'   Mg     U	R                  U5      UR                  U'   M     URV                  S:X  ae  UR@                  (       dT  UR                   (       dC  T R6                  RB                  RD                  RG                  URI                  5       5      4Ul+        OT nST l+        ST l         0 T l,        T R                  (       at  T R                  R                  5        VVs0 s H  u  nn[        UU5      U_M     nnnUR                  5        VV	s0 s H  u  pyXyR                  U5      _M     sn	nT l
        OUT l
        T R                  U5        UR<                  R!                  5        Vs/ s H  nURZ                  PM     nn[        S U 5       5      (       + nUR?                  SS9  UR]                  5         SUl        SUl        UR_                  UUS9nURa                  [b        5      n U c  Un OEURe                  UR<                  R!                  5       5      n![g        URi                  U 4U!5      5      n [k        [m        UR<                  U 5      5      $ s  snf s  snnf s  sn	nf s  snf )zE
Return the dictionary with the values of the existing aggregations.
FTNallow_joinsreuse	summarizez!%s is not an aggregate expressionc              3   X   >#    U  H  n[        TR                  U   S S5      v   M!     g7f)contains_subqueryFNr   r   r9   refry   s     r;   r<   (Query.get_aggregation.<locals>.<genexpr>  s0      !)C ((-/BEJJ)   '*c              3   X   >#    U  H  n[        TR                  U   S S5      v   M!     g7f)contains_over_clauseTNr  r  s     r;   r<   r    s0      )C ((-/EtLL)r  c              3   <   #    U  H  n[        US S5      v   M     g7f)contains_aggregateTN)r   )r9   
annotations     r;   r<   r    s$      ";J 
$8$??";s   r   )AggregateQueryforce)resolve_refsrU   __colr   c              3   0   #    U  H  o[         L v   M     g 7fr4   )NotImplemented)r9   r   s     r;   r<   r  b  s     VEU66EUs   )r   )7r   r   check_aliasrM   r  	TypeErrorappend_annotation_maskget_refsanyreplace_expressionsr   r   r   set_annotation_maskr   split_having_qualifyr   rF   group_byrO   	is_sliceddistinct
combinatordjango.db.models.sql.subqueriesr  r}   subqueryr   select_for_updater   rx   clear_orderingdefault_colsr   pkr	  get_initial_aliasr>   get_group_by_colsadd	_gen_colsgetr   r   r   empty_result_set_valueclear_limitsr   execute_sqlr#   get_convertersr   apply_convertersr   zip)"ry   rs   aggregate_exprsrefs_subqueryrefs_windowreplacementsr   r   aggregate_expr	aggregateaggregate_refs
aggregates_havingqualifyhas_existing_aggregationr  inner_queryouter_queryannotation_maskrI   annotation_aliasr  col_refscolcol_refindex	col_alias
expressionempty_set_resultr   r   r   
converterss"   `                                 r;   get_aggregationQuery.get_aggregation  s%    I !%!<!<%4%:%:%<!EU#&99$dd : I // Ce KLL ''0&//1NS !)!  M 3 )  K "55lCI&/DU#2;U./+ &=0 GVVoUT--11%88o
V  !78 "ZZ<<>7 "&"2"2"9"9";    	!$ t}}e,,~~'}}F**,K#'K ([AK,1K)).K&++D,B,BC &&U&3''' ++0H

((++33K4Q4Q4ST,K( ,1(t
 '*eO!$--77$(MMD+t}}>O %2%/%6%6%8	'9+=+=+?? &9 9=8N8N8T8T8V4(*%7799+//0@A 9W  33OD H$.$4$4$6 !>>9+E>JC'/||C'88G8 #H 1&+E7O	"%i"5(/=@//	:#::I;G(/L% K 2;1N1N 2''. %7 ""b(#00#:: JJ$$''//0M0M0OP&" KDK %DDJ .2-=-=-C-C-E -E)z z*J6-E    -7,<,<,>$,>( 88FF,>$ 
 $. $$Z0 *;;BBD
D
 --D 	 
 VEUVVV""".  "(-%%*"++E{+K%%f->%F!001N1N1U1U1WXJ(33VIzJKFC55v>??] Wb $
s   ;%Z",Z'Z-7Z3c                 b    U R                  5       nUR                  US[        S5      05      S   $ )z?
Perform a COUNT() query using the current filter constraints.
__count*)r}   rY  r   )ry   rs   r   s      r;   	get_countQuery.get_countq  s0     jjl""59eCj*AB9MMrC   c                     U R                   $ r4   )r   r   s    r;   has_filtersQuery.has_filtersx  s    zzrC   c                 8   U R                  5       nUR                  (       a  UR                  (       de  UR                  SL aF  UR	                  S U R
                  R                  R                   5       S5        UR                  SS9  UR                  5         UR                  (       a1  UR                  S:X  a!  [        S UR                   5       5      Ul
        UR                  SS9  U(       a  UR                  SS	9  UR                  [!        S5      S
5        U$ )NTc              3   8   #    U  H  oR                   v   M     g 7fr4   r7   r8   s     r;   r<   Query.exists.<locals>.<genexpr>  s     I(H1YY(H   Fallow_aliasesunionc              3   @   #    U  H  nUR                  S S9v   M     g7f)F)limitN)exists)r9   combined_querys     r;   r<   rf    s%      '&8N %%E%2&8s   r  rU   )higha)r}   r.  r-  r,  
add_fieldsr   r   concrete_fieldsset_group_byclear_select_clauser   r/  rO   r3  
set_limitsadd_annotationr   )ry   rl  rP   s      r;   rm  Query.exists{  s    JJL

q{{zzT!I

(8(8(H(HI5
 U3!!#!,,'"9!& '&'&8&8' "A 	
t$LLaL 	q3'rC   c                 b    U R                  U5      nUR                  US9nUR                  5       $ )Nrs   )rm  r   has_results)ry   rs   rP   r   s       r;   rz  Query.has_results  s/    KK>>>.##%%rC   c                    U R                  5       nU H3  n[        R                  U5      (       a  SU;   d  M%  [        SU< S35      e   [	        X#5      Ul        UR                  US9nSR                  UR                  5       5      $ )Nz--zInvalid option name: .ry  
)	r}   EXPLAIN_OPTIONS_PATTERN	fullmatchr\   r   explain_infor   joinexplain_query)ry   rs   r   r   rP   option_namer   s          r;   explainQuery.explain  s{    JJL"K+55kBB;& #8q!IJJ # %V5>>>.yy//122rC   c                   ^  T R                   UR                   :w  a  [        S5      eT R                  (       a  [        S5      eT R                  UR                  :w  a  [        S5      eT R                  UR                  :w  a  [        S5      eT R                  5       nUR                  5       nUR                  T U1S9  0 nU[        :H  nU(       a
  [        5       O[        T R                  5      n[        USS5      nUR                  U 4S jT R                   5       5        [        5       n[        UR                  5      S	S
 n	U	 H  n
UR                  U
   nUR                  U5      nT R                  XS9nUR                   ["        :X  a  UR%                  U5        UR'                  U5        X:w  a  XU
'   UR(                  U
   (       a  M  T R+                  U5        M     UR                  U5        UR-                  T 5        T =R.                  UR.                  -  sl        UR0                  R                  5       nUR3                  U5        T R0                  R%                  X5        UR4                  (       a;  T R7                  UR4                   Vs/ s H  oR                  U5      PM     sn5        OST l        U[8        :X  a-  T R:                  (       a  UR:                  (       a  [=        S5      eT R:                  R?                  UR:                  5        [        5       nT R@                  b  UR?                  T R@                  5        UR@                  b  UR?                  UR@                  5        U(       a  T RC                  U5        T =RD                  URD                  -  sl"        URF                  =(       d    T RF                  T l#        URH                  =(       d    T RH                  T l$        g
s  snf )a  
Merge the 'rhs' query into the current one (with any 'rhs' effects
being applied *after* (that is, "to the right of") anything in the
current query. 'rhs' is not modified during a call to this function.

The 'connector' parameter describes how to connect filters from the
'rhs' query.
z4Cannot combine queries on two different base models.z3Cannot combine queries once a slice has been taken.z6Cannot combine a unique query with a non-unique query.z6Cannot combine queries with different distinct fields.)exclude   Fc              3   n   >#    U  H*  nTR                   U   R                  [        :X  d  M&  Uv   M,     g 7fr4   )r   	join_typer    )r9   jry   s     r;   r<    Query.combine.<locals>.<genexpr>  s,      
%!):)D)D)MAA~s   %5	5rU   Nr  r   zSWhen merging querysets using 'or', you cannot have extra(select=...) on both sides.)%r   r%  r-  r.  distinct_fieldsr6  r}   bump_prefixr(   r>   r   JoinPromoter	add_votesr   r  r  r  r    r8  discardr   unref_aliasupdate_join_typesr   r   relabel_aliasesr   
set_selectr)   r   r\   updater   set_extra_maskextra_tablesorder_byextra_order_by)ry   rR   rd   initial_aliasr  conjunctionr  joinpromoter	rhs_votes
rhs_tablesr   r  	new_aliaswrR  r   s   `               r;   combineQuery.combine  s%    ::"RSS>>QRR==CLL(TUU3#6#66TUU ..0iik}o6 
3& %#dnn*=#Iq%8 
~~
 	
 E	 #--(,
E=='D ''
3D		$	4I~~&i( MM)$!$-5!%%e,,
   +'  ( 	y)&&t, 	S--- IIOO	*%

q$ ::OO

S
00<
STDK? zzcii 7  	

#))$E!!-$$T%;%;<  ,$$S%:%:; 12S--- 5!00GD4G4G5 Ts   O1c                    Uc  0 n0 X1R                   '   UR                  UR                  -    H  nUR                  UR                  S 5      nS n[        USS 5      =n(       a  UR                  US 5      nUc  Uc  UR                  U0 5        Ma  U(       d  Mj  UR                  (       d  [        [        [        U5      5      5      eUR                  (       a  M  UR                  U0 5      nUR                  R                  R                  n	U R                  U	R                  XX5        M     UR!                  5        H  u  pU R"                  R%                  U
5      =n(       al  UR'                  UR(                  5      nUR                  X40 5      nUR                  R                  R                  n	U R                  U	R                  XX5        M  UR'                  U
5        M     U$ )Nr7   )r5  rr  related_objectsr   r6   r   
setdefaultis_relationr   r   r   many_to_manyrelated_modelr   concrete_model_get_defer_select_maskr   r   r:  	get_fieldrelation_name)ry   rA   maskselect_maskr   
field_maskfield_att_maskfield_attnamefield_select_maskr  
field_namefiltered_relationrelations                r;   r  Query._get_defer_select_mask  s   K!GG
 ))D,@,@@E%**d3J!N 'y$ ??}?!%-!>!n&<&&ub1(($T$z*:%;<< %%$/$:$:5"$E! % 3 3 9 9 H H++!''' A0 '+jjl"J$($<$<$@$@$LL L>>*;*I*IJ$/$:$:J;QSU$V! ( 6 6 < < K K++!'' z* '3 rC   c                    Uc  0 n0 X1R                   '   UR                  5        H  u  pEUR                  U5      nUR                  U0 5      nU(       d  M1  UR                  (       d  [        [        [        U5      5      5      eUR                  R                  R                  R                  nU R                  UR                  XW5        M     U$ r4   )r5  r   r  r  r  r   r   r   remote_fieldr   r   r  _get_only_select_mask)	ry   rA   r  r  r  r  r   r  r  s	            r;   r  Query._get_only_select_maskB  s    K!GG&*jjl"JNN:.E + 6 6ub Az(($T$z*:%;<< % 2 2 8 8 > > M M**!'' '3 rC   c                    U R                   u  pU(       d  0 $ 0 nU H3  nUnUR                  [        5       H  nUR                  U0 5      nM     M5     U R	                  5       nU(       a  U R                  Xs5      $ U R                  Xs5      $ )a  
Convert the self.deferred_loading data structure to an alternate data
structure, describing the field that *will* be loaded. This is used to
compute the columns to select from the database and also by the
QuerySet class to work out which fields are being initialized on each
model. Models that have all their fields included aren't mentioned in
the result, only those that have field restrictions in place.
)deferred_loadingsplitr   r  r   r  r  )ry   field_namesdeferr  r  	part_maskpartrA   s           r;   get_select_maskQuery.get_select_maskS  s     "22I%JI"((4%00r:	 5 & }}..t::))$55rC   c                 |   U R                   R                  U5      nU(       d'  U(       a   US   nU R                  U==   S-  ss'   US4$ U(       a9  SU R                  [	        U R
                  5      S-   4-  nUR                  U5        O!Ub  UR                  OUnU/U R                   U'   SU R                  U'   US4$ )z
Return a table alias for the given table_name and whether this is a
new alias or not.

If 'create' is true, a new alias is always created. Otherwise, the
most recently created alias for the table (if one exists) is reused.
r   rU   F%s%dT)r   r:  r   alias_prefixr   r   appendr   )ry   
table_namerc   r  
alias_listr   s         r;   table_aliasQuery.table_aliasi  s     ^^''
3
*qME&!+&%< d//T^^1Dq1HIIEe$ ,=+H!''j  +0DNN:&%&E"d{rC   c                 2    U R                   U==   S-  ss'   g)z-Increases the reference count for this alias.rU   Nr   r   s     r;   	ref_aliasQuery.ref_alias  s    E"a'"rC   c                 2    U R                   U==   U-  ss'   g)z-Decreases the reference count for this alias.Nr  )ry   r   amounts      r;   r  Query.unref_alias  s    E"f,"rC   c                   ^ ^^ [        T5      mT(       Ga3  TR                  S5      mT R                  T   R                  c  M5  T R                  T   R                  c   eT R                  T   R                  nU=(       a     T R                  U   R                  [
        :H  nT R                  T   R                  [
        :H  nT R                  T   R                  (       d  U(       aX  U(       dQ  T R                  T   R                  5       T R                  T'   TR                  UUU 4S jT R                   5       5        T(       a  GM2  gg)a  
Promote recursively the join type of given aliases and its children to
an outer join. If 'unconditional' is False, only promote the join if
it is nullable or the parent join is an outer join.

The children promotion is done to avoid join chains that contain a LOUTER
b INNER c. So, if we have currently a INNER b INNER c and a->b is promoted,
then we must also promote b->c automatically, or otherwise the promotion
of a->b doesn't actually change anything in the query results.
r   Nc              3   v   >#    U  H.  nTR                   U   R                  T:X  d  M"  UT;  d  M*  Uv   M0     g 7fr4   )r   parent_alias)r9   r  r   aliasesry   s     r;   r<   &Query.promote_joins.<locals>.<genexpr>  s@       .~~d+88EA  G+ D .s   !99	9)	r   r   r   r  r  r!   nullablepromoteextend)ry   r  r  parent_louteralready_louterr   s   ``   @r;   promote_joinsQuery.promote_joins  s     w-KKNE~~e$..6 >>%(22>>>>>%0==LQ!=!G!G6!Q  "^^E2<<FNu%..-(,u(=(E(E(Gu%   $ % grC   c                    [        U5      nU(       a  UR                  S5      nU R                  U   R                  [        :X  au  U R                  U   R                  5       U R                  U'   U R                  U   R                  nU R                  U   R                  [        :X  a  UR                  U5        U(       a  M  gg)a  
Change join type from LOUTER to INNER for all joins in aliases.

Similarly to promote_joins(), this method must ensure no join chains
containing first an outer, then an inner join are generated. If we
are demoting b->c join in chain a LOUTER b LOUTER c then we must
demote a->b automatically, or otherwise the demotion of b->c doesn't
actually change anything in the query results. .
r   N)	r   r   r   r  r!   demoter  r    r  )ry   r  r   r  s       r;   demote_joinsQuery.demote_joins  s     w-KKNE~~e$..&8(,u(=(D(D(Fu%#~~e4AA>>,/99UBNN<0 grC   c                     U R                   R                  5       R                  5        H*  u  p#X1R                  US5      -
  nU R	                  X$5        M,     g)zX
Reset reference counts for aliases so that they match the value passed
in `to_counts`.
r   N)r   r[   r   r:  r  )ry   	to_countsr   cur_refcountunref_amounts        r;   reset_refcountsQuery.reset_refcounts  sK    
 $(#6#6#;#;#=#C#C#EE'--q*AALU1 $FrC   c                    U(       d  U $ [        U5      R                  UR                  5       5      (       d   eU R                  R	                  U5        [        U R                  [        5      (       a9  [        U R                   Vs/ s H  o"R                  U5      PM     sn5      U l        [        U R                   Vs/ s H  o"R                  U5      PM     sn5      U l	        U R                  =(       a>    U R                  R                  5        VVs0 s H  u  p2X2R                  U5      _M     snnU l
        UR                  5        H  u  pEX@R                  ;  a  M  U R                  U   R                  U5      nX`R                  U'   U R                  U   U R                  U'   U R                  U	 U R                  U	 U R                  UR                     n[!        U5       H  u  pX:X  d  M  XWU'     M     M     U R"                  R                  5        V	V
s0 s H"  u  pUR%                  X5      U
=(       d    X;   _M$     sn
n	U l        gs  snf s  snf s  snnf s  sn
n	f )z
Change the aliases in change_map (which maps old-alias -> new-alias),
relabelling any references to them in select columns and the where
clause.
N)r>   
isdisjointr   r   r  rF   r,  rO   r  r   r   r   r   r   r   r  	enumerater   r:  )ry   r  rR  r   	old_aliasr  
alias_datatable_aliasesposr   aliaseds              r;   r  Query.change_aliases  s    K :))**;*;*=>>>> 	

"":.dmmU++!<@MMJMS$$Z0MJDM T00<TU++ 
 ,,2241
4 $$Z0041
 %/$4$4$6 I.	2BB:NJ(2NN9%-1-@-@-KD	*##I.y) NN:+@+@AM'6
%)2#& 7 %7" #'"7"7"="="?!
 #@ NN5(7+Ie6II"?!
/ KT1
(!
s   I?II)I c           
      \  ^  U 4S jnT R                   UR                   :w  a  g[        R                  " 5       S-  n[        U" 5       5       H.  u  pVUT R                  ;  a	  UT l           OXT:  d  M%  [        S5      e   T R                  R                  T R                   /5      T l        UR                  R                  T R                  5      Ul        Uc  0 nT R                  [        T R                  5       VVs0 s H  u  pWXr;  d  M  UST R                   U4-  _M      snn5        gs  snnf )a  
Change the alias prefix to the next letter in the alphabet in a way
that the other query's aliases and this query's aliases will not
conflict. Even tables that previously had no alias will get an alias
after this call. To prevent changing aliases use the exclude parameter.
c               3   
  >#    [         n [        [        TR                  5      S-   5      nUv   [	        S5       HD  nU(       a  X R                  U5      S OU n[        X2S9 H  nSR                  U5      v   M     SnMF     g7f)z
Generate a sequence of characters in alphabetical order:
    -> 'A', 'B', 'C', ...

When the alphabet is finished, the sequence will continue with the
Cartesian product:
    -> 'AA', 'AB', 'AC', ...
rU   N)repeat )r
   chrordr  r   rT  r	   r  )alphabetr_   nseqsry   s        r;   
prefix_gen%Query.bump_prefix.<locals>.prefix_gen  st      'HT../!34FL1X<Bh~~f578 /A''!*$ 0	 s   B BN   z6Maximum recursion depth exceeded: too many subqueries.r  )	r  sysgetrecursionlimitr  r   RecursionErrorrj  r  r   )ry   other_queryr  r  local_recursion_limitr  r_   r   s   `       r;   r  Query.bump_prefix  s   	$  8 88 !$ 5 5 72 =$Z\2KCT...$*!*$L  3 !--33T5F5F4GH#.#;#;#A#A$BSBS#T ?G #,DNN";";JC' 9v!2!2C 888";	
s   :D(
	D(
c                    U R                   (       a  U R                  nU R                  U5        U$ U R                  (       a;  U R	                  U R                  U R                  5       R                  S5      5      nU$ SnU$ )zN
Return the first alias for this query, after increasing its reference
count.
N)r   r   r  r   r  base_table_classr   db_tabler   s     r;   r6  Query.get_initial_alias3  sj    
 >>OOENN5!
 	 ZZIId33DMMO4L4LdSTE  ErC   c                     [        U R                  R                  5        Vs/ s H  o(       d  M  SPM     sn5      $ s  snf )z
Return the number of tables in this query with a non-zero reference
count. After execution, the reference counts are zeroed, so tables
added in compiler will not be seen by this method.
rU   )r   r   r   )ry   r   s     r;   count_active_tablesQuery.count_active_tablesA  s4     4#6#6#=#=#?I#?%5A#?IJJIs   
==c                 `   U R                   R                  5        VVs/ s H  u  p4Ub  X2;   d  M  XA:X  d  M  UPM     nnnU(       a5  UR                  U;   a  UR                  nOUS   nU R                  U5        U$ U R                  UR                  SUR
                  S9u  pxUR                  (       aO  U R                   UR                     R                  [        :X  d  UR                  (       a  [        n	O[        n	Xl        Xql        XR                   U'   UR
                  =n
(       a~  UnUb  [        U5      U1-  n[        U R                   5      nU
R                  XS9Ul        U[        U R                   5      :  a(  U R                   R                  U5      U R                   U'   U$ s  snnf )a  
Return an alias for the 'join', either reusing an existing alias for
that join or creating a new one. 'join' is either a base_table_class or
join_class.

The 'reuse' parameter can be either None which means all joins are
reusable, or it can be a set containing the aliases that can be reused.

A join is always created as LOUTER if the lhs alias is LOUTER to make
sure chains like t1 LOUTER t2 INNER t3 aren't generated. All new
joins are created as LOUTER if the join is nullable.
T)rc   r  r  )r   r   r  r  r  r  r  r  r!   r  r    r>   r   rM   r   )ry   r  r  rp  r  reuse_aliasesreuse_aliasr   rI  r  r  resolve_reuse	joins_lens                r;   r  
Query.joinI  s    ,,.
. 12 . 	 

 =0".. ,B/NN;' ##OODD<R<R $ 
 >>~~d//0::fD"	!	&N  $u $ 6 666!M( #E
eW 4DNN+I%6%I%I &J &D"
 3t~~..(,(:(:5(Au%O
s   F*F*F*c                    X$;   a  XB   $ UR                  U5      nU(       d  U$ UnU H  nXt;   a  UR                  nXG   nM  UR                  U   (       d  UR                  nM<  UR                  U5      nU R	                  UR
                  /Xc5      n	UR                  nU	R                  S   =o4U'   M     U=(       d    US   $ )aM  
Make sure the given 'model' is joined in the query. If 'model' isn't
a parent of 'opts' or if it is None this method is a no-op.

The 'alias' is the root alias for starting the join, 'seen' is a dict
of model -> alias of existing joins. It must also contain a mapping
of None -> some alias. This will be returned in the no-op case.
r  N)get_base_chainr   parentsget_ancestor_linksetup_joinsr6   rj   )
ry   rA   r   r   seenr   	curr_opts	int_model
link_field	join_infos
             r;   join_parent_modelQuery.join_parent_model  s     =;##E*L	I %OO	
 $$Y/%OO	"44Y?J((*//):IMI!I&/oob&99EO   "T
"rC   c                 N    [         R                  U5      (       a  [        S5      eg )NzbColumn aliases cannot contain whitespace characters, quotation marks, semicolons, or SQL comments.)FORBIDDEN_ALIAS_PATTERNsearchr\   r   s     r;   r$  Query.check_alias  s*    "))%00/  1rC   c                   ^ U R                  T5        UR                  U SSS9nU(       a  U R                  T/5        O:U4S j[        R	                  U R
                  5       5       nU R                  U5        XR                  T'   g)z0Add a single annotation expression to the Query.TN)r  r  c              3   8   >#    U  H  nUT:w  d  M  Uv   M     g 7fr4   r   )r9   valuer   s     r;   r<   'Query.add_annotation.<locals>.<genexpr>  s!      BEE> Bs   
	)r$  rM   r&  r   fromkeysrx   r*  r   )ry   r  r   r   rO  s     `  r;   rv  Query.add_annotation  sx    224TQU2V
''0!]]4+A+ABO
 $$_5",rC   c           
         U R                  5       nUR                  U5        SUl        UR                  R                  " U/UQ70 UD6  UR
                  (       a?  [        UR                   Vs/ s H  nUR                  " U/UQ70 UD6PM     sn5      Ul        UR                  R                  5        H_  u  pgUR                  " U/UQ70 UD6n[        US5      (       a%  UR                  R                  UR                  5        XR                  U'   Ma     UR                  R                  5        H  u  p[        U
[        5      =(       a-    U
R                   R"                  R$                  R&                  U	:g  =(       d0    [        U
[(        5      =(       a    U
R*                  U
R,                  :g  UR                  U	'   M     U$ s  snf )NTr   )r}   r  r1  r   rM   r/  rO   r   r   r   rG   r   r  r   rF   r&   
join_fieldr  r   r
  r$   r  r  )ry   r   argskwargsr}   rn  r   r(  resolvedr   tables              r;   rM   Query.resolve_expression  s   

% &&u>t>v>%* +0*@*@*@ #55eMdMfM*@&E"  ++113JC//GGGHx!344))001G1GH%-c"	 4 "OO113LE5$' K$$2288AAUJ- 5),V1A1AUEVEV1V	 ""5) 4 %s   - Gc                     [        U R                  R                  5       U R                  R                  5      nU R                  USS9 Vs/ s H!  nUR                  U R                  ;   d  M  UPM#     sn$ s  snf )NT)include_external)r   r   r   r   rN   r9  r   r   )ry   exprsrR  s      r;   get_external_colsQuery.get_external_cols  sm    d&&--/1D1DE ~~ed~C
CyyD111 C
 	
 
s   A6-A6c                 l    U R                  5       n[        S U 5       5      (       a  U=(       d    U /$ U$ )Nc              3   8   #    U  H  oR                   v   M     g 7fr4   )possibly_multivalued)r9   rR  s     r;   r<   *Query.get_group_by_cols.<locals>.<genexpr>  s     A=C''=rg  )r6  r(  )ry   wrapperexternal_colss      r;   r7  Query.get_group_by_cols  s6    
 ..0A=AAAOt$$rC   c                 ,   U R                   (       aL  UR                  R                  (       d1  U R                  SS9  U R                   H  nUR                  SS9  M     U R                  US9R                  5       u  pEU R                   (       a  SU-  nXE4$ )NFr  )r   z(%s))r1  r   *ignores_unnecessary_order_by_in_subqueriesr3  r   r   r   )ry   r   r   r   rr   rq   s         r;   r   Query.as_sql  s     MM''RRe,..$$5$1 /'':'>EEG==3,C{rC   c                    ^ ^^^ [        US5      (       a  UR                  T TTTS9nU$ [        U[        [        45      (       a8  UUU U4S jU 5       n[        U5      n[        US5      (       a  U" U6 $ U" U5      $ U$ )NrM   )r  r  r  c              3   L   >#    U  H  nTR                  UTTT5      v   M     g 7fr4   )resolve_lookup_value)r9   	sub_valuer  	can_reusery   r  s     r;   r<   -Query.resolve_lookup_value.<locals>.<genexpr>  s,      !&I )))YYWW!&s   !$_make)rG   rM   rF   r   rO   type)ry   r(  rF  r  r  r   type_s   ` ```  r;   rD  Query.resolve_lookup_value  s    5.//,,'#	 - E"  e}--!&F KEug&&f~%= rC   c                    UR                  [        5      nU R                  (       aD  [        X0R                  5      u  pEU(       a&  U R                  U   nU(       a  [	        XF5      nUSU4$ U R                  X0R                  5       5      u  pxpyUS[        U5      [        U	5      -
   n
[        U	5      S:  a=  U
(       d6  [        SU< SU R                  5       R                  R                  < S35      eXS4$ )zH
Solve the lookup type from the lookup (e.g.: 'foobar__id__icontains').
r   r   rU   zInvalid lookup "z" for model ".F)r  r   r   r   r   names_to_pathr   r   r   r   r   )ry   lookupr  lookup_splittedr  expression_lookupsrV  rI  r   lookup_partsfield_partss              r;   solve_lookup_typeQuery.solve_lookup_type	  s     !,,z2-<!1!1.*J !--j9
!$Z!<J)2z99$($6$6$X!!%a#o*>\AR*RS|q 4==?0099;  %//rC   c                     [        US5      (       aD  [        UR                  R                  X#5      (       d  [	        SU< SUR
                  < S35      egg)z
Check whether the object passed while querying is of the correct type.
If not, raise a ValueError specifying the wrong object.
r   zCannot query "z": Must be "z" instance.N)rG   r   r   r   r\   object_name)ry   r(  rA   r   s       r;   check_query_object_typeQuery.check_query_object_type   sP    
 5'""1%++2C2CTQQ d..0  R #rC   c                    UR                   (       a  [        U[        5      (       ah  UR                  (       dW  [	        UR
                  X15      (       d<  [        SUR
                  R                  R                  < SUR                  < S35      e[        US5      (       a  U R                  X#U5        g[        US5      (       a  U H  nU R                  XCU5        M     ggg)z3Check the type of object passed to query relations.zCannot use QuerySet for "z": Use a QuerySet for "rM  r   r   N)r  rF   r0   has_select_fieldsr   r   r\   r   rW  rG   rX  )ry   r   r(  rA   vs        r;   check_related_objectsQuery.check_related_objects,  s     5%((//6u{{DPP {{((44d6F6FH  ((,,U%@
++A00%@  ,# rC   c                    [        US5      (       a4  [        USS5      (       d"  [        UR                  R                  S-   5      e[        US5      (       a)  UR                  5        H  nU R                  U5        M     gg)z>Raise an error if expression cannot be used in a WHERE clause.rM   
filterableTz$ is disallowed in the filter clause.r^   N)rG   r   r   r   r   r^   check_filterable)ry   rV  rI   s      r;   ra  Query.check_filterableC  s    :344Wd>
 >
 $$$-- 1   :788"99;%%d+ < 9rC   c                 $   U=(       d    S/Gt pEU H  nU R                  X&5      nM     UR                  U5      nU(       d,  U R                  X%5      nSnUR                  U5      nU(       d  gU" X#5      nUR                  c:  UR                  (       d)  US;  a  [	        S5      eUR                  S5      " US5      $ US:X  aN  UR                  S:X  a>  [
        [           R                  R                  (       a  UR                  S5      " US5      $ U$ )a  
Try to extract transforms and lookup from given lhs.

The lhs value is something that works like SQLExpression.
The rhs value is what the lookup is going to compare against.
The lookups is a list of names to extract using get_lookup()
and get_transform().
exactN)rd  iexactz Cannot use None as a query valueisnullTr  )	try_transform
get_lookuprR   can_use_none_as_rhsr\   r   r   r   !interprets_empty_strings_as_nulls)	ry   lookupsrQ   rR   
transformslookup_namer6   lookup_classrO  s	            r;   build_lookupQuery.build_lookupP  s     $+#7wi D$$S/C  ~~k2 $$S6C!K>>+6Lc' ::f&@&@"55 !CDD>>(+C66 7"

b ,-66XX>>(+C66rC   c                 H   UR                  U5      nU(       a  U" U5      $ UR                  R                  n[        R                  " X!R                  R                  5       5      nU(       a  SSR                  U5      -  nOSn[        SU< SUR                  < SU< 35      e)zh
Helper method for build_lookup(). Try to fetch and initialize
a transform for name parameter from lhs.
z, perhaps you meant %s?z or r}  zUnsupported lookup 'z' for z# or join on the field not permitted)	get_transformr   r   difflibget_close_matchesget_lookupsr  r   r   )ry   rQ   r6   transform_classr   suggested_lookups
suggestions          r;   rg  Query.try_transform~  s    
 ++D1"3''++55L ' 9 9&&224! !6EV9WW
 
!%|'<'<jJ rC   c
                 
   [        U[        5      (       a  [        S5      e[        U[        5      (       a  U R	                  UUUUUUUUU	S9	$ [        US5      (       ag  [        USS5      (       d  [        S5      eUR                  XXHS9n
[        U
[        5      (       d  U R                  S/U
S	5      n
[        U
/[        S
9/ 4$ Uu  pU(       d  [        SU-  5      eU R                  X5      u  pnU(       a  U R                  U5        U(       d  [        U5      S:  a  [        S5      eU R                   R#                  5       nU R%                  XXX5      nU R                   R'                  5        VVs1 s H   u  nnUUR)                  US5      :  d  M  UiM"     nnnU(       a  U R                  U5        U(       a#  U R                  XU5      n
[        U
/[        S
9/ 4$ U R+                  5       nU R-                  5       nU(       + =(       d    U(       + n U R/                  UUUUUS9n[        U[0        5      (       a  [3        U5      nU R5                  UR6                  UUR8                  5        UR:                  U l        URE                  UR:                  5        U RG                  URH                  UR:                  URJ                  5      u  nnnUb  URE                  U5        UR6                  RL                  (       aS  [        U5      S:X  a!  U RO                  US   UR6                  U5      nOC[Q        UUURH                  UR6                  5      nO U RO                  US   UR6                  U5      nU R                  UUU5      n
U
RR                  n[        U
/[        S
9nUS:H  =(       a    U
RT                  S	L =(       a    U(       + nU(       Ga*  US:w  d  U
RT                  SL Ga  U
RT                  Gb  S	nUS:w  a  U RW                  US   5      (       d$  U RX                  US      RZ                  [\        :X  aT  US   R_                  S5      nU RO                  US   URH                  S   U5      nURa                  U" US5      [        5        [        U[b        5      (       aX  U RW                  URd                  5      (       a8  URd                  R_                  S5      nURa                  U" US5      [        5        UU(       d  U4$ S4$ s  snnf ! [>         a&  nU RA                  XURB                  5      s SnA$ SnAff = f)a  
Build a WhereNode for a single filter clause but don't add it
to this Query. Query.add_q() will then add this filter to the where
Node.

The 'branch_negated' tells us if the current branch contains any
negations. This will be used to determine if subqueries are needed.

The 'current_negated' is used to determine if the current filter is
negated or not and this will be used to determine if IS NULL filtering
is needed.

The difference between current_negated and branch_negated is that
branch_negated is set on first negation, but current_negated is
flipped for each negation.

Note that add_filter will not do any negating itself, that is done
upper in the code by add_q().

The 'can_reuse' is a set of reusable joins for multijoins.

The method will create a filter clause that can be added to the current
query. However, if the filter isn't added to the query then the caller
is responsible for unreffing the joins used.
z"Cannot parse keyword query as dict)branch_negatedcurrent_negatedr   r  
split_subqra  r  r  rM   conditionalFz3Cannot filter against a non-conditional expression.r  rd  T)rd   zCannot parse keyword query %rrU   7Joined field references are not permitted in this queryr   )rF  
allow_manyNrf  r  r   )3rF   r   r   r   _add_qrG   r   r%  rM   r   ro  r,   r(   rT  ra  r   r   r[   rD  r   r:  r   r6  r  r   r   r]  rh   rA   rj   _lookup_joinsr'   split_excludenames_with_pathr  
trim_joinsri   rk   r  r
  r   rm  rR   is_nullabler   r  r!   rh  r8  r   r   ) ry   filter_exprr{  r|  rF  r  r}  ra  r  r  	conditionargr(  rk  partsreffed_expression	pre_joinskr\  
used_joinsrA   r   r  r  eri   	join_listrR  lookup_typeclauserequire_outerrn  s                                    r;   build_filterQuery.build_filter  s   J k4((ABBk1%%;;- /&'%!1#"3  
 
 ; 455;u== UVV#66Y 7 I i00 --wiDI	i[C8"<< 
<sBCC,0,B,B3,R))!!"34s5zA~VWW'',,.	))%KS--335
5$!QY]]1a=P9PA5 	 
 !!%())'eLIi[C8"<<}}&&(''9z>
	Q((#% ) I %**U&&y'<'<eY^^T "+D 	)//*$(OOy	%
!	  Y'  ,,7|q mmGAJ	0E0EuM$7I$5$5y7L7L --
I,A,A5IC%%gsE:	++I;#6 8#U	(=UoBU 	 (IMMU,B) Mh& $$WQZ00~~im4>>&H#*1:#8#8#BL--
I4E4Ea4H%PCJJ|C7= eS))d.>.>u||.L.L#(<<#:#:8#DLJJ|E593?z>>B>>s
@  	Q%%ka>O>OPP	Qs+   4T	T	A+T 
T?T:4T?:T?c                 :    U R                  [        X45      5        g r4   )add_qr   )ry   
filter_lhs
filter_rhss      r;   
add_filterQuery.add_filter8  s    

1j-./rC   c                 4   U R                    Vs1 s H'  o R                   U   R                  [        :X  d  M%  UiM)     nnU R                  XR                  5      u  pEU(       a   U R
                  R                  U[        5        U R                  U5        gs  snf )zW
A preprocessor for the internal _add_q(). Responsible for doing final
join promotion.
N)	r   r  r    r  r   r   r8  r(   r  )ry   q_objectrp  existing_innerr  rI  s         r;   r  Query.add_q;  sx     ~~
%!):)D)D)MA~ 	 
 KK*;*;<	JJNN63'.)
s
   $BBc                 (    U R                  USS9S   $ )NF)r  r   )r  )ry   r  s     r;   build_whereQuery.build_whereN  s      % @CCrC   c                 "    [        5       U l        g r4   )r,   r   r   s    r;   clear_whereQuery.clear_whereQ  s    [
rC   c
                    UR                   n
XAR                  -  nU=(       d    UR                  n[        XR                  S9n[        UR                   [	        UR
                  5      U5      nUR
                   HG  nU R                  UUUUUUUUU	S9	u  pUR                  U5        U(       d  M6  UR                  X5        MI     U	(       a  UR                  U 5      nX4$ / nX4$ )z%Add a Q-object to the current filter.)rd   re   )rF  r{  r|  r  r}  ra  r  r  )
rd   re   r,   r  r   rN   r  r  r8  r  )ry   r  r   r{  r|  r  r}  ra  r  r  rd   target_clauser  rJ   child_clauseneeded_inners                   r;   r  Query._add_qT  s     &&	+++';8+;+;!I?O?OP#H$5$5 6
 &&E)-):):&- /'%!1#"3 *; 
*&L ""<0|!!,: ' '99$?L ** L**rC   c           	      V   X!l         U R                  UR                  5      u  p4nU(       a  [        SUR                  -  5      e[	        UR
                  5       H  nU R                  U5      u  pxnU(       d  SOSn	US U	*  n
[        U
5       HX  u  p[        U5      U:  a(  XK   U:w  a  [        SUR                  < SU< S35      eM<  [        SU< SUR                  < S35      e   M     [        UR                  UUR
                  5      Ul        XR                  UR                   '   g )	NzAFilteredRelation's relation_name cannot contain lookups (got %r).r  rU   zCFilteredRelation's condition doesn't support relations outside the z (got z).zaFilteredRelation's condition doesn't support nested relations deeper than the relation_name (got z for )
r   rT  r  r\   rH   r  r  r   rW   r   )ry   r  r   relation_lookup_partsrelation_field_partsrI  rO  rR  lookup_field_partsshiftlookup_field_pathidxlookup_field_parts                r;   add_filtered_relationQuery.add_filtered_relation}  sF   "'9=9O9O++:
6Q !/==>  **;*E*EFF262H2H2P/La)AqE 27UF ;*34E*F&+,s2+04EE(  1>>H  F % #)*;*I*IK  +G	 G$ ';++'''
#
 =N  !2!8!89rC   c           	      H   / / pe[        U5       GH  u  pxU/ 4n	US:X  a  Ub  UR                  R                  nSn
Sn Uc  [        eUR	                  U5      n
U
bR  U
R                  (       a  U
R                  (       d  [        SU-  5      e U
R                   R"                  R$                  nOeUS-  nUS:X  d  U(       aP  [)        / [+        U5      QU R
                  QU R                  Q5      n[        SU< SS	R-                  U5      < 35      e  GOUbZ  XR                   LaL  UR/                  U5      nU(       a4  UR                  U5        U	S   R                  U5        US   R0                  n[3        U
S
5      (       a  U(       a  U
R5                  U5      nOU
R6                  nU(       d`  [        U5       HQ  u  nnUR8                  (       d  M  U	S   R                  USUS-    5        UR;                  U	5        [=        US-   U5      e   US   nUR                  U5        UR>                  nUR0                  nUR@                  nU	S   R                  U5        UR;                  U	5        GM^  U
nU
4nU(       a+  US-   [C        U5      :w  a  [        SXS-      < SU< S35      e  O   UWWUWS-   S 4$ ! [         a    XR
                  ;   a  U R
                  U   R                  n
 GNXR                  ;   a  US:X  a  U R                  U   n[        UR                  ;   aN  UR                  R                  [        5      nU R                  UUUU5      u  p  nUR                  USS 5         GNUR	                  UR                  5      n
 GN6f = f! [&         a    Sn GNf = f)a  
Walk the list of names and turns them into PathInfo tuples. A single
name in 'names' can generate multiple PathInfos (m2m, for example).

'names' is the path of names to travel, 'opts' is the model Options we
start the name resolving from, 'allow_many' is as for setup_joins().
If fail_on_missing is set to True, then a name that can't be resolved
will generate a FieldError.

Return a list of PathInfo tuples. In addition return the final field
(the last used join field) and target (which is a field guaranteed to
contain the same value as the final field). Finally, return those names
that weren't found (which are likely transforms and the final lookup).
r5  Nr   r  zField %r does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.rU   zCannot resolve keyword 'z' into field. Choices are: , 
path_infosCannot resolve keyword z into field. Join on 'z' not permitted.)"r  r5  r6   r   r  rx   r   r   r   r  r  rN  r  r  r  r   r   r   r  AttributeErrorsortedrB   r  get_path_to_parentto_optsrG   get_path_infor  m2mr  r'   r-  target_fieldsr   )ry   namesrA   r  fail_on_missingrk   r  r  r6   cur_names_with_pathr   r  r  filtered_relation_pathrI  r   	availablepath_to_parent	pathinfos	inner_posplastrh   ri   s                           r;   rN  Query.names_to_path  s    !#Bo"5)IC#'*t| 0ww||E $P<++t,"   $$U-@-@$4 7;; !!KK--<<E q"9 &6t<!33 "55!I %-1499Y3GI   E$;!%!8!8!?!KK/'*11.A)"-55Dul++$ % 3 34E FI % 0 0I!(1)(<	1555/299)A	TU:VW+223FG"+C!G_"EE	 )=
 !}I&"oo||,,#A&--i8&&':; $ ("sQw#e*'<$-27^TC  C *D ['5q+;;;o % P111 2248EEE555#((,(@(@(F%!%6%D%DD 1 ? ? E Ej Q>B>P>P! &+	?;.q! $:3B$?@ $/@/N/N OP: & ! !E!s0   J4 N42N)BN/NNN! N!c                   ^ ^^ U/nU 4S jnSm[        [        U5      SS5       H!  n T R                  USU UUSS9u  n	mpXS n  O   W H'  nUUU 4S jn[        R
                  " XUS	9nSUl        M)     W	 H  nUR                  (       a'  UR                  R                  5       nUR                  nOSnSnUR                  nUR                  (       a  T R                  UR                  5      nOSnT R                  UR                  UU[         UR                  UUS
9nUR"                  (       a  UOSnT R%                  UUS9nUR'                  U5        M     [)        TW
X&X5      $ ! [         a  nUS:X  a  e Um SnAGM]  SnAff = f)a  
Compute the necessary table joins for the passage through the fields
given in 'names'. 'opts' is the Options class for the current model
(which gives the table we are starting from), 'alias' is the alias for
the table to start the joining from.

The 'can_reuse' defines the reverse foreign key joins we can reuse. It
can be None in which case all joins are reusable or a set of aliases
that can be reused. Note that non-reverse foreign keys are always
reusable when using setup_joins().

If 'allow_many' is False, then any reverse foreign key seen will
generate a MultiJoin exception.

Return the final field involved in the joins, the target field (used
for any 'where' constraint), the final 'opts' value, the joins, the
field path traveled to generate the joins, and a transform function
that takes a field and alias and is equivalent to `field.get_col(alias)`
in the simple case but wraps field transforms if they were included in
names.

The target field is the field containing the concrete value. Final
field can be something different, for example foreign key pointing to
that value. Final field is needed for example in some value
conversions (convert 'obj' in fk__id=obj to pk val using the foreign
key field for example).
c                 L   > TR                   (       d  S nU R                  U5      $ r4   r  )r   r   ry   s     r;   final_transformer,Query.setup_joins.<locals>.final_transformer=  s    ??==''rC   Nr   r  T)r  rU   c                   >  U" X5      nTR                  XB5      $ ! [         a     [        T[        5      (       a	  T(       a  Tee f = fr4   )rg  r   rF   r   )r   r   r6   previouswrappedrh   last_field_exceptionry   s        r;   	transform$Query.setup_joins.<locals>.transform[  sK    &u4G--g<<! !+u55:N22s	    *A)r6   r  )r  r  )ranger   rN  r   	functoolspartialhas_transformsr  r}   r   r  directr  r-  
join_classr
  r    r  r  r  rg   )ry   r  rA   r   rF  r  rj   r  pivotrk   ri   restrl  excr6   r  r  r  r  r  r   r  rh   r  s   `                     @@r;   r  Query.setup_joins  s   F 	(  $3u:q"-E373E3E&5M$(	 4F 40k7  #6]
' .( D	 !* 1 1/@! 04,! ( D%%$($:$:$@$@$B!/55$(!"<<D{{++DOO<"3 ) J "&ItEIIjI6ELL/ 0 Wd4SSq  /A: +.(/s   E((
F2	FFc                   ^
 USS n[        [        U5      5       GH  u  pE[        U5      S:X  d  UR                  (       d    OUR                  (       a    OUR
                  R                   Vs1 s H  ofR                  iM     nnU Vs1 s H  ofR                  iM     nnUR                  U5      (       d    OUR
                  R                   V	s0 s H+  n	U	S   R                  U;   d  M  U	S   R                  U	S   _M-     sn	m
[        U
4S jU 5       5      nU R                  UR                  5       5        GM!     XS   U4$ s  snf s  snf s  sn	f )a  
The 'target' parameter is the final field being joined to, 'joins'
is the full list of join aliases. The 'path' contain the PathInfos
used to create the joins.

Return the final target field and table alias and the new active
joins.

Always trim any direct join if the target column is already in the
previous table. Can't trim reverse joins as it's unknown if there's
anything on the other side of the join.
NrU   r   c              3   B   >#    U  H  nTUR                      v   M     g 7fr4   )column)r9   ttargets_dicts     r;   r<   #Query.trim_joins.<locals>.<genexpr>  s     DGqL2Gs   r  )r  reversedr   r  r  r-  foreign_related_fieldsr  issubsetrelated_fieldsrO   r  r   )ry   ri   rj   rk   r  infor  join_targetscur_targetsrr  s             @r;   r  Query.trim_joins  s     a"8D>2IC5zQdkk%%.2oo.T.TU.THH.TLU-45W88WK5''55 777AQ4;;+- "!QqT!7L
 DGDDGUYY[) 3  b	5(( V5s   .E	E
E+Ec              #     #    U H  n[        U[        5      (       a  Uv   M  U(       a5  [        [        USS 5      5      (       a  UR	                  5        S h  vN   MZ  [        US5      (       d  Mm  U(       d  [        U[        5      (       a  M  U R                  UR                  5       UUS9 S h  vN   M     g  Nc N7f)Nr6  r^   )r4  r   )	rF   r   callabler   r6  rG   r   r9  r^   )clsr5  r4  r   rI   s        r;   r9  Query._gen_cols  s     D$$$
!h148' '  11333788#
4(=(===//1%5!- )     4s+   AC B<C 2A C 2B>3
C >C c              #   P   #    S U R                  U5       5        S h  vN   g  N7f)Nc              3   8   #    U  H  oR                   v   M     g 7fr4   )r   )r9   rI   s     r;   r<   )Query._gen_col_aliases.<locals>.<genexpr>  s     @+?4JJ+?rg  )r9  )r  r5  s     r;   _gen_col_aliasesQuery._gen_col_aliases  s     @3==+?@@@s   &$&c                    U R                   R                  U5      nUb  U(       dF  U R                  U/5       H0  n[        U R                  U   [
        5      (       d  M'  [        S5      e   U(       a5  XR                  ;  a  [        SU-  5      e[        XR                  U   5      $ U$ UR                  [        5      nU R                   R                  US   5      nUb  USS   H  nU R                  XX5      nM     U$ U R                  XpR                  5       U R                  5       US9n	U R                  U	R                   U	R"                  U	R$                  5      u  pnU(       d  ['        U5      S:  a  [        S5      e['        U
5      S:  a  [        S5      eU	R)                  U
S   U5      nUb  UR+                  U5        U$ )Nr  zCCannot aggregate over the '%s' alias. Use annotate() to promote it.r   rU   )rF  z?Referencing multicolumn fields with F() objects isn't supported)r   r:  r  rF   r   r&   r   rx   r   r  r   rg  r  r   r6  r  ri   rj   rk   r   rl   r  )ry   r6   r  r  r  r  r   
field_listr  r  ri   final_aliasr  s                r;   resolve_refQuery.resolve_ref  s   %%))$/
!!22J<@E!$.."7>>(U  A
 
 555$)+/0  4!7!7!=>>!!J/J))--jm<J%!+ABI!%!3!3J!JJ "0!!((MMOT-C-C-EQV ) I /3oo!!9??INN/+G) 3y>A#5 M  7|a U 
 "44WQZMI Y'rC   c                 L   U R                  U R                  5      nU R                  Ul        Uu  pV[        U[        5      (       a  [	        U5      nO*[        U[
        5      (       a  [	        UR                  5      nUR                  XV5        UR                  SS9  UR                  U5      u  pxUR                  S   n	U	R                  n
U	R                  nX;   a  U
R                  R                  R                  nUR                  U 5        U
R!                  S5      nU" UR#                  UR                  S   R                  5      UR#                  U5      5      nUR$                  R'                  U[(        5        SUR*                  U'   OCU
R!                  S5      nU" U	[-        U5      5      nUR$                  R'                  U[(        5        U R/                  [1        U5      5      u  nnU(       a0  U R/                  SU-  S4SSUS9u  nnUR'                  U[2        5        UU4$ )aL  
When doing an exclude against any kind of N-to-many relation, we need
to use a subquery. This method constructs the nested query, given the
original exclude filter (filter_expr) and the portion up to the first
N-to-many relation field.

For example, if the origin filter is ~Q(child__name='foo'), filter_expr
is ('child__name', 'foo') and can_reuse is a set of joins usable for
filters in the original query.

We will turn this into equivalent of:
    WHERE NOT EXISTS(
        SELECT 1
        FROM child
        WHERE name = 'foo' AND child.parent_id = parent.id
        LIMIT 1
    )
Tr  r   rd  z
%s__isnull)r|  r{  rF  )r   r   r   rF   r   r   r6   r  r3  
trim_startr   r   r   r   r5  r  rh  r	  r   r8  r(   r   r   r  r   r)   )ry   r  rF  r  r   r  r  trimmed_prefixcontains_louterrR  select_fieldr   r5  rn  rO  r  r  or_null_conditionrI  s                      r;   r  Query.split_exclude  s   ( tzz*$($<$<!!,
j(++!*-J
A&&!*//2J04( +0*:*:?*K'll1ozz		##)),,B d#'227;L ""**U\\!_-B-B"CRZZPUEVWFKKOOFC(,0E""5)'227;L!#'7'GHFKKOOFC("&"3"3F5M"B	<#'#4#4.5 $##	 $5 $ q MM+R0 ,&&rC   c                     U R                   R                  [        5       [        5        U R                   H  nUR                  5         M     g r4   )r   r8  r+   r(   r   	set_empty)ry   r   s     r;   r  Query.set_empty4  s0    

{}c***EOO +rC   c                 N    [        S U R                  R                   5       5      $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr4   )rF   r+   )r9   rf   s     r;   r<   !Query.is_empty.<locals>.<genexpr>:  s     K7J!:a--7Js   )r(  r   rN   r   s    r;   is_emptyQuery.is_empty9  s    Ktzz7J7JKKKrC   c                    UbJ  U R                   b)  [        U R                   U R                  U-   5      U l         OU R                  U-   U l         UbJ  U R                   b)  [        U R                   U R                  U-   5      U l        OU R                  U-   U l        U R                  U R                   :X  a  U R                  5         gg)aW  
Adjust the limits on the rows retrieved. Use low/high to set these,
as it makes it more Pythonic to read and write. When the SQL query is
created, convert them to the appropriate offset and limit values.

Apply any limits passed in here to the existing constraints. Add low
to the current low value and clamp both to any existing high value.
N)rv   minru   r  )ry   lowro  s      r;   ru  Query.set_limits<  s     ~~)!$T^^T]]T5I!J!%!5?~~) #DNNDMMC4G H $ 3==DNN*NN +rC   c                 "    Su  U l         U l        g)zClear any existing limits.rp   Nru   rv   r   s    r;   r<  Query.clear_limitsS  s    (/%t~rC   c                 J    U R                   S:g  =(       d    U R                  S L$ r   r  r   s    r;   r-  Query.is_slicedW  s    }}!?T^^4%??rC   c                 d    U R                   S L=(       a    U R                   U R                  -
  S:H  $ )NrU   )rv   ru   r   s    r;   has_limit_oneQuery.has_limit_one[  s)    ~~T)St~~/MRS.SSrC   c                 $    U R                   (       + $ )z
Return True if adding filters to this instance is still possible.

Typically, this means no limits or offsets have been put on the results.
)r-  r   s    r;   
can_filterQuery.can_filter^  s     >>!!rC   c                 r    SU l         SU l        SU l        U R                  S5        U R	                  S5        g)z%Remove all fields from SELECT clause.r   FN)r   r4  r   r  r*  r   s    r;   rt  Query.clear_select_clausef  s5    !#B  $rC   c                      SU l         SU l        g)z
Clear the list of fields to select (but not extra_select columns).
Some queryset types completely replace any existing list of select
columns.
r   Nr   values_selectr   s    r;   clear_select_fieldsQuery.clear_select_fieldsn  s     rC   c                 \    U =R                   U4-  sl         U =R                  U4-  sl        g r4   r  )ry   rR  r6   s      r;   add_select_colQuery.add_select_colw  s%    vtg%rC   c                 2    SU l         [        U5      U l        g )NF)r4  rO   r   )ry   colss     r;   r  Query.set_select{  s    !DkrC   c                     Xl         SU l        g)zG
Add and resolve the given fields to the query's "distinct on" clause.
TN)r  r.  )ry   r  s     r;   add_distinct_fieldsQuery.add_distinct_fields  s      +rC   c           	         U R                  5       nU R                  5       n / nU H  nU R                  UR                  [        5      XCUS9nU R                  UR                  UR                  UR                  5      u  pn
U H#  nUR                  UR                  X5      5        M%     M     U(       a  U R                  U5        gg! [         a    [        SW-  5      e[         ah    [        W;   a  e [        / [        U5      QU R                   QU R"                  QU R$                  Q5      n[        SU< SSR'                  U5      < 35      ef = f)z]
Add the given (model) fields to the select set. Add the field names in
the order specified.
)r  zInvalid field name: '%s'r  z into field. Choices are: r  N)r6  r   r  r  r   r  ri   rj   rk   r  rl   r  r'   r   r  rB   r   rx   r   r  )ry   r  	allow_m2mr   rA   r%  r6   r  ri   r  rj   r   r  s                r;   rq  Query.add_fields  s^   
 &&(}}$	D# !,,JJz*DI - 	 /3oo%%OONN/+e
 &FKK	 < <V QR & $ %  	@7$>?? 	T! 248 // 11	 !)-tyy/?A 	s   B$C B	Ec                 F   / nU H  n[        U[        5      (       a  US:X  a  M   UR                  S5      nX0R                  ;   a  MB  U R                  (       a  X0R                  ;   a  Md  U R                  UR                  [        5      U R                  R                  5        O"[        US5      (       d  UR                  U5        [        USS5      (       d  M  [        SU-  5      e   U(       a  [        SU-  5      eU(       a  U =R                  U-  sl        gSU l        g)	a  
Add items from the 'ordering' sequence to the query's "order by"
clause. These items are either field names (not column names) --
possibly with a direction prefix ('-' or '?') -- or OrderBy
expressions.

If 'ordering' is empty, clear all ordering from the query.
?-rM   r  Fz[Using an aggregate in order_by() without also including it in annotate() is not allowed: %szInvalid order_by arguments: %sN)rF   strremoveprefixr   r   rN  r  r   r   r   rG   r  r   r   r  default_ordering)ry   orderingerrorsitems       r;   add_orderingQuery.add_ordering  s     D$$$3;((-+++::$**"4 ""4::j#94::;K;KLT#788d#t1599 :<@A  & =FGGMMX%M$)D!rC   c                     U(       d4  U R                   (       d"  U R                  (       d  U R                  (       a  gSU l        SU l        U(       a  SU l        gg)z
Remove any ordering settings if the current query allows it without
side effects, set 'force' to True to clear the ordering regardless.
If 'clear_default' is True, there will be no ordering in the resulting
query (not even the model's default).
Nr   F)r-  r  r2  r  r  r2  )ry   r  clear_defaults      r;   r3  Query.clear_ordering  s@     NNd22d6L6L $)D! rC   c                    U(       a  U R                   (       a  0 n0 n[        U R                   U R                  5       H$  u  pE[        U[        5      (       a  XSU'   M   XRU'   M&     0 UEU R
                  EU l        U R                  U5        [        UR                  5       5      U l        [        U5      U l         [        U R                  5      nU R                  R                  5        Hc  u  pGUR                  5       =n(       d  M  U(       a-  UR                  (       d  UR                  [        XG5      5        MR  UR!                  U5        Me     [        U5      U l        g)a  
Expand the GROUP BY clause required by the query.

This will usually be the set of all non-aggregate fields in the
return data. If the database backend supports grouping by the
primary key, and the query would be equivalent, the optimization
will be made automatically.
N)r  r@  r   rF   r   r   r&  rO   r   r   rx   r   r7  r  r  r   r  r,  )	ry   ri  group_by_annotationsr  r   rI   r,  r  group_by_colss	            r;   rs  Query.set_group_by  s    T// $& M"4#5#5t{{CdC((+/%(26/	  D
  L"6K$:J:JKD''(<= 4 4 67DK!&}!5D$!%!7!7!=!=!?E%/%A%A%CCMCZ%B%BE 67. "@ hrC   c                     [        U R                  [        5      (       a  0 nOU R                  nU H3  nUnUR                  [        5       H  nUR                  U0 5      nM     M5     X l        g)z
Set up the select_related data structure so that we only select
certain related models (as opposed to all models, when
self.select_related=True).
N)rF   r   boolr  r   r  )ry   fields
field_dictr   dr  s         r;   add_select_relatedQuery.add_select_related		  sa     d))400J,,JEAJ/LLr* 0  )rC   c                    U(       a  0 nU(       a  [        U5      nO[        / 5      nUR                  5        H  u  pU R                  U	5        [        U
5      n
/ nU
R	                  S5      nUS:w  aH  US:X  d  XS-
     S:w  a  UR                  [        U5      5        U
R	                  SUS-   5      nUS:w  a  MH  X4Xy'   M     U R                  R                  U5        U(       d  U(       a)  U R                  R                  [        X45      [        5        U(       a  U =R                  [        U5      -  sl        U(       a  X`l        gg)zU
Add data to the various extra_* attributes for user-created additions
to the query.
z%sr  r   rU   %r  N)r   r   r$  r0  findr  r   r   r  r   r8  r*   r(   r  rO   r  )ry   r   select_paramsr   rq   tablesr  select_pairs
param_iterr6   entryentry_paramsr  s                r;   	add_extraQuery.add_extra	  s   
 
 L!-0
!"X
%||~  &E
!jj&Riax5q>S#8$++D,<=**T373C Ri ',%:"  . JJl+FJJNN:e4c:v."* rC   c                 &    [        5       S4U l        g)z0Remove any fields from the deferred loading set.TN)	frozensetr  r   s    r;   clear_deferred_loadingQuery.clear_deferred_loading:	  s    !*d 3rC   c                    U R                   u  p#U(       a  UR                  U5      S4U l         gUR                  U5      =n(       a
  US4U l         gU R                  5         [	        U5      R                  U5      =n(       a
  US4U l         gg)a:  
Add the given list of model field names to the set of fields to
exclude from loading from the database when automatic column selection
is done. Add the new field names to any existing field names that
are deferred (or removed from any existing field names that are marked
as the only ones for immediate loading).
TFN)r  rj  
differencerS  r>   )ry   r  existingr  new_existingnew_onlys         r;   add_deferred_loadingQuery.add_deferred_loading>	  s     //$,NN;$?$ED!  (22;??|?(4e(;%++-";/::8DD8D,4dND) ErC   c                 .   U R                   u  p#[        U5      nSU;   aD  UR                  S5        UR                  U R	                  5       R
                  R                  5        U(       a  UR                  U5      S4U l         g[        U5      S4U l         g)a  
Add the given list of model field names to the set of fields to
retrieve when the SQL is executed ("immediate loading" fields). The
field names replace any existing immediate loading field names. If
there are field names already specified for deferred loading, remove
those names from the new field_names before storing the new names
for immediate loading. (That is, immediate loading overrides any
existing immediate values, but respects existing deferrals.)
r5  FN)	r  r>   remover8  r   r5  r6   rV  rR  )ry   r  rW  r  s       r;   add_immediate_loadingQuery.add_immediate_loadingW	  s     //+&;t$OODMMO..334 %0$:$:8$De$KD! %.k$:E$AD!rC   c                 n    Uc  SU l         O#[        [        R                  U5      5      U l         SU l        g)z@Set the mask of annotations that will be returned by the SELECT.N)r   r   r   r*  r   ry   r  s     r;   r*  Query.set_annotation_masko	  s-    =*.D'*.t}}U/C*DD'(,%rC   c                 `    U R                   b!  U R                  / U R                   QUQ75        g g r4   )r   r*  ra  s     r;   r&  Query.append_annotation_maskw	  s2    &&2$$%Kt'B'B%KU%KL 3rC   c                 H    Uc  SU l         O[        U5      U l         SU l        g)z
Set the mask of extra select items that will be returned by SELECT.
Don't remove them from the Query since they might be used later.
N)r   r>   r   ra  s     r;   r  Query.set_extra_mask{	  s%    
 =%)D"%(ZD"#' rC   c                    SU l         U R                  5         U R                  5         SU l        U(       GaN  U H  nU R	                  U5        M     / n/ n/ nU R
                  (       d  U R                  (       d  [        U5      nOSU l        U H  nX`R                  ;   a  UR                  U5        M%  X`R                  ;   a  UR                  U5        MG  X`R                  ;   a  [        SU S35      eU R                  (       a9  U R                  UR                  [        5      U R                   R"                  5        UR                  U5        M     U R%                  U5        U R'                  U5        [)        X4-   U-   5      nODU R                   R"                  R*                   Vs/ s H  ofR,                  PM     nn[)        U5      nU R.                  SL aW  U R1                  S U R                   R"                  R*                   5       S5        U R3                  SS9  U R                  5         OU R.                  (       at  / nU R.                   HR  n	[5        U	[6        5      (       a)  U	R8                  U;  a  U R                  U	R8                     n	UR                  U	5        MT     [;        U5      U l        [;        U5      U l        U R1                  US5        g s  snf )NFTzCannot select the 'z&' alias. Use annotate() to promote it.c              3   8   #    U  H  oR                   v   M     g 7fr4   re  r8   s     r;   r<   #Query.set_values.<locals>.<genexpr>	  s     E$Dq$Drg  rh  )r   rS  r  r[  r$  r   r   r   r4  rw   r  rx   r   rN  r  r   r   r   r  r*  rR  rr  r7   r,  rq  rs  rF   r   refsrO   r  )
ry   rA  r   r  extra_namesannotation_namesr:   selectedr,  rI   s
             r;   
set_valuesQuery.set_values	  sT   ###%  "!%  '  KK!::d&6&6 #6l$)!A---#**1-444(//2...(1! 5* *   11 ..qwwz/BDJJDTDTU#**1-!  " ,$$%56 !:=M!MNH.2jj.>.>.N.NO.N99.NKO -H ==D OOEDJJ$4$4$D$DEu
 E2$$&]] HdC((TYYh-F++DII6D% & "(ODM";/T*/ Ps   'Kc                 .   U R                   b  U R                   $ U R                  (       d  0 $ U R                  bL  U R                   Vs0 s H#  nXR                  ;   d  M  XR                  U   _M%     snU l         U R                   $ U R                  $ s  snf )z
Return the dictionary of aggregate columns that are not masked and
should be used in the SELECT clause. Cache this result for performance.
)r   r   r   )ry   r  s     r;   rx   Query.annotation_select	  s     ((4000!!I((4 44-4A((( '##A&&4-D)
 000###-s   BBc                 :   U R                   b  U R                   $ U R                  (       d  0 $ U R                  bQ  U R                  R                  5        VVs0 s H  u  pXR                  ;   d  M  X_M     snnU l         U R                   $ U R                  $ s  snnf r4   )r   r   r   r   )ry   r  r\  s      r;   rw   Query.extra_select	  s    ##/+++zzI##/!%!1!1!3(!3q<R<R7R!3(D$ +++::(s   B0Bc                    / nU H  u  p4UR                  U5        M     SnU R                   Vs/ s H%  ofU R                  ;   d  X`R                  :X  d  M#  UPM'     nn[	        U5       HU  u  pU	R
                  (       a    OAU R                  XxS-         R                  [        :X  a  SnXx   n
U R                  U
5        MW     W	R                  R                  nWn/ nU H8  u  pU[        U	5      -
  S:  a    O#UR                  U5        U[        U	5      -  nM:     UR                  UR                  S   R                  5        [        R                   " U5      nU R                  XxS-         nUR                  [        :w  a  UR"                  (       dx  UR$                   Vs/ s H  nUS   PM
     nnXxS-      nU R                  Xx   5        UR'                  SXxS-      5      nU(       a   U R(                  R+                  U[,        5        O#UR$                   Vs/ s H  nUS   PM
     nnXx   nU R                   HN  nU R.                  U   S:  d  M  U R1                  U R                  U   R2                  U5      U R                  U'     O   U R5                  U Vs/ s H  nUR7                  U5      PM     sn5        X4$ s  snf s  snf s  snf s  snf )a  
Trim joins from the start of the join path. The candidates for trim
are the PathInfos in names_with_path structure that are m2m joins.

Also set the select column so the start matches the join.

This method is meant to be used for generating the subquery joins &
cols in split_exclude().

Return a lookup usable for doing outerq.filter(lookup=self) and a
boolean indicating if the joins in the prefix contain a LEFT OUTER join.
_FrU   Tr   N)r  r   r  r   r  r  r  r!   r  r-  r   r   r  r  r6   r   r  r  r  get_extra_restrictionr   r8  r(   r   r	  r  r  r	  )ry   r  	all_pathsrI  pathsr  r  lookup_tablestrimmed_pathsrk   r   r-  paths_in_prefixr  r6   
first_joinr  select_fieldsselect_aliasextra_restrictionr1  r:   s                         r;   r  Query.trim_start	  s    	'HAU# (
 ~~
%!d.@.@)@ADXA~ 	 
 $-Y#7Mxx~~mA,=>?IIVS"&!0EU# $8 __**
')JDT*Q.!!$'s4y(O	 *
 	j??BGGH#8 ^^M!2C$DE
6)*2N2N+5+D+DE+DaQqT+DME():;L]9: * @ @mA$56! !

0#6
 ,6+D+DE+DaQqT+DME(7L ^^E""5)A-(,(=(=NN5)44)u%  $ 	-H-Q<0-HI..o
< F F Is   "J=J=KKKc                     UR                   =(       d4    UR                  =(       a!    [        [           R                  R
                  $ )z
Check if the given field should be treated as nullable.

Some backends treat '' as null and Django treats such fields as
nullable for those backends. In such situations field.null can be
False even if we should treat the field as nullable.
)nullempty_strings_allowedr   r   r   rj  )ry   r   s     r;   r  Query.is_nullable5
  s9     zz 
'' Y,-66XX	
rC   ) r   r   r   r  r   r   r  r   r   r   r4  r2  r  r.  r  r   r   r  r   r,  r[  rv   ru   r   r  r   r   r   r   r   r  r   )T)NNTr4   )FN)rU   )F)FFNTTTFT)FFTTTFT)TF)NT)FT)TNF)NN)r   r   r   r   r   r  r;  rR  r   r   r$   r	  r&   r  r4  r2  standard_orderingr   r1  r   r,  r  ru   rv   r.  r  r2  select_for_update_nowaitselect_for_update_skip_lockedselect_for_update_ofselect_for_no_key_updater   r[  	max_depthr  r   r   r/  combinator_allr   r   r   r  r  r  r  rz   r   r   r-   r   r   r   r   r   r   r}   r   r  r
  rY  r^  ra  rm  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r6  r  r  r   r$  rv  rM   r6  r7  r   rD  rT  rX  r]  ra  ro  rg  r  r  r  r  r  r  r  rN  r  r  classmethodr9  r  r  r  r  r	  ru  r<  r-  r  r  rt  r  r"  r  r(  rq  r6  r3  rs  rD  rO  rS  rZ  r^  r*  r&  r  rn  rx   rw   r  r  r   r   rC   r;   r0   r0      s   L!l^,LH JLH F HHHIHO$$)!$NI M "# JN LN "T*L&8 L L  	<
$+Z
,
r@hN,&

3rHh*X"6,6(-#J1&2-
^5
nK4l#B-:
*0.
A.,,\2 b?H0*&D! '+R!NFr<r qTf)@  " A A0dC'J
L.0 @ @T"% &",\"*H* (B) +B4;2B0-M	(>+@ $ $&  K/Z
rC   c                 F    [         U   nU S   S:X  a
  U SS US   4$ XS   4$ )z
Return the field name and direction for an order specification. For
example, '-foo' is returned as ('foo', 'DESC').

The 'default' param is used to indicate which way no prefix (or a '+'
prefix) should sort. The '-' prefix always sorts the opposite way.
r   r/  rU   N)r"   )r   defaultdirns      r;   get_order_dirr  H
  s:     WDQx3QRy$q'!!q'>rC   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r  iV
  zQ
A class to abstract away join promotion problems for complex filter
conditions.
c                     Xl         X0l        U R                  (       a"  U[        :X  a  [        U l        O[        U l        OU R                   U l        X l        [        5       U l        g r4   )rd   re   r(   r)   effective_connectornum_childrenr   votes)ry   rd   r  re   s       r;   rz   JoinPromoter.__init__\
  sH    "<<C+-(+.('+~~D$( Y
rC   c                     U R                   R                   SU R                  < SU R                  < SU R                  < S3$ )Nz(connector=z, num_children=z
, negated=))r   r   rd   r  re   r   s    r;   r   JoinPromoter.__repr__k
  sG    ~~**+;t~~6H I --0
4<<:J!M	
rC   c                 :    U R                   R                  U5        g)zH
Add single vote per item to self.votes. Parameter can be any
iterable.
N)r  r  )ry   r  s     r;   r  JoinPromoter.add_votesq
  s    
 	

% rC   c                    [        5       n[        5       nU R                  R                  5        H  u  pEU R                  [        :X  a   XPR
                  :  a  UR                  U5        U R                  [        :X  d'  U R                  [        :X  d  Mc  XPR
                  :X  d  Mt  UR                  U5        M     UR                  U5        UR                  U5        U$ )z
Change join types so that the generated query is as efficient as
possible, but still correct. So, change as many joins as possible
to INNER, but don't make OUTER joins INNER if that could remove
results from the query.
)
r>   r  r   r  r)   r  r8  r(   r  r  )ry   r   
to_promote	to_demoter1  r  s         r;   r  JoinPromoter.update_join_typesx
  s     U
E	 !JJ,,.LE ''2-%:K:K2Ku% ''3.((B.5<M<M3Me$5 /T 	J'9%rC   )rd   r  re   r  r  N)
r   r   r   r   r   rz   r   r  r  r   r   rC   r;   r  r  V
  s    

!7rC   r  )ASC)Sr   r[   rs  r  r  collectionsr   r   collections.abcr   r   	itertoolsr   r   r	   stringr
   django.core.exceptionsr   r   	django.dbr   r   r   django.db.models.aggregatesr   django.db.models.constantsr   django.db.models.expressionsr   r   r   r   r   r   r   r   django.db.models.fieldsr   'django.db.models.fields.related_lookupsr   django.db.models.lookupsr   django.db.models.query_utilsr   r   r   django.db.models.sql.constantsr    r!   r"   r#   #django.db.models.sql.datastructuresr$   r%   r&   r'   django.db.models.sql.wherer(   r)   r*   r+   r,   django.utils.functionalr-   django.utils.regex_helperr.   django.utils.treer/   __all__r#  r  rB   rK   rH   rZ   rW   rg   r1   r   r0   r  r  r   rC   r;   <module>r     s      
 + - + + " @ F F - 1	 	 	 * B + 
 L K Q Q R R 3 6 "J
 ++FG  +95 6
8!H MF. F.R (=>j%
N j%
ZKY YrC   