o
    uvXjß  ã                   @   sh   d Z ddlmZm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dS )z;Database functions that do comparisons or type conversions.é    )ÚFuncÚValuec                       sP   e Zd ZdZdZdZ‡ fdd„Z‡ fdd„Z‡ fdd	„Zd
d„ Z	dd„ Z
‡  ZS )ÚCastz)Coerce an expression to a new field type.ÚCASTz,%(function)s(%(expressions)s AS %(db_type)s)c                    s   t ƒ j||d� d S )N)Úoutput_field)ÚsuperÚ__init__)ÚselfÚ
expressionr   ©Ú	__class__© úb/var/www/html/myproject/venv/lib/python3.10/site-packages/django/db/models/functions/comparison.pyr   
   s   zCast.__init__c                    s&   | j  |¡|d< tƒ j||fi |¤ŽS )NÚdb_type)r   Úcast_db_typer   Úas_sql©r	   ÚcompilerÚ
connectionÚextra_contextr   r   r   r      s   zCast.as_sqlc           	         s”   | j  |¡}|dv r-d}tƒ j||fd|i|¤Ž\}}|dkr!dnd}| d|¡ ||fS |dkr@d	}tƒ j||fd|i|¤ŽS | j||fi |¤ŽS )
N>   ÚtimeÚdatetimezstrftime(%%s, %(expressions)s)Útemplater   z%H:%M:%fz%Y-%m-%d %H:%M:%fr   Údatezdate(%(expressions)s))r   r   r   r   Úinsert)	r	   r   r   r   r   r   ÚsqlÚparamsÚformat_stringr   r   r   Ú	as_sqlite   s   zCast.as_sqlitec                 K   s.   | j  ¡ dkr	dnd }| j||fd|i|¤ŽS )NÚ
FloatFieldz(%(expressions)s + 0.0)r   )r   Úget_internal_typer   )r	   r   r   r   r   r   r   r   Úas_mysql   s   zCast.as_mysqlc                 K   s   | j ||fddi|¤ŽS )Nr   z(%(expressions)s)::%(db_type)s)r   r   r   r   r   Úas_postgresql$   s   zCast.as_postgresql)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úfunctionr   r   r   r   r!   r"   Ú__classcell__r   r   r   r   r      s    r   c                       ó0   e Zd ZdZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚCoalescez:Return, from left to right, the first non-null expression.ÚCOALESCEc                    ó*   t |ƒdk r
tdƒ‚tƒ j|i |¤Ž d S )Né   z+Coalesce must take at least two expressions©ÚlenÚ
ValueErrorr   r   ©r	   ÚexpressionsÚextrar   r   r   r   /   ó   zCoalesce.__init__c                    s\   | j  ¡ dkr$|  ¡ }| dd„ |  ¡ D ƒ¡ tt|ƒj||fi |¤ŽS | j||fi |¤ŽS )NÚ	TextFieldc                 S   s   g | ]}t |d d�‘qS )ÚTO_NCLOB)r'   )r   )Ú.0r
   r   r   r   Ú
<listcomp>9   s    ÿz&Coalesce.as_oracle.<locals>.<listcomp>)r   r    ÚcopyÚset_source_expressionsÚget_source_expressionsr   r*   r   )r	   r   r   r   Úcloner   r   r   Ú	as_oracle4   s   
ÿzCoalesce.as_oracle)r#   r$   r%   r&   r'   r   r=   r(   r   r   r   r   r*   +   s
    r*   c                       r)   )ÚGreatestzý
    Return the maximum expression.

    If any expression is null the return value is database-specific:
    On PostgreSQL, the maximum not-null expression is returned.
    On MySQL, Oracle, and SQLite, if any expression is null, null is returned.
    ÚGREATESTc                    r,   )Nr-   z+Greatest must take at least two expressionsr.   r1   r   r   r   r   J   r4   zGreatest.__init__c                    ó   t ƒ j||fddi|¤ŽS )zUse the MAX function on SQLite.r'   ÚMAX©r   r   r   r   r   r   r   O   ó   zGreatest.as_sqlite©r#   r$   r%   r&   r'   r   r   r(   r   r   r   r   r>   @   ó
    r>   c                       r)   )ÚLeastzó
    Return the minimum expression.

    If any expression is null the return value is database-specific:
    On PostgreSQL, return the minimum not-null expression.
    On MySQL, Oracle, and SQLite, if any expression is null, return null.
    ÚLEASTc                    r,   )Nr-   z(Least must take at least two expressionsr.   r1   r   r   r   r   ^   r4   zLeast.__init__c                    r@   )zUse the MIN function on SQLite.r'   ÚMINrB   r   r   r   r   r   c   rC   zLeast.as_sqliterD   r   r   r   r   rF   T   rE   rF   c                       s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )ÚNullIfÚNULLIFr-   c                    s>   |   ¡ d }t|tƒr|jd u rtdƒ‚tƒ j||fi |¤ŽS )Nr   z2Oracle does not allow Value(None) for expression1.)r;   Ú
isinstancer   Úvaluer0   r   r   )r	   r   r   r   Úexpression1r   r   r   r=   l   s   zNullIf.as_oracle)r#   r$   r%   r'   Úarityr=   r(   r   r   r   r   rI   h   s    rI   N)	r&   Údjango.db.models.expressionsr   r   r   r*   r>   rF   rI   r   r   r   r   Ú<module>   s    &