Skip to content

Commit 4cf5560

Browse files
committed
🐞 Fix(Fix some bugs in the sql execution interface): 修复一些sql执行接口bug
1 parent 166ea95 commit 4cf5560

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

backend/plugin/api_testing/api/v1/sql.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44
SQL执行API
55
"""
66
from typing import Any, Dict, List
7-
8-
from fastapi import APIRouter, Body
9-
from fastapi.responses import JSONResponse
10-
11-
from backend.common.response.response_schema import response_base
7+
from fastapi import APIRouter
8+
from backend.common.response.response_schema import response_base, ResponseModel, ResponseSchemaModel
129
from backend.plugin.api_testing.utils.sql_executor import SQLExecutor, SQLQuery
1310

1411
router = APIRouter()
1512

1613

1714
@router.post("/execute", response_model=Dict[str, Any], summary="执行SQL查询")
18-
async def execute_sql_query(query: SQLQuery) -> JSONResponse:
15+
async def execute_sql_query(query: SQLQuery) -> ResponseModel:
1916
"""
2017
执行SQL查询接口
2118
@@ -24,35 +21,51 @@ async def execute_sql_query(query: SQLQuery) -> JSONResponse:
2421
try:
2522
# 执行SQL查询
2623
result = await SQLExecutor.execute_query(query)
27-
28-
return response_base.success(data=result.model_dump())
24+
response = response_base.success(data=result.model_dump())
25+
return response.model_dump()
2926
except Exception as e:
30-
return response_base.fail(msg=f"SQL执行失败: {str(e)}")
27+
response = response_base.fail(data=f"SQL执行失败: {str(e)}")
28+
return response.model_dump()
3129

3230

3331
@router.post("/batch-execute", response_model=Dict[str, Any], summary="批量执行SQL查询")
34-
async def execute_batch_sql_queries(queries: List[SQLQuery]) -> JSONResponse:
32+
async def execute_batch_sql_queries(queries: List[SQLQuery]) -> Dict[str, Any]:
3533
"""
3634
批量执行SQL查询接口
37-
35+
3836
批量执行指定的SQL查询并返回结果列表
3937
"""
4038
try:
4139
results = []
4240
for query in queries:
43-
result = await SQLExecutor.execute_query(query)
44-
results.append(result.model_dump())
45-
41+
try:
42+
result = await SQLExecutor.execute_query(query)
43+
response = response_base.success(data=result.model_dump())
44+
results.append(response.model_dump())
45+
except Exception as query_error:
46+
# 单个查询失败时,记录错误但继续执行其他查询
47+
error_response = response_base.fail(data=f"查询执行失败: {str(query_error)}")
48+
results.append(error_response.model_dump())
4649
# 构建响应
4750
data = {
4851
"results": results,
4952
"summary": {
5053
"total": len(results),
51-
"success": sum(1 for result in results if result["success"]),
52-
"failed": sum(1 for result in results if not result["success"])
54+
"success": sum(1 for result in results if result.get("success", False)),
55+
"failed": sum(1 for result in results if not result.get("success", True))
5356
}
5457
}
55-
56-
return response_base.success(data=data)
58+
59+
return {
60+
"code": 200,
61+
"msg": "批量执行完成",
62+
"data": data,
63+
"success": True
64+
}
5765
except Exception as e:
58-
return response_base.fail(msg=f"批量SQL执行失败: {str(e)}")
66+
return {
67+
"code": 400,
68+
"msg": "请求错误",
69+
"data": f"批量SQL执行失败: {str(e)}",
70+
"success": False
71+
}

backend/plugin/api_testing/requirements.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@ jsonpath-ng>=1.5.0
33
jinja2>=3.0.0
44
pymysql>=1.0.2
55
pandas>=1.3.0
6-
xlrd>=2.0.1
7-
8-
# 可选依赖,PostgreSQL支持
9-
# 如果不需要PostgreSQL支持,可以注释掉下面这行
10-
# psycopg>=3.1.8
6+
xlrd>=2.0.1

backend/plugin/api_testing/utils/sql_executor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ def get_default_db_config() -> DBConfig:
7070
7171
:return: 数据库配置
7272
"""
73-
db_type = DatabaseType.MYSQL if settings.DB_ENGINE == "mysql" else DatabaseType.POSTGRESQL
73+
db_type = DatabaseType.MYSQL if settings.DATABASE_TYPE == "mysql" else DatabaseType.POSTGRESQL
7474

7575
return DBConfig(
7676
type=db_type,
77-
host=settings.DB_HOST,
78-
port=settings.DB_PORT,
79-
username=settings.DB_USER,
80-
password=settings.DB_PASSWORD,
81-
database=settings.DB_DATABASE
77+
host=settings.DATABASE_HOST,
78+
port=settings.DATABASE_PORT,
79+
username=settings.DATABASE_USER,
80+
password=settings.DATABASE_PASSWORD,
81+
database=settings.DATABASE_SCHEMA
8282
)
8383

8484
@staticmethod

0 commit comments

Comments
 (0)