3
W­E^ß  ã               @   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.ZCASTz,%(function)s(%(expressions)s AS %(db_type)s)c                s   t ƒ j||d� d S )N)Úoutput_field)ÚsuperÚ__init__)ÚselfÚ
expressionr   )Ú	__class__© úG/tmp/pip-build-78s3knkw/Django/django/db/models/functions/comparison.pyr   
   s    zCast.__init__c                s"   | j j|ƒ|d< tƒ j||f|ŽS )NÚdb_type)r   Zcast_db_typer   Úas_sql)r   ÚcompilerÚ
connectionÚextra_context)r
   r   r   r      s    zCast.as_sqlc       	         s�   | j j|ƒ}|d
krZd}tƒ j||fd|i|—Ž\}}|dkrBdnd}|jd|ƒ ||fS |dkr€d	}tƒ j||fd|i|—ŽS | j||f|ŽS )NÚdatetimeÚtimezstrftime(%%s, %(expressions)s)Útemplatez%H:%M:%fz%Y-%m-%d %H:%M:%fr   Údatezdate(%(expressions)s)>   r   r   )r   r   r   r   Úinsert)	r   r   r   r   r   r   ZsqlÚparamsÚformat_string)r
   r   r   Ú	as_sqlite   s    zCast.as_sqlitec             K   s.   | j jƒ dkrdnd }| j||fd|i|—ŽS )NZ
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   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                   s0   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.ZCOALESCEc                s&   t |ƒdk rtdƒ‚tƒ j||Ž d S )Né   z+Coalesce must take at least two expressions)ÚlenÚ
ValueErrorr   r   )r   ÚexpressionsÚextra)r
   r   r   r   /   s    zCoalesce.__init__c                sT   | j jƒ dkrD| jƒ }|jdd„ | jƒ D ƒƒ tt|ƒj||f|ŽS | j||f|ŽS )NZ	TextFieldc             S   s   g | ]}t |d d�‘qS )ZTO_NCLOB)r!   )r   )Ú.0r	   r   r   r   ú
<listcomp>:   s    z&Coalesce.as_oracle.<locals>.<listcomp>)r   r   ÚcopyZset_source_expressionsÚget_source_expressionsr   r#   r   )r   r   r   r   Úclone)r
   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                   s0   e Zd ZdZdZ‡ fdd„Z‡ fdd„Z‡  ZS )Ú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.
    ZGREATESTc                s&   t |ƒdk rtdƒ‚tƒ j||Ž d S )Nr$   z+Greatest must take at least two expressions)r%   r&   r   r   )r   r'   r(   )r
   r   r   r   J   s    zGreatest.__init__c                s   t ƒ j||fddi|—ŽS )zUse the MAX function on SQLite.r!   ÚMAX)r   r   )r   r   r   r   )r
   r   r   r   O   s    zGreatest.as_sqlite)r   r   r   r    r!   r   r   r"   r   r   )r
   r   r/   @   s   r/   c                   s0   e Zd ZdZdZ‡ fdd„Z‡ fdd„Z‡  ZS )Ú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.
    ZLEASTc                s&   t |ƒdk rtdƒ‚tƒ j||Ž d S )Nr$   z(Least must take at least two expressions)r%   r&   r   r   )r   r'   r(   )r
   r   r   r   ^   s    zLeast.__init__c                s   t ƒ j||fddi|—ŽS )zUse the MIN function on SQLite.r!   ZMIN)r   r   )r   r   r   r   )r
   r   r   r   c   s    zLeast.as_sqlite)r   r   r   r    r!   r   r   r"   r   r   )r
   r   r1   T   s   r1   c                   s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )ÚNullIfZNULLIFr$   c                s:   | j ƒ d }t|tƒr(|jd kr(tdƒ‚tƒ j||f|ŽS )Nr   z2Oracle does not allow Value(None) for expression1.)r,   Ú
isinstancer   Úvaluer&   r   r   )r   r   r   r   Zexpression1)r
   r   r   r.   l   s    zNullIf.as_oracle)r   r   r   r!   Úarityr.   r"   r   r   )r
   r   r2   h   s   r2   N)	r    Zdjango.db.models.expressionsr   r   r   r#   r/   r1   r2   r   r   r   r   Ú<module>   s   &