o
    KDi                     @   s`   d Z ddlmZ ddlmZ ddlT ddlmZ ddlm	Z	 G dd	 d	eZ
d
d e
fD ZdS )z|
This file contains system readonly models that can be got from the database
https://clickhouse.tech/docs/en/system_tables/
    )unicode_literals   )Database)*)Model)
comma_joinc                   @   s   e Zd ZdZeh dZdZdZe Z	e Z
e Ze Ze Ze Ze Ze Ze Ze Ze Zedd Z	 dddZdd	d
ZdddZdddZdddZdddZedddZ edddZ!dS )
SystemPartz
    Contains information about parts of a table in the MergeTree family.
    This model operates only fields, described in the reference. Other fields are ignored.
    https://clickhouse.tech/docs/en/system_tables/system.parts/
    >   DROPFETCHATTACHDETACHFREEZETc                 C   s   dS )Nparts )clsr   r   Z/var/www/Datamplify/venv/lib/python3.10/site-packages/infi/clickhouse_orm/system_models.py
table_name4   s   zSystemPart.table_nameNc                 C   sf   |  }|| jv sJ dt| j d| jj| j|| jf }|dur(|d| 7 }| jj||dd dS )aA  
        Performs some operation over partition

        - `db`: Database object to execute operation on
        - `operation`: Operation to execute from SystemPart.OPERATIONS set
        - `settings`: Settings for executing request to ClickHouse over db.raw() method

        Returns: Operation execution result
        zoperation must be in [%s]z%ALTER TABLE `%s`.`%s` %s PARTITION %sNz FROM %sF)settingsstream)upper
OPERATIONSr   	_databasedb_nametable	partitionraw)self	operationr   	from_partsqlr   r   r   _partition_operation_sql<   s   
z#SystemPart._partition_operation_sqlc                 C      | j d|dS )z
        Move a partition to the 'detached' directory and forget it.

        - `settings`: Settings for executing request to ClickHouse over db.raw() method

        Returns: SQL Query
        r   r   r    r   r   r   r   r   detachN      zSystemPart.detachc                 C   r!   )z
        Delete a partition

        - `settings`: Settings for executing request to ClickHouse over db.raw() method

        Returns: SQL Query
        r	   r"   r#   r$   r   r   r   dropX   r&   zSystemPart.dropc                 C   r!   )z
         Add a new part or partition from the 'detached' directory to the table.

        - `settings`: Settings for executing request to ClickHouse over db.raw() method

        Returns: SQL Query
        r   r"   r#   r$   r   r   r   attachb   r&   zSystemPart.attachc                 C   r!   )z
        Create a backup of a partition.

        - `settings`: Settings for executing request to ClickHouse over db.raw() method

        Returns: SQL Query
        r   r"   r#   r$   r   r   r   freezel   r&   zSystemPart.freezec                 C   s   | j d||dS )z
        Download a partition from another server.

        - `zookeeper_path`: Path in zookeeper to fetch from
        - `settings`: Settings for executing request to ClickHouse over db.raw() method

        Returns: SQL Query
        r
   )r   r   r#   )r   zookeeper_pathr   r   r   r   fetchv   s   	zSystemPart.fetch c                 C   s^   t |ts	J dt |tsJ d|r|d7 }d|  }|jd||  ||jf | dS )z
        Get all data from system.parts table

        - `database`: A database object to fetch data from.
        - `conditions`: WHERE clause conditions. Database condition is added automatically

        Returns: A list of SystemPart objects
        z1database must be database.Database class instancezconditions must be a stringz AND,z1SELECT %s FROM `system`.%s WHERE %s database='%s')model_class)
isinstancer   strjoinfieldsselectr   r   )r   database
conditionsfield_namesr   r   r   get   s   
zSystemPart.getc                 C   s"   |r|d7 }|d7 }t j||dS )a  
        Gets active data from system.parts table

        - `database`: A database object to fetch data from.
        - `conditions`: WHERE clause conditions. Database and active conditions are added automatically

        Returns: A list of SystemPart objects
        z AND active)r5   )r   r7   )r   r4   r5   r   r   r   
get_active   s   
zSystemPart.get_active)NN)N)r,   )"__name__
__module____qualname____doc__	frozensetr   	_readonly_systemStringFieldr4   r   enginer   name
UInt8Fieldr8   UInt64FieldmarksbytesDateTimeFieldmodification_timeremove_timeUInt32Fieldrefcountclassmethodr   r    r%   r'   r(   r)   r+   r7   r9   r   r   r   r   r      s:    










r   c                 C   s   g | ]}|j qS r   )r:   ).0cr   r   r   
<listcomp>   s    rP   N)r=   
__future__r   r4   r   r2   modelsr   utilsr   r   __all__r   r   r   r   <module>   s     