|
@@ -603,28 +603,25 @@ def ddl_identify():
|
|
|
if not ddl_list:
|
|
|
return jsonify(failed("未找到有效的CREATE TABLE语句"))
|
|
|
|
|
|
- # 创建最终返回结果的结构
|
|
|
+ # 处理结果 - 假设ddl_list已经包含tables结构
|
|
|
result = {}
|
|
|
data_source = None
|
|
|
- tables = {}
|
|
|
|
|
|
- # 为每个表名添加exist字段
|
|
|
+ # 处理数据源和表的存在状态
|
|
|
if isinstance(ddl_list, dict):
|
|
|
- # 检查是否有data_source键
|
|
|
+ # 处理数据源信息
|
|
|
if "data_source" in ddl_list:
|
|
|
- # 提取数据源信息
|
|
|
data_source = ddl_list.pop("data_source", None)
|
|
|
|
|
|
- # 检查数据源是否存在
|
|
|
if data_source:
|
|
|
# 检查数据源是否包含en_name
|
|
|
if "en_name" not in data_source:
|
|
|
+ logger.debug(f"data_source内容: {json.dumps(data_source, ensure_ascii=False) if data_source is not None else 'None'}")
|
|
|
return jsonify(failed("数据源信息不完整:缺少en_name字段"))
|
|
|
|
|
|
try:
|
|
|
- # 使用Neo4j查询检查数据源是否存在
|
|
|
+ # 查询数据源是否存在
|
|
|
data_source_name = data_source["en_name"]
|
|
|
- # 注意:数据源的标签应该是data_source而不是data_resource
|
|
|
with neo4j_driver.get_session() as session:
|
|
|
source_query = """
|
|
|
MATCH (n:data_source {en_name: $name})
|
|
@@ -640,48 +637,42 @@ def ddl_identify():
|
|
|
logger.error(f"检查数据源存在状态失败: {str(e)}")
|
|
|
data_source["exist"] = False
|
|
|
|
|
|
- # 获取所有表名 - 过滤掉可能的非表结构键
|
|
|
- table_names = []
|
|
|
- for key, value in list(ddl_list.items()):
|
|
|
- # 检查值是否是字典且包含meta键,这表明它是一个表结构
|
|
|
- if isinstance(value, dict) and "meta" in value:
|
|
|
- table_names.append(key)
|
|
|
- # 将表信息添加到tables字典中
|
|
|
- tables[key] = value
|
|
|
- # 如果不是表结构,则不处理
|
|
|
-
|
|
|
- # 只有在有表名时才查询表是否存在
|
|
|
- if table_names:
|
|
|
- try:
|
|
|
- # 在Neo4j中查询表名是否存在于data_resource节点
|
|
|
- with neo4j_driver.get_session() as session:
|
|
|
- table_query = """
|
|
|
- UNWIND $names AS name
|
|
|
- OPTIONAL MATCH (n:data_resource {en_name: name})
|
|
|
- RETURN name, n IS NOT NULL AS exists
|
|
|
- """
|
|
|
- table_results = session.run(table_query, names=table_names)
|
|
|
-
|
|
|
- # 处理结果
|
|
|
- for record in table_results:
|
|
|
- table_name = record["name"]
|
|
|
- exists = record["exists"]
|
|
|
- if table_name in tables:
|
|
|
- tables[table_name]["exist"] = exists
|
|
|
-
|
|
|
- # 确保所有表都有exist字段
|
|
|
+ # 处理表的存在状态 - 假设tables已经在ddl_list中
|
|
|
+ if "tables" in ddl_list and isinstance(ddl_list["tables"], dict):
|
|
|
+ table_names = list(ddl_list["tables"].keys())
|
|
|
+
|
|
|
+ if table_names:
|
|
|
+ try:
|
|
|
+ # 查询表是否存在
|
|
|
+ with neo4j_driver.get_session() as session:
|
|
|
+ table_query = """
|
|
|
+ UNWIND $names AS name
|
|
|
+ OPTIONAL MATCH (n:data_resource {en_name: name})
|
|
|
+ RETURN name, n IS NOT NULL AS exists
|
|
|
+ """
|
|
|
+ table_results = session.run(table_query, names=table_names)
|
|
|
+
|
|
|
+ # 处理结果
|
|
|
+ for record in table_results:
|
|
|
+ table_name = record["name"]
|
|
|
+ exists = record["exists"]
|
|
|
+ if table_name in ddl_list["tables"]:
|
|
|
+ ddl_list["tables"][table_name]["exist"] = exists
|
|
|
+
|
|
|
+ # 确保所有表都有exist字段
|
|
|
+ for table_name in table_names:
|
|
|
+ if "exist" not in ddl_list["tables"][table_name]:
|
|
|
+ ddl_list["tables"][table_name]["exist"] = False
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"检查表存在状态失败: {str(e)}")
|
|
|
+ # 如果查询失败,所有表默认为不存在
|
|
|
for table_name in table_names:
|
|
|
- if "exist" not in tables[table_name]:
|
|
|
- tables[table_name]["exist"] = False
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- logger.error(f"检查表存在状态失败: {str(e)}")
|
|
|
- # 如果查询失败,所有表默认为不存在
|
|
|
- for table_name in table_names:
|
|
|
- tables[table_name]["exist"] = False
|
|
|
-
|
|
|
- # 构建最终返回的结构
|
|
|
- result["tables"] = tables
|
|
|
+ ddl_list["tables"][table_name]["exist"] = False
|
|
|
+
|
|
|
+ # 构建最终结果
|
|
|
+ result = ddl_list
|
|
|
+
|
|
|
+ # 添加数据源信息
|
|
|
if data_source:
|
|
|
result["data_source"] = data_source
|
|
|
|