o
    KDi^                     @   s  d dl mZ 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mZmZmZmZ dd
lmZmZ d dlmZmZ ed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 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 d-d. d.e+Z,G d/d0 d0e+Z-G d1d2 d2eZ.G d3d4 d4e.Z/G d5d6 d6e.Z0G d7d8 d8e.Z1G d9d: d:eZ2G d;d< d<e2Z3G d=d> d>e2Z4G d?d@ d@eZ5G dAdB dBeZ6G dCdD dDeZ7G dEdF dFeZ8G dGdH dHeZ9G dIdJ dJeZ:ee; eZ<dS )K    )unicode_literalsN)timegm)Decimallocalcontext)UUID)	getLogger)
BaseTzInfo   )escapeparse_array
comma_joinstring_or_funcget_subclass_names)FFunctionOperatorsMixin)IPv4AddressIPv6Addressclickhouse_ormc                   @   s   e Zd ZdZdZd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dZdddZdd Zdd Zdd ZdS )Fieldz2
    Abstract base class for all field types.
    Nr   c                 C   s  |||g d dksJ d|d u s$t|ts$t|tr |dks$J d|d u s:t|ts:t|tr6|dks:J d|d u sHt|tu sHJ d|d u sYt|trU|dksYJ dtj| _t jd7  _|d u rk| jn|| _	|| _
|| _t| j
p|| jp||| _|| _d S )	N   zCOnly one of default, alias and materialized parameters can be given z=Alias parameter must be a string or function object, if givenzDMaterialized parameter must be a string or function object, if givenz(readonly parameter must be bool if givenz$Codec field must be string, if givenr	   )count
isinstancer   strtypeboolr   creation_counterclass_defaultdefaultaliasmaterializedreadonlycodec)selfr   r   r    r!   r"    r$   S/var/www/Datamplify/venv/lib/python3.10/site-packages/infi/clickhouse_orm/fields.py__init__   s(   &&
zField.__init__c                 C   s   | j S N)namer#   r$   r$   r%   __str__.   s   zField.__str__c                 C   s   d| j j S )Nz<%s>)	__class____name__r)   r$   r$   r%   __repr__1      zField.__repr__c                 C   s   |S )a  
        Converts the input value into the expected Python data type, raising ValueError if the
        data can't be converted. Returns the converted value. Subclasses should override this.
        The timezone_in_use parameter should be consulted when parsing datetime fields.
        r$   r#   valuetimezone_in_user$   r$   r%   	to_python4   s   zField.to_pythonc                 C   s   dS )z
        Called after to_python to validate that the value is suitable for the field's database type.
        Subclasses should override this.
        Nr$   r#   r0   r$   r$   r%   validate<   s   zField.validatec                 C   s,   ||k s||krt d| jj|||f dS )zb
        Utility method to check that the given value is between min_value and max_value.
        z-%s out of range - %s is not between %s and %sN)
ValueErrorr+   r,   )r#   r0   	min_value	max_valuer$   r$   r%   _range_checkC   s   zField._range_checkTc                 C   s
   t ||S )z
        Returns the field's value prepared for writing to the database.
        When quote is true, strings are surrounded by single quotes.
        )r
   r#   r0   quoter$   r$   r%   to_db_stringJ   s   
zField.to_db_stringc                 C   s8   | j }|  }|r|dt| 7 }|r|| |7 }|S )a,  
        Returns an SQL expression describing the field (e.g. for CREATE TABLE).

        - `with_default_expression`: If True, adds default value to sql.
            It doesn't affect fields with alias and materialized values.
        - `db`: Database, used for checking supported features.
        z(%s))db_typeget_db_type_argsr   _extra_params)r#   with_default_expressiondbsqlargsr$   r$   r%   get_sqlQ   s   zField.get_sqlc                 C   s   g S )zReturns field type argumentsr$   r)   r$   r$   r%   r=   a   s   zField.get_db_type_argsc                 C   s   d}| j r|dt| j  7 }n,| jr|dt| j 7 }nt| jtr,|d| j  7 }n| jr;| | j}|d| 7 }| jrM|rM|j	rM| j sM|d| j 7 }|S )Nr   z	 ALIAS %sz MATERIALIZED %sz DEFAULT %s
 CODEC(%s))
r   r   r    r   r   r   to_sqlr;   r"   has_codec_support)r#   r@   rA   r   r$   r$   r%   r>   e   s   zField._extra_paramsc                 C   s@   t | |rdS t| dd}|rt ||rdS t|dd}|sdS )a>  
        Checks if the instance if one of the types provided or if any of the inner_field child is one of the types
        provided, returns True if field or any inner_field is one of ths provided, False otherwise

        - `types`: Iterable of types to check inclusion of instance

        Returns: Boolean
        Tinner_fieldNF)r   getattr)r#   typesrG   r$   r$   r%   r   t   s   
	
zField.isinstanceNNNNNTTN)r,   
__module____qualname____doc__r(   parentr   r   r<   r&   r*   r-   r2   r4   r8   r;   rC   r=   r>   r   r$   r$   r$   r%   r      s$    


r   c                   @   s   e Zd ZdZdZdd ZdS )StringFieldr   Stringc                 C   s6   t |tr|S t |tr|dS td| jj|f NUTF-8zInvalid value for %s: %r)r   r   bytesdecoder5   r+   r,   r/   r$   r$   r%   r2      s
   


zStringField.to_pythonN)r,   rM   rN   r   r<   r2   r$   r$   r$   r%   rQ      s    rQ   c                       s2   e Zd Zd fdd	Z fddZdd Z  ZS )	FixedStringFieldNc                    s*   || _ d| | _tt| |||| d S )NzFixedString(%d))_lengthr<   superrW   r&   )r#   lengthr   r   r    r!   r+   r$   r%   r&      s   
zFixedStringField.__init__c                    s   t t| ||}|dS )N )rY   rW   r2   rstripr/   r[   r$   r%   r2      s   
zFixedStringField.to_pythonc                 C   s<   t |tr
|d}t|| jkrtdt|| jf d S )NrT   z6Value of %d bytes is too long for FixedStringField(%d))r   r   encodelenrX   r5   r3   r$   r$   r%   r4      s
   

zFixedStringField.validateNNNN)r,   rM   rN   r&   r2   r4   __classcell__r$   r$   r[   r%   rW      s    rW   c                   @   sJ   e Zd ZedddZedddZeZdZdd Z	d	d
 Z
dddZdS )	DateFieldi  r	   i9        Datec                 C   s   t |tjr|jr|tj S | S t |tjr|S t |tr+tj	tj
|d S t |tr@|dkr7tjS tj|d S td| jj|f )N)daysz
0000-00-00z%Y-%m-%dInvalid value for %s - %r)r   datetimetzinfo
astimezonepytzutcdateintrb   r   	timedeltar   r6   strptimer5   r+   r,   r/   r$   r$   r%   r2      s   

zDateField.to_pythonc                 C   s   |  |tjtj d S r'   )r8   rb   r6   r7   r3   r$   r$   r%   r4         zDateField.validateTc                 C   s   t | |S r'   )r
   	isoformatr9   r$   r$   r%   r;         zDateField.to_db_stringNrK   )r,   rM   rN   rh   rm   r6   r7   r   r<   r2   r4   r;   r$   r$   r$   r%   rb      s    rb   c                       sP   e Zd ZejdejZdZ		d fdd	Z	dd Z
dd	 ZdddZ  ZS )DateTimeFieldr   DateTimeNc                    s:   t  ||||| |rt|tr|nt|}|| _d S r'   )rY   r&   r   r   rk   timezone)r#   r   r   r    r!   r"   rv   r[   r$   r%   r&      s   
zDateTimeField.__init__c                 C   s    g }| j r|t| j j |S r'   )rv   appendr
   zoner#   rB   r$   r$   r%   r=      s   zDateTimeField.get_db_type_argsc              
   C   s8  t |tjr|jr|S |jtjdS t |tjr%tj|j|j|j	tjdS t |t
r5tj|jtjdS t |tr|dkrA| jS t|dkrazt
|}tj|jtjdW S  ty`   Y nw z	tj|d d}W n tjy} } ztt|d }~ww |jd u s|j|d u r||}|S td| jj|f )Nri   0000-00-00 00:00:00
   )default_timezonerg   )r   rh   ri   replacerk   rl   rm   yearmonthdayrn   utcfromtimestampr   r   r_   r5   iso8601
parse_date
ParseError	utcoffsetlocalizer+   r,   )r#   r0   r1   dter$   r$   r%   r2      s4   


zDateTimeField.to_pythonTc                 C   s   t dt|  |S )Nz%010d)r
   r   utctimetupler9   r$   r$   r%   r;      rq   zDateTimeField.to_db_string)NNNNNNrK   )r,   rM   rN   rh   fromtimestamprk   rl   r   r<   r&   r=   r2   r;   ra   r$   r$   r[   r%   rt      s    rt   c                       sD   e Zd ZdZ		d fdd	Zdd Zdd	d
Z fddZ  ZS )DateTime64Field
DateTime64N   c                    s:   t  |||||| |d u st|tsJ d|| _d S )NzPrecision must be int type)rY   r&   r   rn   	precision)r#   r   r   r    r!   r"   rv   r   r[   r$   r%   r&      s   
zDateTime64Field.__init__c                 C   s(   t | jg}| jr|t| jj |S r'   )r   r   rv   rw   r
   rx   ry   r$   r$   r%   r=      s   z DateTime64Field.get_db_type_argsTc                 C   s"   t dj| d| j | jd|S )z
        Returns the field's value prepared for writing to the database

        Returns string in 0000000000.000000 format, where remainder digits count is equal to precision
        z!{timestamp:0{width}.{precision}f}   )	timestampwidthr   )r
   formatr   r   r9   r$   r$   r%   r;     s   zDateTime64Field.to_db_stringc                    s   zt  ||W S  ty[   t|ttfr#tj|jt	j
d Y S t|trZ|dd }|dkr8| j Y S t|dkrZzt|}tj|jt	j
dW  Y S  tyY   Y  w  w )Nrz   .r   r{   r|   )rY   r2   r5   r   rn   floatrh   r   r~   rk   rl   r   splitr   r_   )r#   r0   r1   	left_partr[   r$   r%   r2     s&   

zDateTime64Field.to_python)NNNNNNr   rK   )	r,   rM   rN   r<   r&   r=   r;   r2   ra   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 )BaseIntFieldz:
    Abstract base class for all integer-type fields.
    c                 C   &   zt |W S    td| jj|f Nrg   )rn   r5   r+   r,   r/   r$   r$   r%   r2   (     
zBaseIntField.to_pythonTc                 C      t |S r'   r   r9   r$   r$   r%   r;   .     zBaseIntField.to_db_stringc                 C      |  || j| j d S r'   r8   r6   r7   r3   r$   r$   r%   r4   3  rq   zBaseIntField.validateNrK   )r,   rM   rN   rO   r2   r;   r4   r$   r$   r$   r%   r   $  s
    
r   c                   @      e Zd ZdZdZdZdS )
UInt8Fieldr      UInt8Nr,   rM   rN   r6   r7   r<   r$   r$   r$   r%   r   7      r   c                   @   r   )UInt16Fieldr   i  UInt16Nr   r$   r$   r$   r%   r   >  r   r   c                   @   r   )UInt32Fieldr   l    UInt32Nr   r$   r$   r$   r%   r   E  r   r   c                   @   r   )UInt64Fieldr   l    UInt64Nr   r$   r$   r$   r%   r   L  r   r   c                   @   r   )	Int8Fieldi   Int8Nr   r$   r$   r$   r%   r   S  r   r   c                   @   r   )
Int16Fieldi i  Int16Nr   r$   r$   r$   r%   r   Z  r   r   c                   @   r   )
Int32Fieldi   iInt32Nr   r$   r$   r$   r%   r   a  r   r   c                   @   r   )
Int64Fieldl         l    Int64Nr   r$   r$   r$   r%   r   h  r   r   c                   @   s"   e Zd ZdZdd ZdddZdS )	BaseFloatFieldz8
    Abstract base class for all float-type fields.
    c                 C   r   r   )r   r5   r+   r,   r/   r$   r$   r%   r2   t  r   zBaseFloatField.to_pythonTc                 C   r   r'   r   r9   r$   r$   r%   r;   z  r   zBaseFloatField.to_db_stringNrK   )r,   rM   rN   rO   r2   r;   r$   r$   r$   r%   r   o  s    r   c                   @      e Zd ZdZdS )Float32FieldFloat32Nr,   rM   rN   r<   r$   r$   r$   r%   r         r   c                   @   r   )Float64FieldFloat64Nr   r$   r$   r$   r%   r     r   r   c                       sD   e Zd ZdZd fdd	Zdd Zddd	Zd
d Zdd Z  Z	S )DecimalFieldzG
    Base class for all decimal fields. Can also be used directly.
    Nc                    s   d|  krdksJ d J dd|  kr|ks$J d J d|| _ || _d| j | jf | _t '}d|_td| j  | _td| j | j  | j | _| j | _W d    n1 s`w   Y  t	t
| |||| d S )Nr	   &   z"Precision must be between 1 and 38r   z/Scale must be between 0 and the given precisionzDecimal(%d,%d)r|   )r   scaler<   r   precr   expr7   r6   rY   r   r&   )r#   r   r   r   r   r    r!   ctxr[   r$   r%   r&     s   $$zDecimalField.__init__c                 C   sX   t |tszt|}W n   td| jj|f | s'td| jj|f | |S )Nrg   zNon-finite value for %s - %r)r   r   r5   r+   r,   	is_finite_roundr/   r$   r$   r%   r2     s   

zDecimalField.to_pythonTc                 C   r   r'   r   r9   r$   r$   r%   r;     r   zDecimalField.to_db_stringc                 C   s   | | jS r'   )quantizer   r3   r$   r$   r%   r     r.   zDecimalField._roundc                 C   r   r'   r   r3   r$   r$   r%   r4     rq   zDecimalField.validater`   rK   )
r,   rM   rN   rO   r&   r2   r;   r   r4   ra   r$   r$   r[   r%   r     s    

r   c                          e Zd Zd fdd	Z  ZS )Decimal32FieldNc                    (   t t| d||||| d| | _d S )N	   zDecimal32(%d))rY   r   r&   r<   r#   r   r   r   r    r!   r[   r$   r%   r&        zDecimal32Field.__init__r`   r,   rM   rN   r&   ra   r$   r$   r[   r%   r         r   c                       r   )Decimal64FieldNc                    r   )N   zDecimal64(%d))rY   r   r&   r<   r   r[   r$   r%   r&     r   zDecimal64Field.__init__r`   r   r$   r$   r[   r%   r     r   r   c                       r   )Decimal128FieldNc                    r   )Nr   zDecimal128(%d))rY   r   r&   r<   r   r[   r$   r%   r&     r   zDecimal128Field.__init__r`   r   r$   r$   r[   r%   r     r   r   c                       sH   e Zd ZdZd fdd	Zdd Zddd	Zd
d Zedd Z	  Z
S )BaseEnumFieldz7
    Abstract base class for all enum-type fields.
    Nc                    s6   || _ |d u rt|d }tt| ||||| d S )Nr   )enum_clslistrY   r   r&   )r#   r   r   r   r    r!   r"   r[   r$   r%   r&     s   zBaseEnumField.__init__c              	   C   s   t || jr|S zJt |tr%z| j| W W S  ty$   | | Y W S w t |trF|d}z| j| W W S  tyE   | | Y W S w t |trQ| |W S W n ttfy]   Y nw td| jj	|f rS   )
r   r   r   	ExceptionrU   rV   rn   KeyErrorr5   r,   )r#   r0   r1   decodedr$   r$   r%   r2     s.   



zBaseEnumField.to_pythonTc                 C   s   t |j|S r'   )r
   r(   r9   r$   r$   r%   r;     r.   zBaseEnumField.to_db_stringc                 C   s   dd | j D S )Nc                 S   s    g | ]}d t |j|jf qS )z%s = %d)r
   r(   r0   ).0itemr$   r$   r%   
<listcomp>  s     z2BaseEnumField.get_db_type_args.<locals>.<listcomp>)r   r)   r$   r$   r%   r=        zBaseEnumField.get_db_type_argsc                 C   sf   ddl }ddlm} i }|d|D ]}t|d||d< q|d|}|dr-tnt}||S )	z
        Give an SQL column description such as "Enum8('apple' = 1, 'banana' = 2, 'orange' = 3)"
        this method returns a matching enum field.
        r   N)Enumz'([\w ]+)' = (-?\d+)r   r	   	AdHocEnumEnum8)	reenumr   finditerrn   group
startswith
Enum8FieldEnum16Field)clsr<   r   r   membersmatchr   field_classr$   r$   r%   create_ad_hoc_field  s   
z!BaseEnumField.create_ad_hoc_fieldrJ   rK   )r,   rM   rN   rO   r&   r2   r;   r=   classmethodr   ra   r$   r$   r[   r%   r     s    
r   c                   @   r   )r   r   Nr   r$   r$   r$   r%   r     r   r   c                   @   r   )r   Enum16Nr   r$   r$   r$   r%   r     r   r   c                       sF   e Zd Zg Zd fdd	Zdd Zdd Zdd	d
ZdddZ  Z	S )
ArrayFieldNc                    sF   t |ts	J dt |trJ d|| _tt| ||||| d S )Nz9The first argument of ArrayField must be a Field instancez:Multidimensional array fields are not supported by the ORM)r   r   r   rG   rY   r&   r#   rG   r   r   r    r!   r"   r[   r$   r%   r&     s   zArrayField.__init__c                    s`   t |tr
t|}nt |trt|d}nt |ttfs&tdt|  fdd|D S )NrT   z(ArrayField expects list or tuple, not %sc                    s   g | ]	} j |qS r$   rG   r2   r   vr#   r1   r$   r%   r     s    z(ArrayField.to_python.<locals>.<listcomp>)	r   r   r   rU   rV   r   tupler5   r   r/   r$   r   r%   r2     s   


zArrayField.to_pythonc                 C   s   |D ]}| j | qd S r'   rG   r4   )r#   r0   r   r$   r$   r%   r4     s   zArrayField.validateTc                    s"    fdd|D }dt | d S )Nc                    s   g | ]
} j j|d dqS )Tr:   rG   r;   r   r)   r$   r%   r     s    z+ArrayField.to_db_string.<locals>.<listcomp>[])r   )r#   r0   r:   arrayr$   r)   r%   r;     s   zArrayField.to_db_stringc                 C   s:   d| j jd|d }|r| jr|r|jr|d| j 7 }|S )Nz	Array(%s)Fr?   r@   rD   )rG   rC   r"   rF   r#   r?   r@   rA   r$   r$   r%   rC   "  s   zArrayField.get_sqlrJ   rK   rL   
r,   rM   rN   r   r&   r2   r4   r;   rC   ra   r$   r$   r[   r%   r     s    	
r   c                   @   s,   e Zd ZeddZdZdd Zd
ddZd	S )	UUIDFieldr   rn   r   c                 C   sh   t |tr|S t |trt|dS t |trt|S t |tr$t|dS t |tr.t|dS td| )N)rU   r   )fieldszInvalid value for UUIDField: %r)r   r   rU   r   rn   r   r5   r/   r$   r$   r%   r2   .  s   







zUUIDField.to_pythonTc                 C      t t||S r'   r
   r   r9   r$   r$   r%   r;   <  rs   zUUIDField.to_db_stringNrK   )r,   rM   rN   r   r   r<   r2   r;   r$   r$   r$   r%   r   )  s
    
r   c                   @   &   e Zd ZdZdZdd Zd	ddZdS )
	IPv4Fieldr   IPv4c                 C   2   t |tr|S t |tttfrt|S td| )Nz!Invalid value for IPv4Address: %r)r   r   rU   r   rn   r5   r/   r$   r$   r%   r2   E  
   
zIPv4Field.to_pythonTc                 C   r   r'   r  r9   r$   r$   r%   r;   M  rs   zIPv4Field.to_db_stringNrK   r,   rM   rN   r   r<   r2   r;   r$   r$   r$   r%   r  @  
    r  c                   @   r  )
	IPv6Fieldr   IPv6c                 C   r  )Nz!Invalid value for IPv6Address: %r)r   r   rU   r   rn   r5   r/   r$   r$   r%   r2   V  r  zIPv6Field.to_pythonTc                 C   r   r'   r  r9   r$   r$   r%   r;   ^  rs   zIPv6Field.to_db_stringNrK   r  r$   r$   r$   r%   r	  Q  r  r	  c                       sJ   e Zd ZdZ		d fdd	Zdd Zdd Zdd	d
ZdddZ  Z	S )NullableFieldNc                    sT   t |tsJ d||| _d g| _|r| j| tt| j|||d |d d S )NzEThe first argument of NullableField must be a Field instance. Not: {})r!   r"   )	r   r   r   rG   _null_valuesextendrY   r  r&   )r#   rG   r   r   r    extra_null_valuesr"   r[   r$   r%   r&   f  s   zNullableField.__init__c                 C   s$   |dks	|| j v rd S | j||S )N\N)r  rG   r2   r/   r$   r$   r%   r2   o  s   zNullableField.to_pythonc                 C   s    || j v p| j| d S  d S r'   )r  rG   r4   r3   r$   r$   r%   r4   t  s    zNullableField.validateTc                 C   s   || j v rdS | jj||dS )Nr  r   )r  rG   r;   r9   r$   r$   r%   r;   w  s   
zNullableField.to_db_stringc                 C   s*   d| j jd|d }|r|| |7 }|S )NzNullable(%s)Fr   )rG   rC   r>   r   r$   r$   r%   rC   |  s   zNullableField.get_sqlrJ   rK   rL   r   r$   r$   r[   r%   r  b  s    	
r  c                       sB   e Zd Zd fdd	Zdd Zdd Zdd	d
ZdddZ  ZS )LowCardinalityFieldNc                    sh   t |tsJ d|t |trJ dt |trJ d|| _| jj| _tt| ||||| d S )NzKThe first argument of LowCardinalityField must be a Field instance. Not: {}z8LowCardinality inner fields are not supported by the ORMzaArray field inside LowCardinality are not supported by the ORM. Use Array(LowCardinality) instead)	r   r   r   r  r   rG   r   rY   r&   r   r[   r$   r%   r&     s   
zLowCardinalityField.__init__c                 C   s   | j ||S r'   r   r/   r$   r$   r%   r2     rs   zLowCardinalityField.to_pythonc                 C   s   | j | d S r'   r   r3   r$   r$   r%   r4     r   zLowCardinalityField.validateTc                 C   s   | j j||dS )Nr   r   r9   r$   r$   r%   r;     r   z LowCardinalityField.to_db_stringc                 C   sX   |r|j rd| jjdd }n| jjdd}td| jjj |r*|| |7 }|S )NzLowCardinality(%s)F)r?   zZLowCardinalityField not supported on clickhouse-server version < 19.0 using {} as fallback)	has_low_cardinality_supportrG   rC   loggerwarningr   r+   r,   r>   r   r$   r$   r%   rC     s   
zLowCardinalityField.get_sqlrJ   rK   rL   )	r,   rM   rN   r&   r2   r4   r;   rC   ra   r$   r$   r[   r%   r    s    
r  )=
__future__r   rh   r   rk   calendarr   decimalr   r   uuidr   loggingr   r   utilsr
   r   r   r   r   funcsr   r   	ipaddressr   r   r  r   rQ   rW   rb   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  locals__all__r$   r$   r$   r%   <module>   sZ    v30(6"!