修复获取db执行历史,添加sqlite数据库支持

This commit is contained in:
junleea 2025-09-08 20:10:42 +08:00
parent f0e025bec5
commit 5e184badca
4 changed files with 27 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
"videoplayer/proto"
@ -21,6 +22,9 @@ func Init() error {
} else if proto.Config.DB == 1 {
dsn = proto.Config.PG_DSN
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
} else if proto.Config.DB == 2 { //sqlite
dsn = proto.Config.SQLITE_FILE
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{})
}
if err != nil {

View File

@ -136,7 +136,6 @@ func FindDBRunHistoryByID(id uint) (proto.SQLRunHistory, error) {
}
return history, nil
}
func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
var histories []proto.SQLRunHistory
var db2 *gorm.DB
@ -152,6 +151,21 @@ func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
return histories, nil
}
func FindDBRunHistoryByAuthIDAndDbId(auth_id int, db_id uint) ([]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.Where("user_id = ? and db_id = ?", auth_id, db_id).Find(&histories)
if res.Error != nil {
return nil, res.Error
}
return histories, nil
}
func DelSQLRunHistoryByID(id uint) error {
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {

View File

@ -78,9 +78,10 @@ type StructValue struct {
}
type ConfigStruct struct {
DB int `json:"db"` // 0: mysql, 1: pg
DB int `json:"db"` // 0: mysql, 1: pg, 2:sqlite
MYSQL_DSN string `json:"mysql_dsn"`
PG_DSN string `json:"pg_dsn"`
SQLITE_FILE string `json:"sqlite_file"`
REDIS_ADDR string `json:"redis_addr"`
TOKEN_USE_REDIS bool `json:"token_use_redis"`
REDIS_User_PW bool `json:"redis_user_pw"` // 是否使用密码
@ -186,6 +187,7 @@ func DefaultConfig() {
Config.MYSQL_DSN = MYSQL_DSN
Config.PG_DSN = ""
Config.REDIS_ADDR = REDIS_ADDR
Config.SQLITE_FILE = ""
Config.TOKEN_USE_REDIS = false
Config.REDIS_User_PW = false
Config.REDIS_PASSWORD = REDIS_PASSWORD

View File

@ -103,7 +103,11 @@ func GetSQLRunHistory(req *proto.GetSQLRunHistoryReq, userID int) ([]proto.SQLRu
var err error
if req.GET_TYPE == 0 { // 获取自己的SQL执行历史
historyList, err = dao.FindDBRunHistoryByAuthID(userID)
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" {