ddl.py 825 B

12345678910111213141516171819202122232425262728
  1. from typing import NamedTuple, Sequence
  2. from clickhouse_connect.datatypes.base import ClickHouseType
  3. class TableColumnDef(NamedTuple):
  4. """
  5. Simplified ClickHouse Table Column definition for DDL
  6. """
  7. name: str
  8. ch_type: ClickHouseType
  9. expr_type: str = None
  10. expr: str = None
  11. @property
  12. def col_expr(self):
  13. expr = f'{self.name} {self.ch_type.name}'
  14. if self.expr_type:
  15. expr += f' {self.expr_type} {self.expr}'
  16. return expr
  17. def create_table(table_name: str, columns: Sequence[TableColumnDef], engine: str, engine_params: dict):
  18. stmt = f"CREATE TABLE {table_name} ({', '.join(col.col_expr for col in columns)}) ENGINE {engine} "
  19. if engine_params:
  20. for key, value in engine_params.items():
  21. stmt += f' {key} {value}'
  22. return stmt