o
    þÞhù  ã                   @   sò   d Z ddlmZmZ ddlmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZ g d¢ZG dd	„ d	eƒZG d
d„ deeeƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZdS )z>
Classes to represent the definitions of aggregate functions.
é    )Ú
FieldErrorÚFullResultSet)ÚCaseÚColPairsÚFuncÚStarÚValueÚWhen)ÚIntegerField)ÚCoalesce)ÚFixDurationInputMixinÚNumericOutputFieldMixin)Ú	AggregateÚAvgÚCountÚMaxÚMinÚStdDevÚSumÚVariancec                       s¢   e Zd ZdZdZdZdZdZdZdZ	ddddœ‡ fdd„
Z
‡ fd	d
„Z‡ fdd„Z‡ fdd„Z	d‡ fdd„	Zedd„ ƒZdd„ Z‡ fdd„Z‡ fdd„Z‡  ZS )r   z)%(function)s(%(distinct)s%(expressions)s)TNz%s FILTER (WHERE %%(filter)s)F)ÚdistinctÚfilterÚdefaultc                   sf   |r| j std| jj ƒ‚|d ur| jd urt| jj› d�ƒ‚|| _|| _|| _tƒ j	|i |¤Ž d S )Nz%s does not allow distinct.z does not allow default.)
Úallow_distinctÚ	TypeErrorÚ	__class__Ú__name__Úempty_result_set_valuer   r   r   ÚsuperÚ__init__)Úselfr   r   r   ÚexpressionsÚextra©r   © ú[/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/db/models/aggregates.pyr   #   s   
zAggregate.__init__c                    s   dd„ t ƒ  ¡ D ƒS )Nc                 S   s   g | ]}|j ‘qS r$   )Ú_output_field_or_none)Ú.0Úer$   r$   r%   Ú
<listcomp>1   s    z/Aggregate.get_source_fields.<locals>.<listcomp>)r   Úget_source_expressions©r    r#   r$   r%   Úget_source_fields/   s   zAggregate.get_source_fieldsc                    s   t ƒ  ¡ }|| jg S ©N)r   r*   r   )r    Úsource_expressionsr#   r$   r%   r*   3   s   
z Aggregate.get_source_expressionsc                    s   |�^ }| _ tƒ  |¡S r-   )r   r   Úset_source_expressions)r    Úexprsr#   r$   r%   r/   7   s   z Aggregate.set_source_expressionsc              	      s"  t ƒ  ||||¡}|r(| ¡ D ]}|j| jr&td|j› d|› d|› d�ƒ‚qn2| jsZt t|ƒ ¡ }t	|ƒD ]#\}	}
|
j
rY|  ¡ |	 }t|dƒrK|jnt|ƒ}td|j||f ƒ‚q6|j }d u rc|S t|dƒrz| ||||¡}|jd u ry|j|_nt||jƒ}d |_t|||jd�}|j|_|S )	NzCannot compute z('z'): 'z' is an aggregateÚnamez-Cannot compute %s('%s'): '%s' is an aggregateÚresolve_expression)Úoutput_field)r   r2   Úget_refsÚannotationsÚ
is_summaryr   r1   r   r*   Ú	enumerateÚcontains_aggregateÚhasattrÚreprr   r&   r3   r   r   )r    ÚqueryÚallow_joinsÚreuseÚ	summarizeÚfor_saveÚcÚrefr!   ÚindexÚexprÚbefore_resolvedr1   r   Úcoalescer#   r$   r%   r2   ;   sH   ÿÿÿÿý
ÿÿù

€zAggregate.resolve_expressionc                 C   sL   dd„ |   ¡ D ƒ}t|ƒdkr"t|d dƒr"d|d j| j ¡ f S tdƒ‚)Nc                 S   s   g | ]}|d ur|‘qS r-   r$   )r'   rC   r$   r$   r%   r)   f   s    z+Aggregate.default_alias.<locals>.<listcomp>é   r   r1   z%s__%sz$Complex expressions require an alias)r*   Úlenr9   r1   Úlowerr   )r    r!   r$   r$   r%   Údefault_aliasd   s   ÿzAggregate.default_aliasc                 C   s   g S r-   r$   r+   r$   r$   r%   Úget_group_by_colsm   s   zAggregate.get_group_by_colsc                    s  | j rdnd|d< | jrw|jjrHz| j ||¡\}}W n	 ty$   Y nSw | j| d| j¡ }t	ƒ j||f||dœ|¤Ž\}}|g |¢|¢R fS |  
¡ }	d |	_|	 ¡ }
t| j|
d d�}|	 t|ƒg|
dd …  ¡ t	t|	ƒj||fi |¤ŽS t	ƒ j||fi |¤ŽS )	Nz	DISTINCT Ú r   Útemplate)rL   r   r   )ÚthenrF   )r   r   ÚfeaturesÚ supports_aggregate_filter_clauseÚas_sqlr   Úfilter_templateÚgetrL   r   Úcopyr*   r	   r/   r   r   )r    ÚcompilerÚ
connectionÚextra_contextÚ
filter_sqlÚfilter_paramsrL   ÚsqlÚparamsrS   r.   Ú	conditionr#   r$   r%   rP   p   s@   ÿÿþü
û
ÿÿzAggregate.as_sqlc                    s.   t ƒ  ¡ }| jr| j|d< | jr| j|d< |S )Nr   r   )r   Ú_get_repr_optionsr   r   )r    Úoptionsr#   r$   r%   r\   �   s   


zAggregate._get_repr_options)NTNFF)r   Ú
__module__Ú__qualname__rL   r8   r1   rQ   Úwindow_compatibler   r   r   r,   r*   r/   r2   ÚpropertyrI   rJ   rP   r\   Ú__classcell__r$   r$   r#   r%   r      s(    ÿÿ)
r   c                   @   ó   e Zd ZdZd ZdZdZdS )r   ÚAVGTrF   N©r   r^   r_   Úfunctionr1   r   Úarityr$   r$   r$   r%   r   ˜   ó
    r   c                       sH   e Zd ZdZd Zeƒ ZdZdZdZ	dZ
d
‡ fdd„	Z‡ fdd	„Z‡  ZS )r   ÚCOUNTTr   rF   Nc                    sD   |dkrt ƒ }t|t ƒr|d urtdƒ‚tƒ j|fd|i|¤Ž d S )NÚ*z8Star cannot be used with filter. Please specify a field.r   )r   Ú
isinstanceÚ
ValueErrorr   r   )r    Ú
expressionr   r"   r#   r$   r%   r   ¨   s
   zCount.__init__c                    sR   t ƒ j|i |¤Ž}|jd }t|tƒr'| jrtdƒ‚| ¡ }t|d |j	d�S |S )Nr   z6COUNT(DISTINCT) doesn't support composite primary keys)r   )
r   r2   r.   rk   r   r   rl   Úget_colsr   r   )r    ÚargsÚkwargsÚresultrC   Úcolsr#   r$   r%   r2   ¯   s   

ÿzCount.resolve_expressionr-   )r   r^   r_   rf   r1   r
   r3   r   r   rg   Úallows_composite_expressionsr   r2   rb   r$   r$   r#   r%   r   Ÿ   s    r   c                   @   ó   e Zd ZdZd ZdZdS )r   ÚMAXrF   N©r   r^   r_   rf   r1   rg   r$   r$   r$   r%   r   À   ó    r   c                   @   rt   )r   ÚMINrF   Nrv   r$   r$   r$   r%   r   Æ   rw   r   c                       ó2   e Zd Zd ZdZd‡ fdd„	Z‡ fdd„Z‡  ZS )r   rF   Fc                    ó&   |rdnd| _ tƒ j|fi |¤Ž d S )NÚSTDDEV_SAMPÚ
STDDEV_POP©rf   r   r   ©r    rm   Úsampler"   r#   r$   r%   r   Ð   ó   zStdDev.__init__c                    ó   i t ƒ  ¡ ¥d| jdki¥S )Nr   r{   ©r   r\   rf   r+   r#   r$   r%   r\   Ô   ó   zStdDev._get_repr_options©F©r   r^   r_   r1   rg   r   r\   rb   r$   r$   r#   r%   r   Ì   ó
    r   c                   @   rc   )r   ÚSUMTrF   Nre   r$   r$   r$   r%   r   Ø   rh   r   c                       ry   )r   rF   Fc                    rz   )NÚVAR_SAMPÚVAR_POPr}   r~   r#   r$   r%   r   ã   r€   zVariance.__init__c                    r�   )Nr   rˆ   r‚   r+   r#   r$   r%   r\   ç   rƒ   zVariance._get_repr_optionsr„   r…   r$   r$   r#   r%   r   ß   r†   r   N)Ú__doc__Údjango.core.exceptionsr   r   Údjango.db.models.expressionsr   r   r   r   r   r	   Údjango.db.models.fieldsr
   Údjango.db.models.functionsr   Ú!django.db.models.functions.mixinsr   r   Ú__all__r   r   r   r   r   r   r   r   r$   r$   r$   r%   Ú<module>   s     ~!