o
    ^"<f*"                     @   sp   d Z ddlmZ ddlmZ ddlmZ dZdZG dd dej	Z
G d	d
 d
ZG dd dZG dd dZdS )zG
Code to manage the creation and SQL rendering of 'where' constraints.
    EmptyResultSet)tree)cached_propertyANDORc                   @   s   e Zd ZdZeZdZdZd'ddZdd Z	d(d	d
Z
dd Zdd Zdd Zdd Zdd Zdd Zedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zd%d& ZdS ))	WhereNodea  
    An SQL WHERE clause.

    The class is tied to the Query class that created it (in order to create
    the correct SQL).

    A child is usually an expression producing boolean values. Most likely the
    expression is a Lookup instance.

    However, a child could also be any class with as_sql() and either
    relabeled_clone() method or relabel_aliases() and clone() methods and
    contains_aggregate attribute.
    FTc                 C   s   | j s| dfS || jA }|r| jtkp| o| jtk}|r$| j r$d| fS g }g }| jD ]/}t|drL||\}}|durB|| |durK|| q+|j rU|| q+|| q+|rf| 	|| j| jnd}	|rs| 	|| j| jnd}
|
|	fS )z
        Return two possibly None nodes: one for those parts of self that
        should be included in the WHERE clause and one for those parts of
        self that must be included in the HAVING clause.
        Nsplit_having)
contains_aggregatenegated	connectorr   r   childrenhasattrr	   append	__class__)selfr   
in_negatedmay_need_splitwhere_partshaving_partsc
where_parthaving_parthaving_node
where_node r   Q/var/www/html/kck/venv/lib/python3.10/site-packages/django/db/models/sql/where.pyr	       s0   





zWhereNode.split_havingc              	   C   s  g }g }| j tkrt| jd}}ndt| j}}| jD ]H}z	||\}}	W n ty5   |d8 }Y nw |rC|| ||	 n|d8 }|dkrV| jrTdg f  S t|dkre| jr_tdg f  S qd| j  }
|
	|}|r| jr}d| }||fS t|dks| j
rd| }||fS )a  
        Return the SQL version of the where clause and the value to be
        substituted in. Return '', [] if this node matches everything,
        None, [] if this node is empty, and raise EmptyResultSet if this
        node can't match anything.
           r    z %s zNOT (%s)(%s))r   r   lenr   compiler   r   extendr   joinresolved)r   compiler
connectionresultresult_paramsfull_neededempty_neededchildsqlparamsconn
sql_stringr   r   r   as_sqlA   sB   




zWhereNode.as_sqlNc                 C   s"   g }| j D ]	}||  q|S N)r   r"   get_group_by_cols)r   aliascolsr+   r   r   r   r2   u   s   
zWhereNode.get_group_by_colsc                 C   s   | j d d  S r1   )r   r   r   r   r   get_source_expressions{   s   z WhereNode.get_source_expressionsc                 C   s    t |t | jksJ || _d S r1   )r    r   )r   r   r   r   r   set_source_expressions~   s   
z WhereNode.set_source_expressionsc                 C   sH   t | jD ]\}}t|dr|| qt|dr!||| j|< qdS )z
        Relabel the alias values of any children. 'change_map' is a dictionary
        mapping old (current) alias values to the new values.
        relabel_aliasesrelabeled_cloneN)	enumerater   r   r8   r9   )r   
change_mapposr+   r   r   r   r8      s   

zWhereNode.relabel_aliasesc                 C   sN   | j jg | j| jd}| jD ]}t|dr|j|  q|j| q|S )z
        Create a clone of the tree. Must only be called on root nodes (nodes
        with empty subtree_parents). Childs must be either (Constraint, lookup,
        value) tuples, or objects supporting .clone().
        )r   r   r   clone)r   _new_instancer   r   r   r   r   r=   )r   r=   r+   r   r   r   r=      s   


zWhereNode.clonec                 C   s   |   }|| |S r1   )r=   r8   )r   r;   r=   r   r   r   r9      s   
zWhereNode.relabeled_clonec                 C   s   |   S r1   )r=   r5   r   r   r   copy   s   zWhereNode.copyc                    *   t |tjrt fdd|jD S |jS )Nc                 3       | ]}  |V  qd S r1   _contains_aggregate.0r   clsr   r   	<genexpr>       z0WhereNode._contains_aggregate.<locals>.<genexpr>)
isinstancer   Nodeanyr   r
   rG   objr   rF   r   rC         zWhereNode._contains_aggregatec                 C   
   |  | S r1   rB   r5   r   r   r   r
         
zWhereNode.contains_aggregatec                    r@   )Nc                 3   rA   r1   _contains_over_clauserD   rF   r   r   rH      rI   z2WhereNode._contains_over_clause.<locals>.<genexpr>)rJ   r   rK   rL   r   contains_over_clauserM   r   rF   r   rS      rO   zWhereNode._contains_over_clausec                 C   rP   r1   rR   r5   r   r   r   rT      rQ   zWhereNode.contains_over_clausec                 C   s   t dd | jD S )Nc                 s   s    | ]}|j V  qd S r1   )
is_summary)rE   r+   r   r   r   rH      s    z'WhereNode.is_summary.<locals>.<genexpr>)rL   r   r5   r   r   r   rU      s   zWhereNode.is_summaryc                 O   s&   t | dr| j|g|R i |} | S )Nresolve_expression)r   rV   )exprqueryargskwargsr   r   r   _resolve_leaf   s   
zWhereNode._resolve_leafc                 O   s   t |dr|jD ]}| j||g|R i | qt |dr,| j|j|g|R i ||_t |drB| j|j|g|R i ||_d S d S )Nr   lhsrhs)r   r   _resolve_noder[   r\   r]   )rG   noderX   rY   rZ   r+   r   r   r   r^      s   



"zWhereNode._resolve_nodec                 O   s*   |   }|j|g|R i | d|_|S )NT)r=   r^   r$   )r   rY   rZ   r=   r   r   r   rV      s   zWhereNode.resolve_expression)Fr1   )__name__
__module____qualname____doc__r   defaultr$   conditionalr	   r0   r2   r6   r7   r8   r=   r9   r?   classmethodrC   r   r
   rS   rT   propertyrU   staticmethodr[   r^   rV   r   r   r   r   r      s:    
!
4






	r   c                   @   s   e Zd ZdZdZdddZdS )NothingNodezA node that matches nothing.FNc                 C   s   t r1   r   )r   r%   r&   r   r   r   r0      s   zNothingNode.as_sqlNN)r`   ra   rb   rc   r
   r0   r   r   r   r   ri      s    ri   c                   @   s"   e Zd ZdZdd ZdddZdS )
ExtraWhereFc                 C   s   || _ || _d S r1   )sqlsr-   )r   rl   r-   r   r   r   __init__   s   
zExtraWhere.__init__Nc                 C   s(   dd | j D }d|t| jpdfS )Nc                 S   s   g | ]}d | qS )r   r   )rE   r,   r   r   r   
<listcomp>   s    z%ExtraWhere.as_sql.<locals>.<listcomp>z AND r   )rl   r#   listr-   )r   r%   r&   rl   r   r   r   r0      s   zExtraWhere.as_sqlrj   r`   ra   rb   r
   rm   r0   r   r   r   r   rk      s    rk   c                   @   s    e Zd ZdZdd Zdd ZdS )SubqueryConstraintFc                 C   s   || _ || _|| _|| _d S r1   )r3   columnstargetsquery_object)r   r3   rr   rs   rt   r   r   r   rm      s   
zSubqueryConstraint.__init__c                 C   s0   | j }|| j |j|d}|| j| j|S )N)r&   )rt   
set_valuesrs   get_compileras_subquery_conditionr3   rr   )r   r%   r&   rX   query_compilerr   r   r   r0      s   zSubqueryConstraint.as_sqlNrp   r   r   r   r   rq      s    rq   N)rc   django.core.exceptionsr   django.utilsr   django.utils.functionalr   r   r   rK   r   ri   rk   rq   r   r   r   r   <module>   s     G