添加删除接口,修改运行sql返回数据结构
This commit is contained in:
parent
29c05b0af7
commit
1cfb517fc9
37
dao/dbm.go
37
dao/dbm.go
|
|
@ -111,6 +111,17 @@ func DeleteDBManageByID(id uint) error {
|
|||
return res.Error
|
||||
}
|
||||
|
||||
func DeleteDBManageByUserID(id uint) error {
|
||||
var db2 *gorm.DB
|
||||
if proto.Config.SERVER_SQL_LOG {
|
||||
db2 = DB.Debug()
|
||||
} else {
|
||||
db2 = DB
|
||||
}
|
||||
res := db2.Where("user_id = ?", id).Delete(&proto.DBManage{})
|
||||
return res.Error
|
||||
}
|
||||
|
||||
func FindDBRunHistoryByID(id uint) (proto.SQLRunHistory, error) {
|
||||
var history proto.SQLRunHistory
|
||||
var db2 *gorm.DB
|
||||
|
|
@ -141,6 +152,28 @@ func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
|
|||
return histories, nil
|
||||
}
|
||||
|
||||
func DelSQLRunHistoryByID(id uint) error {
|
||||
var db2 *gorm.DB
|
||||
if proto.Config.SERVER_SQL_LOG {
|
||||
db2 = DB.Debug()
|
||||
} else {
|
||||
db2 = DB
|
||||
}
|
||||
res := db2.Where("id = ?", id).Delete(&proto.SQLRunHistory{})
|
||||
return res.Error
|
||||
}
|
||||
|
||||
func DelSQLRunHistoryByAuthID(auth_id int) error {
|
||||
var db2 *gorm.DB
|
||||
if proto.Config.SERVER_SQL_LOG {
|
||||
db2 = DB.Debug()
|
||||
} else {
|
||||
db2 = DB
|
||||
}
|
||||
res := db2.Where("user_id = ?", auth_id).Delete(&proto.SQLRunHistory{})
|
||||
return res.Error
|
||||
}
|
||||
|
||||
func FindAllSQLRunHistory() ([]proto.SQLRunHistory, error) {
|
||||
var histories []proto.SQLRunHistory
|
||||
var db2 *gorm.DB
|
||||
|
|
@ -177,7 +210,9 @@ func RunSQLWithOrder(sql string, db_ *gorm.DB) (result proto.SQLResult, err erro
|
|||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Columns = columns // 保存列名顺序
|
||||
for _, col := range columns {
|
||||
result.Columns = append(result.Columns, proto.SQLResultColumnsValue{Value: col})
|
||||
}
|
||||
|
||||
// 遍历每行数据
|
||||
for rows.Next() {
|
||||
|
|
|
|||
|
|
@ -15,7 +15,55 @@ func SetDBManageGroup(router *gin.Engine) {
|
|||
dbm.POST("/get_db_manage", GetDBManageHandler) // 获取数据库管理信息
|
||||
dbm.POST("/get_sql_history", GetSQLRunHistoryHandler) // 获取SQL运行历史
|
||||
dbm.POST("/update_db_manage", UpdateDBManageHandler) // 更新数据库管理信息
|
||||
dbm.POST("/del_db_manage", DeleteDBManageHandler) // 删除数据库管理信息
|
||||
dbm.POST("/del_dbm_sql_history", DeleteSQLRunHistoryHandler) // 删除SQL运行历史
|
||||
}
|
||||
|
||||
func DeleteSQLRunHistoryHandler(c *gin.Context) {
|
||||
id, _ := c.Get("id")
|
||||
userID := int(id.(float64))
|
||||
|
||||
var req proto.DeleteDBManageSQLHistoryReq
|
||||
var resp proto.GeneralResp
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
resp.Code = proto.ParameterError
|
||||
resp.Message = "请求参数解析错误"
|
||||
} else {
|
||||
err2 := service.DeleteSQLRunHistory(&req, userID)
|
||||
if err2 != nil {
|
||||
resp.Code = proto.SQLRunHistoryDeleteFailed
|
||||
resp.Message = "删除SQL运行历史失败: " + err2.Error()
|
||||
} else {
|
||||
resp.Code = proto.SuccessCode
|
||||
resp.Message = "删除SQL运行历史成功"
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
|
||||
}
|
||||
|
||||
func DeleteDBManageHandler(c *gin.Context) {
|
||||
id, _ := c.Get("id")
|
||||
userID := int(id.(float64))
|
||||
|
||||
var req proto.DeleteDBManageReq
|
||||
var resp proto.GeneralResp
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
resp.Code = proto.ParameterError
|
||||
resp.Message = "请求参数解析错误"
|
||||
} else {
|
||||
err2 := service.DeleteDBManage(&req, userID)
|
||||
if err2 != nil {
|
||||
resp.Code = proto.DBMDeleteFailed
|
||||
resp.Message = "删除数据库管理失败: " + err2.Error()
|
||||
} else {
|
||||
resp.Code = proto.SuccessCode
|
||||
resp.Message = "删除数据库管理成功"
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func UpdateDBManageHandler(c *gin.Context) {
|
||||
id, _ := c.Get("id")
|
||||
userID := int(id.(float64))
|
||||
|
|
|
|||
23
proto/dbm.go
23
proto/dbm.go
|
|
@ -1,6 +1,8 @@
|
|||
package proto
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
DB_TYPE_MYSQL = 0 // DBTypeMySQL MySQL数据库
|
||||
|
|
@ -70,6 +72,23 @@ type GetSQLRunHistoryReq struct {
|
|||
|
||||
// SQLResult 包含查询结果的列名顺序和对应数据
|
||||
type SQLResult struct {
|
||||
Columns []string // 列名顺序(与 SQL 查询的列顺序一致)
|
||||
Columns []SQLResultColumnsValue // 列名顺序(与 SQL 查询的列顺序一致)
|
||||
Rows []map[string]interface{} // 每行数据(map 便于按列名访问)
|
||||
}
|
||||
|
||||
type SQLResultColumnsValue struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type DeleteDBManageReq struct {
|
||||
DB_ID uint `json:"db_id" form:"db_id"` // 数据库ID
|
||||
Del_Type uint `json:"del_type" form:"del_type"` // 删除类型: 0为删除单条,1为所有
|
||||
UserID uint `json:"user_id" form:"user_id"` // 用户ID
|
||||
}
|
||||
|
||||
type DeleteDBManageSQLHistoryReq struct {
|
||||
DB_ID uint `json:"db_id" form:"db_id"` // 数据库ID
|
||||
Del_Type uint `json:"del_type" form:"del_type"` // 删除类型: 0为删除单条,1为所有
|
||||
History_ID uint `json:"history_id" form:"history_id"` // SQL执行历史ID,如果为0则删除所有
|
||||
UserID uint `json:"user_id" form:"user_id"` // 用户ID
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,4 +85,6 @@ const (
|
|||
DBMCreateFailed = 1002 // 创建数据库管理失败
|
||||
DBMGetFailed = 1003 // 获取数据库管理信息失败`
|
||||
DBMUpdateFailed = 1004 // 更新数据库管理信息失败
|
||||
DBMDeleteFailed = 1005 // 删除数据库管理信息失败
|
||||
DBMRunSQLHistoryDeleteFailed = 1006 // 删除SQL运行历史失败
|
||||
)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import (
|
|||
"videoplayer/proto"
|
||||
)
|
||||
|
||||
func RunSQL(req *proto.RunSQLRequest) ([]map[string]interface{}, error) {
|
||||
func RunSQL(req *proto.RunSQLRequest) (*proto.SQLResult, error) {
|
||||
|
||||
dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
|
||||
if err != nil {
|
||||
|
|
@ -23,7 +23,7 @@ func RunSQL(req *proto.RunSQLRequest) ([]map[string]interface{}, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := dao.RunSQL(req.SQL, db_)
|
||||
res, err := dao.RunSQLWithOrder(req.SQL, db_)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ func RunSQL(req *proto.RunSQLRequest) ([]map[string]interface{}, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
|
||||
|
|
@ -140,3 +140,57 @@ func UpdateDBManage(req *proto.UpdateDBManageReq, userID int) (proto.DBManage, e
|
|||
}
|
||||
return dbmInfo, nil
|
||||
}
|
||||
|
||||
func DeleteDBManage(req *proto.DeleteDBManageReq, userId int) error {
|
||||
user := GetUserByIDFromUserCenter(userId)
|
||||
if req.Del_Type == 0 && req.DB_ID > 0 {
|
||||
dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dbmInfo.UserID != uint(req.UserID) && user.Role != "admin" {
|
||||
return errors.New("unauthorized access to the database management system")
|
||||
}
|
||||
err = dao.DeleteDBManageByID(req.DB_ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if req.Del_Type == 1 && req.UserID > 0 {
|
||||
if req.UserID != userId && user.Role != "admin" {
|
||||
return errors.New("unauthorized access to delete all database management systems")
|
||||
}
|
||||
err := dao.DeleteDBManageByUserID(req.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.New("invalid delete type or parameters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteSQLRunHistory(req *proto.DeleteDBManageSQLHistoryReq, userId int) error {
|
||||
user := GetUserByIDFromUserCenter(userId)
|
||||
if req.Del_Type == 0 && req.History_ID > 0 {
|
||||
history, err := dao.FindDBRunHistoryByID(req.History_ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if history.UserID != uint(req.UserID) && user.Role != "admin" {
|
||||
return errors.New("unauthorized access to the SQL run history")
|
||||
}
|
||||
err = dao.DelSQLRunHistoryByID(req.History_ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if Del_Type == 1 && req.UserID > 0 {
|
||||
if req.UserID != userId && user.Role != "admin" {
|
||||
return errors.New("unauthorized access to delete all SQL run history")
|
||||
}
|
||||
err := dao.DelSQLRunHistoryByAuthID(req.UserID)
|
||||
return err
|
||||
} else {
|
||||
return errors.New("invalid delete type or parameters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue