
    Evg@_                       d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZmZ d dlmZ d dlmZmZmZmZmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z, d dl-m.Z. d dl/m0Z0  G d de          Z1 G d d          Z2 G d de2          Z3 G d de2          Z4 G d de2          Z5 G d de2          Z6d Z7dS )    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                   $     e Zd Z fdZd Z xZS )PositionRefc                 Z    || _         t                                          ||           d S N)ordinalsuper__init__)selfr%   refssource	__class__s       Z/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/db/models/sql/compiler.pyr'   zPositionRef.__init__   s*    v&&&&&    c                 .    t          | j                  dfS )N )strr%   )r(   compiler
connections      r,   as_sqlzPositionRef.as_sql#   s    4<  "$$r-   )__name__
__module____qualname__r'   r3   __classcell__r+   s   @r,   r"   r"      sG        ' ' ' ' '% % % % % % %r-   r"   c                      e Zd Z edej        ej        z            Zd$dZd Z	d%dZ
d%dZd Zd	 Zd%d
Zd Zd Zd Zd Zd Zd Zd Zd&dZ	 d'dZd Z	 d(dZd Zd Z	 	 	 	 	 d)dZd Zd Zd Zddde fdZ!d  Z"e#de fd!Z$d" Z%d# Z&dS )*SQLCompilerz^(.*)\s(?:ASC|DESC).*Tc                     || _         || _        || _        || _        ddi| _        d | _        d | _        d | _        d | _        d S )N*)	queryr2   usingelide_emptyquote_cacheselectannotation_col_map
klass_info_meta_ordering)r(   r=   r2   r>   r?   s        r,   r'   zSQLCompiler.__init__.   sS    
$
 ':
 "&"r-   c           	      f    d| j         j         d| j        j        j         d| j        d| j        d	S )N<z model=z connection=z using=>)r+   r6   r=   modelr2   r>   r(   s    r,   __repr__zSQLCompiler.__repr__?   s_    D+ D DZ%2D D/D D48JD D D	
r-   Fc                     t           fd j        j        D                       r j                                                              |          \   _         _         _        t           j                   _	        d S )Nc              3   D   K   | ]}j         j        |         d k    V  dS r   Nr=   alias_refcount).0ar(   s     r,   	<genexpr>z*SQLCompiler.setup_query.<locals>.<genexpr>G   s2      OOQtz(+q0OOOOOOr-   with_col_aliases)
allr=   	alias_mapget_initial_alias
get_selectrA   rC   rB   len	col_count)r(   rT   s   ` r,   setup_queryzSQLCompiler.setup_queryF   s    OOOO$*:NOOOOO 	+J((***@D- AP A
 A
=T_d&= T[))r-   c                 x   |                      |           |                                 }| j        j                            | j        j        du          \  | _        | _        | _        |                     || j	                  }t          |          | _        |                     | j	        |z   |          }|||fS )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.
        rS   N)must_group_by)r[   get_order_byr=   wheresplit_having_qualifygroup_byhavingqualifyget_extra_selectrA   boolhas_extra_selectget_group_by)r(   rT   order_byextra_selectra   s        r,   pre_sql_setupzSQLCompiler.pre_sql_setupN   s     	*:;;;$$&&04
0@0U0U*-T9 1V 1
 1
-
DK ,,Xt{CC $\ 2 2$$T[<%?JJXx//r-   c                    | j         j        g S g }t                      }| j         j        dur| j         j        D ]}t          |d          s| j                             |          }t          |t                    r>|j        |vr4|                    |j                   |	                    |j
                   |	                    |           i }t          |d          D ]<\  }\  }}}	|	r|||<   |	|v r|                    |                                           =| j        s5|D ]2\  }\  }
}}|s'|                    |                                           3| j        r| j                                        nd}|D ]}|	                    |           g }t                      }|                     ||          }| j        j        j        }|D ]}	 |                     |          \  }
}n# t*          t,          f$ r Y /w xY w|r)|                    |          x}t1          |          d}}
n|                    | |
|          \  }
}t5          |          }|
|f|vr.|	                    |
|f           |                    |
|f           |S )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".
        NTr3      startr/   )r=   ra   sethasattrresolve_ref
isinstancer   r)   addappendr*   	enumerateextendget_group_by_colsrD   rb   collapse_group_byr2   featuresallows_group_by_select_indexcompiler   r   getr0   select_formatr   )r(   rA   rh   expressionsgroup_by_refsexprselected_expr_positionsr%   _aliassqlparamsis_refhaving_group_byresultseenrz   positionparams_hashs                      r,   rg   zSQLCompiler.get_group_by^   s
   L :&I:d** 
+ - -tX.. 8:11$77DdC(( -y55%))$)444#**4;777&&t,,,, #%)26)C)C)C 	9 	9%G%dAu 807'- %%t55778888" 	A/7 A A++sFF  A&&t'='='?'?@@@=A[P$+77999b# 	% 	%Dt$$$$uu,,[/JJ O$A 	%   	- 	-D"ll400VV"M2    -D!8!<!<T!B!BBXO!(mmRV"00sFCCV'//K[!--sFm,,,#{+,,,s   G++G?>G?c                       j         j        j        r* fd|D             d D             fd|D             }|S )Nc                     h | ]I}t          |d           |j        j        j        j                            |j        j                  G|JS )target)rp   r   primary_keyr2   ry   %allows_group_by_selected_pks_on_modelrH   rP   r   r(   s     r,   	<setcomp>z0SQLCompiler.collapse_group_by.<locals>.<setcomp>   so     
 
 
D(++	

 /
 0VV) 

 
 
r-   c                     h | ]	}|j         
S r/   )r   )rP   r   s     r,   r   z0SQLCompiler.collapse_group_by.<locals>.<setcomp>   s    222dtz222r-   c                 J    g | ]}|v s|v st          |d d          v| S )r   N)getattr)rP   r   aliasesrb   pkss     r,   
<listcomp>z1SQLCompiler.collapse_group_by.<locals>.<listcomp>   sN       3;;6>>4$//w>>	  ?>>r-   )r2   ry   allows_group_by_selected_pks)r(   r~   rb   r   r   s   ` `@@r,   rx   zSQLCompiler.collapse_group_by   s     ?#@ 	
 
 
 
'
 
 
C 32c222G     '  K r-   c                    g }d}i }d}| j         j                                        D ]7\  }\  }}|||<   |                    t	          ||          |f           |dz  }8| j         j        r| j         j        rJ | j                                         }	| j         j        r|                     |	          }
n| j         j        }
|
rGg }|
D ]3}|                    |           |                    |df           |dz  }4| j         j	        |d}| j         j
                                        D ]&\  }}|||<   |                    ||f           |dz  }'| j         j        r+|                     ||	          }||d<   fd |           g }d}|D ]\  }}	 |                     |          \  }}|                    | ||          \  }}n# t          $ rM t!          |dt"                    }|t"          u rd\  }}n%|                     t%          |                    \  }}Y n4t&          $ r( |                     t%          d	                    \  }}Y nw xY w||r
d
| }|dz  }|                    |||f|f           |||fS )a  
        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   rl   )rH   select_fieldsrelated_klass_infosc                 l    | d         D ])}|d         r| d         |d         z   |d<    |           *d S )Nr   from_parentr   r/   )rC   kiget_select_from_parents     r,   r   z6SQLCompiler.get_select.<locals>.get_select_from_parent  sa    $%:; / /B-( &7"_:MM ?+ +*2..../ /r-   empty_result_set_value)0r/   Tcol)r=   ri   itemsrt   r   rA   default_colsget_select_maskget_default_columnsrH   annotation_selectselect_relatedget_related_selectionsr{   r}   r   r   NotImplementedr   r   )r(   rT   rA   rC   annotations
select_idxr   r   r   select_maskcolsselect_listr   
annotationr   retcol_idxr   r   s                     @r,   rX   zSQLCompiler.get_select   s.   $ 

$(J$;$A$A$C$C 	 	 E=C!+KMM6#v..6777!OJJJ%B$**ABBBj0022:" 	%++K88DD :$D 		K    "":...sDk***a

)!, J "&!=!C!C!E!E 	 	E:!+KMM:u-...!OJJ:$ 	/"&"="=fk"R"R0CJ,-/ / / / / #":...  	4 	4JCC"ll3//V "//c6BBVV " N N N)01>* *& *^;;")KC"&,,u5K/L/L"M"MKC  8 8 8"ll5;;77VVV8 }!1}'g1JJc6]E23333J++s   GAI.IIc           
   #   \
  K   | j         j        r| j         j        }ne| j         j        s| j         j        }nL| j         j        r| j         j        }n3| j                                         x}r|j        r|j        }|| _        ng }| j         j        rt          d         \  }}nt          d         \  }}i }|rA| j	        x}r8t          |d          D ]&\  }\  }}}	t          ||	|          }
|	r|
||	<   |
||<   '|D ]5}t          |d          rbt          |t                    rt          ||j                  }t          |t"                    s|                                }| j         j        s(|                                }|                                 |                    |j                  }|s;t          |j        t.                    r|                    |j        j                  x}rz|j        |j        | j        j        j        r|                                }||_        n?| j         j        r3|                                }t?          |j         |j!                  |_        ||d ufV  v|dk    r!t#          tE                                dfV  tG          ||          \  }}|dk    }|                    |          x}rt#          ||          d	fV  | j         j$                            |          x}r|}g }n;|%                    tL                    ^}}| j         j$                            |          }|r| j         j        r'| j	        r |rtO          d
          t/          |          }|r |D ]}| j         (                    ||          }t          |t                    rt          ||j                  }t#          ||          dfV  d|v rV|%                    dd          \  }}t#          tS          | *                    |          d|g           |          dfV  7| j         j+        r|| j         j+        v rx|| j         j,        v r<t#          t?          |tS          | j         j+        |                    |          d	fV  t#          tS          | j         j+        |          |          dfV  | j         j        r+| j	        r$t#          t/          |          |          dfV   | -                    || j                                         |          E d {V  7d S )NASCDESCrl   rm   resolve_expression?F
descendingTz;Ordering combined queries by transforms is not implemented..)default_order).r=   extra_order_bydefault_orderingrh   get_metaorderingrD   standard_orderingr   rA   ru   r"   rp   rr   r   r   output_fieldr   asccopyreverse_orderingr|   
expressionr   namenulls_first
nulls_lastr2   ry    supports_order_by_nulls_modifier
combinatorr   r)   r*   r   r   r   splitr
   NotImplementedErrortry_transformr   quote_name_unless_aliasextrari   find_ordering_name)r(   r   metar   r   selected_exprsrA   r%   r   r   pos_exprfield
select_refr   orderr   ref
transformsr   tables                       r,   _order_by_pairszSQLCompiler._order_by_pairs<  s9     :$ 
	z0HH, 	z*HHZ  	z*HHj))+++d 	 	}H"*DH:' 	1(/M11(0M1  	04;. 	0-6vQ-G-G-G 0 0))$5&wt<< 5,4N5)'/t$$ r	 r	Eu233 eU++ < (:;;E!%11 (!IIKKEz3 -!JJLLE**,,,+//0@AA
 Su/33S'5'9'9%:J:O'P'PPS )1e6F6N1R 7O %

+5(( . S %

+.z
@Q+R+R(Zt33333||fhh''....&um<<JC&J+//444z 	 "#-       z-11#666t 7

#&99Z#8#8 jz-11#66 :( "T[ "! 1+   S66D D * D D#z77dCCdE** 9d&788Ddz:::EAAAAe|| !YYsA..
s'+'C'CE'J'J'J'JCCPRT  $.	       z C4:+;$;$;$*111VTZ-=c-B%CDD'1         
(8(= >:VVV    
 :( T[  "!C&&Z@@@%GGGGG  $66
++--&3  7            ]r	 r	r-   c                     g }t                      }|                                 D ]\  }}|                    | j        dd          }|sA| j        j        r4| j        r,|j        }|j        }| j        D ]{\  }}	}
||k    ro| j        j        r/|
| j        j        v r!t          |t                    r|
|j        k    sG|                    t          |
r|
n|j        j        |          g            n|t!          | j        j                  dz   }d| }
| j        j        D ].}|j        rt%          d          |                    ||
           /| j                            ||
           |                    t          |
|          g           |                     |          \  }}| j                            |          d         }t1          |          }||f|v r|                    ||f           |                    ||||ff           |S )a/  
        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reuserl   __orderbycolz:ORDER BY term does not match any column in the result set.)ro   r   r   r=   r   rA   r   has_select_fieldsr   rr   r   r   set_source_expressionsr   r   columnrY   combined_queriesr   add_annotationadd_select_colr{   ordering_partssearchr   rs   rt   )r(   r   r   r   r   resolvedsrcexpr_srcsel_exprr   	col_aliasorder_by_idxqr   r   without_orderingr   s                    r,   r^   zSQLCompiler.get_order_by  sj    uu 0022 /	= /	=LD&..tztSW.XXH "Kdj3 "K "K)?.2k K K*Hah !J8% )TZ-I I I *8Q 7 7 !J<E<V<V % 77 i!NSZ=NPSTTU    '$ $'tz'8#9#9A#=L =| = =I!Z8 > > . "/!2# #  ((9====J--h	BBB33SC5H5H4IJJJ,,x00KC
  $299#>>qA'//K +.$66HH&4555MM8c66%:;<<<<r-   c                     g }| j         j        rf| j         j        sZd |D             }|D ]K\  }\  }}}| j                            |          d         }	|s |	|f|vr|                    ||	|fd f           L|S )Nc                     g | ]
}|d          S )rl   r/   )rP   ts     r,   r   z0SQLCompiler.get_extra_select.<locals>.<listcomp>  s    ///1!A$///r-   rl   )r=   distinctdistinct_fieldsr   r   rt   )
r(   rh   rA   ri   
select_sqlr   r   r   r   r   s
             r,   rd   zSQLCompiler.get_extra_select  s    : 	Rtz'A 	R/////J/7 R R++sFF#'#6#=#=c#B#B1#E  R#3V"<J"N"N ''0@&/I4(PQQQr-   c                 J   || j         v r| j         |         S || j        j        v r|| j        j        vs;|| j        j        v s-| j        j                            |          r|| j        j        vr|| j         |<   |S | j        j        	                    |          }|| j         |<   |S )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).
        )
r@   r=   rV   	table_mapri   external_aliasesr|   r2   ops
quote_name)r(   r   rs      r,   r   z#SQLCompiler.quote_name_unless_alias  s     4####D))TZ)))d$*:N.N.Ntz...
+//55 / 
 444 &*DT"KO**400!"r-   c                     t          |d| j        j        z   d           }|r || | j                  \  }}n|                    | | j                  \  }}||fS )Nas_)r   r2   vendorr3   )r(   nodevendor_implr   r   s        r,   r{   zSQLCompiler.compile(  sa    dEDO,B$BDII 	=%+dDO<<KC++dDO<<KCF{r-   c                      j         j        } fd j        j        D             }|j        sD|D ]@}|j        j        rt          d          |                                rt          d          An j        j        r|dk    r|D ]	}d|_        
d}|D ]4}	 |j        j	        si j        j	        r]|j        
                                |_        |j                            g  j        j         j        j	         j        j        R            |                    d          \  }}|j        j        rF|j        sd	                    |          }nQ j        j        s|j        sd
                    |          }n( j        j        r|j        rd
                    |          }|||ffz  }# t&          $ r |dk    s|dk    r|rY 1 w xY w|st&           j         j        j        |         }	|r|dk    r|	dz  }	d j        j        s	|j        rd
t-          fd|D              \  }
}d                    |	                              |
          g}g }|D ]}|                    |           ||fS )Nc                 \    g | ](}|                     j        j        j                  )S r/   )get_compilerr>   r2   r?   )rP   r=   r(   s     r,   r   z2SQLCompiler.get_combinator_sql.<locals>.<listcomp>2  sB     
 
 
 tz4?D<LMM
 
 
r-   z>LIMIT/OFFSET not allowed in subqueries of compound statements.z:ORDER BY not allowed in subqueries of compound statements.unionFr/   TrS   zSELECT * FROM ({})z({})
differencez ALLz{}c              3   L   K   | ]\  }}                     |          |fV  d S r$   )format)rP   r   argsbracess      r,   rR   z1SQLCompiler.get_combinator_sql.<locals>.<genexpr>x  s8      @@YS$v}}S!!4(@@@@@@r-   z {} )r2   ry   r=   r   %supports_slicing_ordering_in_compound	is_slicedr   r^   r?   values_selectclone
set_valuesri   r   r3   r    supports_parentheses_in_compoundr  subqueryr   r   set_operatorszipjoinrv   )r(   r   rU   ry   	compilersr1   partspart_sql	part_argscombinator_sql	sql_parts
args_partsr   r   partr	  s   `              @r,   get_combinator_sqlzSQLCompiler.get_combinator_sql0  sJ   ?+
 
 
 
4
 
 
	 = 	-%  >+ 'X   ((** 'T   Z! 	-jG&;&;% - -
 (-$$! &	 &	H%  ~3 
8P %-^%9%9%;%;HNN--!Z4!Z5 "Z9    '/ooto&L&L#)>, 7 $D ;#7#>#>x#H#H 
+;'M; $*==#:#:J'7 F7  &}}X66H8Y/11!    ((Z<-G-GE-GH  	!  ,::F 	%:((f$Nz" 	x'U 	F #@@@@%@@@!
	: --//44Y??@ 	  	 DMM$v~s   DF**GGc                 v    g } j         r|                     j                     j        r|                     j                    j                                        d_        j                             |          _         d                      d          d         D             t          	                                          t                      i fd} |t           j                                                              j                            d                                 D                        _        g }                                 D ]e^}} ||                                           |                    |                    d                                 D                                  f                     j         j         j                  }|                    d	d
          \  }}                      j                  \  }	}
d|d j        j                            d          d|	g}r[ fd	                                D             }dd                    |          dg|d j        j                            d          }t          |          |
z   }|rsg }|D ]D}                     |          \  }}|                    |           |                    |           E|                    dd                    |          g           ||fS )NTc                     i | ]	\  }}}||
S r/   r/   )rP   r   r   r   s       r,   
<dictcomp>z/SQLCompiler.get_qualify_sql.<locals>.<dictcomp>  s-     
 
 
*D!UD%
 
 
r-   rS   r   c                    | r|                                  }|v r                    |          x}r||<   nt          |t                    r(|                     |                                           nt          |t                    r1|j        vr'|                     |                                           nDt                    }d| }	                    |           
                    ||           ||<   | d S d S )Nqual)popr|   rr   r   rv   get_source_expressionsr   r)   rY   rs   r   )	r~   r   select_aliasnum_qual_aliasinner_queryqual_aliasesreplacementsrA   select_aliasess	       r,   collect_replacementsz9SQLCompiler.get_qualify_sql.<locals>.collect_replacements  s/    6"((<''%+ZZ%5%55\ 6)5L&&f-- 
6&&t'B'B'D'DEEEEc** 6y66#**4+F+F+H+HIII%(%6%6N#:.#:#:L $$\222..t\BBB)5L&!  6 6 6 6 6r-   c                 6    i | ]\  }}|t          ||          S r/   r   rP   r   r   s      r,   r  z/SQLCompiler.get_qualify_sql.<locals>.<dictcomp>  s(    LLLeT3ud##LLLr-   c                 6    i | ]\  }}|t          ||          S r/   r,  r-  s      r,   r  z/SQLCompiler.get_qualify_sql.<locals>.<dictcomp>  s(    TTTeT3ud++TTTr-   )r2   r?   F)with_limitsrT   SELECT * FROM ()rc   WHEREc                 N    g | ]!}j         j                            |          "S r/   )r2   r   r   )rP   r   r(   s     r,   r   z/SQLCompiler.get_qualify_sql.<locals>.<listcomp>  s,    WWWeDO'22599WWWr-   SELECT, zFROM (qualify_maskzORDER BY)r_   rt   rb   r=   r  r  r+   rX   ro   valueslistrc   leavesreplace_expressionsr   r^   r#  r  r>   r2   r?   r3   r{   r   r   r  rv   )r(   where_partsr*  rh   order_by_exprr   inner_query_compiler	inner_sqlinner_paramsqualify_sqlqualify_paramsr   r   r   ordering_sqlsr   ordering_sqlordering_paramsr&  r'  r(  rA   r)  s   `                 @@@@@r,   get_qualify_sqlzSQLCompiler.get_qualify_sql  s   : 	+tz***; 	,t{+++j&&((#'-77DD

 
.2ooto.T.TUV.W
 
 
 V]]__--uu	6 	6 	6 	6 	6 	6 	6 	6 	6& 	T$,"5"5"7"788999|77LL|7I7I7K7KLLL
 
 !%!2!2!4!4 	 	MA  !E!E!G!GHHHOO11TT|?Q?Q?S?STTT    
  +77J4?@P  8  
  
 #7"="=  " #> #
 #
	< '+ll4<&@&@#^O**955
  	 XWWWv}}WWWD		$ 	
  #..~>>F l##n4  	BM$ / /04X0F0F-o$$\222o....MM:tyy'?'?@AAAv~r-   c                 0   | j         j                                        }	 | j         j        }|                     |pt          |                    \  }}}d}|o| j         j        }	| j         j        }| j        j        }
|rjt          |
d
                    |                    s"t          d
                    |                    |                     || j         j                  \  }}nQ| j        r|                                 \  }}d}n/|                                 \  }}|                                 \  }}	 | j        |                     | j                  ndg f\  }}n+# t(          $ r | j        r dg }}Y nt,          $ r dg }}Y nw xY w	 | j        |                     | j                  ndg f\  }}n# t,          $ r dg }}Y nw xY wdg}g }| j         j        r-| j        j                            ||          \  }}||z  }||z  }g }| j        |z   D ]Y\  }\  }}}|r$|d| j        j                            |          }|                    |           |                    |           Z|d	                    |          gz  }|r	|d
g|z  }n&| j        j        j         r|| j        j        j         gz  }|                    |           | j         j!        r|
j"        r| j        #                                r|
j$        rtK          d          |	r|
j&        st          d          | j         j'        }| j         j(        }| j         j)        }| j         j*        }|r|
j+        st          d          |r|
j,        st          d          |r|
j-        st          d          |r|
j.        st          d          | j        j        /                    ||| 0                                |          }|r|
j1        r|                    |           |r-|                    d|z             |                    |           g }|D ]/\  } }!|                    |            |                    |!           0|re|rte          d          |p| j        j        3                                }|                    dd	                    |          z             | j4        rd}|r`|s1|                    | j        j        5                                           |                    d|z             |                    |           | j         j6        rJ|7                    d | j        j        j8        | j         j6        j
        fi | j         j6        j9                   |rxg }"|D ]3\  }\  }#}$}|"                    |#           |                    |$           4dd	                    |"          z  }%|r|
j:        r
dg|d|%}n|                    |%           |	rG|                    | j        j        ;                    | j         j<        | j         j=                             |r|
j1        s|                    |           | j         j>        r=|r:g }&g }'t          | j        d          D ]\  }(\  })}}|rU|&                    | j        j                            d          d| j        j                            |                     `|)@                    |)jA        di          }*|*B                    | | j                  \  }+},|&                    |+           |'                    |,           dd	                    |&          dd                     |          d!t          |'|z             f| j         D                    |           S d                     |          t          |          f| j         D                    |           S # | j         D                    |           w xY w)"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.
        rS   Nzsupports_select_{}z-{} is not supported on this database backend. z0 = 1r4  z AS r5  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 %sr0  r1  rl   rm   r  r   SELECT  FROM ( 
) subquery)Er=   rO   r   r   rj   re   r  r2   ry   r   r  r	   r  combinator_allrc   rE  get_distinctget_from_clauser_   r{   r   r?   r   rb   r   r   distinct_sqlrA   r   rv   rt   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_orderingrD   force_group_byexplain_infoinsertexplain_query_prefixoptions#requires_compound_order_by_subquerylimit_offset_sqllow_mark	high_markr  ru   relabeled_cloner   r3   tuplereset_refcounts)-r(   r/  rT   refcounts_beforer   ri   rh   ra   for_update_partwith_limit_offsetry   r   r   r   distinct_paramsfrom_f_paramsr_   w_paramsrb   h_paramsdistinct_resultout_colsr   s_sqls_paramsr   rI  rJ  rK  rL  groupingg_sqlg_paramsr   o_sqlo_paramsorder_by_sqlsub_selects
sub_paramsindexrA   select_clone	subselect	subparamss-                                                r,   r3   zSQLCompiler.as_sql  s	     :499;;X	9.J/3/A/A!1!ET*5E5E 0B 0 0,L(H #O + D
0D.J/H K,x)=)D)DZ)P)PQQ +GNN&   
 "&!8!8
 9" "  A,!%!5!5!7!7373D3D3F3F0 #'"6"6"8"8x
-48J4JTZ000QSUWPX $E88 & 2 2 2' &-r8EEE$ - - -&("8EEE-.  ;2 T[111 "X %FHH
 % . . .')2HFFF.":& .7;7J7W7W''8 84O_ o-Fo-F37;3M + +/A(x% !EE O/::5AAA! MM(+++OOE****499X..// Lv...FF_-@ Lt7JKKFh''':/ 0H4R 06688 %:	 9X  
 * ( N 0J   "Z@F"&*"JK8B!Z@F  h&K /O   % X-W /T    H$E /V     (M /0   '+o&9&H&H%$/BBDD%	 'I ' 'O # 3x'E 3MM/222 ,MM*u"4555MM(+++'/ , ,OE8OOE***MM(++++ (& 1O    (R4?+>+P+P+R+RHMM-$))H2E2E"EFFF* (#' ,# Ldo&9&H&H&J&JKKKMM+"6777MM(+++z& <DO'<
/6 *19     	0/7 , ,+A+xOOE***MM(++++,tyy/B/BB 0("N 0/L&L#L|LFFMM,///  O'88
+TZ-A     /x'E /o...z" .| . !
1:4;a1P1P1P 5 5-E-FAu 5#** !% 3 > >z J J J J $ 3 > >u E E E    (.'='=#\:6( ( 0</B/B $/0 0,	9 $**9555")))4444#5& IIk****HHV$$$$ f,--. J&&'78888 88F##U6]]2 J&&'78888DJ&&'78888sg   D_9 :(E# "_9 #F9_9 ;F_9 
F_9 (F8 7_9 8G	_9 G		W_9 ;#_9 9`Nc                    g }|| j                                         x}|S |p| j                                         }d|i}|j        D ]}|j        j        j        }||j        k    rd}|r'|%t          |j        j        |j        j                  rI|r||vrP| j                             ||||          }	|	                    |	          }
|
                    |
           |S )aT  
        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)r=   r   rW   concrete_fieldsrH   _metaconcrete_model
issubclassjoin_parent_modelget_colrt   )r(   r   start_aliasoptsr   r   seen_modelsr   rH   r   r   s              r,   r   zSQLCompiler.get_default_columns  s    <
++---6!CTZ%A%A%C%C [)) 	" 	"EK%4E 
""%%4ek6P  &  uK77J00uk;WWE]]5))FMM&!!!!r-   c                 8   g }g }| j                                         }| j         j        D ]}|                    t                    }|                     ||d          \  }}}}	}
}}| j                             ||	|
          \  }}}|D ]}|| j         j        v r3|                    | j	        j
                            |                     C|                      |||                    \  }}|                    |           |                    |           ||fS )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)r=   r   r   r   r
   _setup_joins
trim_joinsr   rt   r2   r   r   r{   )r(   r   r   r  r   r  r   targetsr   joinspathtransform_functionr   r   ps                  r,   rS  zSQLCompiler.get_distinct  s8    z""$$J. 	% 	%DJJz**EDHDUDUtTE EAAwudA/A !%
 5 5gud K KGUA! % %4:777MM$/"5"@"@"F"FGGGG<<(:(:65(I(IJJDAqMM!$$$MM!$$$$% v~r-   r   c                     t          |          \  }|dk                        t                    }                     ||          \  }}	}
}}|j        rn|j        rft          |dd          |d         k    rJdk    rCt          dd          s1|pt                      }t           fd|
D                       }||v rt          d	          |
                    |           g }|j        D ]}t          |d
          r?t          |t                    s*r|                                n|                                }t          |t                    r4|                    |                     t                     df           |                    fd                     ||||          D                        |S  j                            |	|
|          \  }	}fd|	D             S )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   Z   K   | ]%}t          j        j        |         d d          V  &dS )	join_colsN)r   r=   rV   )rP   jr(   s     r,   rR   z1SQLCompiler.find_ordering_name.<locals>.<genexpr>1  sJ        HI
,Q/dCC     r-   z!Infinite loop caused by ordering.r   c              3   ^   K   | ]'\  }}|                      t                     |fV  (d S r$   )prefix_referencesr
   )rP   r   r   r   s      r,   rR   z1SQLCompiler.find_ordering_name.<locals>.<genexpr>C  sY        $f ++t,AZ,A,ABBFK     r-   c                 J    g | ]}t           |                     df S )r   F)r   )rP   r   r   r   r  s     r,   r   z2SQLCompiler.find_ordering_name.<locals>.<listcomp>K  sI     
 
 
 ''511jIII5Q
 
 
r-   )r   r   r
   r  is_relationr   r   ro   rr  r   rs   rp   rr   r   descr   rt   r  rv   r   r=   r  )r(   r   r  r   r   already_seenr   piecesr   r  r  r  
join_tupleresultsitemr   r   r  s   `` `            @@r,   r   zSQLCompiler.find_ordering_name  s    $D-88ef_
J'' fdE22	
 !	!	 y$//6":==.0@%HH  (0355L    MR    J \)) !DEEEZ(((G  4!566 Ez'@ @ E +5D499;;;$((**DdG,, NN//40E0E0EFFN       (,(?(?dE5,) )       N J11'5$GG
 
 
 
 
 

 
 
 	
r-   c                     |p| j                                         }| j                             |||          \  }}}}}}|d         }|||||||fS )a$  
        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  )r=   rW   setup_joins)	r(   r  r  r   r   r  r  r  r  s	            r,   r  zSQLCompiler._setup_joinsP  sk     75577@D
@V@VD%A
 A
=weT+= b	gueT49KKKr-   c                 2   g }g }t          | j        j                  D ]{}| j        j        |         s	 | j        j        |         }n# t          $ r Y 5w xY w|                     |          \  }}|                    |           |                    |           || j        j        D ]n}| j        	                    |          \  }}|| j        j        vs| j        j        |         dk    r+|                    d| 
                    |          z             o||fS )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.
        rl   z, %s)rr  r=   rV   rO   KeyErrorr{   rt   rv   extra_tablestable_aliasr   )	r(   r   r   r   from_clause
clause_sqlclause_paramsr   r   s	            r,   rT  zSQLCompiler.get_from_clause_  s<    4:/00 	) 	)E:,U3 "j259     )-[(A(A%JMM*%%%MM-((((( 		L 		LAz--a00HE1
 TZ111:,U3q88ft'C'CE'J'JJKKKv~s   A
AArl   c           
      4     fd}g }	|s| j         j        k    r|	S s2 j                                          j                                         }t	                      }
|-t           j         j        t                    }|r j         j        }d }j        D ]}|
	                    |j
                   |re|                    |j
        i           }|j        sB|s	|j
        |v r7t          d|j
        dd                     |                      pd          nd}t          ||||          s|                    |          pi }|j        j        |d|j        |j        r|j        j        nd	 dd
}|	                    |           g } j                             |j
        g|          \  }}}}}}|d         }                     |||j        j        j                  }|D ];}|                    t1          |                     |                    |df           <||d<                        |||j        j        j        ||dz   ||          } |||           |rd j        D             }|D ]w\  }}}t          ||||          s|                    |          pi }|                                }|
	                    |            j                             |g|          }|j        d         }t;          |j                  o|j        u}||d|j        |j        |d
}|	                    |           g }                     |||j        j                  }|D ];}|                    t1          |                     |                    |df           <||d<   |                    |i           }                     |||j        ||dz   ||          } |||           yd }d }d } t=          |          D ]}!|dk    r n|! j         j        v r||
	                    |!            j                             |!g|          \  }"}}#}}}|#j        }|d         }t;          |j                  o|j        u}||"dt1          |          dk    rtA          ||"          n|tA          | |!          |d
}|	                    |           g }|                    |!|"f          pi }$                     |$||j        j                  }|D ];}|                    t1          |                     |                    |df           <||d<   |                    |!i           }%                     ||$|j        ||dz   |%|          } |||           t	          |          !                    |
          }&|&rQd |&D             }'t          dd                    |'          dd                     |                      pd          |	S )a  
        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                  ~    d j         D             } d j        D             }t          | |j        j                  S )Nc              3   2   K   | ]}|j         	|j        V  d S r$   )r  r   rP   fs     r,   rR   zQSQLCompiler.get_related_selections.<locals>._get_field_choices.<locals>.<genexpr>  s+      KKQ]KafKKKKKKr-   c              3   `   K   | ])}|j         j        |j                                         V  *d S r$   )r   uniquerelated_query_namer  s     r,   rR   zQSQLCompiler.get_related_selections.<locals>._get_field_choices.<locals>.<genexpr>  sN        7>**,,     r-   )fieldsrelated_objectsr   r=   _filtered_relations)direct_choicesreverse_choicesr  r(   s     r,   _get_field_choicesz>SQLCompiler.get_related_selections.<locals>._get_field_choices  sY    KKdkKKKN -  O
 1O  r-   Nc                     || d<   d S )Nr   r/   )rC   r   s     r,   get_related_klass_infoszCSQLCompiler.get_related_selections.<locals>.get_related_klass_infos  s    0CJ,---r-   z/Non-relational field given in select_related: 'z'. Choices are: r5  z(none)Fc                     d S r$   r/   )xys     r,   <lambda>z4SQLCompiler.get_related_selections.<locals>.<lambda>  s    RV r-   )rH   r   reverselocal_setterremote_setterr   r  )r  r  r   rl   c                 R    g | ]$}|j         j        |j        ||j         |j        f%S r/   )r   r  many_to_manyrelated_model)rP   os     r,   r   z6SQLCompiler.get_related_selections.<locals>.<listcomp>  sI       7> +,.AGQ_-  r-   T)r  r  r   c                 D    |r| j                             ||           d S d S r$   )remote_fieldset_cached_value)final_fieldobjfrom_objs      r,   r  z8SQLCompiler.get_related_selections.<locals>.local_setter#  s6     M,==hLLLLLM Mr-   c                     d S r$   r/   )r  r  s     r,   local_setter_noopz=SQLCompiler.get_related_selections.<locals>.local_setter_noop(  s    r-   c                 (    t          || |           d S r$   )setattr)r   r  r  s      r,   r  z9SQLCompiler.get_related_selections.<locals>.remote_setter+  s    $,,,,,r-      )r  
root_alias	cur_depth	requested
restrictedc              3       K   | ]	}d |z  V  
dS )z'%s'Nr/   rP   ss     r,   rR   z5SQLCompiler.get_related_selections.<locals>.<genexpr>b  s&      !G!G&1*!G!G!G!G!G!Gr-   z/Invalid field name(s) given in select_related: z. Choices are: )"r=   	max_depthr   rW   ro   rr   r   dictr  rs   r   r|   r  r   r  r   r  rH   r  r  rt   r  r   r  rY   r   r  r  r  r  r8  r  r   r  )(r(   rA   r   r  r  r  r  r  r  r   fields_foundr  r  nextrelated_select_maskrC   r   r   r  r   columnsr   next_klass_infosrelated_fieldsrelated_objectrelated_fieldrH   related_field_name	join_infor   r  r  r  r   r  	join_optsfield_select_masknext_requestedfields_not_foundinvalid_fieldss(   `  `                                    r,   r   z"SQLCompiler.get_related_selections  s   "		 		 		 		 		 		 ! 	'i$**>>>&& 	8:&&((D5577J uu#DJ$=tDDJ 6 J5		D 	D 	D  5	B 5	BAQV$$$  }}QVR00}   qv22(j !" $		*<*<*>*> ? ? K8 K	   )!ZKPP "-//!"4"4":-  ! 278xVAN33EVEV$	 	J  &&z222M#':#9#916(D*#U#U Aq!UAq"IE..#Q^=Q=W /  G  + +$$S[[111sDk*****7J'#::#$*A    $#J0@AAAA 	 -  N
 9G 0F 0F4u-"	   &1oon&E&E&K#%2%E%E%G%G"  !3444 J22'($
 	 "+(
;;WTZ@W"*#$2$C%2%C#. 
 $**:666 "22' % $
	 3   # / /C!((V555MM3+.....;
?+ }}%7<<#'#>#>'KM$ $  ('
4DEEEEM M M
  - - - Y 1J 1Jq==E4:999 $$T***=AZ=S=Sj> >:KIua &OE!"IE"5$*55Q%tz:Q   "'!,#'  #5zzQ $L+>>>!2)0)E)E'2" "J (..z:::$&M(3{8K(L(L(RPR%"66)$)"[$(J	 7  G  ' 3 3%,,S[[999sDk22222?J/%.]]4%<%<N'+'B'B)"[#("+a-"0#- (C ( ($ ,+J8HIII"9~~88FF 	!G!G6F!G!G!G j 		.1111		"4"4"6"677C8C	   #"r-   c           	      (     fd fd} fd} j         sg S g }g } j        j        D ]} j         }|dk    r ||          }n|                    t                    D ]U}g |                    dg            |          R }	|	D ](}
|
d         }|
d         r|j        }|j        |k    r|
} n)d} nV||                    |            ||          }|m j	        j
        j        r/|                                         |          d	                    |                                         |j                              |rDt          d
d                    |          dd                     |                      d          |S )zk
        Return a quoted list of arguments for the SELECT FOR UPDATE OF part of
        the query.
        c              3      K   | d         j         j        }|j         j                                        D ].\  }j         j        |dfd| d         D             dV  /d S )NrH   Fc                     g | ]D}j         |         d          j        j        k    sj         |         d          j        j        v B|ES r   )rA   r   rH   )rP   select_indexall_parentsparent_modelr(   s     r,   r   zbSQLCompiler.get_select_for_update_of_arguments.<locals>._get_parent_klass_info.<locals>.<listcomp>{  sb     & & &( !K5a8?EUU#{<8;BHKWW %
  XWWr-   r   )rH   r   r  r   )r  r  parentsr   r  )rC   r  parent_linkr  r  r(   s      @@r,   _get_parent_klass_infozNSQLCompiler.get_select_for_update_of_arguments.<locals>._get_parent_klass_infos  s      '06EN-;-A-I-O-O-Q-Q  )k*0<)($& & & & & &,6,G& & &	      r-   c                     | d         j         j        }| d         D ]8}j        |         d         j        j        |k    rj        |         d         c S 9dS )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.
            rH   r   r   N)r  r  rA   r   rH   )rC   r  r  r(   s      r,   "_get_first_selected_col_from_modelzZSQLCompiler.get_select_for_update_of_arguments.<locals>._get_first_selected_col_from_model  sn     (06EN *? ; 8 8;|,Q/6<NN;|4Q7777 O8 8r-   c               3     K   t          j        dj        fg          } | r|                                 \  }}|g dV  n8|d         }|d         r|j        }||j        gz   t          j                  V  |                     fd |          D                        |                     fd|	                    dg           D                        | dS dS )z<Yield all allowed field paths in breadth-first search order.Nr(   r   r  c              3       K   | ]}|fV  	d S r$   r/   rP   rC   r  s     r,   rR   z]SQLCompiler.get_select_for_update_of_arguments.<locals>._get_field_choices.<locals>.<genexpr>  <        " :&     r-   c              3       K   | ]}|fV  	d S r$   r/   r  s     r,   rR   z]SQLCompiler.get_select_for_update_of_arguments.<locals>._get_field_choices.<locals>.<genexpr>  r  r-   r   )
collectionsdequerC   popleftr  r   r
   r  rv   r|   )queueparent_pathrC   r   r  r   r(   s       @r,   r  zJSQLCompiler.get_select_for_update_of_arguments.<locals>._get_field_choices  sG     %do'>&?@@E */--//'Z&D LLLL&w/E!), 3 % 2&%*5D$/$/////    &<&<Z&H&H          &0nn5JB&O&O           r-   r(   r   r   r  Nr   z<Invalid field name(s) given in select_for_update(of=(...)): r5  zI. Only relational fields followed in the query are allowed. Choices are: r   )rC   r=   r^  r   r
   r|   r  r   rt   r2   ry   select_for_update_of_columnr{   r   r   r   r  )r(   r  r  r   invalid_namesr   rC   r   r  klass_infosrelated_klass_infor   r   s   `           @r,   re  z.SQLCompiler.get_select_for_update_of_argumentsm  sB   	 	 	 	 	&	8 	8 	8 	8 	8	 	 	 	 	 	.  	IJ3 	K 	KDJv~~88DD JJz22  D##(=rBB#//
;;# #K /: 	 	* 27 ;-i8 7$)$6E :--);J!E . &*
 " %!((...88DD?+G KMM$,,s"3"3A"67777MM$">">sy"I"IJJJ 		*
 IIm,,,,II00223333   r-   c                     i }t          |          D ]N\  }}|rG| j        j                            |          }|                    | j                  }|s|r
||z   |f||<   O|S r$   )ru   r2   r   get_db_converters)r(   r~   
convertersir   backend_convertersfield_converterss          r,   get_converterszSQLCompiler.get_converters  s    
&{33 	X 	XMAz X%)_%8%J%J:%V%V"#-#?#?#P#P % X)9 X%7:J%JJ$WJqMr-   c              #      K   | j         }t          |                                          }t          t          |          D ]0}|D ]'\  }\  }}||         }|D ]}	 |	|||          }|||<   (|V  1d S r$   )r2   r8  r   map)
r(   rowsr  r2   rowposconvsr   value	converters
             r,   apply_converterszSQLCompiler.apply_converters  s      _
***,,--
tT?? 	 	C,6 ! !((eZC!& E EI%IeZDDEE CIIII	 	r-   c                 0   ||                      t          ||          }d | j        d| j                 D             }|                     |          }t          j        |          }|r-|                     ||          }|rt          t          |          }|S )z>Return an iterator over the results from executing this query.N)chunked_fetch
chunk_sizec                     g | ]
}|d          S r  r/   r  s     r,   r   z,SQLCompiler.results_iter.<locals>.<listcomp>  s    @@@1!A$@@@r-   r   )
execute_sqlr   rA   rZ   r  r   from_iterabler   r  rr  )r(   r  tuple_expectedr"  r#  r  r  r  s           r,   results_iterzSQLCompiler.results_iter  s     ?&&]z '  G A@A,> ?@@@((00
"7++ 	(((z::D (5$''r-   c                 P    t          |                     t                              S )z
        Backends (e.g. NoSQL) can override this in order to use optimized
        versions of "query has any results."
        )re   r%  r   rI   s    r,   has_resultszSQLCompiler.has_results  s     
 D$$V,,---r-   c                 |   |pt           }	 |                                 \  }}|st          n-# t          $ r  |t          k    rt	          g           cY S Y dS w xY w|r| j                                        }n| j                                        }	 |                    ||           n## t          $ r |
                                  w xY w|t          k    r|S |t          k    ri	 |                                }|r#|d| j                 |
                                 S ||
                                 S # |
                                 w xY w|t           k    r|
                                 dS t          || j        j        j        | j        r| j        nd|          }|r| j        j        j        st)          |          S |S )a  
        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   r3   r   r   iterr2   chunked_cursorcursorexecute	Exceptioncloser   r   fetchonerZ   cursor_iterry   empty_fetchmany_valuerf   can_use_chunked_readsr8  )	r(   result_typer"  r#  r   r   r.  valr   s	            r,   r%  zSQLCompiler.execute_sql  s    "/Z	++--KC %$$% 	 	 	e##Bxx		
  	._3355FF_++--F	NN3'''' 	 	 	LLNNN	
 &  M&  oo'' 3q4>12   *$$LLNNNFO$:"3=DNN	
 
  	 DO$<$R 	 
 <<s-    , $AAB& & C"$D1 D1 1Ec                    |j         }| j        j        j        }t	          | j        j                  D ]t\  }}|                     |          \  }}	 ||          d |||                   }
| j        j        	                    t          |d|
|	          t                     u|                                 \  }}d|z  |fS )Nr    = zEXISTS (%s))r   r2   r   r   ru   r=   rA   r{   r_   rs   r   r   r3   )r(   r   r  r1   qnqn2r  
select_collhs_sql
lhs_paramsrhsr   r   s                r,   as_subquery_conditionz!SQLCompiler.as_subquery_conditionJ  s    -o!,!*4:+<!=!= 	V 	VE:"&,,z":":GZRYYYYGEN(;(;(;<CJ  WWWcc(BJ!O!OQTUUUUkkmmVs"F**r-   c              #   X  K   t          |                                           }| j        j        j        }|r$|                                dk    rt          j        nt          |D ]D}|D ]?}t          |t                    s$d
                    fd|D                       V  ;|V  @Ed S )NjsonrP  c                 &    g | ]} |          S r/   r/   )rP   coutput_formatters     r,   r   z-SQLCompiler.explain_query.<locals>.<listcomp>_  s%    #G#G#GA$4$4Q$7$7#G#G#Gr-   )r8  r%  r=   ri  r  lowerrB  dumpsr0   rr   r  )r(   r   format_r  r  rE  s        @r,   explain_queryzSQLCompiler.explain_queryV  s      d&&(()) *)0)0WW]]__5N5N4::TW 	  	 C    !%--  ((#G#G#G#G#G#G#GHHHHHHKKKK	 	  	 r-   )T)F)TF)NNN)Nr   N)NNrl   NN)'r4   r5   r6   r    re	MULTILINEDOTALLr   r'   rJ   r[   rj   rg   rx   rX   r   r^   rd   r   r{   r  rE  r3   r   rS  r   r  rT  r   re  r  r   r   r(  r*  r   r%  r@  rI  r/   r-   r,   r:   r:   '   s       %% 
ry  N
# # # #"
 
 
* * * *0 0 0 0 e e eN  @U, U, U, U,nM M M^; ; ;z    ,  N N N`^ ^ ^@a9 a9 a9 a9H EI0 0 0 0d  6 IM?
 ?
 ?
 ?
BL L L# # #R g# g# g# g#Rh h hT  	 	 	 *   *. . .  uAX@ @ @ @D
+ 
+ 
+         r-   r:   c                   <    e Zd ZdZdZd Zd Zd Zd Zd Z	d	dZ
dS )
SQLInsertCompilerNr/   c                    ||g }}n^t          |d          r|                     |          \  }}n5t          |d          r |                    || | j                  |g}}nd|g}}| j        j                            ||          }||fS )at  
        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.
        Nr3   get_placeholder%s)rp   r{   rP  r2   r   modify_insert_params)r(   r   r7  r   r   s        r,   field_as_sqlzSQLInsertCompiler.field_as_sqlh  s     =rCCS(## 		&,,s++KCU-.. 	&  //T4?KKcUCC C $99#vFFF{r-   c                 V   t          |d          r~|                    | j        dd          }|j        rt	          d|d|d          |j        rt          d|j        d	|d
          |j        rt          d|j        d	|d
          |	                    || j
                  S )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 (r2   )rp   r   r=   contains_column_references
ValueErrorcontains_aggregater   r   contains_over_clauseget_db_prep_saver2   )r(   r   r  s      r,   prepare_valuezSQLInsertCompiler.prepare_value  s    
 5.// 	,,
 -  E /  jDIEE555R   '  j"'***eee5   )  jzzz555*   %%e%HHHr-   c                 r    | j         j        rt          ||j                  S |                    |d          S )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)rs   )r=   rawr   r  pre_save)r(   r   r  s      r,   pre_save_valzSQLInsertCompiler.pre_save_val  s8    
 :> 	/3...~~ct~,,,r-   c                      |sg g fS  fd|D             }d |D             }t          | \  }}d |D             }||fS )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   K   | ] }fd t          |          D             V  !dS )c              3   J   K   | ]\  }}                     ||          V  d S r$   )rS  )rP   r   vr(   s      r,   rR   z>SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>.<genexpr>  s7      JJXUATua((JJJJJJr-   Nr  )rP   r  r  r(   s     r,   rR   z4SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>  sV       !
 !
 KJJJVS9I9IJJJ!
 !
 !
 !
 !
 !
r-   c              3   (   K   | ]}t          | V  d S r$   rj  rP   r  s     r,   rR   z4SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>  s&      "N"N39"N"N"N"N"N"Nr-   c                 &    g | ]}d  |D             S )c                     g | ]	}|D ]}|
S r/   r/   rP   psr  s      r,   r   z@SQLInsertCompiler.assemble_as_sql.<locals>.<listcomp>.<listcomp>  s%    333R331q3333r-   r/   rl  s     r,   r   z5SQLInsertCompiler.assemble_as_sql.<locals>.<listcomp>  s'    JJJ333333JJJr-   rj  )r(   r  
value_rowsrows_of_fields_as_sqlsql_and_param_pair_rowsplaceholder_rows
param_rowss   ``     r,   assemble_as_sqlz!SQLInsertCompiler.assemble_as_sql  s      	r6M!
 !
 !
 !
 !
!!
 !
 !
 #O"N8M"N"N"N (+,C'D$* KJzJJJ
++r-   c                    
  j         j        j         j                                        } j         j                             j        j                  }|d |j                  g j        j        p|j	        g

                    dd                    fd
D                       z              j        j        r
 fd j        j        D             }n fd j        j        D             }d g
 j         o j         j        j        }                     
|          \  }} j         j                            
 j        j        d  j        j        D             d	  j        j        D                       } j        r/ j         j        j        r j         j        j        r6
                     j         j                            
|                     |}n:
                    d
d                    |d                   z             |d         g}|r
                    |            j         j                             j                  \  }	 _        |	r 
                    |	           | j        gz  }d                              t3          t5          j        |                    fgS |ry
                     j         j                            
|                     |r
                    |           d                              t3          d |D                       fgS |r
                    |           fdt9          ||          D             S )N)on_conflictrP  z(%s)r5  c              3   8   K   | ]} |j                   V  d S r$   r   )rP   r  r:  s     r,   rR   z+SQLInsertCompiler.as_sql.<locals>.<genexpr>  s-      (F(F!AH(F(F(F(F(F(Fr-   c                 0    g | ]fd D             S )c           	      d    g | ],}                     |                    |                    -S r/   )ra  re  )rP   r   r  r(   s     r,   r   z7SQLInsertCompiler.as_sql.<locals>.<listcomp>.<listcomp>  sI        &&ud.?.?s.K.KLL  r-   r/   )rP   r  r  r(   s    @r,   r   z,SQLInsertCompiler.as_sql.<locals>.<listcomp>  sR       
 	    !'    r-   c                 N    g | ]!}j         j                                        g"S r/   )r2   r   pk_default_value)rP   r   r(   s     r,   r   z,SQLInsertCompiler.as_sql.<locals>.<listcomp>  s:       =>$55778  r-   c              3   $   K   | ]}|j         V  d S r$   rz  r  s     r,   rR   z+SQLInsertCompiler.as_sql.<locals>.<genexpr>  $      88!QX888888r-   c              3   $   K   | ]}|j         V  d S r$   rz  r  s     r,   rR   z+SQLInsertCompiler.as_sql.<locals>.<genexpr>  r  r-   VALUES (%s)r   c              3   $   K   | ]}|D ]}|V  d S r$   r/   ro  s      r,   rR   z+SQLInsertCompiler.as_sql.<locals>.<genexpr>  s/      ,P,P2R,P,PQ,P,P,P,P,P,P,Pr-   c           	      x    g | ]6\  }}d                      dd                     |          z  gz             |f7S )rP  r  r5  )r  )rP   r  valsr   s      r,   r   z,SQLInsertCompiler.as_sql.<locals>.<listcomp>  sU       At &MDIIaLL$@#AABBDI  r-   )r2   r   r   r=   r   insert_statementrx  db_tabler  r  rt   r  objsreturning_fieldsry   has_bulk_insertrv  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_paramsrr  r   r&  r  )r(   r  r  rq  can_bulkrt  ru  r  r   r_sqlr  r:  r   s   `         @@@r,   r3   zSQLInsertCompiler.as_sql  s    _ +z""$$?.??
. @ 
 
 .--rr$-/@/@/@AB"/twiftyy(F(F(F(Fv(F(F(FFFFGGG: 	    
  :?  JJ   BF*/  J VF %%R$/*B*R 	 (,';';FJ'O'O$*!%!4!K!KJ"88tz788888tz7888	"
 "
 !	L(G	L 'H )O'77@PQQ   $mdii8H8K.L.LLMMM$Q-% 64555 ,0?+>+T+T%, ,(E4(  2e$$$4011XXf%%uU-@-H-H'I'IJKK 	MM$/-==fFVWWXXX% 64555XXf%%u,P,P,P,P,P'P'PQRR% 64555   "#3Z@@   r-   c                 J   |r0t          | j        j                  dk    r| j        j        j        sJ | j                                        || _        g }| j                                        5 }| 	                                D ]\  }}|
                    ||           | j        sg cd d d            S | j        j        j        rPt          | j        j                  dk    r3| j        j                            |          }fd| j        D             }n| j        j        j        rYt          | j        j                  dk    sJ | j        j                            || j                  g}fd| j        D             }nRj                            j                  g}| j        j                            |j        j        j                  fg}d d d            n# 1 swxY w Y   |                     |          }|r#t-          |                     ||                    }|S )Nrl   c                 D    g | ]}|                     j                  S r/   r  r  rP   r   r  s     r,   r   z1SQLInsertCompiler.execute_sql.<locals>.<listcomp>4  '    XXXdm44XXXr-   c                 D    g | ]}|                     j                  S r/   r  r  s     r,   r   z1SQLInsertCompiler.execute_sql.<locals>.<listcomp>=  r  r-   )rY   r=   r  r2   ry   r  r   r  r.  r3   r/  r   fetch_returned_insert_rowsr  fetch_returned_insert_columnsr  r  r  r  last_insert_idr   r  r8  r   )	r(   r  r   r.  r   r   r  r  r  s	           @r,   r%  zSQLInsertCompiler.execute_sql!  s   	
DJO$$))O,M *)) z""$$ 0_##%% 	#{{}} , ,VsF++++( 		 	 	 	 	 	 	 	 (I
((1,,*EEfMMXXXX$BWXXX)H 4:?++q0000O'EE-  YXXX$BWXXX667 +::" M GN -	 	 	 	 	 	 	 	 	 	 	 	 	 	 	> ((..
 	A--dJ??@@Ds   /9G5DGG"%G"r$   )r4   r5   r6   r  r  rS  ra  re  rv  r3   r%  r/   r-   r,   rN  rN  d  s          @I I I:- - -!, !, !,FN N N`+ + + + + +r-   rN  c                   \    e Zd Zed             Zed             Zed             Zd Zd Z	dS )SQLDeleteCompilerc                       j                                          t           fd j         j        D                       dk    S )Nc              3   D   K   | ]}j         j        |         d k    V  dS rM   rN   )rP   r   r(   s     r,   rR   z1SQLDeleteCompiler.single_alias.<locals>.<genexpr>T  s2      RR4:,Q/!3RRRRRRr-   rl   )r=   rW   sumrV   rI   s   `r,   single_aliaszSQLDeleteCompiler.single_aliasP  sG     	
$$&&&RRRRTZ=QRRRRRVWWWr-   c                      t          |t                    r|j        k    S t          |d          sdS t	           fd|                                D                       S )Nr#  Fc              3   D   K   | ]}                     |          V  d S r$   )_expr_refs_base_model)rP   source_expr
base_modelclss     r,   rR   z:SQLDeleteCompiler._expr_refs_base_model.<locals>.<genexpr>\  sG       
 
 %%k:>>
 
 
 
 
 
r-   )rr   r   rH   rp   anyr#  )r  r   r  s   ` `r,   r  z'SQLDeleteCompiler._expr_refs_base_modelV  s    dE"" 	,:++t566 	5 
 
 
 
 
#::<<
 
 
 
 
 	
r-   c                      t           fdt           j        j                                         j        j        j                  D                       S )Nc              3   X   K   | ]$}                     |j        j                  V  %d S r$   )r  r=   rH   r   s     r,   rR   zESQLDeleteCompiler.contains_self_reference_subquery.<locals>.<genexpr>c  sL       
 
 &&tTZ-=>>
 
 
 
 
 
r-   )r  r   r=   r   r7  r_   childrenrI   s   `r,    contains_self_reference_subqueryz2SQLDeleteCompiler.contains_self_reference_subquerya  sc     
 
 
 

&--//1A1J 
 
 
 
 
 	
r-   c                     d|                      |j                  z  }	 |                     |j                  \  }}n# t          $ r |dfcY S w xY w| d| t          |          fS )NzDELETE FROM %sr/   z WHERE )r   
base_tabler{   r_   r   rr  )r(   r=   deleter_   r   s        r,   _as_sqlzSQLDeleteCompiler._as_sqlj  s    !D$@$@AQ$R$RR	 LL55ME66 	 	 	2:	((((%--77s   = AAc                    | j         r2| j        j        j        s| j        s|                     | j                  S | j                                        }t          |_	        |
                                 | j        j        j        j        }|                    | j                                                  g|_        t          | j        j                  }| j        j        j        sC|                    | j                                                  \  }}t)          d|z  |          }|                    d|           |                     |          S )f
        Create the SQL for this query. Return the SQL string and list of
        parameters.
        r[  zSELECT * FROM (%s) subquerypk__in)r  r2   ry   "delete_can_self_reference_subqueryr  r  r=   r  r   r+   clear_select_clauserH   r  r  r  rW   rA   update_can_self_selectr  r3   r   
add_filter)r(   innerqr  outerqr   r   s         r,   r3   zSQLDeleteCompiler.as_sqlr  s+   
  	,O$G	,8	, <<
+++!!## ""$$$Z#&DJ$@$@$B$BCCDtz'(('> 	I !---IIPPRRKC9C?HHF(F+++||F###r-   N)
r4   r5   r6   r   r  classmethodr  r  r  r3   r/   r-   r,   r  r  O  s        X X _X
 
 
 [
 
 
 _
8 8 8$ $ $ $ $r-   r  c                   .     e Zd Zd Z fdZ fdZ xZS )SQLUpdateCompilerc           
      z   |                                   | j        j        sdS | j        }g g }}| j        j        D ]\  }}}t	          |d          rb|                    | j        dd          }|j        rt          d|j        d|d          |j	        rt          d	|j        d|d          nPt	          |d
          r@|j
        r|                    |          }n#t          d|d|d|j        j        d          |                    || j                  }t	          |d          r|                    || | j                  }nd}|j        }t	          |d          rU|                     |          \  }	}
|                     ||          d||	z             |                    |
           |:|                     ||          d|           |                    |           |                    d ||          z             | j        j        }d ||          z  d                    |          g}	 |                     | j        j                  \  }}
|                    d|z             n# t2          $ r g }
Y nw xY wd                    |          t5          ||
z             fS )r  )rG  r/   r   FTrU  rW  rX  rY  rZ  prepare_database_savezTried to update field z with a model instance, z. Use a value compatible with r   r[  rP  rQ  r3   r9  Nz	%s = NULLzUPDATE %s SETr5  rM  rP  )rj   r=   r7  r   rp   r   r^  r   r   r_  r  r  	TypeErrorr+   r4   r`  r2   rP  r   r{   rt   rv   r  r  r_   r   rr  )r(   r:  r7  update_paramsr   rH   r7  placeholderr   r   r   r   r   r_   s                 r,   r3   zSQLUpdateCompiler.as_sql  s?   
 	z  	6) "B!%!2 (	6 (	6E5#s011 ,,JED -   ) $*&+jjj###7   + $*&+jjj###7  
 566 % 33E::CC#) !55###u'?'?'?A  
 (((IIC u/00 ##33CtOO"<DsH%% 6"ll3//V22d8888[35F5FGHHH$$V,,,,22d8888[[ABBB$$S))))kBBtHH45555
%bbii'IIf
	. LL)9::ME6 MM*u,----  	 	 	FFF	 xx}v'=!>!>>>s   	"J JJc                 r   t                                          |          }	 |r|j        nd}|du }|r|                                 n# |r|                                 w w xY w| j                                        D ]7}|                    | j                                      |          }|r|r|}d}8|S )a  
        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&   r%  rowcountr1  r=   get_related_updatesr  r>   )r(   r6  r.  r  is_emptyr=   aux_rowsr+   s          r,   r%  zSQLUpdateCompiler.execute_sql  s     $$[11	&,36??!D~H   Z3355 	! 	!E))$*55AA+NNH !H ! s   A
 
A#c                    | j         j                                        }| j                                          | j                                         }| j         j        s|dk    rdS | j                             t                    }d|_        |	                    d           i |_
        g |_        |                                }|j        j        g}g }| j         j        D ]}t          d |                    |          D                       r|                    |df           F|                    |t%          |          f           |                    |j        j        j                   |                    |           t+                                                       |dk    o| j        j        j         }| j                                          | j         j        s|rg }	t7          j        t:                    }
|                    | j                                       tB                    D ]P}|	"                    d	 |D                        |D ],\  }|
|         "                    fd
|D                        -Q| j         #                    d|	           |
| j         _$        n| j         #                    d|           | j         %                    |           dS )aO  
        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.
        rl   N)klassFT)forcec              3   .   K   | ]}|j         j        V  d S r$   )
join_fieldr   )rP   r  s     r,   rR   z2SQLUpdateCompiler.pre_sql_setup.<locals>.<genexpr>  s8        04+     r-   r   c              3   &   K   | ]}|d          V  dS rM   r/   )rP   r   s     r,   rR   z2SQLUpdateCompiler.pre_sql_setup.<locals>.<genexpr>  s&      11qad111111r-   c              3   (   K   | ]}|         V  d S r$   r/   )rP   r   r  s     r,   rR   z2SQLUpdateCompiler.pre_sql_setup.<locals>.<genexpr>  s'      .F.FAqx.F.F.F.F.F.Fr-   r  )&r=   rO   r   rW   count_active_tablesrelated_updatesr   r   r   clear_orderingr   rA   r   r  r   rU   get_path_to_parentrt   rY   r  
add_fieldsr&   rj   r2   ry   r  clear_wherer  defaultdictr8  r  r>   r%  r   rv   r  related_idsrs  )r(   rt  countr=   r   r  related_ids_indexrelatedmust_pre_selectidentsr  r  parentr  r+   s                @r,   rj   zSQLUpdateCompiler.pre_sql_setup  s     :499;;
$$&&&
..00z) 	eqjjF
  u --$4(((~~',z1 	5 	5G  8<8O8OPW8X8X     5
 "(('16666
 "(('3v;;)?@@@gm.34444    AIMdo6MM 	 	
   :% 	3 	3 F%1$77K**4:66BB5II G G11D111111%6 G GMFE'...F.F.F.F.F.F.FFFFFGJ!!(F333%0DJ"" J!!(E222
""#344444r-   )r4   r5   r6   r3   r%  rj   r7   r8   s   @r,   r  r    sa        >? >? >?@    *=5 =5 =5 =5 =5 =5 =5 =5 =5r-   r  c                       e Zd Zd ZdS )SQLAggregateCompilerc                 0   g g }}| j         j                                        D ]^}|                     |          \  }}|                    | ||          \  }}|                    |           |                    |           _t          | j         j                  | _        d	                    |          }t          |          }| j         j                            | j        | j                                      d          \  }}d|d|d}||z  }||fS )r  r5  )r?   TrS   rN  rO  rQ  )r=   r   r7  r{   r}   rt   rv   rY   rZ   r  rr  r&  r  r>   r?   r3   )r(   r   r   r   ann_sql
ann_paramsinner_query_sqlinner_query_paramss           r,   r3   zSQLAggregateCompiler.as_sql"  s$   
 "V*6==?? 	& 	&J"&,,z":":GZ",":":4*"U"UGZJJwMM*%%%%TZ9::iinnv.2j.D.Q.QJ( /R /
 /
 &$&
'
' 	,++ 14___E$$F{r-   N)r4   r5   r6   r3   r/   r-   r,   r  r  !  s#            r-   r  c              #       K   	 t           fd|          D ]}|nfd|D             V  	                                   dS #                                   w xY w)z[
    Yield blocks of rows from a cursor and ensure the cursor is closed when
    done.
    c                  .                                    S r$   )	fetchmany)r.  itersizes   r,   r  zcursor_iter.<locals>.<lambda>@  s    &"2"28"<"< r-   Nc                 $    g | ]}|d          S r$   r/   )rP   r   rZ   s     r,   r   zcursor_iter.<locals>.<listcomp>A  s!    1N1N1NA!JYJ-1N1N1Nr-   )r,  r1  )r.  sentinelrZ   r  r  s   ` `` r,   r3  r3  :  s      
<<<<<xHH 	O 	OD#+$$1N1N1N1N1N1N1NNNNN	O 	s   +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  r3  r/   r-   r,   <module>r     s        				             L L L L L L L L L L 6 6 6 6 6 6 6 6 1 1 1 1 1 1 G G G G G G G G G G G G G G 3 3 3 3 3 3 3 3 + + + + + + ? ? ? ? ? ?                < ; ; ; ; ; ; ; * * * * * * < < < < < < 3 3 3 3 3 3 / / / / / / 6 6 6 6 6 6% % % % %# % % %z  z  z  z  z  z  z  z z1h h h h h h h hV9$ 9$ 9$ 9$ 9$ 9$ 9$ 9$xS5 S5 S5 S5 S5 S5 S5 S5l    ;   2	 	 	 	 	r-   