|
| 1 | +"""SQL 控制台接口(仅外部数据库连接)。""" |
| 2 | + |
| 3 | +from typing import Annotated |
| 4 | + |
| 5 | +from fastapi import APIRouter, Body, Depends, Path, Query, Security, status |
| 6 | +from fastapi.responses import JSONResponse |
| 7 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 8 | + |
| 9 | +from app.common.response import ResponseSchema, SuccessResponse |
| 10 | +from app.core.base_schema import AuthSchema |
| 11 | +from app.core.dependencies import AuthPermission, db_getter |
| 12 | +from app.core.router_class import OperationLogRoute |
| 13 | + |
| 14 | +from .schema import ( |
| 15 | + DbConnectionCreateSchema, |
| 16 | + DbConnectionOutSchema, |
| 17 | + DbConnectionTestSchema, |
| 18 | + DbConnectionUpdateSchema, |
| 19 | + DbObjectSchema, |
| 20 | + DbQueryResultSchema, |
| 21 | + DbQuerySchema, |
| 22 | +) |
| 23 | +from .service import DbConsoleService |
| 24 | + |
| 25 | +DbConsoleRouter = APIRouter(route_class=OperationLogRoute, prefix="/connection", tags=["SQL控制台"]) |
| 26 | + |
| 27 | + |
| 28 | +@DbConsoleRouter.get("/list", summary="查询数据库连接列表", response_model=ResponseSchema[list[DbConnectionOutSchema]]) |
| 29 | +async def list_db_connection_controller( |
| 30 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:conn:query"]))], |
| 31 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 32 | +) -> JSONResponse: |
| 33 | + result = await DbConsoleService(auth, db).get_list() |
| 34 | + return SuccessResponse(data=result, msg="查询连接列表成功") |
| 35 | + |
| 36 | + |
| 37 | +@DbConsoleRouter.get("/detail/{id}", summary="查询数据库连接详情", response_model=ResponseSchema[DbConnectionOutSchema]) |
| 38 | +async def detail_db_connection_controller( |
| 39 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:conn:query"]))], |
| 40 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 41 | + id: Annotated[int, Path(description="连接ID", ge=1)], |
| 42 | +) -> JSONResponse: |
| 43 | + result = await DbConsoleService(auth, db).detail(id=id) |
| 44 | + return SuccessResponse(data=result, msg="查询连接详情成功") |
| 45 | + |
| 46 | + |
| 47 | +@DbConsoleRouter.post("/create", status_code=status.HTTP_201_CREATED, summary="新增数据库连接", response_model=ResponseSchema[DbConnectionOutSchema]) |
| 48 | +async def create_db_connection_controller( |
| 49 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:conn:create"]))], |
| 50 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 51 | + data: Annotated[DbConnectionCreateSchema, Body(description="连接配置")], |
| 52 | +) -> JSONResponse: |
| 53 | + result = await DbConsoleService(auth, db).create(data=data) |
| 54 | + return SuccessResponse(data=result, msg="新增连接成功") |
| 55 | + |
| 56 | + |
| 57 | +@DbConsoleRouter.put("/update/{id}", summary="修改数据库连接", response_model=ResponseSchema[DbConnectionOutSchema]) |
| 58 | +async def update_db_connection_controller( |
| 59 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:conn:update"]))], |
| 60 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 61 | + id: Annotated[int, Path(description="连接ID", ge=1)], |
| 62 | + data: Annotated[DbConnectionUpdateSchema, Body(description="连接配置")], |
| 63 | +) -> JSONResponse: |
| 64 | + result = await DbConsoleService(auth, db).update(id=id, data=data) |
| 65 | + return SuccessResponse(data=result, msg="修改连接成功") |
| 66 | + |
| 67 | + |
| 68 | +@DbConsoleRouter.delete("/delete", summary="删除数据库连接", response_model=ResponseSchema[None]) |
| 69 | +async def delete_db_connection_controller( |
| 70 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:conn:delete"]))], |
| 71 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 72 | + ids: Annotated[list[int], Body(description="连接ID列表")], |
| 73 | +) -> JSONResponse: |
| 74 | + await DbConsoleService(auth, db).delete(ids=ids) |
| 75 | + return SuccessResponse(msg="删除连接成功") |
| 76 | + |
| 77 | + |
| 78 | +@DbConsoleRouter.post("/test", summary="按填写中的凭据测试连接(不落库)", response_model=ResponseSchema[bool]) |
| 79 | +async def test_db_connection_config_controller( |
| 80 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:conn:update"]))], |
| 81 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 82 | + data: Annotated[DbConnectionTestSchema, Body(description="连接配置")], |
| 83 | +) -> JSONResponse: |
| 84 | + result = await DbConsoleService(auth, db).test_connection(data) |
| 85 | + return SuccessResponse(data=result, msg="连接成功") |
| 86 | + |
| 87 | + |
| 88 | +@DbConsoleRouter.post("/test/{id}", summary="测试已保存的连接", response_model=ResponseSchema[bool]) |
| 89 | +async def test_db_connection_controller( |
| 90 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:conn:query"]))], |
| 91 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 92 | + id: Annotated[int, Path(description="连接ID", ge=1)], |
| 93 | +) -> JSONResponse: |
| 94 | + result = await DbConsoleService(auth, db).test_by_id(id=id) |
| 95 | + return SuccessResponse(data=result, msg="连接成功") |
| 96 | + |
| 97 | + |
| 98 | +@DbConsoleRouter.get("/{id}/databases", summary="查询库/schema 列表", response_model=ResponseSchema[list[DbObjectSchema]]) |
| 99 | +async def list_db_databases_controller( |
| 100 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:query:execute"]))], |
| 101 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 102 | + id: Annotated[int, Path(description="连接ID", ge=1)], |
| 103 | +) -> JSONResponse: |
| 104 | + result = await DbConsoleService(auth, db).list_databases(id=id) |
| 105 | + return SuccessResponse(data=result, msg="查询库列表成功") |
| 106 | + |
| 107 | + |
| 108 | +@DbConsoleRouter.get("/{id}/tables", summary="查询表列表", response_model=ResponseSchema[list[DbObjectSchema]]) |
| 109 | +async def list_db_tables_controller( |
| 110 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:query:execute"]))], |
| 111 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 112 | + id: Annotated[int, Path(description="连接ID", ge=1)], |
| 113 | + database: Annotated[str | None, Query(max_length=128, description="库/schema 名")] = None, |
| 114 | +) -> JSONResponse: |
| 115 | + result = await DbConsoleService(auth, db).list_tables(id=id, database=database) |
| 116 | + return SuccessResponse(data=result, msg="查询表列表成功") |
| 117 | + |
| 118 | + |
| 119 | +@DbConsoleRouter.post("/{id}/query", summary="执行 SQL(只读校验;写需 confirmed)", response_model=ResponseSchema[DbQueryResultSchema]) |
| 120 | +async def run_db_query_controller( |
| 121 | + auth: Annotated[AuthSchema, Security(AuthPermission(["module_dbconsole:query:execute"]))], |
| 122 | + db: Annotated[AsyncSession, Depends(db_getter)], |
| 123 | + id: Annotated[int, Path(description="连接ID", ge=1)], |
| 124 | + data: Annotated[DbQuerySchema, Body(description="SQL 与确认标记")], |
| 125 | +) -> JSONResponse: |
| 126 | + result = await DbConsoleService(auth, db).run_query(id=id, sql=data.sql, confirmed=data.confirmed) |
| 127 | + return SuccessResponse(data=result, msg="执行成功") |
0 commit comments