summaryrefslogtreecommitdiffstats
path: root/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql
diff options
context:
space:
mode:
authorvitalyisaev <[email protected]>2023-11-14 09:58:56 +0300
committervitalyisaev <[email protected]>2023-11-14 10:20:20 +0300
commitc2b2dfd9827a400a8495e172a56343462e3ceb82 (patch)
treecd4e4f597d01bede4c82dffeb2d780d0a9046bd0 /contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql
parentd4ae8f119e67808cb0cf776ba6e0cf95296f2df7 (diff)
YQ Connector: move tests from yql to ydb (OSS)
Перенос папки с тестами на Коннектор из папки yql в папку ydb (синхронизируется с github).
Diffstat (limited to 'contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql')
-rw-r--r--contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/__init__.py15
-rw-r--r--contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/ddlcompiler.py24
-rw-r--r--contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/preparer.py11
3 files changed, 50 insertions, 0 deletions
diff --git a/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/__init__.py b/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/__init__.py
new file mode 100644
index 00000000000..68becd54d64
--- /dev/null
+++ b/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/__init__.py
@@ -0,0 +1,15 @@
+from typing import Optional
+
+from sqlalchemy import Table
+
+from clickhouse_connect.driver.query import quote_identifier
+
+
+def full_table(table_name: str, schema: Optional[str] = None) -> str:
+ if table_name.startswith('(') or '.' in table_name or not schema:
+ return quote_identifier(table_name)
+ return f'{quote_identifier(schema)}.{quote_identifier(table_name)}'
+
+
+def format_table(table: Table):
+ return full_table(table.name, table.schema)
diff --git a/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/ddlcompiler.py b/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/ddlcompiler.py
new file mode 100644
index 00000000000..5a972547057
--- /dev/null
+++ b/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/ddlcompiler.py
@@ -0,0 +1,24 @@
+from sqlalchemy import Column
+from sqlalchemy.sql.compiler import DDLCompiler
+
+from clickhouse_connect.cc_sqlalchemy.sql import format_table
+from clickhouse_connect.driver.query import quote_identifier
+
+
+class ChDDLCompiler(DDLCompiler):
+
+ def visit_create_schema(self, create, **_):
+ return f'CREATE DATABASE {quote_identifier(create.element)}'
+
+ def visit_drop_schema(self, drop, **_):
+ return f'DROP DATABASE {quote_identifier(drop.element)}'
+
+ def visit_create_table(self, create, **_):
+ table = create.element
+ text = f'CREATE TABLE {format_table(table)} ('
+ text += ', '.join([self.get_column_specification(c.element) for c in create.columns])
+ return text + ') ' + table.engine.compile()
+
+ def get_column_specification(self, column: Column, **_):
+ text = f'{quote_identifier(column.name)} {column.type.compile()}'
+ return text
diff --git a/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/preparer.py b/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/preparer.py
new file mode 100644
index 00000000000..a31b3e7af6d
--- /dev/null
+++ b/contrib/python/clickhouse-connect/clickhouse_connect/cc_sqlalchemy/sql/preparer.py
@@ -0,0 +1,11 @@
+from sqlalchemy.sql.compiler import IdentifierPreparer
+
+from clickhouse_connect.driver.query import quote_identifier
+
+
+class ChIdentifierPreparer(IdentifierPreparer):
+
+ quote_identifier = staticmethod(quote_identifier)
+
+ def _requires_quotes(self, _value):
+ return True