testing.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from typing import Sequence, Optional, Union, Dict, Any
  2. from clickhouse_connect.driver import Client
  3. from clickhouse_connect.driver.query import quote_identifier, str_query_value
  4. class TableContext:
  5. def __init__(self, client: Client,
  6. table: str,
  7. columns: Union[str, Sequence[str]],
  8. column_types: Optional[Sequence[str]] = None,
  9. engine: str = 'MergeTree',
  10. order_by: str = None,
  11. settings: Optional[Dict[str, Any]] = None):
  12. self.client = client
  13. if '.' in table:
  14. self.table = table
  15. else:
  16. self.table = quote_identifier(table)
  17. self.settings = settings
  18. if isinstance(columns, str):
  19. columns = columns.split(',')
  20. if column_types is None:
  21. self.column_names = []
  22. self.column_types = []
  23. for col in columns:
  24. col = col.strip()
  25. ix = col.find(' ')
  26. self.column_types.append(col[ix + 1:].strip())
  27. self.column_names.append(quote_identifier(col[:ix].strip()))
  28. else:
  29. self.column_names = [quote_identifier(name) for name in columns]
  30. self.column_types = column_types
  31. self.engine = engine
  32. self.order_by = self.column_names[0] if order_by is None else order_by
  33. def __enter__(self):
  34. if self.client.min_version('19'):
  35. self.client.command(f'DROP TABLE IF EXISTS {self.table}')
  36. else:
  37. self.client.command(f'DROP TABLE IF EXISTS {self.table} SYNC')
  38. col_defs = ','.join(f'{quote_identifier(name)} {col_type}' for name, col_type in zip(self.column_names, self.column_types))
  39. create_cmd = f'CREATE TABLE {self.table} ({col_defs}) ENGINE {self.engine} ORDER BY {self.order_by}'
  40. if self.settings:
  41. create_cmd += ' SETTINGS '
  42. for key, value in self.settings.items():
  43. create_cmd += f'{key} = {str_query_value(value)}, '
  44. if create_cmd.endswith(', '):
  45. create_cmd = create_cmd[:-2]
  46. self.client.command(create_cmd)
  47. return self
  48. def __exit__(self, exc_type, exc_val, exc_tb):
  49. self.client.command(f'DROP TABLE IF EXISTS {self.table}')