From 55ccfa8133d6d203e517c6e85a9f2454707d9201 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 17 Aug 2023 10:37:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- features/dbrest/dbrest.go | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/features/dbrest/dbrest.go b/features/dbrest/dbrest.go index f69373bd..2086fc25 100644 --- a/features/dbrest/dbrest.go +++ b/features/dbrest/dbrest.go @@ -48,6 +48,11 @@ var ( CustomXormCommonUri = config.UriPrefix + "/databaseManagement/{apiVersion}/{databaseName}/{tableName}" // for internal CustomXormExtDataUri = config.UriPrefix + "/dataManagement/{apiVersion}/{dataStorage}/{dataObject}" // for external CustomXormDataSQLUri = config.UriPrefix + "/dataManagement/{apiVersion}/{dataStorage}/{dataObject}" // for external + + // 查询数据库连接情况 + UriDbConnection = config.UriPrefix + "/dataManagement/{apiVersion}/dbConnection" + // 终结非法的数据库连接 + UriDbStop = config.UriPrefix + "/dataManagement/{apiVersion}/dbStop" ) var xormResponse XormResponse @@ -680,3 +685,52 @@ func DatabaseDeleteData(w http.ResponseWriter, r *http.Request) { mapRow["data"] = row services.ResponseWithJson(w, http.StatusOK, mapRow) } + +// 连接用户实例 +func DbConnection(w http.ResponseWriter, r *http.Request) { + if dborm.DbClient.XEngine == nil { + services.ResponseErrorWithJson(w, 400, "无连接") + } + // 查询实例 + result, err := dborm.DbClient.XEngine.QueryString("SHOW PROCESSLIST;") + if err != nil { + services.ResponseErrorWithJson(w, 500, err.Error()) + } + filterData := []map[string]string{} + for _, r := range result { + if r["User"] != "system user" { + filterData = append(filterData, r) + } + } + // Sleep:连接处于空闲状态,没有执行任何操作。 + // Query:连接正在执行一个查询语句。 + // Execute:连接正在执行一个准备好的 SQL 语句。 + // Connect:连接正在建立但尚未完成。 + services.ResponseWithJson(w, 200, filterData) +} + +// 关闭数据库连接 +func DbStop(w http.ResponseWriter, r *http.Request) { + if dborm.DbClient.XEngine == nil { + services.ResponseErrorWithJson(w, 400, "无连接") + } + + // json 請求參數獲取 + var bodyArgs struct { + ID string `json:"ID" validate:"required"` + } + err := services.ShouldBindJSON(r, &bodyArgs) + if err != nil { + log.Error("io.ReadAll is failed:", err) + services.ResponseErrorWithJson(w, 400, err.Error()) + return + } + + // 关闭 + rse, err := dborm.DbClient.XEngine.Exec("KILL ?;", bodyArgs.ID) + if err != nil { + services.ResponseErrorWithJson(w, 500, err.Error()) + return + } + services.ResponseWithJson(w, 200, rse) +}