"""SQL助手 API — 人性化辅助功能。""" from typing import Optional from fastapi import APIRouter, Depends, HTTPException from pydantic import BaseModel from app.core.security import get_current_user from app.models.user import User from app.services.sql_assistant import SQLAssistant router = APIRouter(prefix="/sql-assistant", tags=["SQL助手"]) class SQLAnalyzeRequest(BaseModel): sql: str db_type: str = "mysql" class SQLFormatRequest(BaseModel): sql: str @router.post("/analyze") async def analyze_sql( req: SQLAnalyzeRequest, current_user: User = Depends(get_current_user), ): """全面分析SQL — 格式化+高亮+风险+建议。 一次调用获取所有辅助信息,帮助用户写出更好的SQL。 """ if not req.sql or not req.sql.strip(): raise HTTPException(status_code=400, detail="SQL内容不能为空") result = SQLAssistant.analyze(req.sql, req.db_type) return result @router.post("/format") async def format_sql( req: SQLFormatRequest, current_user: User = Depends(get_current_user), ): """格式化SQL — 统一代码风格。 - 关键字大写 - 合理换行缩进 """ if not req.sql or not req.sql.strip(): raise HTTPException(status_code=400, detail="SQL内容不能为空") result = SQLAssistant.format_sql(req.sql) return { "formatted": result.formatted, "is_valid": result.is_valid, "error": result.error, } @router.post("/highlight") async def highlight_sql( req: SQLAnalyzeRequest, current_user: User = Depends(get_current_user), ): """SQL语法高亮+风险检测。 返回高亮文本和风险提示列表。 """ if not req.sql or not req.sql.strip(): raise HTTPException(status_code=400, detail="SQL内容不能为空") result = SQLAssistant.highlight_sql(req.sql) return { "highlighted": result.highlighted, "keywords": result.keywords, "tables": result.tables, "risks": result.risks, } @router.post("/suggestions") async def query_suggestions( req: SQLAnalyzeRequest, current_user: User = Depends(get_current_user), ): """获取查询建议 — 优化提示。 基于SQL内容提供人性化的改进建议。 """ if not req.sql or not req.sql.strip(): raise HTTPException(status_code=400, detail="SQL内容不能为空") suggestions = SQLAssistant.get_query_suggestions(req.sql, req.db_type) return {"suggestions": suggestions, "count": len(suggestions)} @router.post("/parse-explain") async def parse_explain( explain_rows: list, current_user: User = Depends(get_current_user), ): """解析MySQL EXPLAIN结果为人类可读格式。""" if not explain_rows: raise HTTPException(status_code=400, detail="EXPLAIN结果不能为空") result = SQLAssistant.parse_explain(explain_rows) return { "is_simple": result.is_simple, "has_index": result.has_index, "has_full_scan": result.has_full_scan, "estimated_rows": result.estimated_rows, "warnings": result.warnings, "human_readable": result.human_readable, }