| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package handler
- import (
- "dbview/service/internal/common/response"
- "dbview/service/internal/modules/sql_editor/api"
- "dbview/service/internal/modules/sql_editor/service"
- "net/http"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- )
- // SQLEditorHandler SQL编辑器处理器
- type SQLEditorHandler struct {
- service *service.SQLEditorService
- logger *zap.Logger
- }
- // NewSQLEditorHandler 创建SQL编辑器处理器
- func NewSQLEditorHandler(service *service.SQLEditorService, logger *zap.Logger) *SQLEditorHandler {
- return &SQLEditorHandler{
- service: service,
- logger: logger,
- }
- }
- // HandleExecuteSQL 处理执行SQL请求
- // func (h *SQLEditorHandler) HandleExecuteSQL(c *gin.Context) {
- // var req api.SQLExecuteRequest
- // if err := c.ShouldBindJSON(&req); err != nil {
- // h.logger.Error("解析SQL执行请求失败", zap.Error(err))
- // c.JSON(http.StatusBadRequest, response.ParamError("请求参数无效"))
- // return
- // }
- // // 参数验证
- // if req.ConnectionID == "" {
- // c.JSON(http.StatusBadRequest, response.ParamError("缺少连接ID"))
- // return
- // }
- // if req.SQL == "" {
- // c.JSON(http.StatusBadRequest, response.ParamError("SQL语句不能为空"))
- // return
- // }
- // // 执行SQL
- // result, err := h.service.ExecuteSQL(c.Request.Context(), &req)
- // if err != nil {
- // h.logger.Error("执行SQL失败", zap.Error(err), zap.String("sql", req.SQL))
- // c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("执行SQL失败", err))
- // return
- // }
- // // 如果是异步执行,返回202状态码
- // if req.Options.Async {
- // c.JSON(http.StatusAccepted, response.Success(result))
- // return
- // }
- // // 如果SQL执行失败,但没有服务错误
- // if !result.Success {
- // c.JSON(http.StatusOK, response.ErrorWithCode(response.CodeDBQueryError, result.ErrorMessage))
- // return
- // }
- // c.JSON(http.StatusOK, response.Success(result))
- // }
- // HandleGetTaskStatus 处理获取任务状态请求
- func (h *SQLEditorHandler) HandleGetTaskStatus(c *gin.Context) {
- taskID := c.Param("taskId")
- if taskID == "" {
- c.JSON(http.StatusBadRequest, response.ParamError("缺少任务ID"))
- return
- }
- status, err := h.service.GetTaskStatus(c.Request.Context(), taskID)
- if err != nil {
- h.logger.Error("获取任务状态失败", zap.Error(err), zap.String("taskId", taskID))
- c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("获取任务状态失败", err))
- return
- }
- c.JSON(http.StatusOK, response.Success(status))
- }
- // HandleCancelTask 处理取消任务请求
- func (h *SQLEditorHandler) HandleCancelTask(c *gin.Context) {
- taskID := c.Param("taskId")
- if taskID == "" {
- c.JSON(http.StatusBadRequest, response.ParamError("缺少任务ID"))
- return
- }
- err := h.service.CancelTask(c.Request.Context(), taskID)
- if err != nil {
- h.logger.Error("取消任务失败", zap.Error(err), zap.String("taskId", taskID))
- c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("取消任务失败", err))
- return
- }
- c.JSON(http.StatusOK, response.Success("任务已取消"))
- }
- // HandleListHistory 处理获取历史记录请求
- func (h *SQLEditorHandler) HandleListHistory(c *gin.Context) {
- var req api.SQLListHistoryRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- h.logger.Error("解析历史记录请求失败", zap.Error(err))
- c.JSON(http.StatusBadRequest, response.ParamError("请求参数无效"))
- return
- }
- records, total, err := h.service.ListHistory(c.Request.Context(), &req)
- if err != nil {
- h.logger.Error("获取历史记录失败", zap.Error(err))
- c.JSON(http.StatusInternalServerError, response.ErrorWithDetail("获取历史记录失败", err))
- return
- }
- c.JSON(http.StatusOK, response.Success(gin.H{
- "records": records,
- "total": total,
- }))
- }
|