
    hhh                       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Jr  S SK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'J(r(  S SK)J*r*J+r+  S SK,J-r-  S SK.J/r/  S SK0J1r1  S SK2J3r3   " S S\5      r4 " S S5      r5 " S S\55      r6 " S S\55      r7 " S S\55      r8 " S S\55      r9S r:g)    N)partial)chain)EmptyResultSet
FieldErrorFullResultSet)DatabaseErrorNotSupportedError)
LOOKUP_SEP)ColPairsFOrderByRawSQLRefValue)	AutoField	composite)CastRandom)Lookup)select_related_descend)CURSORGET_ITERATOR_CHUNK_SIZEMULTI
NO_RESULTS	ORDER_DIR	ROW_COUNTSINGLE)Queryget_order_dir)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       XD:\Anime\Ugyen\janka_web_project\venv\Lib\site-packages\django/db/models/sql/compiler.pyr+   PositionRef.__init__    s    &    c                 0    [        U R                  5      S4$ )N )strr)   )r,   compiler
connections      r0   as_sqlPositionRef.as_sql$   s    4<< "$$r2   )r)   )__name__
__module____qualname____firstlineno__r+   r8   __static_attributes____classcell__r/   s   @r0   r%   r%      s    '% %r2   r%   c                   R   \ 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 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 r"S r#SSS\$4S  jr%S! r&\'S\$4S" j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*)	queryr7   usingelide_emptyquote_cacheselectannotation_col_map
klass_info_meta_ordering)r,   rF   r7   rG   rH   s        r0   r+   SQLCompiler.__init__/   sE    
$
 ':
 "&"r2   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/   r<   rF   modelr7   rG   r,   s    r0   __repr__SQLCompiler.__repr__@   sU    ++, -ZZ%%223 4//,GDJJ>D	
r2   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rF   alias_refcount).0ar,   s     r0   	<genexpr>*SQLCompiler.setup_query.<locals>.<genexpr>H   s'     O:NQtzz((+q0:N   ),with_col_aliases)
allrF   	alias_mapget_initial_alias
get_selectrJ   rL   rK   len	col_count)r,   ra   s   ` r0   setup_querySQLCompiler.setup_queryG   sg    O$**:N:NOOOJJ((*@D- AP A
=T_d&= T[[)r2   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)rh   get_order_byrF   wheresplit_having_qualifygroup_byhavingqualifyget_extra_selectrJ   boolhas_extra_selectget_group_by)r,   ra   order_byextra_selectro   s        r0   pre_sql_setupSQLCompiler.pre_sql_setupO   s     	*:;$$&04

0@0@0U0U**--T9 1V 1
-
DK ,,X{{C $\ 2$$T[[<%?Jx//r2   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".
Tr8      startr4   )rF   ro   sethasattrresolve_ref
isinstancer   r-   addappendr.   	enumerateextendget_group_by_colsrM   rp   collapse_group_byr7   featuresallows_group_by_select_indexcompiler   r   getr5   select_formatr"   )r,   rJ   rv   expressionsgroup_by_refsexprselected_expr_positionsr)   _aliassqlparamsis_refhaving_group_byresultseenr   positionparams_hashs                      r0   ru   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   )
r7   r   allows_group_by_selected_pksr   r   primary_key%allows_group_by_selected_pks_on_modelrR   r   getattr)r,   r   rp   r   pksaliasess         r0   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           	      8  ^ / nSn0 n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/ nSnU R                   R
                  c  / S U R                   R                  R                  5        5       QS U 5       QU R                   R                  R                  5       Qn[        [        [        U R                   R                  5      [        U R                   R                  5      [        U5      -   5      5      nO/ n[        U R                   R
                  R                  5       5       H  u  n	u  p[        U[        5      (       a  U R                   R                  U   nO*[        U[         5      (       a  UR#                  U	5        Xk   n[        U[$        5      (       a  Sn
UR#                  X45        M     U(       a  U R                   R&                  US.n[        U5       H%  u  nu  pU
(       a  XU
'   UR#                  X45        M'     U R                   R(                  (       a#  U R+                  X%5      nXS'   U4S jmT" U5        / nSnU H[  u  nn
 U R-                  U5      u  nnUR/                  U UU5      u  nnU
c  U(       a
  SU 3n
US-  nUR#                  UUU4U
45        M]     XU4$ ! [0         aA    [3        US[4        5      nU[4        L a  S	u  nn N]U R-                  [7        U5      5      u  nn N|[8         a     U R-                  [7        S
5      5      u  nn 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.
Nc              3   :   #    U  H  u  pU[        U6 4v   M     g 7fr(   )r   )r[   r   argss      r0   r]   )SQLCompiler.get_select.<locals>.<genexpr>  s"      'F FDM*'Fs   c              3   *   #    U  H	  nS U4v   M     g 7fr(   r4   )r[   cols     r0   r]   r     s     .#4+s   )rR   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   r4   )rL   kiget_select_from_parents     r0   r   6SQLCompiler.get_select.<locals>.get_select_from_parent.  sA    $%:;B-(&7"_:MM ?+ +2. <r2   r{   empty_result_set_value)0r4   Tr   )rF   rJ   default_colsget_select_maskget_default_columnsselectedrw   itemsannotation_selectlistrangerf   r   r   r5   annotationsintr   r   rR   select_relatedget_related_selectionsr   r   r   r   NotImplementedr   r   )r,   ra   rJ   rL   r   select_maskcolsr   r   indexr   
expression
select_idxr   retcol_idxr   r   r   r   r   s                       @r0   re   SQLCompiler.get_select   s8   $ 
JJ%%$***A*ABBjj002::""++K8D ::$$D::&'+zz'>'>'D'D'F
 /. --335H !

//0

//03t9<M M.7

8K8K8Q8Q8S.T**j#..!%!7!7
!CJ
C00!((/!%!1Jj(33 E 34 /U #'::#3#3mTJ/8/B+J+%/E"MM:-. 0C
 ::$$"&"="=f"R0C,-/ #:. JCC"ll3/V "//c6BV}!1gY1JJc6]E23' !( ++# " N)01>*& *^;")KC"&,,u5K/L"MKC  8"ll5;7V8s   
L&&)NN0&NN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DESCr{   r|   resolve_expression?F
descendingTz;Ordering combined queries by transforms is not implemented..)default_order).rF   extra_order_bydefault_orderingrv   get_metaorderingrM   standard_orderingr   rJ   r   r%   r   r   r   r   output_fieldr   asccopyreverse_orderingr   r   r   namenulls_first
nulls_lastr7   r    supports_order_by_nulls_modifier
combinatorr   r-   r.   r   r   r   splitr
   NotImplementedErrortry_transformr   quote_name_unless_aliasextrarw   find_ordering_name)r,   r   metar   r   selected_exprsrJ   r)   r   r   pos_exprfield
select_refr   orderr   ref
transformsr   tables                       r0   _order_by_pairsSQLCompiler._order_by_pairsP  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reuser{   __orderbycolz:ORDER BY term does not match any column in the result set.)r~   r   r   rF   r   rJ   r   has_select_fieldsr   r   r   r   set_source_expressionsr   r   columnrf   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                    r0   rl   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` r2   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 )Nr{   )rF   distinctdistinct_fieldsr   r   r   )r,   rv   rJ   rw   t
select_sqlr   r   r   r   r  s              r0   rr   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).
)
rI   rF   rc   	table_maprw   external_aliasesr   r7   ops
quote_name)r,   r   rs      r0   r   #SQLCompiler.quote_name_unless_alias&  s     #####D))ZZ)))d**:N:N.Nzz...

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

 4 44 &*T"KOO**40!"r2   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   r7   vendorr8   )r,   nodevendor_implr   r   s        r0   r   SQLCompiler.compile<  sZ    dEDOO,B,B$BDI%dOO<KC { ++dOO<KC{r2   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                  (       dO  U HI  nUR                  R                  (       a  [        S5      eUR                  5       (       d  M@  [        S5      e   / nS nU H$  n UR                  U R                  U5      5        M&     U(       d  [        e[        U5      S:X  aH  US:X  aB  U R                  R                  (       a'  SUl        UR                  U R                  U5      5        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[%        U4S
 jU 5       6 u  pSR'                  U	5      R)                  U
5      /n/ nU H  nUR+                  U5        M     X4$ s  snf ! [         a    US:X  d  US:X  a  U(       a  Un GMm  e f = f)Nz>LIMIT/OFFSET not allowed in subqueries of compound statements.z:ORDER BY not allowed in subqueries of compound statements.union
differencer{   Fz ALLz{}({})c              3   N   >#    U  H  u  pTR                  U5      U4v   M     g 7fr(   )format)r[   r   r   bracess      r0   r]   1SQLCompiler.get_combinator_sql.<locals>.<genexpr>q  s!     @%YSv}}S!4(%   "%z {} )r7   r   rF   r   get_compilerrG   rH   %supports_slicing_ordering_in_compound	is_slicedr   rl   r   _get_combinator_part_sqlr   rf   r  set_operatorssubqueryzipr  joinr   )r,   r   rb   r   rF   	compilersr6   partsempty_compilercombinator_sql	sql_parts
args_partsr   r   partr  s                  @r0   get_combinator_sqlSQLCompiler.get_combinator_sqlD  s   ??++ 44
4 tzz4??D<L<LM4 	 
 ==%>>++'X  ((**'T  & !HT::8DE "   Z1_w!64::;O;O */N&LL66~FG,,:::F:(f$Nzz""x'U'UF #@%@!
	 --/44Y?@DMM$ ~c
& "  (Z<-GE%-Ns   ;H HI Ic                    U R                   R                  nU R                  R                  nUbQ  UR                  R                  c:  UR                  R	                  5       Ul        UR                  R                  U5        UR                  SS9u  pEUR                  R                  (       ae  UR                  (       d  SR                  U5      nXE4$ U R                  R                  (       d  UR                  (       d  SR                  U5      nXE4$ U R                  R                  (       a"  UR                  (       a  SR                  U5      nXE4$ )NTr`   zSELECT * FROM ({})r  )r7   r   rF   r   clone
set_valuesr8   r    supports_parentheses_in_compoundr  r'  r#  )r,   r6   r   r   part_sql	part_argss         r0   r%  $SQLCompiler._get_combinator_part_sqly  s   ??++ ::&&HNN$;$;$C%^^113HNNN%%h/&ootoD>>$$ <</66x@ "" 

##EE!==2 "" ZZ  X%S%S}}X.H""r2   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-   rf   r   r   )	r   r   select_aliasnum_qual_aliasinner_queryqual_aliasesreplacementsrJ   select_aliasess	       r0   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&! +r2   )r7   rH   F)with_limitsra   SELECT * FROM ()rq   WHERESELECT, zFROM (qualify_maskzORDER BY)rm   r   rp   rF   r4  r'  r/   re   r~   valuesr   rq   leavesreplace_expressionsr   r   rl   r>  r"  rG   r7   rH   r8   r   r  r  r)  r   )r,   where_partsr   r   r   rE  rv   order_by_exprinner_query_compiler	inner_sqlinner_paramsqualify_sqlqualify_paramsr   r   r   ordering_sqlsr   ordering_sqlordering_paramsrA  rB  rC  rJ   rD  s                       @@@@@r0   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 = 1rK  z AS rL  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 %srH  rI  r{   r|   r'  r   SELECT  FROM ( 
) subquery)ErF   rZ   r   r   rx   rs   r$  r7   r   r   r  r	   r1  combinator_allrq   r[  get_distinctget_from_clauserm   r   r   rH   r   rp   r  r  distinct_sqlrJ   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_orderingrM   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   r8   tuplereset_refcounts)-r,   rG  ra   refcounts_beforer   rw   rv   ro   for_update_partwith_limit_offsetr   r   r   r  distinct_paramsfrom_f_paramsrm   w_paramsrp   h_paramsdistinct_resultout_colsr   s_sqls_paramsr   r`  ra  rb  rc  groupingg_sqlg_paramsr   o_sqlo_paramsorder_by_sqlsub_selects
sub_paramsr   rJ   select_clone	subselect	subparamss-                                                r0   r8   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                    / nUc  U R                   R                  5       =nc  U$ U=(       d    U R                   R                  5       nSU0n[        [        R
                  " U5      5      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  X;  a  M  U R                   R                  X9X&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)rF   r   rd   r~   r   unnestconcrete_fieldsrR   _metaconcrete_model
issubclassjoin_parent_modelget_colr   )r,   r   start_aliasoptsr   r   seen_modelsselect_mask_fieldsr   rR   r   r   s               r0   r   SQLCompiler.get_default_columns  s    <

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

"%%%44ekk6P6P  u>JJ00kWE]]5)FMM&!/ *0 r2   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)rF   r   r  r   r
   _setup_joins
trim_joinsr   r   r7   r  r  r   )r,   r   r   r  r   r+  r   targetsr   joinspathtransform_functionr   r  ps                  r0   rj  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!$ " / ~r2   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   rF   rc   )r[   jr,   s     r0   r]   1SQLCompiler.find_ordering_name.<locals>.<genexpr>E  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
   )r[   r   r   r   s      r0   r]   r  W  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   rF   r  )r,   r   r  r   r   already_seenr   r   piecesr   r  r  r  r  
join_tupleresultsitemr   r	  s   ``                 r0   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  )rF   rd   setup_joins)	r,   r  r  r   r   r  r  r  r  s	            r0   r  SQLCompiler._setup_joinsd  sW     7557@D

@V@V%A
=T b	uT9KKKr2   c                 P   / n/ n[        U R                  R                  R                  5       5       HZ  u  p4U R                  R                  U   (       d  M%  U R                  U5      u  pVUR                  U5        UR                  U5        M\     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  
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.
r{   z, %s)r  rF   rc   r   rZ   r   r   r   extra_tablestable_aliasr   )	r,   r   r   r   from_clause
clause_sqlclause_paramsr	  r   s	            r0   rk  SQLCompiler.get_from_clauses  s      #(

(<(<(B(B(D"EE::,,U3(,[(A%JMM*%MM-( #F ((Azz--a0HE
 ZZ111::,,U3q8ft'C'CE'JJK ) ~r2   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   r[   fs     r0   r]   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     r0   r]   r    s4      -A77>> -**,,-s
   A!A)fieldsrelated_objectsr   rF   _filtered_relations)direct_choicesreverse_choicesr  r,   s     r0   _get_field_choices>SQLCompiler.get_related_selections.<locals>._get_field_choices  sD    KdkkKN--O
 1O1O r2   Nc                     XS'   g )Nr   r4   )rL   r   s     r0   get_related_klass_infosCSQLCompiler.get_related_selections.<locals>.get_related_klass_infos  s    0C,-r2   z/Non-relational field given in select_related: 'z'. Choices are: rL  z(none)Fc                     g r(   r4   )xys     r0   <lambda>4SQLCompiler.get_related_selections.<locals>.<lambda>  s    RVr2   )rR   r   reverselocal_setterremote_setterr   r  )r  r  r   r{   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      r0   r  8SQLCompiler.get_related_selections.<locals>.local_setter4  s    ,,==hL r2   c                     g r(   r4   )r  r  s     r0   local_setter_noop=SQLCompiler.get_related_selections.<locals>.local_setter_noop9  s    r2   c                     [        X U5        g r(   )setattr)r   r  r  s      r0   r  9SQLCompiler.get_related_selections.<locals>.remote_setter<  s    ,r2      )r  
root_alias	cur_depth	requested
restrictedc              3   ,   #    U  H
  nS U-  v   M     g7f)z'%s'Nr4   )r[   ss     r0   r]   5SQLCompiler.get_related_selections.<locals>.<genexpr>s  s     !G6F&1*6Fs   z/Invalid field name(s) given in select_related: z. Choices are: )%rF   	max_depthr   rd   r~   r   r   dictr  r   r   r   r  r   r)  r   r  rR   r  r  r   r  r   r  rf   r   r  r   many_to_manyrelated_modelr  r  r  r   r  r   r  ))r,   rJ   r   r  r  r   r  r  r  r   fields_foundr  r  nextrelated_select_maskrL   r   r   r  r   columnsr   next_klass_infosorelated_fieldsrelated_objectrelated_fieldrR   related_field_name	join_infor   r  r  r  r   r  	join_optsfield_select_masknext_requestedfields_not_foundinvalid_fieldss)   `  `                                     r0   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)NrR   Fr   r   )rR   r   r  r   )r  r  parentsr   all_parentsrJ   r   rR   )rL   r  parent_modelparent_linkr  select_indexr,   s         r0   _get_parent_klass_infoNSQLCompiler.get_select_for_update_of_arguments.<locals>._get_parent_klass_info  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.
rR   r   r   N)r  r  rJ   r   rR   )rL   r  r!  r,   s      r0   "_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 !<r2   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(   r4   r[   rL   r  s     r0   r]   ]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(   r4   r)  s     r0   r]   r*    s      &O
 :&&Or+  r   )
collectionsdequerL   popleftr  r   r
   r)  r   r   )queueparent_pathrL   r   r  r"  r,   s       @r0   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=(...)): rL  zI. Only relational fields followed in the query are allowed. Choices are: r   )rL   rF   ru  r   r
   r   r  r   r   r7   r   select_for_update_of_columnr   r   r   r   r)  )r,   r%  r  r   invalid_namesr   rL   r   r0  klass_infosrelated_klass_infor   r"  s   `           @r0   r|  .SQLCompiler.get_select_for_update_of_arguments~  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  r2   c                    Sn0 nU H  n[        U[        5      (       aU  UR                  5       nU R                  U5      nUR	                  5        H  u  nu  pX4X2U-   '   M     U[        U5      -  nMm  U(       a]  U R                  R                  R                  U5      n
UR                  U R                  5      nU
(       d  U(       a  X-   U4X2'   US-  nM  US-  nM     U$ )Nr   r{   )	r   r   r>  get_convertersr   rf   r7   r  get_db_converters)r,   r   i
convertersr   r   cols_convertersr  convsr   backend_convertersfield_converterss               r0   r9  SQLCompiler.get_converters  s    
%J*h//!88:"&"5"5d";'6'<'<'>OA|).J1u% (?S_$%)__%8%8%J%J:%V"#-#?#?#P %)9%7%JJ$WJMQQ &  r2   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(   )r7   r   r   map)
r,   rowsr<  r7   rowposr>  r   value	converters
             r0   apply_convertersSQLCompiler.apply_converters  sn     __
***,-
tT?C,6((e!&I%eDE "' C	 -7
 I #s   A*A,c                 &    [        S U 5       5      $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr(   )r   r   )r[   r   s     r0   r]   3SQLCompiler.has_composite_fields.<locals>.<genexpr>  s     Rk
:j(33ks   )any)r,   r   s     r0   has_composite_fields SQLCompiler.has_composite_fields	  s     RkRRRr2   c              #     #    [        U5       VVs/ s H3  u  p4[        U[        5      (       d  M  [        X3[	        U5      -   5      PM5     nnn[        [        U5       H   nU H  n[        Xg   5      4Xg'   M     Uv   M"     g s  snnf 7fr(   )r   r   r   slicerf   rC  r   r  )r,   rD  r   r;  r   col_pair_slicesrE  rF  s           r0   composite_fields_to_tuples&SQLCompiler.composite_fields_to_tuples  s      "+;!7
!7*h/ *E!Z()!7 	 
 tT?C&!#(O- ' I	 #
s   B	BB>B	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 R                  U5      (       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   rJ   rg   r9  r   from_iterablerI  rO  rT  rC  r  )	r,   r  tuple_expectedrW  rX  r  r  r<  rD  s	            r0   results_iterSQLCompiler.results_iter  s     ?&&] ' G !%A ?@ ?1A$ ?@((0
""7+((:D$$V,,224@Dud#D As   Cc                 >    [        U R                  [        5      5      $ )zh
Backends (e.g. NoSQL) can override this in order to use optimized
versions of "query has any results."
)rs   rY  r   rS   s    r0   has_resultsSQLCompiler.has_results2  s    
 D$$V,--r2   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R                  UR                  5         $ 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! UR                  5         f = f)a  
Run the query against the database and return the result(s). The
return value depends on the value of result_type.

When result_type is:
- MULTI: Retrieves all rows using fetchmany(). Wraps in an iterator for
   chunked reads when supported.
- SINGLE: Retrieves a single row using fetchone().
- ROW_COUNT: Retrieves the number of rows in the result.
- CURSOR: Runs the query, and returns the cursor object. It is the
   caller's responsibility to close the cursor.
Nr   )r   r8   r   r   iterr7   chunked_cursorcursorexecute	Exceptioncloser   rowcountr   r   fetchonerg   cursor_iterr   empty_fetchmany_valuert   can_use_chunked_readsr   )	r,   result_typerW  rX  r   r   rd  valr   s	            r0   rY  SQLCompiler.execute_sql9  s    "/Z	++-KC$$  __335F__++-F	NN3' )#& M& oo'q4>>2   *$LLNOO$$::"33DNN	
 DOO$<$<$R$R
 <c  	e#Bx		  	LLN	  s;   F" .G	 
G( =%G= 3G= "GG	G%(G:=H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jsonrg  )r   rY  rF   r  r  lowerrq  dumpsr5   r   r)  )r,   r   format_output_formatterrE  rG  cs          r0   explain_querySQLCompiler.explain_query  s     d&&() **))00)0W]]_5N4::TWC!%--((#GA$4Q$7#GHHK	   $Hs   BC	C,C	)rM   rK   rg   r7   rH   rt   rp   rL   rq   rF   rI   rJ   rG   rm   )T)F)TF)NNN)Nr   N)NNr{   NN)+r:   r;   r<   r=   r#   re	MULTILINEDOTALLr   r+   rT   rh   rx   ru   r   re   r   rl   rr   r   r   r1  r%  r[  r8   r   rj  r   r  rk  r   r|  r9  rI  rO  rT  r   r\  r_  r   rY  rw  r>   r4   r2   r0   rB   rB   (   s	   % 
ryy N
#"
*0 eN@h,TM^;z,3j#4^@a9H EI1f6 IM?
BL L g#RhT,	S
 *..  uAXEN r2   rB   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)SQLInsertCompileri  Nr4   c                     Uc  U/ pTOB[        US5      (       a  U R                  U5      u  pEOUb  U" X0U R                  5      U/pTOSU/pTU R                  R                  R	                  XE5      nXE4$ )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.
r8   %s)r   r   r7   r  modify_insert_params)r,   r   get_placeholderrn  r   r   s         r0   field_as_sqlSQLInsertCompiler.field_as_sql  sz     =rS(##,,s+KC( *#T__Eu  $$99#F{r2   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 (r7   )r   r   rF   contains_column_references
ValueErrorcontains_aggregater   r   contains_over_clauseget_db_prep_saver7   )r,   r   rG  s      r0   prepare_valueSQLInsertCompiler.prepare_value  s    
 5.//,,

 - E // DI5R  '' "'**e5  )) zz5*  %%e%HHr2   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   )rF   rawr   r  pre_save)r,   r   r  s      r0   pre_save_valSQLInsertCompiler.pre_save_val  s1    
 ::>>3..~~ct~,,r2   c                 &  ^ ^^ U(       d  / / 4$ T Vs/ s H  n[        USS5      PM     snmUUU 4S jU 5       nS U 5       n[        U6 u  pgU VV	V
s/ s H  o V	V
s/ s H  o  H  oPM     M     sn
n	PM!     nn	nn
Xg4$ s  snf 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.
r  Nc              3   R   >#    U  H  nU4S  j[        TTU5       5       v   M     g7f)c              3   N   >#    U  H  u  pnTR                  XU5      v   M     g 7fr(   )r  )r[   r   r  rG  r,   s       r0   r]   >SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>.<genexpr>  s-      5W1EE !!%%@@5Wr!  Nr(  )r[   rE  r  get_placeholdersr,   s     r0   r]   4SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>  s4      !

 "	58AQSV5W  "s   $'c              3   2   #    U  H  n[        U6 v   M     g 7fr(   r  )r[   rE  s     r0   r]   r    s     "N8M398Ms   )r   r(  )r,   r  
value_rowsr   rows_of_fields_as_sqlsql_and_param_pair_rowsplaceholder_rows
param_rowsrE  psr  r  s   ``         @r0   assemble_as_sql!SQLInsertCompiler.assemble_as_sql  s     r6M RXXQWGE+<dCQWX!

 "!
 #O8M"N (+,C'D$ @JJz333R1qq33z
J++) Y$ 4Js   B
BB2BB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_conflictrg  z(%s)rL  c              3   H   >#    U  H  nT" UR                   5      v   M     g 7fr(   r   )r[   r  qns     r0   r]   +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     r0   r]   r  '       87!XX7   c              3   8   #    U  H  oR                   v   M     g 7fr(   r  r  s     r0   r]   r  (  r  r  zVALUES (%s)r   c              3   6   #    U  H  o  H  o"v   M     M     g 7fr(   r4   )r[   r  r  s      r0   r]   r  F  s     ,P2RQRQs   ) r7   r  r  rF   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   rZ  r(  )r,   r  r  r   r  r  r   r  r   can_bulkr  r  r  r   r_sqlr  valsr  s                    @r0   r8   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O6U 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OU(       at  [%        US   =n	[&        5      (       aZ  U	R                  UR                  5      /nU R                  R                  R)                  UUR                  U	R*                  5      4/nO/ sS S S 5        $ S S S 5        U R-                  U5      n
U
(       a  U R/                  WU
5      n[1        W5      $ s  snf s  snf ! , (       d  f       NM= f)Nr{   r   )rf   rF   r  r7   r   r  r   r  rd  r8   re  r  fetch_returned_insert_rowsr  r  r  fetch_returned_insert_columnsr  r   r   last_insert_idr   r9  rI  r   )r,   r  r  r   rd  r   r   rD  r   returning_fieldr<  s              r0   rY  SQLInsertCompiler.execute_sqlO  sU   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!j#3A#66	' ' (//>? ++::" MM+22 I &%%J ((.
((z:DDz= Y Y' &%s3   =K5A;K5$K+4BK5$K0%A?K5+
K55
L)r  r  r(   )r:   r;   r<   r=   r  r  r  r  r  r  r8   rY  r>   r4   r2   r0   r}  r}    s1    @I:-%,NN`1r2   r}  c                   P    \ rS rSr\S 5       r\S 5       r\S 5       rS r	S r
Srg)	SQLDeleteCompileri  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rX   rY   )r[   r	  r,   s     r0   r]   1SQLDeleteCompiler.single_alias.<locals>.<genexpr>  s'     R=Q4::,,Q/!3=Qr_   r{   )rF   rd   sumrc   rS   s   `r0   single_aliasSQLDeleteCompiler.single_alias  s6     	

$$&RTZZ=Q=QRRVWWWr2   c                    ^ ^ [        U[        5      (       a  UR                  T:H  $ [        US5      (       d  g[	        UU 4S jUR                  5        5       5      $ )Nr>  Fc              3   H   >#    U  H  nTR                  UT5      v   M     g 7fr(   )_expr_refs_base_model)r[   source_expr
base_modelclss     r0   r]   :SQLDeleteCompiler._expr_refs_base_model.<locals>.<genexpr>  s(      
< %%k:>><r  )r   r   rR   r   rN  r>  )r  r   r  s   ` `r0   r  'SQLDeleteCompiler._expr_refs_base_model  sS    dE""::++t566 
#::<
 
 	
r2   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  rF   rR   )r[   r   r,   s     r0   r]   ESQLDeleteCompiler.contains_self_reference_subquery.<locals>.<genexpr>  s6      
 &&tTZZ-=-=>>s   36)rN  r   rF   r   rN  rm   childrenrS   s   `r0    contains_self_reference_subquery2SQLDeleteCompiler.contains_self_reference_subquery  sJ     


&&--/1A1A1J1J
 
 	
r2   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 %sr4   z WHERE )r   
base_tabler   rm   r   r  )r,   rF   deleterm   r   s        r0   _as_sqlSQLDeleteCompiler._as_sql  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  r7   r   "delete_can_self_reference_subqueryr  r  rF   r4  r   r/   clear_select_clauserR   r  r  r  rd   rJ   update_can_self_selectr"  r8   r   
add_filter)r,   innerqr  outerqr   r   s         r0   r8   SQLDeleteCompiler.as_sql  s   
 OO$$GG88<<

++!!# ""$ZZ##&&DJJ$@$@$BCDtzz''(''>> !---IPPRKC9C?HF(F+||F##r2   r4   N)r:   r;   r<   r=   r!   r  classmethodr  r  r  r8   r>   r4   r2   r0   r  r    sH    X X
 
 
 
 
8$r2   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      (       a  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[        U[        5      (       a  [        S
UR                  -  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                  R0                  nSU" U5      -  SR3                  U5      /n U R+                  U R                  R4                  5      u  pUR-                  SU-  5        SR3                  U5      [9        X:-   5      4$ ! [6         a    / n
 N,f = f)r  )r^  r4   r   FTr  r  r  r  r  zNComposite primary keys expressions are not allowed in this query (%s=F('pk')).prepare_database_savezTried to update field z with a model instance, z. Use a value compatible with r   r  r  r  r8   z = z	%s = NULLzUPDATE %s SETrL  rd  rg  )rx   rF   rN  r   r   r   r  r   r   r  r   r   r  r  	TypeErrorr/   r:   r  r7   r  r   r   r   r   r  r)  rm   r   r  )r,   r  rN  update_paramsr   rR   rn  placeholderr   r   r   r   r   rm   s                 r0   r8   SQLUpdateCompiler.as_sql  s   
 	zz  )) "B!%!2!2E#s011,,JJED -  ))$&+jj#7  ++$&+jj#7  c8,,$68=

C  -
 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5[ "3\ 

%%bi'IIf
	. LL)9)9:ME MM*u,-xx}'=!>>>	  	F	s   %'K> >LLc                   > [         TU ]  U5      nUSL nU=(       d    Sn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$ )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.
Nr   F)r*   rY  rF   get_related_updatesr"  rG   )r,   rm  	row_countis_emptyrF   aux_row_countr/   s         r0   rY  SQLUpdateCompiler.execute_sql  sz     G'4	$N	ZZ335E!..tzz:FF{SMxMM *	  6 r2   c                 h  >^ 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R.                  nUS:  =(       a%    U R0                  R2                  R4                  (       + n	U R                   R7                  5         U R                   R
                  (       d  U	(       a  / n
[8        R:                  " [<        5      nUR?                  U R@                  5      RC                  [D        5       HZ  nU Vs/ s H  o(       a  UOUS   PM     nnU
RG                  U5        U H#  u  nmX   RG                  U4S	 jU 5       5        M%     M\     U R                   RI                  S
U
5        XR                   l%        OU R                   RI                  S
U5        U R                   RM                  U5        gs  snf )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.
r{   N)klassFT)forcec              3   L   #    U  H  oR                   R                  v   M     g 7fr(   )
join_fieldr   )r[   r  s     r0   r]   2SQLUpdateCompiler.pre_sql_setup.<locals>.<genexpr>2  s      8X++8Xs   "$r   c              3   ,   >#    U  H	  oT   v   M     g 7fr(   r4   )r[   r  r   s     r0   r]   r  S  s     .FAxr+  r  )'rF   rZ   r   rd   count_active_tablesrelated_updatesr   r   r   clear_orderingr   rJ   r   r  r   rb   get_path_to_parentr   rf   r  
add_fieldsr*   rx   is_composite_pkr7   r   r  clear_wherer-  defaultdictr   r"  rG   rY  r   r   r  related_idsr  )r,   r  countrF   r   r  related_ids_indexrelatedr  must_pre_selectidentsr  rD  rE  r   parentr   r/   s                   @r0   rx   SQLUpdateCompiler.pre_sql_setup  s~     ::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EIJTcos3q69TJc"%6MFE'...F.FF &7 J
 JJ!!(F3%0JJ" JJ!!(E2

""#34 Ks   3L/r4   )	r:   r;   r<   r=   r8   rY  rx   r>   r?   r@   s   @r0   r  r    s    C?J*?5 ?5r2   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  rL  )rH   Tr`   re  rf  rh  )rF   r   rN  r   r   r   r   rf   rg   r)  r  rA  r"  rG   rH   r8   )r,   r   r   
annotationann_sql
ann_paramsinner_query_sqlinner_query_paramss           r0   r8   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${r2   )rg   N)r:   r;   r<   r=   r8   r>   r4   r2   r0   r  r  \  s    r2   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)rd  itersizes   r0   r  cursor_iter.<locals>.<lambda>{  s    &"2"28"<r2   N)rb  rg  )rd  sentinelrg   r#  rD  r  s   `  `  r0   rj  rj  u  sY     
<xHD#+$1NAJY-1NN I 	 2Os+   A%A A	A A%A A""A%);r-  rq  ry  	functoolsr   	itertoolsr   django.core.exceptionsr   r   r   	django.dbr   r	   django.db.models.constantsr
   django.db.models.expressionsr   r   r   r   r   r   django.db.models.fieldsr   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   r   django.db.models.sql.queryr   r   django.db.transactionr    django.utils.functionalr!   django.utils.hashabler"   django.utils.regex_helperr#   r%   rB   r}  r  r  r  rj  r4   r2   r0   <module>r6     s      	   L L 6 1 Q Q 8 3 + ?   < < 3 / 6%# %c  c L3r rj9$ 9$xZ5 Z5z; 2	r2   