o
    uvXjœ¾  ã                   @   sh  d dl Z d dlZd dlZd dl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mZ d d	lmZ G d
d„ dƒZG dd„ dƒZeG dd„ dƒƒZG dd„ deeƒZG dd„ deeƒZG dd„ deƒZG dd„ deƒZe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ƒZG d"d#„ d#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ƒZ$G d,d-„ d-eƒZ%G d.d/„ d/eƒZ&G d0d1„ d1eƒZ'G d2d3„ d3eƒZ(G d4d5„ d5eƒZ)G d6d7„ d7eƒZ*G d8d9„ d9eƒZ+G d:d;„ d;e+ƒZ,G d<d=„ d=eƒZ-G d>d?„ d?eƒZ.G d@dA„ dAeƒZ/G dBdC„ dCe/ƒZ0G dDdE„ dEe/ƒZ1dS )Fé    N©ÚDecimal)ÚEmptyResultSetÚ
FieldError©Ú
connection)Úfields)ÚQ)Údeconstructible)Úcached_property)Úmake_hashablec                   @   ó   e Zd ZdZdd„ ZdS )ÚSQLiteNumericMixinzp
    Some expressions with output_field=DecimalField() must be cast to
    numeric to be properly filtered.
    c                 K   s\   | j ||fi |¤Ž\}}z| j ¡ dkrd| }W ||fS W ||fS  ty-   Y ||fS w )NÚDecimalFieldzCAST(%s AS NUMERIC))Úas_sqlÚoutput_fieldÚget_internal_typer   )ÚselfÚcompilerr   Úextra_contextÚsqlÚparams© r   úY/var/www/html/myproject/venv/lib/python3.10/site-packages/django/db/models/expressions.pyÚ	as_sqlite   s   
üþþzSQLiteNumericMixin.as_sqliteN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s    r   c                   @   sè   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Z d2d3„ Z!d4d5„ Z"d6d7„ Z#d8S )9Ú
Combinablezu
    Provide the ability to combine one or two objects with
    some connector. For example F('foo') + F('bar').
    ú+ú-Ú*ú/ú^z%%ú&ú|z<<z>>c                 C   sL   t |dƒst|tjƒrt|t ¡ d�}nt|ƒ}|r t||| ƒS t| ||ƒS )NÚresolve_expression©r   )	ÚhasattrÚ
isinstanceÚdatetimeÚ	timedeltaÚDurationValuer   ÚDurationFieldÚValueÚCombinedExpression)r   ÚotherÚ	connectorÚreversedr   r   r   Ú_combine6   s   
zCombinable._combinec                 C   s   |   d| jd¡S )NéÿÿÿÿF©r4   ÚMUL©r   r   r   r   Ú__neg__F   ó   zCombinable.__neg__c                 C   ó   |   || jd¡S ©NF©r4   ÚADD©r   r1   r   r   r   Ú__add__I   r:   zCombinable.__add__c                 C   r;   r<   ©r4   ÚSUBr?   r   r   r   Ú__sub__L   r:   zCombinable.__sub__c                 C   r;   r<   r6   r?   r   r   r   Ú__mul__O   r:   zCombinable.__mul__c                 C   r;   r<   ©r4   ÚDIVr?   r   r   r   Ú__truediv__R   r:   zCombinable.__truediv__c                 C   r;   r<   ©r4   ÚMODr?   r   r   r   Ú__mod__U   r:   zCombinable.__mod__c                 C   r;   r<   ©r4   ÚPOWr?   r   r   r   Ú__pow__X   r:   zCombinable.__pow__c                 C   ó   t dƒ‚©Nz:Use .bitand() and .bitor() for bitwise logical operations.©ÚNotImplementedErrorr?   r   r   r   Ú__and__[   ó   ÿzCombinable.__and__c                 C   r;   r<   )r4   ÚBITANDr?   r   r   r   Úbitand`   r:   zCombinable.bitandc                 C   r;   r<   )r4   ÚBITLEFTSHIFTr?   r   r   r   Úbitleftshiftc   r:   zCombinable.bitleftshiftc                 C   r;   r<   )r4   ÚBITRIGHTSHIFTr?   r   r   r   Úbitrightshiftf   r:   zCombinable.bitrightshiftc                 C   rN   rO   rP   r?   r   r   r   Ú__or__i   rS   zCombinable.__or__c                 C   r;   r<   )r4   ÚBITORr?   r   r   r   Úbitorn   r:   zCombinable.bitorc                 C   r;   ©NTr=   r?   r   r   r   Ú__radd__q   r:   zCombinable.__radd__c                 C   r;   r]   rA   r?   r   r   r   Ú__rsub__t   r:   zCombinable.__rsub__c                 C   r;   r]   r6   r?   r   r   r   Ú__rmul__w   r:   zCombinable.__rmul__c                 C   r;   r]   rE   r?   r   r   r   Ú__rtruediv__z   r:   zCombinable.__rtruediv__c                 C   r;   r]   rH   r?   r   r   r   Ú__rmod__}   r:   zCombinable.__rmod__c                 C   r;   r]   rK   r?   r   r   r   Ú__rpow__€   r:   zCombinable.__rpow__c                 C   rN   rO   rP   r?   r   r   r   Ú__rand__ƒ   rS   zCombinable.__rand__c                 C   rN   rO   rP   r?   r   r   r   Ú__ror__ˆ   rS   zCombinable.__ror__N)$r   r   r   r   r>   rB   r7   rF   rL   rI   rT   r[   rV   rX   r4   r9   r@   rC   rD   rG   rJ   rM   rR   rU   rW   rY   rZ   r\   r^   r_   r`   ra   rb   rc   rd   re   r   r   r   r   r      sD    r   c                   @   s@  e Zd ZdZdZdZdZdZdCd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dDdd„Zdd„ Zedd„ ƒZedd „ ƒZed!d"„ ƒZd#d$„ Zed%d&„ ƒZed'd(„ ƒZd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Z d1d2„ Z!d3d4„ Z"d5d6„ Z#d7d8„ Z$d9d:„ Z%d;d<„ Z&ed=d>„ ƒZ'd?d@„ Z(dAdB„ Z)dS )EÚBaseExpressionz%Base class for all query expressions.FTNc                 C   s   |d ur	|| _ d S d S ©Nr(   ©r   r   r   r   r   Ú__init__š   s   
ÿzBaseExpression.__init__c                 C   s   | j  ¡ }| dd ¡ |S )NÚconvert_value)Ú__dict__ÚcopyÚpop)r   Ústater   r   r   Ú__getstate__ž   ó   
zBaseExpression.__getstate__c                 C   s$   | j | ju rg n| j g| j |¡ S rg   )rj   Ú_convert_value_noopr   Úget_db_converters©r   r   r   r   r   rr   £   s
   ÿ
üz BaseExpression.get_db_convertersc                 C   ó   g S rg   r   r8   r   r   r   Úget_source_expressionsª   ó   z%BaseExpression.get_source_expressionsc                 C   s   |rJ ‚d S rg   r   ©r   Úexprsr   r   r   Úset_source_expressions­   ó   z%BaseExpression.set_source_expressionsc                 G   s   dd„ |D ƒS )Nc                 S   s4   g | ]}t |d ƒr|nt|tƒrt|ƒnt|ƒ‘qS ©r'   )r)   r*   ÚstrÚFr/   ©Ú.0Úargr   r   r   Ú
<listcomp>±   s
    þþz5BaseExpression._parse_expressions.<locals>.<listcomp>r   )r   Úexpressionsr   r   r   Ú_parse_expressions°   s   ýz!BaseExpression._parse_expressionsc                 C   rN   )aÆ  
        Responsible for returning a (sql, [params]) tuple to be included
        in the current query.

        Different backends can provide their own implementation, by
        providing an `as_{vendor}` method and patching the Expression:

        ```
        def override_as_sql(self, compiler, connection):
            # custom logic
            return super().as_sql(compiler, connection)
        setattr(Expression, 'as_' + connection.vendor, override_as_sql)
        ```

        Arguments:
         * compiler: the query compiler responsible for generating the query.
           Must have a compile method, returning a (sql, [params]) tuple.
           Calling compiler(value) will return a quoted `value`.

         * connection: the database connection used for the current query.

        Return: (sql, params)
          Where `sql` is a string containing ordered sql parameters to be
          replaced with the elements of the list `params`.
        z"Subclasses must implement as_sql()rP   ©r   r   r   r   r   r   r   ·   s   zBaseExpression.as_sqlc                 C   ó   t dd„ |  ¡ D ƒƒS )Nc                 s   ó   � | ]}|o|j V  qd S rg   )Úcontains_aggregate©r   Úexprr   r   r   Ú	<genexpr>Õ   ó   € z4BaseExpression.contains_aggregate.<locals>.<genexpr>©Úanyru   r8   r   r   r   r‡   Ó   ó   z!BaseExpression.contains_aggregatec                 C   r…   )Nc                 s   r†   rg   )Úcontains_over_clauserˆ   r   r   r   rŠ   Ù   r‹   z6BaseExpression.contains_over_clause.<locals>.<genexpr>rŒ   r8   r   r   r   r�   ×   rŽ   z#BaseExpression.contains_over_clausec                 C   r…   )Nc                 s   r†   rg   )Úcontains_column_referencesrˆ   r   r   r   rŠ   Ý   r‹   z<BaseExpression.contains_column_references.<locals>.<genexpr>rŒ   r8   r   r   r   r�   Û   rŽ   z)BaseExpression.contains_column_referencesc                    s4   |   ¡ }ˆ|_| ‡ ‡‡‡fdd„| ¡ D ƒ¡ |S )a
  
        Provide the chance to do any preprocessing or validation before being
        added to the query.

        Arguments:
         * query: the backend query implementation
         * allow_joins: boolean allowing or denying use of joins
           in this query
         * reuse: a set of reusable joins for multijoins
         * summarize: a terminal aggregate clause
         * for_save: whether this expression about to be used in a save or update

        Return: an Expression to be added to the query.
        c                    s$   g | ]}|r|  ˆˆ ˆˆ¡nd ‘qS rg   r{   rˆ   ©Úallow_joinsÚqueryÚreuseÚ	summarizer   r   r�   ð   s    ÿÿþz5BaseExpression.resolve_expression.<locals>.<listcomp>)rl   Ú
is_summaryry   ru   ©r   r“   r’   r”   r•   Úfor_saveÚcr   r‘   r   r'   ß   s   ýz!BaseExpression.resolve_expressionc                 C   ó   | S )z?Hook used by Lookup.get_prep_lookup() to do custom preparation.r   )r   Úfieldr   r   r   Ú_prepare÷   ó   zBaseExpression._preparec                 C   ó   | j S rg   r(   r8   r   r   r   r›   û   s   zBaseExpression.fieldc                 C   s"   |   ¡ }|du rd| _tdƒ‚|S )z+Return the output type of this expressions.NTz4Cannot resolve expression type, unknown output_field)Ú_resolve_output_fieldÚ_output_field_resolved_to_noner   rh   r   r   r   r   ÿ   s
   zBaseExpression.output_fieldc                 C   s&   z| j W S  ty   | js‚ Y dS w )z†
        Return the output field of this expression, or None if
        _resolve_output_field() didn't return an output type.
        N)r   r   r    r8   r   r   r   Ú_output_field_or_none  s   ÿÿz$BaseExpression._output_field_or_nonec                    sD   dd„ |   ¡ D ƒ}|D ]‰ t‡ fdd„|D ƒƒrtdƒ‚ˆ   S dS )a‹  
        Attempt to infer the output type of the expression. If the output
        fields of all source fields match then, simply infer the same type
        here. This isn't always correct, but it makes sense most of the time.

        Consider the difference between `2 + 2` and `2 / 3`. Inferring
        the type here is a convenience for the common case. The user should
        supply their own output_field with more complex computations.

        If a source's output field resolves to None, exclude it from this check.
        If all sources are None, then an error is raised higher up the stack in
        the output_field property.
        c                 s   s   � | ]	}|d ur|V  qd S rg   r   ©r   Úsourcer   r   r   rŠ   "  s   € z7BaseExpression._resolve_output_field.<locals>.<genexpr>c                 3   s   � | ]
}t ˆ |jƒ V  qd S rg   )r*   Ú	__class__r¢   r(   r   r   rŠ   $  s   € z;Expression contains mixed types. You must set output_field.N)Úget_source_fieldsr�   r   )r   Úsources_iterr   r(   r   rŸ     s   ýz$BaseExpression._resolve_output_fieldc                 C   rš   rg   r   ©ÚvalueÚ
expressionr   r   r   r   rq   (  r�   z"BaseExpression._convert_value_noopc                 C   sF   | j }| ¡ }|dkrdd„ S | d¡rdd„ S |dkr dd„ S | jS )zÑ
        Expressions provide their own converters because users have the option
        of manually specifying the output_field which may be a different type
        from the one the database returns.
        Ú
FloatFieldc                 S   ó   | d u rd S t | ƒS rg   )Úfloatr§   r   r   r   Ú<lambda>6  ó    z.BaseExpression.convert_value.<locals>.<lambda>ÚIntegerFieldc                 S   r«   rg   )Úintr§   r   r   r   r­   8  r®   r   c                 S   r«   rg   r   r§   r   r   r   r­   :  r®   )r   r   Úendswithrq   )r   r›   Úinternal_typer   r   r   rj   ,  s   
zBaseExpression.convert_valuec                 C   ó   | j  |¡S rg   )r   Ú
get_lookup)r   Úlookupr   r   r   r´   =  rz   zBaseExpression.get_lookupc                 C   r³   rg   )r   Úget_transform©r   Únamer   r   r   r¶   @  rz   zBaseExpression.get_transformc                    s(   |   ¡ }| ‡ fdd„|  ¡ D ƒ¡ |S )Nc                    s"   g | ]}|d ur|  ˆ ¡nd ‘qS rg   )Úrelabeled_clone©r   Úe©Ú
change_mapr   r   r�   E  s    ÿÿz2BaseExpression.relabeled_clone.<locals>.<listcomp>)rl   ry   ru   )r   r½   Úcloner   r¼   r   r¹   C  s
   þzBaseExpression.relabeled_clonec                 C   s
   t   | ¡S rg   )rl   r8   r   r   r   rl   K  ó   
zBaseExpression.copyc                 C   s0   | j s| gS g }|  ¡ D ]	}| | ¡ ¡ q|S rg   )r‡   ru   ÚextendÚget_group_by_cols©r   Úcolsr£   r   r   r   rÁ   N  s   z BaseExpression.get_group_by_colsc                 C   s   dd„ |   ¡ D ƒS )z9Return the underlying field types used by this aggregate.c                 S   s   g | ]}|j ‘qS r   )r¡   rº   r   r   r   r�   X  s    z4BaseExpression.get_source_fields.<locals>.<listcomp>)ru   r8   r   r   r   r¥   V  s   z BaseExpression.get_source_fieldsc                 K   ó   t | fi |¤ŽS rg   ©ÚOrderBy©r   Úkwargsr   r   r   ÚascZ  r:   zBaseExpression.ascc                 K   ó   t | fddi|¤ŽS ©NÚ
descendingTrÅ   rÇ   r   r   r   Údesc]  ó   zBaseExpression.descc                 C   rš   rg   r   r8   r   r   r   Úreverse_ordering`  rv   zBaseExpression.reverse_orderingc                 c   s,   � | V  |   ¡ D ]}|r| ¡ E dH  qdS )zi
        Recursively yield this expression and all subexpressions, in
        depth-first order.
        N)ru   Úflatten)r   r‰   r   r   r   rÐ   c  s   €€þzBaseExpression.flattenc           	      C   s    t  | j¡}| j\}}|j|i |¤Ž}| ¡  |j ¡ }| jg}|D ])\}}t	|t
jƒr@|jr;|jr;|jjj|jf}n	t|ƒ}nt|ƒ}| ||f¡ q"t|ƒS rg   )ÚinspectÚ	signatureri   Ú_constructor_argsÚbind_partialÚapply_defaultsÚ	argumentsÚitemsr¤   r*   r   ÚFieldr¸   ÚmodelÚ_metaÚlabelÚtyper   ÚappendÚtuple)	r   Úconstructor_signatureÚargsrÈ   rÒ   rÖ   Úidentityr€   r¨   r   r   r   rá   m  s   


zBaseExpression.identityc                 C   s   t |tƒo
|j| jkS rg   )r*   rf   rá   r?   r   r   r   Ú__eq__€  ó   zBaseExpression.__eq__c                 C   ó
   t | jƒS rg   )Úhashrá   r8   r   r   r   Ú__hash__ƒ  r¿   zBaseExpression.__hash__rg   ©NTNFF)*r   r   r   r   r–   r    Ú
filterableÚwindow_compatibleri   ro   rr   ru   ry   rƒ   r   r   r‡   r�   r�   r'   rœ   Úpropertyr›   r   r¡   rŸ   Ústaticmethodrq   rj   r´   r¶   r¹   rl   rÁ   r¥   rÉ   rÍ   rÏ   rÐ   rá   râ   ræ   r   r   r   r   rf   Ž   s\    











rf   c                   @   s   e Zd ZdZdS )Ú
Expressionz:An expression that can be combined with other expressions.N)r   r   r   r   r   r   r   r   rì   ‡  s    rì   c                       sP   e Zd Zd‡ fdd„	Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zddd„Z	‡  Z
S )r0   Nc                    s$   t ƒ j|d� || _|| _|| _d S ©Nr(   )Úsuperri   r2   ÚlhsÚrhs)r   rï   r2   rð   r   ©r¤   r   r   ri   Ž  s   
zCombinedExpression.__init__c                 C   s   d  | jj| ¡S )Nz<{}: {}>)Úformatr¤   r   r8   r   r   r   Ú__repr__”  r:   zCombinedExpression.__repr__c                 C   s   d  | j| j| j¡S )Nz{} {} {})rò   rï   r2   rð   r8   r   r   r   Ú__str__—  rÎ   zCombinedExpression.__str__c                 C   ó   | j | jgS rg   ©rï   rð   r8   r   r   r   ru   š  rz   z)CombinedExpression.get_source_expressionsc                 C   ó   |\| _ | _d S rg   rö   rw   r   r   r   ry   �  r:   z)CombinedExpression.set_source_expressionsc           
      C   s@  z| j j}W n ty   d }Y nw z| jj}W n ty#   d }Y nw |jjsE|r0| ¡ dks8|rE| ¡ dkrEt| j | j| jƒ 	||¡S |rh|rh| j| j
krh| ¡ dv rh| ¡ | ¡ krht| j | jƒ 	||¡S g }g }| | j ¡\}}| |¡ | |¡ | | j¡\}}| |¡ | |¡ d}	|j | j|¡}|	| |fS )Nr.   >   Ú	DateFieldÚ	TimeFieldÚDateTimeFieldú(%s))rï   r   r   rð   ÚfeaturesÚhas_native_duration_fieldr   ÚDurationExpressionr2   r   rB   ÚTemporalSubtractionÚcompilerÝ   rÀ   ÚopsÚcombine_expression)
r   r   r   Ú
lhs_outputÚ
rhs_outputr‚   Úexpression_paramsr   r   Úexpression_wrapperr   r   r   r      sB   ÿÿÿÿ



zCombinedExpression.as_sqlTFc                 C   s>   |   ¡ }||_|j |||||¡|_|j |||||¡|_|S rg   )rl   r–   rï   r'   rð   r—   r   r   r   r'   ¾  s
   z%CombinedExpression.resolve_expressionrg   rç   )r   r   r   ri   ró   rô   ru   ry   r   r'   Ú__classcell__r   r   rñ   r   r0   Œ  s    r0   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )rþ   c                 C   s\   t |tƒs)z|j}W n	 ty   Y nw | ¡ dkr)| |¡\}}|j |¡|fS | |¡S )Nr.   )r*   r-   r   r   r   r   r  Úformat_for_duration_arithmetic)r   Úsider   r   Úoutputr   r   r   r   r   r   Ç  s   

ÿ
zDurationExpression.compilec                 C   s„   |j  | ¡ g }g }|  | j||¡\}}| |¡ | |¡ |  | j||¡\}}| |¡ | |¡ d}|j  | j|¡}|| |fS ©Nrû   )	r  Úcheck_expression_supportr   rï   rÝ   rÀ   rð   Úcombine_duration_expressionr2   )r   r   r   r‚   r  r   r   r  r   r   r   r   Ó  s   



zDurationExpression.as_sqlN)r   r   r   r   r   r   r   r   r   rþ   Æ  s    rþ   c                       s,   e Zd Ze ¡ Z‡ fdd„Zdd„ Z‡  ZS )rÿ   c                    s   t ƒ  || j|¡ d S rg   )rî   ri   rB   )r   rï   rð   rñ   r   r   ri   æ  rã   zTemporalSubtraction.__init__c                 C   s@   |j  | ¡ | | j|¡}| | j|¡}|j  | jj ¡ ||¡S rg   )r  r  r   rï   rð   Úsubtract_temporalsr   r   )r   r   r   rï   rð   r   r   r   r   é  s   zTemporalSubtraction.as_sql)	r   r   r   r   r.   r   ri   r   r  r   r   rñ   r   rÿ   ã  s    rÿ   c                   @   sR   e Zd ZdZdZdd„ Zdd„ Z		dd	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )r}   zDAn object capable of resolving references to existing query objects.Tc                 C   s
   || _ dS )z^
        Arguments:
         * name: the name of the field this expression references
        N)r¸   r·   r   r   r   ri   ö  s   
z
F.__init__c                 C   ó   d  | jj| j¡S ©Nú{}({}))rò   r¤   r   r¸   r8   r   r   r   ró   ý  ó   z
F.__repr__NFc                 C   s   |  | j||||¡S rg   )Úresolve_refr¸   ©r   r“   r’   r”   r•   r˜   Ú
simple_colr   r   r   r'      s   zF.resolve_expressionc                 K   rÄ   rg   rÅ   rÇ   r   r   r   rÉ     r:   zF.ascc                 K   rÊ   rË   rÅ   rÇ   r   r   r   rÍ     rÎ   zF.descc                 C   s   | j |j ko| j|jkS rg   )r¤   r¸   r?   r   r   r   râ   
  ó   zF.__eq__c                 C   rä   rg   )rå   r¸   r8   r   r   r   ræ     r¿   z
F.__hash__©NTNFFF)r   r   r   r   rè   ri   ró   r'   rÉ   rÍ   râ   ræ   r   r   r   r   r}   ð  s    
ÿr}   c                   @   s*   e Zd ZdZdd„ Zd	dd„Zdd„ ZdS )
ÚResolvedOuterRefz¿
    An object that contains a reference to an outer query.

    In this case, the reference to the outer query has been resolved because
    the inner query has been used as a subquery.
    c                 O   rN   )NzXThis queryset contains a reference to an outer query and may only be used in a subquery.)Ú
ValueError)r   rà   rÈ   r   r   r   r     rS   zResolvedOuterRef.as_sqlNc                 C   rš   rg   r   rh   r   r   r   rœ     rv   zResolvedOuterRef._preparec                 C   rš   rg   r   ©r   Úrelabelsr   r   r   r¹   !  rv   z ResolvedOuterRef.relabeled_clonerg   )r   r   r   r   r   rœ   r¹   r   r   r   r   r    s
    
r  c                   @   s$   e Zd Z		ddd„Zd	dd„ZdS )
ÚOuterRefNTFc                 C   s   t | j| jƒr
| jS t| jƒS rg   )r*   r¸   r¤   r  r  r   r   r   r'   &  s   
zOuterRef.resolve_expressionc                 C   rš   rg   r   rh   r   r   r   rœ   ,  rv   zOuterRef._preparer  rg   )r   r   r   r'   rœ   r   r   r   r   r  %  s
    
ÿr  c                       sv   e Zd ZdZdZdZdZdZddœ‡ fdd„
Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zddd„Zddd„Z‡ fdd„Z‡  ZS )ÚFunczAn SQL function call.Nz%(function)s(%(expressions)s)ú, r(   c                   sh   | j d ur"t|ƒ| j kr"td| jj| j | j dkrdndt|ƒf ƒ‚tƒ j|d� | j|Ž | _|| _	d S )Nz#'%s' takes exactly %s %s (%s given)é   ÚargumentrÖ   r(   )
ÚarityÚlenÚ	TypeErrorr¤   r   rî   ri   rƒ   Úsource_expressionsÚextra)r   r   r‚   r&  rñ   r   r   ri   7  s   üÿ
zFunc.__init__c                 C   sl   | j  dd„ | jD ƒ¡}i | j¥|  ¡ ¥}|r.d dd„ t| ¡ ƒD ƒ¡}d | jj	||¡S d | jj	|¡S )Nc                 s   ó   � | ]}t |ƒV  qd S rg   ©r|   r~   r   r   r   rŠ   F  ó   € z Func.__repr__.<locals>.<genexpr>r  c                 s   s(   � | ]\}}t |ƒd  t |ƒ V  qdS )ú=Nr(  )r   ÚkeyÚvalr   r   r   rŠ   I  s   €& ú
{}({}, {})r  )
Ú
arg_joinerÚjoinr%  r&  Ú_get_repr_optionsÚsortedr×   rò   r¤   r   )r   rà   r&  r   r   r   ró   E  s   zFunc.__repr__c                 C   s   i S )zAReturn a dict of extra __init__() options to include in the repr.r   r8   r   r   r   r0  M  r�   zFunc._get_repr_optionsc                 C   rž   rg   ©r%  r8   r   r   r   ru   Q  ó   zFunc.get_source_expressionsc                 C   s
   || _ d S rg   r2  rw   r   r   r   ry   T  r¿   zFunc.set_source_expressionsTFc           	      C   s>   |   ¡ }||_t|jƒD ]\}}| |||||¡|j|< q|S rg   )rl   r–   Ú	enumerater%  r'   )	r   r“   r’   r”   r•   r˜   r™   Úposr€   r   r   r   r'   W  s
   zFunc.resolve_expressionc                 K   s¶   |j  | ¡ g }g }| jD ]}	| |	¡\}
}| |
¡ | |¡ qi | j¥|¥}|d ur1||d< n| d| j¡ |p@| 	d| j
¡}|pI| 	d| j¡}| |¡ |d< |d< || |fS )NÚfunctionÚtemplater.  r‚   r›   )r  r  r%  r   rÝ   rÀ   r&  Ú
setdefaultr6  Úgetr7  r.  r/  )r   r   r   r6  r7  r.  r   Ú	sql_partsr   r€   Úarg_sqlÚ
arg_paramsÚdatar   r   r   r   ^  s   


zFunc.as_sqlc                    s*   t ƒ  ¡ }| jd d … |_| j ¡ |_|S rg   )rî   rl   r%  r&  )r   rl   rñ   r   r   rl   s  s   
z	Func.copyrç   )NNN)r   r   r   r   r6  r7  r.  r"  ri   ró   r0  ru   ry   r'   r   rl   r  r   r   rñ   r   r  0  s    

r  c                       sH   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd‡ fdd„	Zdd„ Z‡  Z	S )r/   z9Represent a wrapped value as a node within an expression.Nc                    s   t ƒ j|d� || _dS )a:  
        Arguments:
         * value: the value this expression represents. The value will be
           added into the sql parameter list and properly quoted.

         * output_field: an instance of the model field type that this
           expression will return, such as IntegerField() or CharField().
        r(   N)rî   ri   r¨   )r   r¨   r   rñ   r   r   ri   |  s   	
zValue.__init__c                 C   r  r  )rò   r¤   r   r¨   r8   r   r   r   ró   ˆ  r  zValue.__repr__c                 C   s|   |j  | ¡ | j}| j}|d ur1| jr|j||d�}n|j||d�}t|dƒr1| |||¡|gfS |d u r9dg fS d|gfS )Nr   Úget_placeholderÚNULLz%s)	r  r  r¨   r¡   r˜   Úget_db_prep_saveÚget_db_prep_valuer)   r>  )r   r   r   r,  r   r   r   r   r   ‹  s   

zValue.as_sqlTFc                    s   t ƒ  |||||¡}||_|S rg   )rî   r'   r˜   r—   rñ   r   r   r'   �  s   zValue.resolve_expressionc                 C   rt   rg   r   r8   r   r   r   rÁ   ¢  rv   zValue.get_group_by_colsrg   rç   )
r   r   r   r   ri   ró   r   r'   rÁ   r  r   r   rñ   r   r/   z  s    r/   c                       s   e Zd Z‡ fdd„Z‡  ZS )r-   c                    s4   |j  | ¡ |jjrtƒ  ||¡S |j  | j¡g fS rg   )r  r  rü   rý   rî   r   Údate_interval_sqlr¨   r„   rñ   r   r   r   §  s   zDurationValue.as_sql)r   r   r   r   r  r   r   rñ   r   r-   ¦  s    r-   c                       s6   e Zd Zd
‡ fdd„	Zdd„ Zdd„ Zdd	„ Z‡  ZS )ÚRawSQLNc                    s0   |d u rt  ¡ }||| _| _tƒ j|d� d S rí   )r   rØ   r   r   rî   ri   )r   r   r   r   rñ   r   r   ri   ¯  s   zRawSQL.__init__c                 C   ó   d  | jj| j| j¡S ©Nr-  )rò   r¤   r   r   r   r8   r   r   r   ró   µ  rã   zRawSQL.__repr__c                 C   s   d| j  | jfS r  )r   r   r„   r   r   r   r   ¸  r:   zRawSQL.as_sqlc                 C   ó   | gS rg   r   r8   r   r   r   rÁ   »  r3  zRawSQL.get_group_by_colsrg   )r   r   r   ri   ró   r   rÁ   r  r   r   rñ   r   rC  ®  s
    rC  c                   @   r  )ÚStarc                 C   ó   dS )Nz'*'r   r8   r   r   r   ró   À  rv   zStar.__repr__c                 C   s   dg fS )Nr"   r   r„   r   r   r   r   Ã  ó   zStar.as_sqlN)r   r   r   ró   r   r   r   r   r   rG  ¿  s    rG  c                   @   s$   e Zd Ze ¡ Zdd„ Zdd„ ZdS )ÚRandomc                 C   rH  )NzRandom()r   r8   r   r   r   ró   Ê  rv   zRandom.__repr__c                 C   s   |j  ¡ g fS rg   )r  Úrandom_function_sqlr„   r   r   r   r   Í  ó   zRandom.as_sqlN)r   r   r   r   rª   r   ró   r   r   r   r   r   rJ  Ç  s    rJ  c                       sJ   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )ÚColTNc                    s,   |d u r|}t ƒ j|d� ||| _| _d S rí   )rî   ri   ÚaliasÚtarget)r   rN  rO  r   rñ   r   r   ri   Õ  s   zCol.__init__c                 C   rD  rE  )rò   r¤   r   rN  rO  r8   r   r   r   ró   Û  ó   ÿzCol.__repr__c                 C   s$   |j }d|| jƒ|| jjƒf g fS )Nz%s.%s)Úquote_name_unless_aliasrN  rO  Úcolumn©r   r   r   Úqnr   r   r   r   ß  s   z
Col.as_sqlc                 C   s   |   | | j| j¡| j| j¡S rg   )r¤   r9  rN  rO  r   r  r   r   r   r¹   ã  s   zCol.relabeled_clonec                 C   rF  rg   r   r8   r   r   r   rÁ   æ  r3  zCol.get_group_by_colsc                 C   ó0   | j | jkr| j |¡S | j |¡| j  |¡ S rg   ©rO  r   rr   rs   r   r   r   rr   é  s
   

ÿzCol.get_db_convertersrg   )r   r   r   r�   ri   ró   r   r¹   rÁ   rr   r  r   r   rñ   r   rM  Ñ  s    rM  c                       sF   e Zd ZdZdZd‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Z	‡  Z
S )Ú	SimpleColz½
    Represents the SQL of a column name without the table name.

    This variant of Col doesn't include the table name (or an alias) to
    avoid a syntax error in check constraints.
    TNc                    s$   |d u r|}t ƒ j|d� || _d S rí   )rî   ri   rO  )r   rO  r   rñ   r   r   ri   ù  s   
zSimpleCol.__init__c                 C   r  r  )rò   r¤   r   rO  r8   r   r   r   ró   ÿ  r  zSimpleCol.__repr__c                 C   s   |j }|| jjƒg fS rg   )rQ  rO  rR  rS  r   r   r   r     s   zSimpleCol.as_sqlc                 C   rF  rg   r   r8   r   r   r   rÁ     r3  zSimpleCol.get_group_by_colsc                 C   rU  rg   rV  rs   r   r   r   rr   	  s   

ÿÿzSimpleCol.get_db_convertersrg   )r   r   r   r   r�   ri   ró   r   rÁ   rr   r  r   r   rñ   r   rW  ð  s    rW  c                       sZ   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zddd„Zdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚRefz~
    Reference to column alias of the query. For example, Ref('sum_cost') in
    qs.annotate(sum_cost=Sum('cost')) query.
    c                    s   t ƒ  ¡  ||| _| _d S rg   )rî   ri   Úrefsr£   )r   rY  r£   rñ   r   r   ri     s   
zRef.__init__c                 C   rD  rE  )rò   r¤   r   rY  r£   r8   r   r   r   ró     rã   zRef.__repr__c                 C   ó   | j gS rg   ©r£   r8   r   r   r   ru     rI  zRef.get_source_expressionsc                 C   s   |\| _ d S rg   r[  rw   r   r   r   ry   !  rz   zRef.set_source_expressionsNTFc                 C   rš   rg   r   )r   r“   r’   r”   r•   r˜   r   r   r   r'   $  s   zRef.resolve_expressionc                 C   rš   rg   r   r  r   r   r   r¹   )  rv   zRef.relabeled_clonec                 C   s   |j  | j¡g fS rg   )r  Ú
quote_namerY  r„   r   r   r   r   ,  r  z
Ref.as_sqlc                 C   rF  rg   r   r8   r   r   r   rÁ   /  r3  zRef.get_group_by_colsrç   )r   r   r   r   ri   ró   ru   ry   r'   r¹   r   rÁ   r  r   r   rñ   r   rX    s    
rX  c                       s,   e Zd ZdZdZ‡ fdd„Zdd„ Z‡  ZS )ÚExpressionListz¬
    An expression containing multiple expressions. Can be used to provide a
    list of expressions as an argument to another expression, like an
    ordering clause.
    z%(expressions)sc                    s*   |s
t d| jj ƒ‚tƒ j|i |¤Ž d S )Nz$%s requires at least one expression.)r  r¤   r   rî   ri   )r   r‚   r&  rñ   r   r   ri   ;  s   zExpressionList.__init__c                 C   s   | j  dd„ | jD ƒ¡S )Nc                 s   r'  rg   r(  r~   r   r   r   rŠ   A  r)  z)ExpressionList.__str__.<locals>.<genexpr>)r.  r/  r%  r8   r   r   r   rô   @  r  zExpressionList.__str__)r   r   r   r   r7  ri   rô   r  r   r   rñ   r   r]  3  s
    r]  c                       s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚExpressionWrapperz”
    An expression that can wrap another expression so that it can provide
    extra context to the inner expression, such as the output_field.
    c                    s   t ƒ j|d� || _d S rí   )rî   ri   r©   )r   r©   r   rñ   r   r   ri   J  s   
zExpressionWrapper.__init__c                 C   ó   |d | _ d S ©Nr   ©r©   rw   r   r   r   ry   N  rL  z(ExpressionWrapper.set_source_expressionsc                 C   rZ  rg   ra  r8   r   r   r   ru   Q  rI  z(ExpressionWrapper.get_source_expressionsc                 C   s   | j  ||¡S rg   )r©   r   r„   r   r   r   r   T  rL  zExpressionWrapper.as_sqlc                 C   r  r  )rò   r¤   r   r©   r8   r   r   r   ró   W  r  zExpressionWrapper.__repr__)
r   r   r   r   ri   ry   ru   r   ró   r  r   r   rñ   r   r^  D  s    r^  c                       sf   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	ddd„Z
ddd„Zdd„ Z‡  ZS )ÚWhenz"WHEN %(condition)s THEN %(result)sNc                    s~   |r|d u rt di |¤Žd }}|d u st|ddƒr|r tdƒ‚t|t ƒr+|s+tdƒ‚tƒ jd d� || _|  |¡d | _	d S )NÚconditionalFzB__init__() takes either a Q object or lookups as keyword argumentsz1An empty Q() can't be used as a When() condition.r(   r   r   )
r	   Úgetattrr$  r*   r  rî   ri   Ú	conditionrƒ   Úresult)r   re  ÚthenÚlookupsrñ   r   r   ri   ^  s   zWhen.__init__c                 C   s   d| j | jf S )NzWHEN %r THEN %r©re  rf  r8   r   r   r   rô   i  r:   zWhen.__str__c                 C   ó   d| j j| f S ©Nz<%s: %s>©r¤   r   r8   r   r   r   ró   l  r:   zWhen.__repr__c                 C   rõ   rg   ri  r8   r   r   r   ru   o  rz   zWhen.get_source_expressionsc                 C   r÷   rg   ri  rw   r   r   r   ry   r  r:   zWhen.set_source_expressionsc                 C   s
   | j jgS rg   )rf  r¡   r8   r   r   r   r¥   u  s   
zWhen.get_source_fieldsTFc                 C   sJ   |   ¡ }||_t|jdƒr|j ||||d¡|_|j |||||¡|_|S )Nr'   F)rl   r–   r)   re  r'   rf  r—   r   r   r   r'   y  s   zWhen.resolve_expressionc                 K   sn   |j  | ¡ |}g }| | j¡\}}||d< | |¡ | | j¡\}	}
|	|d< | |
¡ |p0| j}|| |fS )Nre  rf  )r  r  r   re  rÀ   rf  r7  )r   r   r   r7  r   Útemplate_paramsÚ
sql_paramsÚcondition_sqlÚcondition_paramsÚ
result_sqlÚresult_paramsr   r   r   r   �  s   


zWhen.as_sqlc                 C   ó$   g }|   ¡ D ]	}| | ¡ ¡ q|S rg   ©ru   rÀ   rÁ   rÂ   r   r   r   rÁ   Ž  s   zWhen.get_group_by_cols©NNrç   rg   )r   r   r   r7  ri   rô   ró   ru   ry   r¥   r'   r   rÁ   r  r   r   rñ   r   rb  [  s    

rb  c                       sp   e Zd ZdZdZdZdddœ‡ fdd„
Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
ddd„Z‡ fdd„Zddd„Z‡  ZS )ÚCasezÊ
    An SQL searched CASE expression:

        CASE
            WHEN n > 0
                THEN 'positive'
            WHEN n < 0
                THEN 'negative'
            ELSE 'zero'
        END
    z#CASE %(cases)s ELSE %(default)s ENDú N)Údefaultr   c                   sJ   t dd„ |D ƒƒstdƒ‚tƒ  |¡ t|ƒ| _|  |¡d | _|| _d S )Nc                 s   s   � | ]}t |tƒV  qd S rg   )r*   rb  )r   Úcaser   r   r   rŠ   ¦  r‹   z Case.__init__.<locals>.<genexpr>z.Positional arguments must all be When objects.r   )	Úallr$  rî   ri   ÚlistÚcasesrƒ   rx  r&  )r   rx  r   r|  r&  rñ   r   r   ri   ¥  s   

zCase.__init__c                 C   s    dd  dd„ | jD ƒ¡| jf S )NzCASE %s, ELSE %rr  c                 s   r'  rg   r(  )r   r™   r   r   r   rŠ   ®  r)  zCase.__str__.<locals>.<genexpr>)r/  r|  rx  r8   r   r   r   rô   ­  ó    zCase.__str__c                 C   rj  rk  rl  r8   r   r   r   ró   °  r:   zCase.__repr__c                 C   s   | j | jg S rg   ©r|  rx  r8   r   r   r   ru   ³  rL  zCase.get_source_expressionsc                 C   s   |�^ | _ | _d S rg   r~  rw   r   r   r   ry   ¶  r  zCase.set_source_expressionsTFc           	      C   sT   |   ¡ }||_t|jƒD ]\}}| |||||¡|j|< q|j |||||¡|_|S rg   )rl   r–   r4  r|  r'   rx  )	r   r“   r’   r”   r•   r˜   r™   r5  ry  r   r   r   r'   ¹  s   zCase.resolve_expressionc                    s   t ƒ  ¡ }|jd d … |_|S rg   )rî   rl   r|  )r   r™   rñ   r   r   rl   Á  s   
z	Case.copyc              	   K   sþ   |j  | ¡ | js| | j¡S i | j¥|¥}g }g }| jD ]}	z	| |	¡\}
}W n	 ty1   Y qw | |
¡ | |¡ q| | j¡\}}|sK||fS |pO| j	}| 
|¡|d< ||d< | |¡ |ph| d| j¡}|| }| jd ur{|j  | j¡| }||fS )Nr|  rx  r7  )r  r  r|  r   rx  r&  r   rÝ   rÀ   Úcase_joinerr/  r9  r7  r¡   Úunification_cast_sqlr   )r   r   r   r7  r  r   rm  Ú
case_partsrn  ry  Úcase_sqlÚcase_paramsÚdefault_sqlÚdefault_paramsr   r   r   r   r   Æ  s4   
ÿ



zCase.as_sqlrç   ru  )r   r   r   r   r7  r  ri   rô   ró   ru   ry   r'   rl   r   r  r   r   rñ   r   rv  –  s    
rv  c                       sn   e Zd ZdZdZdZd‡ fdd„	Z‡ fdd„Z‡ fd	d
„Zddd„Z	dd„ Z
dd„ Zddd„Zdd„ Z‡  ZS )ÚSubqueryz”
    An explicit subquery. It may contain OuterRef() references to the outer
    query which will be resolved when it is applied to that query.
    ú(%(subquery)s)FNc                    s   || _ || _tƒ  |¡ d S rg   )Úquerysetr&  rî   ri   )r   rˆ  r   r&  rñ   r   r   ri   ê  s   zSubquery.__init__c                    s,   t | jjjƒdkr| jjjd jS tƒ  ¡ S )Nr   r   )r#  rˆ  r“   Úselectr›   rî   rŸ   r8   rñ   r   r   rŸ   ï  s   
zSubquery._resolve_output_fieldc                    s   t ƒ  ¡ }|j ¡ |_|S rg   )rî   rl   rˆ  rz  )r   r¾   rñ   r   r   rl   ô  rp   zSubquery.copyTc                    s†   |   ¡ ‰ˆˆ_ˆjj ˆ¡ ‡‡fdd„‰‡ ‡‡‡‡‡fdd„‰ˆˆjjjƒ ˆjjj ¡ D ]\}}t|t	ƒr@ˆ|ƒˆjjj|< q.ˆS )Nc                    s<   t | dƒr‡fdd„| jD ƒ t | dƒrˆ | jƒ| _d S d S )NÚchildrenc                    s   g | ]}ˆ |ƒ‘qS r   r   )r   Ú_child)Úresolve_allr   r   r�     r®   zDSubquery.resolve_expression.<locals>.resolve_all.<locals>.<listcomp>rð   )r)   rŠ  rð   )Úchild)ÚresolverŒ  r   r   rŒ  ÿ  s
   

ÿz0Subquery.resolve_expression.<locals>.resolve_allc                    sT   t | dƒr(| jˆˆ ˆˆˆd�}t |dƒr&|j|jjjjkr&ˆjjj	 
|j¡ |S | S )Nr'   )r“   r’   r”   r•   r˜   rN  )r)   r'   rN  rO  rÙ   rÚ   Údb_tablerˆ  r“   Úexternal_aliasesÚadd)r�  Úresolved)r’   r¾   r˜   r“   r”   r•   r   r   rŽ    s   
þz,Subquery.resolve_expression.<locals>.resolve)
rl   r–   rˆ  r“   Úbump_prefixÚwhereÚannotationsr×   r*   r†  )r   r“   r’   r”   r•   r˜   r+  r¨   r   )r’   r¾   r˜   r“   rŽ  rŒ  r”   r•   r   r'   ù  s   
€zSubquery.resolve_expressionc                 C   s    dd„ dd„ | j jjjD ƒD ƒS )Nc                 S   s   g | ]}|r|‘qS r   r   )r   Úxr   r   r   r�     s    üÿz3Subquery.get_source_expressions.<locals>.<listcomp>c                 S   s   g | ]}t |d dƒ‘qS )rï   N)rd  rˆ   r   r   r   r�     s    
ÿÿ)rˆ  r“   r”  rŠ  r8   r   r   r   ru     s
   
þÿzSubquery.get_source_expressionsc                    s@   |   ¡ ‰ ˆ jj |¡ˆ j_ˆ jjj ‡ fdd„| ¡ D ƒ¡ ˆ S )Nc                 3   s"   � | ]}|ˆ j jjvr|V  qd S rg   )rˆ  r“   Ú	alias_map)r   rN  ©r¾   r   r   rŠ   %  s   € ÿþz+Subquery.relabeled_clone.<locals>.<genexpr>)rl   rˆ  r“   r¹   r�  ÚupdateÚvalues)r   r½   r   r˜  r   r¹   "  s   ÿzSubquery.relabeled_clonec                 K   sX   |j  | ¡ i | j¥|¥}| jjj|d� ¡ \|d< }|p#| d| j¡}|| }||fS )Nr   Úsubqueryr7  )	r  r  r&  rˆ  r“   Úget_compilerr   r9  r7  )r   r   r   r7  r   rm  rn  r   r   r   r   r   +  s   zSubquery.as_sqlc                 C   s    | j dkr|  ¡ }d|_ |S | S )Nr‡  z%(subquery)s)r7  rl   )r   r   r¾   r   r   r   rœ   4  s
   
zSubquery._preparerg   rç   )r   r   r   r   r7  r‡   ri   rŸ   rl   r'   ru   r¹   r   rœ   r  r   r   rñ   r   r†  â  s    
!
		r†  c                       s\   e Zd ZdZe ¡ Zddœ‡ fdd„
Zdd„ Zd‡ fd	d
„	Z	d‡ fdd„	Z
ddd„Z‡  ZS )ÚExistszEXISTS(%(subquery)s)F)Únegatedc                   s   || _ tƒ j|i |¤Ž d S rg   )rž  rî   ri   )r   rž  rà   rÈ   rñ   r   r   ri   D  s   zExists.__init__c                 C   s    t | ƒ| jfd| j i| j¤ŽS )Nrž  )rÜ   rˆ  rž  r&  r8   r   r   r   Ú
__invert__H  r}  zExists.__invert__Nc                    s&   | j  ¡ | _ tƒ j|g|¢R i |¤ŽS rg   )rˆ  Úorder_byrî   r'   )r   r“   rà   rÈ   rñ   r   r   r'   K  s   zExists.resolve_expressionc                    s4   t ƒ j|||fi |¤Ž\}}| jrd |¡}||fS )NzNOT {})rî   r   rž  rò   ©r   r   r   r7  r   r   r   rñ   r   r   r   Q  s   
zExists.as_sqlc                 K   s,   | j |||fi |¤Ž\}}d |¡}||fS )NzCASE WHEN {} THEN 1 ELSE 0 END)r   rò   r¡  r   r   r   Ú	as_oracleW  s   
zExists.as_oraclerg   )r   r   r   r7  r   ÚBooleanFieldr   ri   rŸ  r'   r   r¢  r  r   r   rñ   r   r�  @  s    r�  c                   @   sl   e Zd ZdZddd„Zdd„ Z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dS )rÆ   z%(expression)s %(ordering)sFc                 C   s>   |r|rt dƒ‚|| _|| _|| _t|dƒst dƒ‚|| _d S )Nz1nulls_first and nulls_last are mutually exclusiver'   z%expression must be an expression type)r  Únulls_firstÚ
nulls_lastrÌ   r)   r©   )r   r©   rÌ   r¤  r¥  r   r   r   ri   c  s   

zOrderBy.__init__c                 C   rD  )Nz{}({}, descending={}))rò   r¤   r   r©   rÌ   r8   r   r   r   ró   m  rP  zOrderBy.__repr__c                 C   r_  r`  ra  rw   r   r   r   ry   q  rL  zOrderBy.set_source_expressionsc                 C   rZ  rg   ra  r8   r   r   r   ru   t  rI  zOrderBy.get_source_expressionsNc                 K   s‚   |s| j rd| j }n| jrd| j }|j | ¡ | | j¡\}}|| jr'dnddœ|¥}|p1| j}|| d¡9 }||  	¡ |fS )Nz%s NULLS LASTz%s NULLS FIRSTÚDESCÚASC)r©   Úorderingz%(expression)s)
r¥  r7  r¤  r  r  r   r©   rÌ   ÚcountÚrstrip)r   r   r   r7  r   Úexpression_sqlr   Úplaceholdersr   r   r   r   w  s   
þý
zOrderBy.as_sqlc                 C   ó*   d }| j rd}n| jrd}| j|||d�S )Nz3%(expression)s IS NULL, %(expression)s %(ordering)sz7%(expression)s IS NOT NULL, %(expression)s %(ordering)s©r7  ©r¥  r¤  r   ©r   r   r   r7  r   r   r   r   ˆ  ó   zOrderBy.as_sqlitec                 C   r­  )Nz<IF(ISNULL(%(expression)s),1,0), %(expression)s %(ordering)s z<IF(ISNULL(%(expression)s),0,1), %(expression)s %(ordering)s r®  r¯  r°  r   r   r   Úas_mysql�  r±  zOrderBy.as_mysqlc                 C   rs  rg   rt  rÂ   r   r   r   rÁ   ˜  s   zOrderBy.get_group_by_colsc                 C   s.   | j  | _ | js| jr| j | _| j | _| S rg   )rÌ   r¤  r¥  r8   r   r   r   rÏ   ž  s
   


zOrderBy.reverse_orderingc                 C   ó
   d| _ d S r<   ©rÌ   r8   r   r   r   rÉ   ¥  r¿   zOrderBy.ascc                 C   r³  r]   r´  r8   r   r   r   rÍ   ¨  r¿   zOrderBy.desc)FFFrg   )r   r   r   r7  ri   ró   ry   ru   r   r   r²  rÁ   rÏ   rÉ   rÍ   r   r   r   r   rÆ   `  s    


rÆ   c                       sh   e Zd ZdZdZdZdZd‡ fdd„	Zdd„ Zd	d
„ Z	dd„ Z
ddd„Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚWindowz %(expression)s OVER (%(window)s)FTNc                    s¼   || _ || _|| _t|ddƒstd|jj ƒ‚| j d ur/t| j tt	fƒs)| j f| _ t
| j Ž | _ | jd urMt| jt	tfƒrCt
| jŽ | _n
t| jtƒsMtdƒ‚tƒ j|d� |  |¡d | _d S )Nré   Fz3Expression '%s' isn't compatible with OVER clauses.zCorder_by must be either an Expression or a sequence of expressions.r(   r   )Úpartition_byr   Úframerd  r  r¤   r   r*   rÞ   r{  r]  rf   rî   ri   rƒ   Úsource_expression)r   r©   r¶  r   r·  r   rñ   r   r   ri   µ  s,   ÿÿ


ÿzWindow.__init__c                 C   s   | j jS rg   )r¸  r   r8   r   r   r   rŸ   Ð  rI  zWindow._resolve_output_fieldc                 C   s   | j | j| j| jgS rg   ©r¸  r¶  r   r·  r8   r   r   r   ru   Ó  rÎ   zWindow.get_source_expressionsc                 C   s   |\| _ | _| _| _d S rg   r¹  rw   r   r   r   ry   Ö  r  zWindow.set_source_expressionsc                 C   sö   |j  | ¡ | | j¡\}}g g }}| jd ur-| jj||dd�\}}	| |¡ | |	¡ | jd urL| d¡ | | j¡\}
}| d 	|
¡¡ | |¡ | j
rc| | j
¡\}}| d| ¡ | |¡ | |¡ |pl| j}||d 	|¡ ¡ dœ |fS )NzPARTITION BY %(expressions)s)r   r   r7  z
 ORDER BY Ú rw  )r©   Úwindow)r  r  r   r¸  r¶  r   rÀ   r   rÝ   r/  r·  r7  Ústrip)r   r   r   r7  Úexpr_sqlr   Ú
window_sqlÚwindow_paramsÚsql_exprrn  Ú	order_sqlÚorder_paramsÚ	frame_sqlÚframe_paramsr   r   r   r   Ù  s6   


þ







þýzWindow.as_sqlc                 C   sH   d  t| jƒ| jrdt| jƒ nd| jrdt| jƒ ndt| jp!dƒ¡S )Nz{} OVER ({}{}{})zPARTITION BY rº  z	ORDER BY )rò   r|   r¸  r¶  r   r·  r8   r   r   r   rô   ù  s   üzWindow.__str__c                 C   rj  rk  rl  r8   r   r   r   ró     r:   zWindow.__repr__c                 C   rt   rg   r   r8   r   r   r   rÁ     rv   zWindow.get_group_by_cols)NNNNrg   )r   r   r   r7  r‡   r�   rè   ri   rŸ   ru   ry   r   rô   ró   rÁ   r  r   r   rñ   r   rµ  ¬  s    
 rµ  c                   @   sV   e Zd ZdZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚWindowFrameaV  
    Model the frame clause in window expressions. There are two types of frame
    clauses which are subclasses, however, all processing and validation (by no
    means intended to be complete) is done here. Thus, providing an end for a
    frame is optional (the default is UNBOUNDED FOLLOWING, which is the last
    row in the frame).
    z,%(frame_type)s BETWEEN %(start)s AND %(end)sNc                 C   s   t |ƒ| _t |ƒ| _d S rg   )r/   ÚstartÚend©r   rÆ  rÇ  r   r   r   ri     s   
zWindowFrame.__init__c                 C   r÷   rg   ©rÆ  rÇ  rw   r   r   r   ry     r:   z"WindowFrame.set_source_expressionsc                 C   rõ   rg   rÉ  r8   r   r   r   ru     rz   z"WindowFrame.get_source_expressionsc                 C   s>   |j  | ¡ |  || jj| jj¡\}}| j| j||dœ g fS )N©Ú
frame_typerÆ  rÇ  )r  r  Úwindow_frame_start_endrÆ  r¨   rÇ  r7  rË  )r   r   r   rÆ  rÇ  r   r   r   r     s   ýüzWindowFrame.as_sqlc                 C   rj  rk  rl  r8   r   r   r   ró   %  r:   zWindowFrame.__repr__c                 C   rt   rg   r   r8   r   r   r   rÁ   (  rv   zWindowFrame.get_group_by_colsc                 C   sÈ   | j jd ur| j jdk rdt| j jƒtjjf }n| j jd ur*| j jdkr*tjj}ntjj}| jjd urE| jjdkrEd| jjtjj	f }n| jjd urV| jjdkrVtjj}ntjj
}| j| j||dœ S )Nr   z%d %srÊ  )rÆ  r¨   Úabsr   r  Ú	PRECEDINGÚCURRENT_ROWÚUNBOUNDED_PRECEDINGrÇ  Ú	FOLLOWINGÚUNBOUNDED_FOLLOWINGr7  rË  rÈ  r   r   r   rô   +  s   

ýzWindowFrame.__str__c                 C   rN   )Nz3Subclasses must implement window_frame_start_end().rP   ©r   r   rÆ  rÇ  r   r   r   rÌ  ?  rI  z"WindowFrame.window_frame_start_endru  )r   r   r   r   r7  ri   ry   ru   r   ró   rÁ   rô   rÌ  r   r   r   r   rÅ    s    
	rÅ  c                   @   r   )ÚRowRangeÚROWSc                 C   ó   |j  ||¡S rg   )r  Úwindow_frame_rows_start_endrÓ  r   r   r   rÌ  F  rL  zRowRange.window_frame_start_endN©r   r   r   rË  rÌ  r   r   r   r   rÔ  C  ó    rÔ  c                   @   r   )Ú
ValueRangeÚRANGEc                 C   rÖ  rg   )r  Úwindow_frame_range_start_endrÓ  r   r   r   rÌ  M  rL  z!ValueRange.window_frame_start_endNrØ  r   r   r   r   rÚ  J  rÙ  rÚ  )2rl   r+   rÑ   Údecimalr   Údjango.core.exceptionsr   r   Ú	django.dbr   Údjango.db.modelsr   Údjango.db.models.query_utilsr	   Údjango.utils.deconstructr
   Údjango.utils.functionalr   Údjango.utils.hashabler   r   r   rf   rì   r0   rþ   rÿ   r}   r  r  r  r/   r-   rC  rG  rJ  rM  rW  rX  r]  r^  rb  rv  r†  r�  rÆ   rµ  rÅ  rÔ  rÚ  r   r   r   r   Ú<module>   sX    p y: J,
"!;L^ L\;