videoplayer/service/dbmService.go

197 lines
5.7 KiB
Go
Raw Normal View History

2025-08-19 22:43:31 +08:00
package service
import (
"errors"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
2025-08-20 20:59:34 +08:00
"strconv"
2025-08-19 22:43:31 +08:00
"videoplayer/dao"
"videoplayer/proto"
)
func RunSQL(req *proto.RunSQLRequest) (*proto.SQLResult, error) {
2025-08-19 22:43:31 +08:00
dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
if err != nil {
return nil, err
}
if dbmInfo.UserID != req.UserID {
return nil, errors.New("unauthorized access to the database management system")
}
db_, err := GetGORMDBObject(&dbmInfo)
if err != nil {
return nil, err
}
res, err := dao.RunSQLWithOrder(req.SQL, db_)
2025-08-19 22:43:31 +08:00
if err != nil {
return nil, err
}
// 记录执行历史
history := &proto.SQLRunHistory{UserID: req.UserID, DB_ID: req.DB_ID, SQL: req.SQL, Status: 0}
_, err = dao.CreateDBRunHistory(history)
if err != nil {
return nil, err
}
return &res, nil
2025-08-19 22:43:31 +08:00
}
func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
switch dbmInfo.DB_Type {
case proto.DB_TYPE_MYSQL: // MySQL
2025-08-20 20:59:34 +08:00
dsn := dbmInfo.DB_User + ":" + dbmInfo.DB_Password + "@tcp(" + dbmInfo.DB_IP + ":" + strconv.Itoa(int(dbmInfo.DB_Port)) + ")/" + dbmInfo.DB_NAME + "?charset=utf8mb4&parseTime=True&loc=Local"
2025-08-19 22:43:31 +08:00
db_, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
case proto.DB_TYPE_POSTGRES: // PostgreSQL
2025-08-20 20:59:34 +08:00
dsn := "host=" + dbmInfo.DB_IP + " user=" + dbmInfo.DB_User + " password=" + dbmInfo.DB_Password + " dbname=" + dbmInfo.DB_NAME + " port=" + strconv.Itoa(int(dbmInfo.DB_Port)) + " sslmode=disable TimeZone=Asia/Shanghai"
2025-08-19 22:43:31 +08:00
db_, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
default:
err = errors.New("unsupported database type")
}
return db_, err
}
func CreateDBManage(req *proto.CreateDBManageReq, userID uint) (proto.DBManage, error) {
dbmInfo := proto.DBManage{
UserID: userID,
DB_IP: req.DB_IP,
DB_Port: req.DB_Port,
DB_NAME: req.DB_NAME,
DB_User: req.DB_User,
DB_Password: req.DB_Password,
DB_Type: req.DB_Type,
DB_Desc: "",
DB_STATUS: 0, // 初始状态为未连接
}
id, err := dao.CreateDBManage(dbmInfo)
if err != nil {
return proto.DBManage{}, err
}
dbmInfo.ID = id
return dbmInfo, nil
}
func GetDBManageList(req *proto.GetDBManageReq, userID uint) ([]proto.DBManage, error) {
var dbmList []proto.DBManage
var err error
if req.GET_TYPE == 0 { // 获取自己的数据库管理
dbmList, err = dao.FindDBManageByAuthID(userID)
} else if req.GET_TYPE == 1 { // 管理员获取所有数据库管理
user := GetUserByIDFromUserCenter(int(userID))
if user.Role != "admin" {
return nil, errors.New("unauthorized access, only admin can get all database management")
}
dbmList, err = dao.FindAllDBManage()
} else {
return nil, errors.New("invalid get type")
}
if err != nil {
return nil, err
}
return dbmList, nil
}
func GetSQLRunHistory(req *proto.GetSQLRunHistoryReq, userID int) ([]proto.SQLRunHistory, error) {
var historyList []proto.SQLRunHistory
var err error
if req.GET_TYPE == 0 { // 获取自己的SQL执行历史
historyList, err = dao.FindDBRunHistoryByAuthID(userID)
} else if req.GET_TYPE == 1 { // 管理员获取所有SQL执行历史
user := GetUserByIDFromUserCenter(userID)
if user.Role != "admin" {
return nil, errors.New("unauthorized access, only admin can get all SQL run history")
}
historyList, err = dao.FindAllSQLRunHistory()
} else {
return nil, errors.New("invalid get type")
}
if err != nil {
return nil, err
}
return historyList, nil
}
func UpdateDBManage(req *proto.UpdateDBManageReq, userID int) (proto.DBManage, error) {
dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
if err != nil {
return proto.DBManage{}, err
}
if dbmInfo.UserID != uint(userID) && GetUserByIDFromUserCenter(userID).Role != "admin" {
return proto.DBManage{}, errors.New("unauthorized access to the database management system")
}
dbmInfo.DB_IP = req.DB_IP
dbmInfo.DB_Port = req.DB_Port
dbmInfo.DB_NAME = req.DB_NAME
dbmInfo.DB_User = req.DB_User
dbmInfo.DB_Password = req.DB_Password
dbmInfo.DB_Type = req.DB_Type
err = dao.UpdateDBManage(dbmInfo.ID, &dbmInfo)
if err != nil {
return proto.DBManage{}, err
}
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
}