
    `?i                         d Z ddlmZmZ ddlmZ  G d de      Z G d de      Z G d d	e      Z G d
 de      Z	 G d de      Z
 G d de      Zy)z;Database functions that do comparisons or type conversions.    )FuncValue)_lazy_re_compilec                   T     e Zd ZdZdZdZ fdZ fdZ fdZd Z	d Z
 fd	Z xZS )
Castz)Coerce an expression to a new field type.CASTz,%(function)s(%(expressions)s AS %(db_type)s)c                 (    t         |   ||       y )N)output_field)super__init__)self
expressionr
   	__class__s      w/home/cursorai/projects/django-cronjob-utils/venv/lib/python3.12/site-packages/django/db/models/functions/comparison.pyr   zCast.__init__   s    ,?    c                 b    | j                   j                  |      |d<   t        |   ||fi |S )Ndb_type)r
   cast_db_typer   as_sqlr   compiler
connectionextra_contextr   s       r   r   zCast.as_sql   s4    #'#4#4#A#A*#Mi w~h
DmDDr   c                     | j                   j                  |      }|dv r+d}t        	|   ||fd|i|\  }}|dk(  rdnd}|g|}||fS |dk(  rd}t        	|   ||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:%fdatezdate(%(expressions)s))r
   r   r   r   )
r   r   r   r   r   r   sqlparamsformat_stringr   s
            r   	as_sqlitezCast.as_sqlite   s    ##++J7**7H'.*/7;HKC +2V*;JATM#-f-F;.H7>*/7;H  t{{8ZA=AAr   c                     d }| j                   j                         }|dk(  rd}n|dk(  r|j                  rd} | j                  ||fd|i|S )N
FloatFieldz(%(expressions)s + 0.0)	JSONFieldz"JSON_EXTRACT(%(expressions)s, '$')r   )r
   get_internal_typemysql_is_mariadbr   )r   r   r   r   r   output_types         r   as_mysqlzCast.as_mysql&   sZ    ''99;,&0HK'J,G,G;Ht{{8ZT(TmTTr   c                 .     | j                   ||fddi|S )Nr   z(%(expressions)s)::%(db_type)s)r   )r   r   r   r   s       r   as_postgresqlzCast.as_postgresql1   s1     t{{
 6
 	
 	
r   c                     | j                   j                         dk(  rd}t        |   ||fd|i|S  | j                  ||fi |S )Nr%   z JSON_QUERY(%(expressions)s, '$')r   )r
   r&   r   r   )r   r   r   r   r   r   s        r   	as_oraclezCast.as_oracle<   s_    ..0K?9H7>*/7;H  t{{8ZA=AAr   )__name__
__module____qualname____doc__functionr   r   r   r"   r)   r+   r-   __classcell__r   s   @r   r   r      s9    3H=H@EB$	U	
B Br   r   c                   @     e Zd ZdZdZ fdZed        Z fdZ xZ	S )Coalescez:Return, from left to right, the first non-null expression.COALESCEc                 V    t        |      dk  rt        d      t        |   |i | y )N   z+Coalesce must take at least two expressionslen
ValueErrorr   r   r   expressionsextrar   s      r   r   zCoalesce.__init__K   .    {aJKK+//r   c                 b    | j                         D ]  }|j                  }|t        u s||c S  y )N)get_source_expressionsempty_result_set_valueNotImplemented)r   r   results      r   rC   zCoalesce.empty_result_set_valueP   s7    557J66F'6+= 8 r   c           	      *   | j                   j                         dk(  r]| j                         }|j                  | j	                         D cg c]  }t        |d       c}       t        t        |"  ||fi |S  | j                  ||fi |S c c}w )N	TextFieldTO_NCLOB)r2   )	r
   r&   copyset_source_expressionsrB   r   r   r6   r   )r   r   r   r   cloner   r   s         r   r-   zCoalesce.as_oracleX   s     ..0K?IIKE(( '+&A&A&C&C
 j9&C 50:WWWt{{8ZA=AAs   B)
r.   r/   r0   r1   r2   r   propertyrC   r-   r3   r4   s   @r   r6   r6   F   s0    DH0
  B Br   r6   c                   D     e Zd ZdZdZdZ ed      Z fdZ fdZ	 xZ
S )CollateCOLLATEz*%(expressions)s %(function)s %(collation)sFz^[\w-]+$c                     |r| j                   j                  |      st        d|z        || _        t        |   |       y )NzInvalid collation name: %r.)collation_rematchr<   	collationr   r   )r   r   rS   r   s      r   r   zCollate.__init__o   s>    d//55i@:YFGG"$r   c                     |j                  d|j                  j                  | j                               t	        |   ||fi |S )NrS   )
setdefaultops
quote_namerS   r   r   r   s       r   r   zCollate.as_sqlu   s<      jnn.G.G.WXw~h
DmDDr   )r.   r/   r0   r2   r   allowed_defaultr   rQ   r   r   r3   r4   s   @r   rN   rN   g   s/    H;HO $K0L%E Er   rN   c                   0     e Zd ZdZdZ fdZ fdZ x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.
    GREATESTc                 V    t        |      dk  rt        d      t        |   |i | y )Nr9   z+Greatest must take at least two expressionsr:   r=   s      r   r   zGreatest.__init__   r@   r   c                 *    t        |   ||fddi|S )zUse the MAX function on SQLite.r2   MAXr   r"   r   s       r   r"   zGreatest.as_sqlite       w :WWWWr   r.   r/   r0   r1   r2   r   r"   r3   r4   s   @r   rZ   rZ   z   s      H0
X Xr   rZ   c                   0     e Zd ZdZdZ fdZ fdZ x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.
    LEASTc                 V    t        |      dk  rt        d      t        |   |i | y )Nr9   z(Least must take at least two expressionsr:   r=   s      r   r   zLeast.__init__   s.    {aGHH+//r   c                 *    t        |   ||fddi|S )zUse the MIN function on SQLite.r2   MINr_   r   s       r   r"   zLeast.as_sqlite   r`   r   ra   r4   s   @r   rc   rc      s      H0
X Xr   rc   c                   &     e Zd ZdZdZ fdZ xZS )NullIfNULLIFr9   c                     | j                         d   }t        |t              r|j                  t	        d      t        |   ||fi |S )Nr   z2Oracle does not allow Value(None) for expression1.)rB   
isinstancer   valuer<   r   r   )r   r   r   r   expression1r   s        r   r-   zNullIf.as_oracle   sM    113A6k5)k.?.?.GQRRw~h
DmDDr   )r.   r/   r0   r2   arityr-   r3   r4   s   @r   ri   ri      s    HEE Er   ri   N)r1   django.db.models.expressionsr   r   django.utils.regex_helperr   r   r6   rN   rZ   rc   ri    r   r   <module>rs      sd    A 4 6<B4 <B~Bt BBEd E&Xt X*XD X*ET Er   