2
2
from typing import Sequence
3
3
4
4
import clickhouse_connect
5
+ from clickhouse_connect .driver .binding import quote_identifier , format_query_value
5
6
from dotenv import load_dotenv
6
7
from fastmcp import FastMCP
7
8
@@ -39,18 +40,18 @@ def list_databases():
39
40
def list_tables (database : str , like : str = None ):
40
41
logger .info (f"Listing tables in database '{ database } '" )
41
42
client = create_clickhouse_client ()
42
- query = f"SHOW TABLES FROM { database } "
43
+ query = f"SHOW TABLES FROM { format_identifier ( database ) } "
43
44
if like :
44
- query += f" LIKE ' { like } ' "
45
+ query += f" LIKE { format_query_value ( like ) } "
45
46
result = client .command (query )
46
47
47
48
# Get all table comments in one query
48
- table_comments_query = f"SELECT name, comment FROM system.tables WHERE database = ' { database } ' "
49
+ table_comments_query = f"SELECT name, comment FROM system.tables WHERE database = { format_query_value ( database ) } "
49
50
table_comments_result = client .query (table_comments_query )
50
51
table_comments = {row [0 ]: row [1 ] for row in table_comments_result .result_rows }
51
52
52
53
# Get all column comments in one query
53
- column_comments_query = f"SELECT table, name, comment FROM system.columns WHERE database = ' { database } ' "
54
+ column_comments_query = f"SELECT table, name, comment FROM system.columns WHERE database = { format_query_value ( database ) } "
54
55
column_comments_result = client .query (column_comments_query )
55
56
column_comments = {}
56
57
for row in column_comments_result .result_rows :
@@ -61,7 +62,7 @@ def list_tables(database: str, like: str = None):
61
62
62
63
def get_table_info (table ):
63
64
logger .info (f"Getting schema info for table { database } .{ table } " )
64
- schema_query = f"DESCRIBE TABLE { database } .` { table } ` "
65
+ schema_query = f"DESCRIBE TABLE { quote_identifier ( database ) } . { quote_identifier ( table ) } "
65
66
schema_result = client .query (schema_query )
66
67
67
68
columns = []
0 commit comments