videoplayer/service/dbmService.go

251 lines
7.1 KiB
Go

package service
import (
"errors"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"strconv"
"videoplayer/dao"
"videoplayer/proto"
"videoplayer/worker"
)
func RunSQL(req *proto.RunSQLRequest) (*proto.SQLResult, error) {
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_)
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
}
func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
dao.DBMMapRWMutex.RLock()
if dao.DBMMap != nil {
dbValue := dao.DBMMap[dbmInfo.ID]
if dbValue != nil {
dbValue.LastUserTime = worker.GetCurrentTimestamp()
return dbValue.Value, nil
}
}
dao.DBMMapRWMutex.RUnlock()
switch dbmInfo.DB_Type {
case proto.DB_TYPE_MYSQL: // MySQL
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"
db_, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
case proto.DB_TYPE_POSTGRES: // PostgreSQL
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"
db_, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
default:
err = errors.New("unsupported database type")
}
dao.DBMMapRWMutex.Lock()
var dbValue proto.DBValue
dbValue.Value = db_
dbValue.LastUserTime = worker.GetCurrentTimestamp()
dao.DBMMap[dbmInfo.ID] = &dbValue
dao.DBMMapRWMutex.Unlock()
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: req.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执行历史
if req.DB_ID > 0 {
historyList, err = dao.FindDBRunHistoryByAuthIDAndDbId(userID, req.DB_ID)
} else {
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
dbmInfo.DB_Desc = req.DB_Desc
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 != uint(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 req.Del_Type == 1 && req.UserID > 0 {
if req.UserID != uint(userId) && user.Role != "admin" {
return errors.New("unauthorized access to delete all SQL run history")
}
err := dao.DelSQLRunHistoryByAuthID(int(req.UserID))
return err
} else {
return errors.New("invalid delete type or parameters")
}
return nil
}
func GetDBTableDesc(req *proto.GetDBTableDescReq, userId int) (*proto.SQLResult, error) {
//dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
//if err != nil {`
// return nil, err
//}
//if dbmInfo.UserID != uint(userId) && GetUserByIDFromUserCenter(userId).Role != "admin" {
// return nil, errors.New("unauthorized access to the database management system")
//}
//db_, err := GetGORMDBObject(&dbmInfo)
//if err != nil {
// return nil, err
//}
//res, err := dao.GetDBTableDesc(db_, req.Table, req.GetType)
//if err != nil {
// return nil, err
//}
return nil, nil
}
func DelDBMMap() {
dao.DBMMapRWMutex.Lock()
defer dao.DBMMapRWMutex.Unlock()
cur := worker.GetCurrentTimestamp()
for k, v := range dao.DBMMap {
if cur-v.LastUserTime < proto.DBMMap_Max_Keep_Time {
delete(dao.DBMMap, k) //删除
}
}
}