ddlcompiler.py 903 B

123456789101112131415161718192021222324
  1. from sqlalchemy import Column
  2. from sqlalchemy.sql.compiler import DDLCompiler
  3. from clickhouse_connect.cc_sqlalchemy.sql import format_table
  4. from clickhouse_connect.driver.query import quote_identifier
  5. class ChDDLCompiler(DDLCompiler):
  6. def visit_create_schema(self, create, **_):
  7. return f'CREATE DATABASE {quote_identifier(create.element)}'
  8. def visit_drop_schema(self, drop, **_):
  9. return f'DROP DATABASE {quote_identifier(drop.element)}'
  10. def visit_create_table(self, create, **_):
  11. table = create.element
  12. text = f'CREATE TABLE {format_table(table)} ('
  13. text += ', '.join([self.get_column_specification(c.element) for c in create.columns])
  14. return text + ') ' + table.engine.compile()
  15. def get_column_specification(self, column: Column, **_):
  16. text = f'{quote_identifier(column.name)} {column.type.compile()}'
  17. return text