From 5e184badca305704f3564b28828e87cffaad52a1 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Sep 2025 20:10:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96db=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=8E=86=E5=8F=B2=EF=BC=8C=E6=B7=BB=E5=8A=A0sqlite?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/db.go | 4 ++++ dao/dbm.go | 16 +++++++++++++++- proto/conf.go | 4 +++- service/dbmService.go | 6 +++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/dao/db.go b/dao/db.go index 99e8f4b..fe25287 100644 --- a/dao/db.go +++ b/dao/db.go @@ -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 { diff --git a/dao/dbm.go b/dao/dbm.go index 58b7793..892eaab 100644 --- a/dao/dbm.go +++ b/dao/dbm.go @@ -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 { diff --git a/proto/conf.go b/proto/conf.go index 71d5dbe..9df4ed6 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -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 diff --git a/service/dbmService.go b/service/dbmService.go index 5648727..31fbd9a 100644 --- a/service/dbmService.go +++ b/service/dbmService.go @@ -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" {