videoplayer/dao/dbm.go

158 lines
3.1 KiB
Go
Raw Normal View History

2025-08-19 22:43:31 +08:00
package dao
import (
"gorm.io/gorm"
"videoplayer/proto"
)
func CreateDBManage(db_info proto.DBManage) (uint, error) {
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
res := db2.Create(&db_info)
if res.Error != nil {
return 0, res.Error
}
return db_info.ID, nil
}
func CreateDBRunHistory(history *proto.SQLRunHistory) (uint, error) {
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
res := db2.Create(history)
if res.Error != nil {
return 0, res.Error
}
return history.ID, nil
}
2025-08-20 20:59:34 +08:00
func RunSQL(sql string, db_ *gorm.DB) (res []map[string]interface{}, err error) {
2025-08-19 22:43:31 +08:00
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = db_.Debug()
} else {
db2 = db_
}
err = db2.Raw(sql).Scan(&res).Error
return res, err
}
func FindDBManageByID(id uint) (proto.DBManage, error) {
var db_info proto.DBManage
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
res := db2.Where("id = ?", id).First(&db_info)
if res.Error != nil {
return proto.DBManage{}, res.Error
}
return db_info, nil
}
func FindDBManageByAuthID(auth_id uint) ([]proto.DBManage, error) {
var db_infos []proto.DBManage
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
2025-08-20 20:59:34 +08:00
res := db2.Where("user_id = ?", auth_id).Find(&db_infos)
2025-08-19 22:43:31 +08:00
if res.Error != nil {
return nil, res.Error
}
return db_infos, nil
}
func FindAllDBManage() ([]proto.DBManage, error) {
var db_infos []proto.DBManage
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
res := db2.Find(&db_infos)
if res.Error != nil {
return nil, res.Error
}
return db_infos, nil
}
func UpdateDBManage(id uint, db_info *proto.DBManage) error {
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
res := db2.Model(&proto.DBManage{}).Where("id = ?", id).Updates(db_info)
return res.Error
}
func DeleteDBManageByID(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.DBManage{})
return res.Error
}
func FindDBRunHistoryByID(id uint) (proto.SQLRunHistory, error) {
var history proto.SQLRunHistory
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
res := db2.Where("id = ?", id).First(&history)
if res.Error != nil {
return proto.SQLRunHistory{}, res.Error
}
return history, nil
}
func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
var histories []proto.SQLRunHistory
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
2025-08-20 20:59:34 +08:00
res := db2.Where("user_id = ?", auth_id).Find(&histories)
2025-08-19 22:43:31 +08:00
if res.Error != nil {
return nil, res.Error
}
return histories, nil
}
func FindAllSQLRunHistory() ([]proto.SQLRunHistory, error) {
var histories []proto.SQLRunHistory
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
res := db2.Find(&histories)
if res.Error != nil {
return nil, res.Error
}
return histories, nil
}