
    h@_                       S SK r S SKrS SKrS SKJr  S SKJr  S SKJrJ	r	J
r
  S SKJrJr  S SKJr  S SKJrJrJrJrJr  S SK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#  S SK$J%r%J&r&  S SK'J(r(  S SK)J*r*  S SK+J,r,  S SK-J.r.  S SK/J0r0   " S S\5      r1 " S S5      r2 " S S\25      r3 " S S\25      r4 " S S\25      r5 " S S\25      r6S r7g)    N)partial)chain)EmptyResultSet
FieldErrorFullResultSet)DatabaseErrorNotSupportedError)
LOOKUP_SEP)FOrderByRawSQLRefValue)CastRandom)Lookup)select_related_descend)CURSORGET_ITERATOR_CHUNK_SIZEMULTI
NO_RESULTS	ORDER_DIRSINGLE)Queryget_order_dir)AND)TransactionManagementError)cached_property)make_hashable)_lazy_re_compilec                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )PositionRef   c                 0   > Xl         [        TU ]	  X#5        g N)ordinalsuper__init__)selfr&   refssource	__class__s       O/var/www/html/env/lib/python3.13/site-packages/django/db/models/sql/compiler.pyr(   PositionRef.__init__   s    &    c                 0    [        U R                  5      S4$ )N )strr&   )r)   compiler
connections      r-   as_sqlPositionRef.as_sql#   s    4<< "$$r/   )r&   )__name__
__module____qualname____firstlineno__r(   r5   __static_attributes____classcell__r,   s   @r-   r"   r"      s    '% %r/   r"   c                   F   \ rS rSr\" S\R                  \R                  -  5      rS#S jr	S r
S$S jrS$S jrS rS	 rS$S
 jrS rS rS rS rS rS rS rS%S jr S&S jrS r S'S jrS rS r     S(S jrS rS rS r SSS\!4S jr"S r#\$S\!4S jr%S  r&S! r'S"r(g))SQLCompiler'   z^(.*)\s(?:ASC|DESC).*c                 ~    Xl         X l        X0l        X@l        SS0U l        S U l        S U l        S U l        S U l        g )N*)	queryr4   usingelide_emptyquote_cacheselectannotation_col_map
klass_info_meta_ordering)r)   rC   r4   rD   rE   s        r-   r(   SQLCompiler.__init__.   sE    
$
 ':
 "&"r/   c           	          SU R                   R                   SU R                  R                  R                   SU R                  < SU R
                  < S3	$ )N<z model=z connection=z using=>)r,   r9   rC   modelr4   rD   r)   s    r-   __repr__SQLCompiler.__repr__?   sU    ++, -ZZ%%223 4//,GDJJ>D	
r/   Fc                   ^  [        U 4S jT R                  R                   5       5      (       a  T R                  R                  5         T R	                  US9u  T l        T l        T l        [        T R
                  5      T l	        g )Nc              3   \   >#    U  H!  nTR                   R                  U   S :H  v   M#     g7fr   NrC   alias_refcount).0ar)   s     r-   	<genexpr>*SQLCompiler.setup_query.<locals>.<genexpr>G   s'     O:NQtzz((+q0:N   ),with_col_aliases)
allrC   	alias_mapget_initial_alias
get_selectrG   rI   rH   len	col_count)r)   r^   s   ` r-   setup_querySQLCompiler.setup_queryF   sg    O$**:N:NOOOJJ((*@D- AP A
=T_d&= T[[)r/   c                 t   U R                  US9  U R                  5       nU R                  R                  R	                  U R                  R
                  SLS9u  U l        U l        U l        U R                  X R                  5      n[        U5      U l        U R                  U R                  U-   U5      nX2U4$ )z
Do any necessary class setup immediately prior to producing SQL. This
is for things that can't necessarily be done in __init__ because we
might not have all the pieces in place at that time.
r]   N)must_group_by)re   get_order_byrC   wheresplit_having_qualifygroup_byhavingqualifyget_extra_selectrG   boolhas_extra_selectget_group_by)r)   r^   order_byextra_selectrl   s        r-   pre_sql_setupSQLCompiler.pre_sql_setupN   s     	*:;$$&04

0@0@0U0U**--T9 1V 1
-
DK ,,X{{C $\ 2$$T[[<%?Jx//r/   c                 t   U R                   R                  c  / $ / n[        5       nU R                   R                  SLa  U R                   R                   H  n[        US5      (       d  U R                   R	                  U5      n[        U[        5      (       aJ  UR                  U;  a8  UR                  UR                  5        UR                  UR                  5        M  M  UR                  U5        M     0 n[        USS9 H:  u  nu  pXn	U	(       a  XvU'   X;   a  M  UR                  UR                  5       5        M<     U R                  (       d7  U H1  u  nu  pnU(       a  M  UR                  UR                  5       5        M3     U R                  (       a  U R                  R                  5       OSnU H  nUR                  U5        M     / n[        5       nU R!                  X=5      nU R"                  R$                  R&                  nU H  n U R)                  U5      u  pU(       a!  UR/                  U5      =nb  [1        U5      SpOUR3                  X
U5      u  p[5        U5      nU
U4U;  d  Mh  UR                  X45        UR                  U
U45        M     U$ ! [*        [,        4 a     M  f = f)z
Return a list of 2-tuples of form (sql, params).

The logic of what exactly the GROUP BY clause contains is hard
to describe in other words than "if it passes the test suite,
then it is correct".
Tr5      startr1   )rC   rl   sethasattrresolve_ref
isinstancer   r*   addappendr+   	enumerateextendget_group_by_colsrJ   rm   collapse_group_byr4   featuresallows_group_by_select_indexcompiler   r   getr2   select_formatr   )r)   rG   rs   expressionsgroup_by_refsexprselected_expr_positionsr&   _aliassqlparamsis_refhaving_group_byresultseenr   positionparams_hashs                      r-   rr   SQLCompiler.get_group_by^   sk   L ::&I::d* 

++tX..::11$7DdC((yy5%))$))4#**4;;7 6  &&t, , #%)26)C%G%du07- %t5578 *D ""/7++sF v&&t'='='?@	 08
 >B[[$++779b#Dt$ $u,,[J OO$$AA 	%  D"ll40 -!8!<!<T!BBXO!(mRV"00FC'/K[!-sm,#{+,     #M2 s   J##J76J7c                    U R                   R                  R                  (       a  U Vs1 s Hu  n[        US5      (       d  M  UR                  R
                  (       d  M3  U R                   R                  R                  UR                  R                  5      (       d  Ms  UiMw     nnU Vs1 s H  o3R                  iM     nnU Vs/ s H"  nX4;   d  X2;   d  [        USS 5      U;  d  M   UPM$     nnU$ s  snf s  snf s  snf )Ntargetr   )
r4   r   allows_group_by_selected_pksr|   r   primary_key%allows_group_by_selected_pks_on_modelrO   r   getattr)r)   r   rm   r   pksaliasess         r-   r   SQLCompiler.collapse_group_by   s     ??##@@ (
'DD(+  //	 
 00VV)) '  
 /22cdzzcG2 ('D;>4$/w>	 '   '
 3s)   C4C4 <C4 C4,C9C>*C>c                 J  ^ / nSn0 nSnU R                   R                  R                  5        H-  u  nu  pxXTU'   UR                  [	        Xx5      U45        US-  nM/     U R                   R
                  (       a  U R                   R                  (       a   eU R                   R                  5       n	U R                   R                  (       a  U R                  U	5      n
OU R                   R
                  n
U
(       aM  / nU
 H,  nUR                  U5        UR                  US45        US-  nM.     U R                   R                  US.nU R                   R                  R                  5        H   u  pmXTU'   UR                  X45        US-  nM"     U R                   R                  (       a#  U R                  X)5      nXS'   U4S jmT" U5        / nSnU HV  u  p U R                  U5      u  pxUR                  XU5      u  pxUc  U(       a
  S
U 3nUS-  nUR                  XU4U45        MX     XU4$ ! [         a?    [!        US["        5      nU["        L a  Su  px N[U R                  [%        U5      5      u  px Ny[&         a    U R                  [%        S	5      5      u  px Nf = f)a8  
Return three values:
- a list of 3-tuples of (expression, (sql, params), alias)
- a klass_info structure,
- a dictionary of annotations

The (sql, params) is what the expression will produce, and alias is the
"AS alias" for the column (possibly None).

The klass_info structure contains the following information:
- The base model of the query.
- Which columns for that model are present in the query (by
  position of the select clause).
- related_klass_infos: [f, klass_info] to descent into

The annotations is a dictionary of {'attname': column position} values.
Nr   rx   )rO   select_fieldsrelated_klass_infosc                 ^   > U S    H#  nUS   (       a  U S   US   -   US'   T" U5        M%     g )Nr   from_parentr   r1   )rI   kiget_select_from_parents     r-   r   6SQLCompiler.get_select.<locals>.get_select_from_parent  sA    $%:;B-(&7"_:MM ?+ +2. <r/   empty_result_set_value)0r1   Tcol)rC   rt   itemsr   r   rG   default_colsget_select_maskget_default_columnsrO   annotation_selectselect_relatedget_related_selectionsr   r   r   r   NotImplementedr   r   )r)   r^   rG   rI   annotations
select_idxr   r   r   select_maskcolsselect_listr   
annotationr   retcol_idxr   r   s                     @r-   rb   SQLCompiler.get_select   s   $ 

$(JJ$;$;$A$A$C E=C!+MM6#.67!OJ %D JJ%%$***A*ABBjj002::""++K8D ::$$DK"":.sDk*a
 
 ))!,J "&!=!=!C!C!EE!+MM:-.!OJ "F
 ::$$"&"="=f"R0C,-/ #:. JCC"ll3/ "//6B}!1gY1JJ6]E23' !( ++# " N)01>*& *^;")KC"&,,u5K/L"MKC  8"ll5;7V8s   H22(J"J":%J"!J"c           
   #   :  #    U R                   R                  (       a  U R                   R                  nOU R                   R                  (       d  U R                   R                  nOyU R                   R                  (       a  U R                   R                  nOGU R                   R	                  5       =n(       a$  UR
                  (       a  UR
                  nXl        O/ nU R                   R                  (       a  [        S   u  p4O[        S   u  p40 nU(       aE  U R                  =n(       a2  [        USS9 H$  u  nu  pn	[        XyU5      n
U	(       a  XU	'   XU'   M&     U GH  n[        US5      (       Ga  [        U[        5      (       a  [        XR                   5      n[        U["        5      (       d  UR%                  5       nU R                   R                  (       d   UR'                  5       nUR)                  5         UR+                  UR,                  5      nU(       dK  [        UR,                  [.        5      (       a  UR+                  UR,                  R0                  5      =n(       a  UR2                  c  UR4                  b%  U R6                  R8                  R:                  (       a  UR'                  5       nXl        OPU R                   R<                  (       a5  UR'                  5       n[?        UR@                  URB                  5      Ul        XS L4v   GM  US:X  a  [#        [E        5       5      S4v   GM  [G        X5      u  pUS:H  nUR+                  U5      =n(       a  [#        UUS9S	4v   GM  U R                   RH                  R+                  U5      =n(       a  Un/ nO<URK                  [L        5      tnnU R                   RH                  R+                  U5      nU(       a  U R                   R<                  (       a.  U R                  (       a  U(       a  [O        S
5      e[/        U5      nU(       a%  U H  nU R                   RQ                  UU5      nM!     [        U[        5      (       a  [        XR                   5      n[#        XS9S4v   GM;  SU;   aF  URK                  SS5      u  nn[#        [S        U RU                  U5      < SU< 3/ 5      US9S4v   GM  U R                   RV                  (       a  XR                   RV                  ;   a  XR                   RX                  ;   a8  [#        [?        U[S        U R                   RV                  U   6 5      US9S	4v   GM  [#        [S        U R                   RV                  U   6 US9S4v   GM:  U R                   R<                  (       a+  U R                  (       a  [#        [/        U5      US9S4v   GM  U R[                  UU R                   R	                  5       US9 S h  vN   GM     g  N
7f)NASCDESCrx   ry   resolve_expression?F
descendingTz;Ordering combined queries by transforms is not implemented..)default_order).rC   extra_order_bydefault_orderingrs   get_metaorderingrJ   standard_orderingr   rG   r   r"   r|   r~   r   r   output_fieldr   asccopyreverse_orderingr   
expressionr   namenulls_first
nulls_lastr4   r    supports_order_by_nulls_modifier
combinatorr   r*   r+   r   r   r   splitr
   NotImplementedErrortry_transformr   quote_name_unless_aliasextrart   find_ordering_name)r)   r   metar   r   selected_exprsrG   r&   r   r   pos_exprfield
select_refr   orderr   ref
transformsr   tables                       r-   _order_by_pairsSQLCompiler._order_by_pairs<  s    ::$$zz00H,,zz**HZZ  zz**Hjj))++d+}}H"*H::''(/M1(0M 4;;..-6vQ-G))$5&wt<,45)'/t$	 .H Eu233eU++ (:(:;E!%11!IIKEzz33!JJLE**,+//0@0@A
u//33'5'9'9%:J:J:O:O'PPP
 ))1e6F6F6N11RR %

+5( .. %

+.z
@Q@Q+R(t333|fh'..&u<JC&J+//44z4 "#-   zz--11#66t6
#&99Z#8 jzz--11#6::((T[[!1+  S6D *#zz77dC !+dE**&7&78Dd:EAAe| !YYsA.
s'+'C'CE'JCPRT $.	   zzC::+;+;$;**111VTZZ-=-=c-B%CD'1    

(8(8(= >:V 
 ::((T[[ "!C&Z@%GG  $66

++-&3  7    ] \s   XXXXc                    / n[        5       nU R                  5        GH4  u  p4UR                  U R                  SSS9nU(       Gd  U R                  R                  (       Ga  U R
                  (       Gao  UR                  nUR                  nU R
                   H  u  pn
Xh:X  d  M  U R                  R                  (       a?  XR                  R                  ;   a&  [        U[        5      (       a  XR                  :X  d  Mg  UR                  [        U
(       a  U
OUR                  R                  U5      /5          O   [!        U R                  R
                  5      S-   nSU 3n
U R                  R"                   H0  nUR                  (       a  [%        S5      eUR'                  Xz5        M2     U R                  R)                  XZ5        UR                  [        X5      /5        U R+                  U5      u  pU R,                  R/                  U5      S   n[1        U5      nUU4U;   a  GM  UR3                  UU45        UR5                  X]X445        GM7     U$ )z
Return a list of 2-tuples of the form (expr, (sql, params, is_ref)) for
the ORDER BY clause.

The order_by clause can alter the select clause (for example it can add
aliases to clauses that do not yet have one, or it can add totally new
select clauses).
TN)allow_joinsreuserx   __orderbycolz:ORDER BY term does not match any column in the result set.)r{   r   r   rC   r   rG   r   has_select_fieldsr   r~   r   r   set_source_expressionsr   r   columnrc   combined_queriesr   add_annotationadd_select_colr   ordering_partssearchr   r   r   )r)   r   r   r   r   resolvedsrcexpr_srcsel_exprr   	col_aliasorder_by_idxqr   r   without_orderingr   s                    r-   ri   SQLCompiler.get_order_by  s    u 002LD..tzztSW.XHdjj333))??.2kk*H !JJ88 )ZZ-I-I I *8Q 7 7I<V % 77 iSZZ=N=NPSTU  /:& $'tzz'8'8#9A#=L".|n =I!ZZ88 .."/!2#  ((= 9 JJ--hB33S5H4IJ,,x0KC
  $2299#>qA'/K +.$6HH&45MM86%:;<_ 3` r/   c                 P   / nU R                   R                  (       a  U R                   R                  (       dh  U Vs/ s H  oDS   PM	     nnU HN  u  nu  pxn	U R                  R	                  U5      S   n
U	(       a  M0  X4U;  d  M9  UR                  XjU4S 45        MP     U$ s  snf Nrx   )rC   distinctdistinct_fieldsr   r   r   )r)   rs   rG   rt   t
select_sqlr   r   r   r   r   s              r-   ro   SQLCompiler.get_extra_select  s    ::tzz'A'A(./1A$J//7++sF#'#6#6#=#=c#B1#E v#3"<J"N ''&/I4(PQ 08  0s   B#c                    XR                   ;   a  U R                   U   $ XR                  R                  ;   a  XR                  R                  ;  d\  XR                  R                  ;   dC  U R                  R
                  R                  U5      (       a)  XR                  R                  ;  a  XR                   U'   U$ U R                  R                  R                  U5      nX R                   U'   U$ )z
A wrapper around connection.ops.quote_name that doesn't quote aliases
for table names. This avoids problems with some SQL dialects that treat
quoted strings specially (e.g. PostgreSQL).
)
rF   rC   r`   	table_maprt   external_aliasesr   r4   ops
quote_name)r)   r   rs      r-   r   #SQLCompiler.quote_name_unless_alias  s     #####D))ZZ)))d**:N:N.Nzz...

++//55

 4 44 &*T"KOO**40!"r/   c                     [        USU R                  R                  -   S 5      nU(       a  U" X R                  5      u  p4X44$ UR                  X R                  5      u  p4X44$ )Nas_)r   r4   vendorr5   )r)   nodevendor_implr   r   s        r-   r   SQLCompiler.compile(  sZ    dEDOO,B,B$BDI%dOO<KC { ++dOO<KC{r/   c                   ^ U R                   R                  nU R                  R                   Vs/ s H4  nUR	                  U R
                  U R                   U R                  5      PM6     nnUR                  (       dP  U HI  nUR                  R                  (       a  [        S5      eUR                  5       (       d  M@  [        S5      e   O1U R                  R                  (       a  US:X  a  U H
  nSUl        M     SnU GH  n UR                  R                  (       d  U R                  R                  (       a}  UR                  R                  5       Ul        UR                  R                  / U R                  R                  QU R                  R                  QU R                  R                  Q75        UR!                  SS9u  pUR                  R"                  (       aa  UR$                  (       d  SR'                  U5      nO{U R                  R(                  (       d  UR                  (       d  S	R'                  U5      nO=U R                  R(                  (       a"  UR                  (       a  S	R'                  U5      nXxU	44-  nGM     U(       d  [*        eU R                   R,                  R.                  U   n
U(       a  US:X  a  U
S-  n
SmU R                  R(                  (       d  UR                  (       a  S	m[1        U4S jU 5       6 u  pSR'                  U
5      R3                  U5      /n/ nU H  nUR5                  U5        M     X4$ s  snf ! [*         a    US:X  d  US
:X  a  U(       a   GMz  e f = f)Nz>LIMIT/OFFSET not allowed in subqueries of compound statements.z:ORDER BY not allowed in subqueries of compound statements.unionFr1   Tr]   zSELECT * FROM ({})z({})
differencez ALLz{}c              3   N   >#    U  H  u  pTR                  U5      U4v   M     g 7fr%   )format)rX   r   argsbracess      r-   rZ   1SQLCompiler.get_combinator_sql.<locals>.<genexpr>x  s!     @%YSv}}S!4(%s   "%z {} )r4   r   rC   r   get_compilerrD   rE   %supports_slicing_ordering_in_compound	is_slicedr   ri   values_selectclone
set_valuesrt   r   r5   r    supports_parentheses_in_compoundr  subqueryr   r  set_operatorszipjoinr   )r)   r   r_   r   rC   	compilersr3   partspart_sql	part_argscombinator_sql	sql_parts
args_partsr   r   partr  s                   @r-   get_combinator_sqlSQLCompiler.get_combinator_sql0  s   ??++ 44
4 tzz4??D<L<LM4 	 
 ==%>>++'X  ((**'T  & ZZ!!jG&;%
 (-$ & !H%  ~~33

8P8P%-^^%9%9%;HNNN--!ZZ44!ZZ55 "ZZ99 '/ooto&L#>>,, $DD#7#>#>x#H 

++'MM#)==#:JJ'' FF%}}X6HY/11A "N   ,,:::F:(f$Nzz""x'U'UF #@%@!
	 --/44Y?@DMM$ ~Y
n "  (Z<-GEs   ;MFMM?=M?c                   ^^^^^ / nU R                   (       a  UR                  U R                   5        U R                  (       a  UR                  U R                  5        U R                  R	                  5       mSTl        TR                   R                  U5      Tl         U R                  SS9S    VVVs0 s H  u  p#oBU_M
     snnnm[        TR                  5       5      m[        5       m0 mUUUUU4S jnU" [        U R                  R                  5       5      5        U R                  R                  TR                  5        VVs0 s H  u  p$U[        XB5      _M     snn5      U l        / nU R!                  5        Hg  tpsU" UR#                  5       5        UR                  UR                  TR                  5        VVs0 s H  u  p$U[        XB5      _M     snn5      5        Mi     TR%                  U R&                  U R(                  U R*                  S9nUR-                  SSS9u  pU R/                  U R                  5      u  pSU	S	U R(                  R0                  R3                  S
5      SU/nT(       a  TR                  5        Vs/ s H'  o@R(                  R0                  R3                  U5      PM)     nnSSR5                  U5      S/UQS	PU R(                  R0                  R3                  S5      Pn[        U
5      U-   nU(       ac  / nU H9  nU R/                  U5      u  nnUR                  U5        UR7                  U5        M;     UR7                  SSR5                  U5      /5        X4$ s  snnnf s  snnf s  snnf s  snf )NTr]   r   c                   > U (       a  U R                  5       nUT;   a  M  TR                  U5      =n(       a  UTU'   O[        U[        5      (       a   U R	                  UR                  5       5        O|[        U[        5      (       a0  UR                  T;  a  U R	                  UR                  5       5        O7[        T5      nSU 3nTR                  U5        TR                  X5        UTU'   U (       a  M  g g )Nqual)popr   r~   r   r   get_source_expressionsr   r*   rc   r   r   )	r   r   select_aliasnum_qual_aliasinner_queryqual_aliasesreplacementsrG   select_aliasess	       r-   collect_replacements9SQLCompiler.get_qualify_sql.<locals>.collect_replacements  s    "(<'%+ZZ%55\5)5L&f--&&t'B'B'DEc**yy6#**4+F+F+HI%(%6N%).)9#:L $$\2..tB)5L&! +r/   )r4   rE   F)with_limitsr^   SELECT * FROM ()rn   WHERESELECT, zFROM (qualify_maskzORDER BY)rj   r   rm   rC   r  r  r,   rb   r{   valueslistrn   leavesreplace_expressionsr   r   ri   r1  r  rD   r4   rE   r5   r   r  r  r"  r   )r)   where_partsr   r   r   r8  rs   order_by_exprinner_query_compiler	inner_sqlinner_paramsqualify_sqlqualify_paramsr   r   r   ordering_sqlsr   ordering_sqlordering_paramsr4  r5  r6  rG   r7  s                       @@@@@r-   get_qualify_sqlSQLCompiler.get_qualify_sql  s9   ::tzz*;;t{{+jj&&(#'--77D /3ooto.TUV.W
.WNDU%K.W
 V]]_-u	6 	6& 	T$,,"5"5"789||777C7I7I7KL7KT3u##7KL
 !%!2!2!4M !E!E!GHOO11?K?Q?Q?ST?ST3u++?ST "5  +77JJ4??@P@P  8  
 #7"="=  " #> #
	 '+ll4<<&@#OO**95
  HN}}WeOO''2259DW		$ 	
  ##..~>F l#n4 M$04X0F-o$$\2o. % MM:tyy'?@A~c
8 M U4 Xs   7M7M>
N.N
c                 d   U R                   R                  R                  5       n U R                   R                  nU R	                  U=(       d    [        U5      S9u  pVnSnU=(       a    U R                   R                  n	U R                   R                  nU R                  R                  n
U(       ac  [        U
SR                  U5      5      (       d  [        SR                  U5      5      eU R                  X@R                   R                  5      u  pGOU R                  (       a  U R                  5       u  pSnGOU R!                  5       u  pU R#                  5       u  nn U R$                  b  U R'                  U R$                  5      OS/ 4u  nn U R.                  b  U R'                  U R.                  5      OS/ 4u  nnS/n/ nU R                   R0                  (       a2  U R                  R2                  R5                  UU5      u  nnUU-  nX-  n/ nU R6                  U-    H_  u  nu  nnnU(       a,  U< SU R                  R2                  R9                  U5      < 3nUR;                  U5        UR=                  U5        Ma     US	R?                  U5      /-  nU(       a	  US
/UQ-  nOHU R                  R                  R@                  (       a#  XR                  R                  R@                  /-  nUR;                  U5        U R                   RB                  (       Ga  U
RD                  (       Gav  U R                  RG                  5       (       a  U
RH                  (       a  [K        S5      eU	(       a  U
RL                  (       d  [        S5      eU R                   RN                  nU R                   RP                  nU R                   RR                  nU R                   RT                  nU(       a  U
RV                  (       d  [        S5      eU(       a  U
RX                  (       d  [        S5      eU(       a  U
RZ                  (       d  [        S5      eU(       a  U
R\                  (       d  [        S5      eU R                  R2                  R_                  UUU Ra                  5       US9nU(       a"  U
Rb                  (       a  UR=                  U5        U(       a%  UR=                  SU-  5        UR;                  U5        / nU H(  u  n n!UR=                  U 5        UR;                  U!5        M*     U(       au  U(       a  [e        S5      eU=(       d$    U R                  R2                  Rg                  5       nUR=                  SS	R?                  U5      -  5        U Rh                  (       a  SnU(       a_  U(       d3  UR;                  U R                  R2                  Rk                  5       5        UR=                  SU-  5        UR;                  U5        U R                   Rl                  (       as  URo                  SU R                  R2                  Rp                  " U R                   Rl                  R                  40 U R                   Rl                  Rr                  D65        U(       a{  / n"U H,  u  nu  n#n$nU"R=                  U#5        UR;                  U$5        M.     SS	R?                  U"5      -  n%U(       a  U
Rt                  (       a
  S/UQSPU%PnOUR=                  U%5        U	(       a]  UR=                  U R                  R2                  Rw                  U R                   Rx                  U R                   Rz                  5      5        U(       a"  U
Rb                  (       d  UR=                  U5        U R                   R|                  (       GaD  U(       Ga<  / n&/ n'[        U R6                  SS9 H  u  n(u  n)nnU(       a`  U&R=                  U R                  R2                  R9                  S5      < SU R                  R2                  R9                  U5      < 35        Mq  U)R                  U)R                  S05      n*U*R                  X R                  5      u  n+n,U&R=                  U+5        U'R;                  U,5        M     SS	R?                  U&5      < SS R?                  U5      < S!3[        U'U-   5      4U R                   R                  U5        $ S R?                  U5      [        U5      4U R                   R                  U5        $ ! [(         a    U R*                  (       a  e S/ nn GN[,         a    S/ nn GNf = f! [,         a    S/ nn GNf = f! U R                   R                  U5        f = f)"z
Create the SQL for this query. Return the SQL string and list of
parameters.

If 'with_limits' is False, any limit/offset information is not included
in the query.
r]   Nzsupports_select_{}z-{} is not supported on this database backend. z0 = 1r>  z AS r?  FROMz:select_for_update cannot be used outside of a transaction.zNLIMIT/OFFSET is not supported with select_for_update on this database backend.z1NOWAIT is not supported on this database backend.z6SKIP LOCKED is not supported on this database backend.z8FOR UPDATE OF is not supported on this database backend.z<FOR NO KEY UPDATE is not supported on this database backend.)nowaitskip_lockedofno_keyWHERE %sz1annotate() + distinct(fields) is not implemented.zGROUP BY %sz	HAVING %sr   zORDER BY %sr;  r<  rx   ry   r  r   SELECT  FROM ( 
) subquery)ErC   rW   r   r   ru   rp   r  r4   r   r   r  r	   r+  combinator_allrn   rO  get_distinctget_from_clauserj   r   r   rE   r   rm   r   r  distinct_sqlrG   r  r   r   r"  bare_select_suffixselect_for_updatehas_select_for_updateget_autocommitsupports_transactionsr   %supports_select_for_update_with_limitselect_for_update_nowaitselect_for_update_skip_lockedselect_for_update_ofselect_for_no_key_updatehas_select_for_update_nowait!has_select_for_update_skip_lockedhas_select_for_update_ofhas_select_for_no_key_updatefor_update_sql"get_select_for_update_of_argumentsfor_update_after_fromr   force_no_orderingrJ   force_group_byexplain_infoinsertexplain_query_prefixoptions#requires_compound_order_by_subquerylimit_offset_sqllow_mark	high_markr  r   relabeled_cloner   r5   tuplereset_refcounts)-r)   r:  r^   refcounts_beforer   rt   rs   rl   for_update_partwith_limit_offsetr   r   r   r   distinct_paramsfrom_f_paramsrj   w_paramsrm   h_paramsdistinct_resultout_colsr   s_sqls_paramsr   rT  rU  rV  rW  groupingg_sqlg_paramsr   o_sqlo_paramsorder_by_sqlsub_selects
sub_paramsindexrG   select_clone	subselect	subparamss-                                                r-   r5   SQLCompiler.as_sql  s     ::4499;X	9..J/3/A/A!1!ET*5E 0B 0,LH #O + D

0D0D..J//Hx)=)D)DZ)PQQ+GNN& 
 "&!8!8

 9 9" !%!5!5!7373D3D3F0 #'"6"6"8x
-48JJ4JTZZ0QSUWPX $E8.  ;;2 T[[1 "X %FH #::&&7;7J7J7W7W''84O_ o-F-F37;;3M/A(x%! OO//::5A! MM(+OOE* 4N 499X.//v...F__--@@77JJKKFh'::///H4R4R4R6688 %::8X 
 * ( N N/J  "ZZ@@F"&**"J"JK88B!ZZ@@F h&K&K/O  %X-W-W/T  H$E$E/V   (M(M/0  '+oo&9&9&H&H%$/BBD%	 'I 'O #x'E'EMM/2MM*u"45MM(+'/OE8OOE*MM(+ (0 &1O   (R4??+>+>+P+P+RHMM-$))H2E"EF**#'#doo&9&9&H&H&JKMM+"67MM(+zz&&OO''<<

//66**1199 /7+A+xOOE*MM(+ 08  -tyy/BB("N"N/L&L#L|LFMM,/ OO''88

++TZZ-A-A x'E'Eo.zz"""| !
1:4;;a1P-E-FAu#** !% 3 3 > >z J $ 3 3 > >u E (.'='=#\\:6( 0</B/B //0,	9 $**95")))4# 2Q& IIk*HHV$ f,-. JJ&&'78 88F#U6]2 JJ&&'78q & 2''&-r8E8$ -&("8E8- % .')2HFH.V JJ&&'78sa   D=f $/e /e= \0f f  e:&f )e:6f 9e::f =ff ff f/Nc                 F   / nUc  U R                   R                  5       =nc  U$ U=(       d    U R                   R                  5       nSU0nUR                   H  nUR                  R
                  R                  nXR                  :X  a  SnU(       a>  Ub;  [        UR
                  R                  UR
                  R                  5      (       a  My  U(       a  Xq;  a  M  U R                   R                  X8X&5      n	UR                  U	5      n
UR                  U
5        M     U$ )a  
Compute the default columns for selecting every field in the base
model. Will sometimes be called to pull in related models (e.g. via
select_related), in which case "opts" and "start_alias" will be given
to provide a starting point for the traversal.

Return a list of strings, quoted appropriately for use in SQL
directly, as well as a set of aliases used in the select statement (if
'as_pairs' is True, return a list of (alias, col_name) pairs instead
of strings as the first component and None as the second component).
N)rC   r   ra   concrete_fieldsrO   _metaconcrete_model
issubclassjoin_parent_modelget_colr   )r)   r   start_aliasoptsr   r   seen_modelsr   rO   r   r   s              r-   r   SQLCompiler.get_default_columns  s     <

++--6!CTZZ%A%A%C [)))EKK%%44E 

"%%%44ekk6P6P  u7JJ00kWE]]5)FMM&!/ *0 r/   c                 :   / n/ nU R                   R                  5       nU R                   R                   H  nUR                  [        5      nU R                  XSS5      u  pgppnU R                   R                  XyU
5      u  pxnU H  nX@R                   R                  ;   a6  UR                  U R                  R                  R                  U5      5        MR  U R                  U" X5      5      u  pUR                  U5        UR                  U5        M     M     X4$ )z
Return a quoted list of fields to use in DISTINCT ON part of the query.

This method can alter the tables in the query, and thus it must be
called before get_from_clause().
N)rC   r   r   r   r
   _setup_joins
trim_joinsr   r   r4   r  r  r   )r)   r   r   r  r   r$  r   targetsr   joinspathtransform_functionr   r  ps                  r-   r^  SQLCompiler.get_distinct  s     zz""$JJ..DJJz*EDHDUDUTEAAd/A !%

 5 5gd KGA!::777MM$//"5"5"@"@"FG<<(:6(IJDAMM!$MM!$ " / ~r/   c                   ^ ^ [        TU5      u  mnUS:H  nTR                  [        5      nT R                  XU5      u  n	n
nnnnnU	R                  (       Ga[  UR
                  (       GaI  [        U	SS5      US   :w  Ga4  TS:w  Ga-  [        USS5      (       Gd  U=(       d
    [        5       n[        U 4S jU 5       5      nX;   a  [        S	5      eUR                  U5        / nUR
                   H  n[        US
5      (       a<  [        U[        5      (       d'  U(       a  UR                  5       OUR                  5       n[        U[        5      (       a,  UR!                  UR#                  T [         35      S45        M  UR%                  U4S jT R'                  UX#Xe5       5       5        M     U$ T R(                  R+                  XU5      u  pnU
 Vs/ s H  n[        U" UU5      US9S4PM     sn$ s  snf )z
Return the table alias (the name might be ambiguous, the alias will
not be) and column name for ordering by the given 'name' parameter.
The 'name' is of the form 'field1__field2__...__fieldN'.
r   attnameNpkhas_transformsFc              3   l   >#    U  H)  n[        TR                  R                  U   S S5      v   M+     g7f)	join_colsN)r   rC   r`   )rX   jr)   s     r-   rZ   1SQLCompiler.find_ordering_name.<locals>.<genexpr>1  s/      MR

,,Q/dCCUs   14z!Infinite loop caused by ordering.r   c              3   ^   >#    U  H"  u  pUR                  T [         35      U4v   M$     g 7fr%   )prefix_referencesr
   )rX   r   r   r   s      r-   rZ   r  C  s7      ) ++tfZL,ABFK)s   *-r   )r   r   r
   r  is_relationr   r   r{   r}  r   r   r|   r~   r   descr   r   r  r   r   rC   r  )r)   r   r  r   r   already_seenr   r   piecesr   r  r  r  r  
join_tupleresultsitemr   r   s   ``                 r-   r   SQLCompiler.find_ordering_name  s    $D-8ef_
J' fE2	
 y$/6":=.0@%HH (035L MR J ) !DEEZ(G4!566z'@ @ +5499;$((*DdG,,NN//4&0EFN  (,(?(?d5)  &  N JJ11'$G 
 '51jI5Q
 	
 
s   G6c                     U=(       d    U R                   R                  5       nU R                   R                  XU5      u  pEp&pxUS   nXEX6XrU4$ )z
Helper method for get_order_by() and get_distinct().

get_ordering() and get_distinct() must produce same target columns on
same input, as the prefixes of get_ordering() and get_distinct() must
match. Executing SQL where this is not true is an error.
r  )rC   ra   setup_joins)	r)   r  r  r   r   r  r  r  r  s	            r-   r  SQLCompiler._setup_joinsP  sW     7557@D

@V@V%A
=T b	uT9KKKr/   c                    / n/ n[        U R                  R                  5       Hr  nU R                  R                  U   (       d  M#   U R                  R                  U   nU R                  U5      u  pVUR                  U5        UR                  U5        Mt     U R                  R                   H{  nU R                  R                  U5      u  p8X0R                  R                  ;  d  U R                  R                  U   S:X  d  MX  UR                  SU R                  U5      -  5        M}     X4$ ! [         a     GM  f = f)a  
Return a list of strings that are joined together to go after the
"FROM" part of the query, as well as a list any extra parameters that
need to be included. Subclasses, can override this to create a
from-clause via a "select".

This should only be called after any SQL construction methods that
might change the tables that are needed. This means the select columns,
ordering, and distinct must be done first.
rx   z, %s)r}  rC   r`   rW   KeyErrorr   r   r   extra_tablestable_aliasr   )	r)   r   r   r   from_clause
clause_sqlclause_paramsr   r   s	            r-   r_  SQLCompiler.get_from_clause_  s    4:://0E::,,U3"jj2259
 )-[(A%JMM*%MM-( 1 ((Azz--a0HE
 ZZ111::,,U3q8ft'C'CE'JJK ) ~#   s   D22
E Ec                   ^ ^ UU 4S jn/ n	U(       d  UT R                   R                  :  a  U	$ T(       d4  T R                   R                  5       mT R                   R                  5       n[	        5       n
UcA  [        T R                   R                  [        5      nU(       a  T R                   R                  nS nTR                   GH  nU
R                  UR                  5        U(       a  UR                  UR                  0 5      nUR                  (       dQ  U(       d  UR                  U;   a:  [        SUR                  < SSR                  U" 5       5      =(       d    S< 35      eOSn[        XXb5      (       d  M  UR                  U5      =(       d    0 nUR                   R"                  USUR$                  UR&                  (       a  UR                   R$                  OS	 SS
.nU	R)                  U5        / nT R                   R+                  UR                  /TU5      u      nn  nUS   nT R-                  UUUR                   R"                  R.                  S9nU H0  nUR)                  [1        U5      5        UR)                  US45        M2     UUS'   T R3                  UUUR                   R"                  R.                  UUS-   UU5      nU" UU5        GM     U(       Ga  TR4                   Vs/ s HL  nUR6                  R&                  (       d  M   UR8                  (       a  M3  UUR6                  UR:                  4PMN     nnU GH  u  nnn[        UUUU5      (       d  M  UR                  U5      =(       d    0 nUR=                  5       nU
R                  U5        T R                   R+                  U/TU5      nUR>                  S   n[A        UTR"                  5      =(       a    UTR"                  LnUUSUR$                  UR$                  US
.nU	R)                  U5        / nT R-                  UUUR.                  TR"                  S9nU H0  nUR)                  [1        U5      5        UR)                  US45        M2     UUS'   UR                  U0 5      nT R3                  UUUR.                  UUS-   UU5      nU" UU5        GM     S nS n S n![C        U5       GH  n"US:  a    GOU"T R                   RD                  ;   d  M)  U
R                  U"5        T R                   R+                  U"/TU5      u  n#nn$n  nU$R"                  nUS   n[A        UTR"                  5      =(       a    UTR"                  LnUU#S[1        U5      S::  a  [G        UU#5      OU [G        U!U"5      US
.nU	R)                  U5        / nUR                  U"U#45      =(       d    0 n%T R-                  U%UUR.                  TR"                  S9nU H0  nUR)                  [1        U5      5        UR)                  US45        M2     UUS'   UR                  U"0 5      n&T R3                  UU%UR.                  UUS-   U&US9nU" UU5        GM     [	        U5      RI                  U
5      n'U'(       aH  S U' 5       n([        SSR                  U(5      < SSR                  U" 5       5      =(       d    S< 35      eU	$ s  snf )z
Fill in the information needed for a select_related query. The current
depth is measured as the number of connections away from the root model
(for example, cur_depth=1 means we are looking at models with direct
connections to the root model).
c                     > S TR                    5       n S TR                   5       n[        XTR                  R                  5      $ )Nc              3   ^   #    U  H#  oR                   (       d  M  UR                  v   M%     g 7fr%   )r  r   rX   fs     r-   rZ   QSQLCompiler.get_related_selections.<locals>._get_field_choices.<locals>.<genexpr>  s     Kk]]faffks   --c              3      #    U  H<  nUR                   R                  (       d  M   UR                   R                  5       v   M>     g 7fr%   )r   uniquerelated_query_namer  s     r-   rZ   r    s4      -A77>> -**,,-s
   A!A)fieldsrelated_objectsr   rC   _filtered_relations)direct_choicesreverse_choicesr  r)   s     r-   _get_field_choices>SQLCompiler.get_related_selections.<locals>._get_field_choices  sD    KdkkKN--O
 1O1O r/   Nc                     XS'   g )Nr   r1   )rI   r   s     r-   get_related_klass_infosCSQLCompiler.get_related_selections.<locals>.get_related_klass_infos  s    0C,-r/   z/Non-relational field given in select_related: 'z'. Choices are: r?  z(none)Fc                     g r%   r1   )xys     r-   <lambda>4SQLCompiler.get_related_selections.<locals>.<lambda>  s    RVr/   )rO   r   reverselocal_setterremote_setterr   r  )r  r  r   rx   T)r  r  r   c                 J    U(       a  U R                   R                  X!5        g g r%   )remote_fieldset_cached_value)final_fieldobjfrom_objs      r-   r  8SQLCompiler.get_related_selections.<locals>.local_setter#  s    ,,==hL r/   c                     g r%   r1   )r  r  s     r-   local_setter_noop=SQLCompiler.get_related_selections.<locals>.local_setter_noop(  s    r/   c                     [        X U5        g r%   )setattr)r   r  r  s      r-   r  9SQLCompiler.get_related_selections.<locals>.remote_setter+  s    ,r/      )r  
root_alias	cur_depth	requested
restrictedc              3   ,   #    U  H
  nS U-  v   M     g7f)z'%s'Nr1   )rX   ss     r-   rZ   5SQLCompiler.get_related_selections.<locals>.<genexpr>b  s     !G6F&1*6Fs   z/Invalid field name(s) given in select_related: z. Choices are: )%rC   	max_depthr   ra   r{   r~   r   dictr  r   r   r   r  r   r"  r   r  rO   r  r  r   r  r   r  rc   r   r  r   many_to_manyrelated_modelr  r  r  rB  r  r   r  ))r)   rG   r   r  r  r  r  r  r  r   fields_foundr  r  nextrelated_select_maskrI   r   r   r  r   columnsr   next_klass_infosorelated_fieldsrelated_objectrelated_fieldrO   related_field_name	join_infor   r  r  r  r   r  	join_optsfield_select_masknext_requestedfields_not_foundinvalid_fieldss)   `  `                                     r-   r   "SQLCompiler.get_related_selections  s   "		 !i$***>*>>&&::&&(D557J u#DJJ$=$=tDJ JJ55		D AQVV$ }}QVVR0}} qvv2( !" $		*<*> ? K8 K	  )!PP"-//!"4":--  ! 2 278xxANN33EV$	J  &&z2M#'::#9#9166(D*#U Aq!UAq"IE..#Q^^=Q=Q=W=W / G $$S[1sDk*  +8J'#::#$$**A  $J0@Ak n  ---A77>> .*+.. .AGGQ__--  
 9G4u-"	  &1oon&E&K#%2%E%E%G"  !34 JJ22'($
	 "+(

;WTZZ@W"*#$2$C$C%2%C%C#.
 $**:6 "22' % $

	 3  #C!((V5MM3+. # /<
?+ }}%7<#'#>#>'KKM$  (
4DEa 9GdM
- Yq=4::999 $$T*=AZZ=S=Sj>:KIua &OOE!"IE"5$**5Q%tzz:Q   "'!,#'  #5zQ $L+>!2)0)E'2"J (..z:$&M(3{8K(L(RPR%"66)$)"[[$(JJ	 7 G  '%,,S[9sDk2  ' 3@J/%.]]4%<N'+'B'B)"[[#("+a-"0#- (C ($ ,J8HIc (d  #9~88F!G6F!G  		.1		"4"67C8C	  #"s   7[-[--[-c           	      f  ^ ^ U 4S jmU 4S jnUU 4S jnT R                   (       d  / $ / n/ nT R                  R                   GH+  nT R                   nUS:X  a	  U" U5      nOUR                  [        5       H[  n/ UR                  S/ 5      QT" U5      Q7n	U	 H3  n
U
S   nU
S   (       a  UR                  nUR                  U:X  d  M0  U
n  MW     Sn  O   Uc  UR                  U5        M  U" U5      nUc  M  T R                  R                  R                  (       a&  UR                  T R                  U5      S	   5        GM  UR                  T R                  UR                  5      5        GM.     U(       a7  [        S
SR!                  U5      < SSR!                  U" 5       5      < S35      eU$ )zS
Return a quoted list of arguments for the SELECT FOR UPDATE OF part of
the query.
c           	   3     >#    U S   R                   R                  nUR                   R                  R                  5        H  u  p#UR                   R                  nUUSU S    Vs/ s H[  nTR
                  U   S   R                  R                  U:X  d,  TR
                  U   S   R                  R                  U;   d  MY  UPM]     snS.v   M     g s  snf 7f)NrO   Fr   r   )rO   r   r  r   )r  r  parentsr   all_parentsrG   r   rO   )rI   r  parent_modelparent_linkr  select_indexr)   s         r-   _get_parent_klass_infoNSQLCompiler.get_select_for_update_of_arguments.<locals>._get_parent_klass_infos  s     '066EEN-;-A-A-I-I-O-O-Q)*00<<)($ -7,G&,GL !KK5a8??EEU#{{<8;BBHHKW %,G&	  .R&s   A#C&ACCCc                    > U S   R                   R                  nU S    HA  nTR                  U   S   R                  R                  U:X  d  M/  TR                  U   S   s  $    g)z
Find the first selected column from a model. If it doesn't exist,
don't lock a model.

select_fields is filled recursively, so it also contains fields
from the parent models.
rO   r   r   N)r  r  rG   r   rO   )rI   r  r  r)   s      r-   "_get_first_selected_col_from_modelZSQLCompiler.get_select_for_update_of_arguments.<locals>._get_first_selected_col_from_model  s`     (066EEN *? ;;;|,Q/66<<N;;|4Q77 !<r/   c               3     >^#    [         R                  " STR                  4/5      n U (       a  U R                  5       u  pUc  / mSv   OBUS   nUS   (       a  UR                  nXR
                  /-   m[        R                  " T5      v   U R                  U4S jT" U5       5       5        U R                  U4S jUR                  S/ 5       5       5        U (       a  M  gg7f)z<Yield all allowed field paths in breadth-first search order.Nr)   r   r  c              3   ,   >#    U  H	  nTU4v   M     g 7fr%   r1   rX   rI   r  s     r-   rZ   ]SQLCompiler.get_select_for_update_of_arguments.<locals>._get_field_choices.<locals>.<genexpr>  s      &H
 :&&H   c              3   ,   >#    U  H	  nTU4v   M     g 7fr%   r1   r  s     r-   rZ   r    s      &O
 :&&Or  r   )
collectionsdequerI   popleftr  r   r
   r"  r   r   )queueparent_pathrI   r   r  r  r)   s       @r-   r  JSQLCompiler.get_select_for_update_of_arguments.<locals>._get_field_choices  s     %%doo'>&?@E*/--/'&D L&w/E!), % 2 2&**5D$//$// &<Z&H   &0nn5JB&O  %s   CC$"C$r)   r   r   r  Nr   z<Invalid field name(s) given in select_for_update(of=(...)): r?  zI. Only relational fields followed in the query are allowed. Choices are: r   )rI   rC   ri  r   r
   r   r  r   r   r4   r   select_for_update_of_columnr   r   r   r   r"  )r)   r  r  r   invalid_namesr   rI   r   r*  klass_infosrelated_klass_infor   r  s   `           @r-   rp  .SQLCompiler.get_select_for_update_of_argumentsm  s   	&	8	. IJJ33DJv~8D JJz2D##(=rB#/
;#K /:* 27 ;-i8$)$6$6E ::-);J! /: &*
 3 %!((.8D??++GGMM$,,s"3A"67MM$">">syy"IJ9 4: 
 IIm,II023  r/   c                     0 n[        U5       Hf  u  p4U(       d  M  U R                  R                  R                  U5      nUR                  U R                  5      nU(       d	  U(       d  M^  XV-   U4X#'   Mh     U$ r%   )r   r4   r  get_db_converters)r)   r   
convertersir   backend_convertersfield_converterss          r-   get_convertersSQLCompiler.get_converters  sm    
&{3MAz%)__%8%8%J%J:%V"#-#?#?#P %)9)9%7%JJ$WJM 4 r/   c              #      #    U R                   n[        UR                  5       5      n[        [        U5       H/  nU H"  u  nu  pgXE   nU H  n	U	" XU5      nM     XU'   M$     Uv   M1     g 7fr%   )r4   rB  r   map)
r)   rowsr.  r4   rowposconvsr   value	converters
             r-   apply_convertersSQLCompiler.apply_converters  sn     __
***,-
tT?C,6((e!&I%eDE "' C	 -7
 I #s   A*A,c                 @   Uc  U R                  [        X4S9nU R                  SU R                    Vs/ s H  oUS   PM	     nnU R	                  U5      n[
        R                  " U5      nU(       a(  U R                  X5      nU(       a  [        [        U5      nU$ s  snf )z>Return an iterator over the results from executing this query.)chunked_fetch
chunk_sizer   )
execute_sqlr   rG   rd   r2  r   from_iterabler<  r5  r}  )	r)   r  tuple_expectedr?  r@  r  r  r.  r6  s	            r-   results_iterSQLCompiler.results_iter  s     ?&&] ' G !%A ?@ ?1A$ ?@((0
""7+((:D5$' As   Bc                 >    [        U R                  [        5      5      $ )zh
Backends (e.g. NoSQL) can override this in order to use optimized
versions of "query has any results."
)rp   rA  r   rP   s    r-   has_resultsSQLCompiler.has_results  s    
 D$$V,--r/   c                    U=(       d    [         n U R                  5       u  pEU(       d  [        e U(       a  U R
                  R                  5       nOU R
                  R                  5       n UR                  XE5        U[        :X  a  U$ U[        :X  aI   UR                  5       nU(       a  USU R                   UR                  5         $ UUR                  5         $ U[         :X  a  UR                  5         g[        UU R
                  R                   R"                  U R$                  (       a  U R                  OSU5      nU(       a%  U R
                  R                   R&                  (       d  [)        U5      $ U$ ! [         a    U[        :X  a  [	        / 5      s $  gf = f! [         a    UR                  5         e f = f! UR                  5         f = f)aM  
Run the query against the database and return the result(s). The
return value is a single data item if result_type is SINGLE, or an
iterator over the results if the result_type is MULTI.

result_type is either MULTI (use fetchmany() to retrieve all rows),
SINGLE (only retrieve a single row), or None. In this last case, the
cursor is returned if any query is executed, since it's used by
subclasses such as InsertQuery). It's possible, however, that no query
is needed, as the filters describe an empty set. In that case, None is
returned, to avoid any unnecessary database interaction.
Nr   )r   r5   r   r   iterr4   chunked_cursorcursorexecute	Exceptioncloser   r   fetchonerd   cursor_iterr   empty_fetchmany_valuerq   can_use_chunked_readsrB  )	r)   result_typer?  r@  r   r   rL  valr   s	            r-   rA  SQLCompiler.execute_sql  sz    "/Z	++-KC$$  __335F__++-F	NN3' & M& oo'q4>>2   *$LLNOO$$::"33DNN	
 DOO$<$<$R$R
 <Y  	e#Bx		  	LLN	  s/   E; .F" %G G ;FF"F>Gc                    UR                   nU R                  R                  R                  n[	        U R
                  R                  5       Hj  u  pgU R                  U5      u  pU" U5      < SU" X&   5      < 3n
U R
                  R                  R                  [        U< SU
< 3U	5      [        5        Ml     U R                  5       u  pSU-  U4$ )Nr    = zEXISTS (%s))r   r4   r  r  r   rC   rG   r   rj   r   r   r   r5   )r)   r   r  r3   qnqn2r  
select_collhs_sql
lhs_paramsrhsr   r   s                r-   as_subquery_condition!SQLCompiler.as_subquery_conditionJ  s    --oo!!,,!*4::+<+<!=E"&,,z":GYGN(;<CJJ  Wc(BJ!OQTU ">
 kkms"F**r/   c           
   #     #    [        U R                  5       5      nU R                  R                  R                  nU(       a$  UR                  5       S:X  a  [        R                  O[        nU HP  nU HG  n[        U[        5      (       d+  SR                  U Vs/ s H
  oc" U5      PM     sn5      v   MC  Uv   MI     MR     g s  snf 7f)Njsonr[  )rB  rA  rC   rt  r  lowerrb  dumpsr2   r~   r"  )r)   r   format_output_formatterr7  r:  cs          r-   explain_querySQLCompiler.explain_queryV  s     d&&() **))00)0W]]_5N4::TWC!%--((#GA$4Q$7#GHHK	   $Hs   BC	C,C	)rJ   rH   rd   r4   rE   rq   rm   rI   rn   rC   rF   rG   rD   rj   )T)F)TF)NNN)Nr   N)NNrx   NN))r7   r8   r9   r:   r    re	MULTILINEDOTALLr   r(   rQ   re   ru   rr   r   rb   r   ri   ro   r   r   r+  rO  r5   r   r^  r   r  r_  r   rp  r2  r<  r   rD  rG  r   rA  r_  rh  r;   r1   r/   r-   r?   r?   '   s    % 
ryy N
#"
*0 eN@U,nM^;z,N`^@a9H EI0d6 IM?
BL#R g#RhT	 **.  uAX@D
+ r/   r?   c                   D    \ rS rSrSrSrS rS rS rS r	S r
SS	 jrS
rg)SQLInsertCompilerid  Nr1   c                    Uc  U/ pCOY[        US5      (       a  U R                  U5      u  p4O4[        US5      (       a  UR                  X U R                  5      U/pCOSU/pCU R                  R                  R                  X45      nX44$ )aD  
Take a field and a value intended to be saved on that field, and
return placeholder SQL and accompanying params. Check for raw values,
expressions, and fields with get_placeholder() defined in that order.

When field is None, consider the value raw and use it as the
placeholder, with no corresponding parameters returned.
r5   get_placeholder%s)r|   r   rp  r4   r  modify_insert_params)r)   r   rU  r   r   s        r-   field_as_sqlSQLInsertCompiler.field_as_sqlh  s     =rS(##,,s+KCU-..  //4??KcU  $$99#F{r/   c                    [        US5      (       a  UR                  U R                  SSS9nUR                  (       a  [	        SU< SU< S35      eUR
                  (       a  [        SUR                  < S	U< S
35      eUR                  (       a  [        SUR                  < S	U< S
35      eUR                  X R                  S9$ )z
Prepare a value to be used in a query by resolving it if it is an
expression and otherwise calling the field's get_db_prep_save().
r   FTr   for_savezFailed to insert expression "z" on z<. F() expressions can only be used to update, not to insert.3Aggregate functions are not allowed in this query (=).2Window expressions are not allowed in this query (r4   )r|   r   rC   contains_column_references
ValueErrorcontains_aggregater   r   contains_over_clauseget_db_prep_saver4   )r)   r   r:  s      r-   prepare_valueSQLInsertCompiler.prepare_value  s    
 5.//,,

 - E // DI5R  '' "'**e5  )) zz5*  %%e%HHr/   c                     U R                   R                  (       a  [        X!R                  5      $ UR	                  USS9$ )z
Get the given field's value off the given obj. pre_save() is used for
things like auto_now on DateTimeField. Skip it if this is a raw query.
T)r   )rC   rawr   r  pre_save)r)   r   r  s      r-   pre_save_valSQLInsertCompiler.pre_save_val  s1    
 ::>>3..~~ct~,,r/   c                    ^ ^ U(       d  / / 4$ UU 4S jU 5       nS U 5       n[        U6 u  pVU VVV	s/ s H  ow VV	s/ s H  o  H  oPM     M     sn	nPM!     nnnn	XV4$ s  sn	nf s  sn	nnf )a  
Take a sequence of N fields and a sequence of M rows of values, and
generate placeholder SQL and parameters for each field and value.
Return a pair containing:
 * a sequence of M rows of N SQL placeholder strings, and
 * a sequence of M rows of corresponding parameter values.

Each placeholder string may contain any number of '%s' interpolation
strings, and each parameter row will contain exactly as many params
as the total number of '%s's in the corresponding placeholder row.
c              3   P   >#    U  H  nU4S  j[        TU5       5       v   M     g7f)c              3   J   >#    U  H  u  pTR                  X5      v   M     g 7fr%   )rs  )rX   r   vr)   s      r-   rZ   >SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>.<genexpr>  s#     J9IXUTu((9Is    #Nr!  )rX   r7  r  r)   s     r-   rZ   4SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>  s(      !
! KVS9IJJ!s   #&c              3   2   #    U  H  n[        U6 v   M     g 7fr%   r  )rX   r7  s     r-   rZ   r    s     "N8M398Ms   r  )
r)   r  
value_rowsrows_of_fields_as_sqlsql_and_param_pair_rowsplaceholder_rows
param_rowsr7  psr  s
   ``        r-   assemble_as_sql!SQLInsertCompiler.assemble_as_sql  s     r6M!
!!
 #O8M"N (+,C'D$ @JJz333R1qq33z
J++ 4Js   
A'A!A'!A'c                 d	  ^ U R                   R                  R                  mU R                  R	                  5       nU R                   R                  R                  U R                  R                  S9nU< ST" UR                  5      < 3/nU R                  R                  =(       d    UR                  /nUR                  SSR                  U4S jU 5       5      -  5        U R                  R                  (       aX  U R                  R                   VVs/ s H4  nU Vs/ s H#  nU R                  X`R                  Xe5      5      PM%     snPM6     nnnOKU R                  R                   Vs/ s H'  oR                   R                  R                  5       /PM)     nnS /nU R                   (       + =(       a     U R                   R"                  R$                  n	U R'                  XG5      u  pU R                   R                  R)                  UU R                  R                  S U R                  R*                   5       S U R                  R,                   5       5      nU R                   (       GaT  U R                   R"                  R.                  (       Ga.  U R                   R"                  R0                  (       a7  UR                  U R                   R                  R3                  XJ5      5        UnO,UR                  SSR                  U
S	   5      -  5        US	   /nU(       a  UR                  U5        U R                   R                  R5                  U R                   5      u  ol        U(       a   UR                  U5        XR6                  /-  nSR                  U5      [9        [:        R<                  " U5      5      4/$ U	(       ap  UR                  U R                   R                  R3                  XJ5      5        U(       a  UR                  U5        SR                  U5      [9        S
 U 5       5      4/$ U(       a  UR                  U5        [?        X5       VVs/ s H/  u  nnSR                  USSR                  U5      -  /-   5      U4PM1     snn$ s  snf s  snnf s  snf s  snnf )N)on_conflictr[  z(%s)r?  c              3   H   >#    U  H  nT" UR                   5      v   M     g 7fr%   r   )rX   r  rY  s     r-   rZ   +SQLInsertCompiler.as_sql.<locals>.<genexpr>  s     (Fv!AHHv   "c              3   8   #    U  H  oR                   v   M     g 7fr%   r  r  s     r-   rZ   r         87!XX7   c              3   8   #    U  H  oR                   v   M     g 7fr%   r  r  s     r-   rZ   r    r  r  zVALUES (%s)r   c              3   6   #    U  H  o  H  o"v   M     M     g 7fr%   r1   )rX   r  r  s      r-   rZ   r    s     ,P2RQRQs   ) r4   r  r  rC   r   insert_statementr  db_tabler  r  r   r"  objsr  r  pk_default_valuereturning_fieldsr   has_bulk_insertr  on_conflict_suffix_sqlupdate_fieldsunique_fieldscan_return_columns_from_insert can_return_rows_from_bulk_insertbulk_insert_sqlreturn_insert_columnsreturning_paramsr}  r   rB  r!  )r)   r  r  r   r  r  r   r  r   can_bulkr  r  r  r   r_sqlr  valsrY  s                    @r-   r5   SQLInsertCompiler.as_sql  s    __  ++zz""$??..??

.. @ 
 .r$--/@AB""/twwiftyy(Fv(FFFG::  ::??
 +C "(!' &&u.?.?.KL!' +  J CG**//BQQ$$5578/   VF %%%R$//*B*B*R*R 	 (,';';F'O$!%!4!4!K!KJJ""8tzz7788tzz778	"
 !!!((GGG''HHOO''77Q $mdii8H8K.LLM$Q-%45 ,0??+>+>+T+T%%,(E( e$0011XXf%uU-@-@-H'IJKKMM$//--==fWX%45XXf%u,P,P'PQRR%45  ##3@@GAt &MDIIaL$@#AABDI@ {js$   
R!"*RR!/.R'"6R,R!c                    U(       aJ  [        U R                  R                  5      S:w  a'  U R                  R                  R
                  (       d   eU R                  R                  5       nXl        / nU R                  R                  5        nU R                  5        H  u  pVUR                  XV5        M     U R                  (       d  / sS S S 5        $ U R                  R                  R
                  (       a~  [        U R                  R                  5      S:  a[  U R                  R                  R                  U5      nU R                   Vs/ s H  oR                  UR                  5      PM     nnGOU R                  R                  R                  (       a  [        U R                  R                  5      S:X  d   eU R                  R                  R!                  UU R"                  5      /nU R                   Vs/ s H  oR                  UR                  5      PM     nnOmUR$                  R                  UR                  5      /nU R                  R                  R'                  UUR                  UR$                  R(                  5      4/nS S S 5        U R+                  U5      n	U	(       a  [-        U R/                  WU	5      5      nW$ s  snf s  snf ! , (       d  f       NM= fr   )rc   rC   r  r4   r   r  r   r  rL  r5   rM  r  fetch_returned_insert_rowsr  r  r  fetch_returned_insert_columnsr  r  last_insert_idr   r2  rB  r<  )
r)   r  r  r   rL  r   r   r6  r   r.  s
             r-   rA  SQLInsertCompiler.execute_sql!  s,   DJJOO$)OO,,MM	
 

 zz""$ 0__##%#{{}s+  -((	 &% ((II

(1,**EEfMBFBWBWXBWdmm4BWX))HH4::??+q000OO''EE-- CGBWBWXBWdmm4BWX67 ++::" MM GGNN- &> ((.
--dJ?@D1 Y Y' &%s3   =KA;K$K4BK$K%A0K
K
K*)r  r  r%   )r7   r8   r9   r:   r  r  rs  r  r  r  r5   rA  r;   r1   r/   r-   rn  rn  d  s1    @I:-!,FN`+r/   rn  c                   P    \ rS rSr\S 5       r\S 5       r\S 5       rS r	S r
Srg)	SQLDeleteCompileriO  c                    ^  T R                   R                  5         [        U 4S jT R                   R                   5       5      S:H  $ )Nc              3   \   >#    U  H!  nTR                   R                  U   S :  v   M#     g7frU   rV   )rX   r   r)   s     r-   rZ   1SQLDeleteCompiler.single_alias.<locals>.<genexpr>T  s'     R=Q4::,,Q/!3=Qr\   rx   )rC   ra   sumr`   rP   s   `r-   single_aliasSQLDeleteCompiler.single_aliasP  s6     	

$$&RTZZ=Q=QRRVWWWr/   c                    ^ ^ [        U[        5      (       a  UR                  T:H  $ [        US5      (       d  g[	        UU 4S jUR                  5        5       5      $ )Nr1  Fc              3   H   >#    U  H  nTR                  UT5      v   M     g 7fr%   )_expr_refs_base_model)rX   source_expr
base_modelclss     r-   rZ   :SQLDeleteCompiler._expr_refs_base_model.<locals>.<genexpr>\  s(      
< %%k:>><r  )r~   r   rO   r|   anyr1  )r  r   r  s   ` `r-   r  'SQLDeleteCompiler._expr_refs_base_modelV  sS    dE""::++t566 
#::<
 
 	
r/   c                    ^  [        U 4S j[        T R                  R                  R	                  5       T R                  R
                  R                  5       5       5      $ )Nc              3   p   >#    U  H+  nTR                  UTR                  R                  5      v   M-     g 7fr%   )r  rC   rO   )rX   r   r)   s     r-   rZ   ESQLDeleteCompiler.contains_self_reference_subquery.<locals>.<genexpr>c  s6      
 &&tTZZ-=-=>>s   36)r  r   rC   r   rA  rj   childrenrP   s   `r-    contains_self_reference_subquery2SQLDeleteCompiler.contains_self_reference_subquerya  sJ     


&&--/1A1A1J1J
 
 	
r/   c                     SU R                  UR                  5      -  n U R                  UR                  5      u  p4U SU 3[        U5      4$ ! [         a    US4s $ f = f)NzDELETE FROM %sr1   z WHERE )r   
base_tabler   rj   r   r}  )r)   rC   deleterj   r   s        r-   _as_sqlSQLDeleteCompiler._as_sqlj  sm    !D$@$@AQAQ$RR	 LL5ME (%-77  	2:	s   A A A c                     U R                   (       aQ  U R                  R                  R                  (       d  U R                  (       d  U R                  U R                  5      $ U R                  R                  5       n[        Ul	        UR                  5         U R                  R                  R                  R                  nUR                  U R                  R                  5       5      /Ul        [        U R                  R                  5      nU R                  R                  R"                  (       d8  UR%                  U R                  S9R'                  5       u  pE[)        SU-  U5      nUR+                  SU5        U R                  U5      $ )N
Create the SQL for this query. Return the SQL string and list of
parameters.
r|  zSELECT * FROM (%s) subquerypk__in)r  r4   r   "delete_can_self_reference_subqueryr  r  rC   r  r   r,   clear_select_clauserO   r  r  r  ra   rG   update_can_self_selectr  r5   r   
add_filter)r)   innerqr  outerqr   r   s         r-   r5   SQLDeleteCompiler.as_sqlr  s   
 OO$$GG88<<

++!!# ""$ZZ##&&DJJ$@$@$BCDtzz''(''>> !---IPPRKC9C?HF(F+||F##r/   r1   N)r7   r8   r9   r:   r   r  classmethodr  r  r  r5   r;   r1   r/   r-   r  r  O  sH    X X
 
 
 
 
8$r/   r  c                   :   ^  \ rS rSrS rU 4S jrU 4S jrSrU =r$ )SQLUpdateCompileri  c           
         U R                  5         U R                  R                  (       d  gU R                  n/ / p2U R                  R                   GH  u  pEn[	        US5      (       az  UR                  U R                  SSS9nUR                  (       a  [        SUR                  < SU< S35      eUR                  (       a  [        S	UR                  < SU< S35      eO`[	        US
5      (       aO  UR                  (       a  UR                  U5      nO,[        SU< SU< SUR                  R                  < S35      eUR                  X`R                   S9n[	        US5      (       a  UR#                  X`U R                   5      nOSnUR$                  n[	        US5      (       aG  U R'                  U5      u  pUR)                  U" U5      < SXy-  < 35        UR+                  U
5        GM  Ub2  UR)                  U" U5      < SU< 35        UR)                  U5        GM  UR)                  SU" U5      -  5        GM     U R                  R,                  nSU" U5      -  SR/                  U5      /n U R'                  U R                  R0                  5      u  pUR)                  SU-  5        SR/                  U5      [5        X:-   5      4$ ! [2         a    / n
 N,f = f)r  )rR  r1   r   FTrv  rx  ry  rz  r{  prepare_database_savezTried to update field z with a model instance, z. Use a value compatible with r   r|  rp  rq  r5   rX  z	%s = NULLzUPDATE %s SETr?  rX  r[  )ru   rC   rA  r   r|   r   r  r   r   r  r  r  	TypeErrorr,   r7   r  r4   rp  r   r   r   r   r  r"  rj   r   r}  )r)   rY  rA  update_paramsr   rO   rU  placeholderr   r   r   r   r   rj   s                 r-   r5   SQLUpdateCompiler.as_sql  s|   
 	zz  )) "B!%!2!2E#s011,,JJED -  ))$&+jj#7  ++$&+jj#7  ,
 566%%33E:C# !#u'?'?A 
 (((IC u/00#33CtO"<<DsH%%"ll3/2d8[5FGH$$V,2d8[AB$$S)kBtH45Q "3R 

%%bi'IIf
	. LL)9)9:ME MM*u,-xx}'=!>>>	  	F	s   8'K K K c                   > [         TU ]  U5      n U(       a  UR                  OSnUSL nU(       a  UR                  5         U R                  R                  5        HC  nUR                  U R                  5      R                  U5      nU(       d  M6  U(       d  M?  UnSnME     U$ ! U(       a  UR                  5         f f = f)z
Execute the specified update. Return the number of rows affected by
the primary update query. The "primary update query" is the first
non-empty query that is executed. Row counts for any subsequent,
related queries are not available.
r   NF)r'   rA  rowcountrO  rC   get_related_updatesr  rD   )r)   rT  rL  r6  is_emptyrC   aux_rowsr,   s          r-   rA  SQLUpdateCompiler.execute_sql  s     $[1	&,6??!D~HZZ335E))$**5AA+NHxHH 	 6
   s   B% %B?c                   >^ U R                   R                  R                  5       nU R                   R                  5         U R                   R	                  5       nU R                   R
                  (       d  US:X  a  gU R                   R                  [        S9nSUl        UR                  SS9  0 Ul
        / Ul        UR                  5       nUR                  R                  /n/ nU R                   R
                   H  n[        S UR!                  U5       5       5      (       a  UR#                  US45        M>  UR#                  U[%        U5      45        UR#                  UR&                  R                  R                  5        M     UR)                  U5        [*        TU ]Y  5         US:  =(       a%    U R.                  R0                  R2                  (       + nU R                   R5                  5         U R                   R
                  (       d  U(       a  / n	[6        R8                  " [:        5      n
UR=                  U R>                  5      RA                  [B        5       HD  nU	RE                  S	 U 5       5        U H#  u  nmX   RE                  U4S
 jU 5       5        M%     MF     U R                   RG                  SU	5        XR                   l$        OU R                   RG                  SU5        U R                   RK                  U5        g)a  
If the update depends on results from other tables, munge the "where"
conditions to match the format required for (portable) SQL updates.

If multiple updates are required, pull out the id values to update at
this point so that they don't change as a result of the progressive
updates.
rx   N)klassFT)forcec              3   L   #    U  H  oR                   R                  v   M     g 7fr%   )
join_fieldr   )rX   r  s     r-   rZ   2SQLUpdateCompiler.pre_sql_setup.<locals>.<genexpr>  s      8X++8Xs   "$r   c              3   *   #    U  H	  oS    v   M     g7frU   r1   )rX   r  s     r-   rZ   r    s     1DqdDs   c              3   ,   >#    U  H	  oT   v   M     g 7fr%   r1   )rX   r  r  s     r-   rZ   r    s     .FAxr  r  )&rC   rW   r   ra   count_active_tablesrelated_updatesr   r   r   clear_orderingr   rG   r   r  r   r_   get_path_to_parentr   rc   r  
add_fieldsr'   ru   r4   r   r  clear_wherer!  defaultdictrB  r  rD   rA  r   r   r  related_idsr~  )r)   r  countrC   r   r  related_ids_indexrelatedmust_pre_selectidentsr  r6  parentr  r,   s                @r-   ru   SQLUpdateCompiler.pre_sql_setup  sV     ::4499;

$$&

..0zz))eqj

  u -$4(~~'',,zz11G 8<8O8OPW8X  
 "(('16
 "(('3v;)?@gmm..334 2 	  AIMdoo66MMM 	 	

 ::%% F%11$7K**4::6BB5I1D11%6MFE'...F.FF &7 J JJ!!(F3%0JJ" JJ!!(E2

""#34r/   r1   )	r7   r8   r9   r:   r5   rA  ru   r;   r<   r=   s   @r-   r  r    s    >?@*=5 =5r/   r  c                       \ rS rSrS rSrg)SQLAggregateCompileri!  c                 "   / / p!U R                   R                  R                  5        HL  nU R                  U5      u  pEUR	                  XU5      u  pEUR                  U5        UR                  U5        MN     [        U R                   R                  5      U l        SR                  U5      n[        U5      nU R                   R                  R                  U R                  U R                  S9R                  SS9u  pgSU< SU< S3nX'-  nX4$ )r  r?  )rE   Tr]   rY  rZ  r\  )rC   r   rA  r   r   r   r   rc   rd   r"  r}  r4  r  rD   rE   r5   )r)   r   r   r   ann_sql
ann_paramsinner_query_sqlinner_query_paramss           r-   r5   SQLAggregateCompiler.as_sql"  s    
 "V**66==?J"&,,z":G",":":4*"UGJJwMM*%	 @
 TZZ99:iinv.2jj.D.D.Q.QJJ(( /R /
 &$&
' 	,+ 14_E${r/   )rd   N)r7   r8   r9   r:   r5   r;   r1   r/   r-   r  r  !  s    r/   r  c              #      ^ ^#     [        U U4S jU5       H  nUc  UOU Vs/ s H  oUSU PM	     snv   M!     T R                  5         gs  snf ! T R                  5         f = f7f)zO
Yield blocks of rows from a cursor and ensure the cursor is closed when
done.
c                  &   > T R                  T5      $ r%   )	fetchmany)rL  itersizes   r-   r  cursor_iter.<locals>.<lambda>@  s    &"2"28"<r/   N)rJ  rO  )rL  sentinelrd   r  r6  r  s   `  `  r-   rQ  rQ  :  sY     
<xHD#+$1NAJY-1NN I 	 2Os+   A%A A	A A%A A""A%)8r!  rb  rj  	functoolsr   	itertoolsr   django.core.exceptionsr   r   r   	django.dbr   r	   django.db.models.constantsr
   django.db.models.expressionsr   r   r   r   r   django.db.models.functionsr   r   django.db.models.lookupsr   django.db.models.query_utilsr   django.db.models.sql.constantsr   r   r   r   r   r   django.db.models.sql.queryr   r   django.db.models.sql.wherer   django.db.transactionr   django.utils.functionalr   django.utils.hashabler   django.utils.regex_helperr    r"   r?   rn  r  r  r  rQ  r1   r/   r-   <module>r%     s      	   L L 6 1 G G 3 + ?  < * < 3 / 6%# %z  z z1h hV9$ 9$xS5 S5l; 2	r/   