修复获取db执行历史,添加sqlite数据库支持
This commit is contained in:
parent
f0e025bec5
commit
5e184badca
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"log"
|
"log"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
|
|
@ -21,6 +22,9 @@ func Init() error {
|
||||||
} else if proto.Config.DB == 1 {
|
} else if proto.Config.DB == 1 {
|
||||||
dsn = proto.Config.PG_DSN
|
dsn = proto.Config.PG_DSN
|
||||||
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
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 {
|
if err != nil {
|
||||||
|
|
|
||||||
16
dao/dbm.go
16
dao/dbm.go
|
|
@ -136,7 +136,6 @@ func FindDBRunHistoryByID(id uint) (proto.SQLRunHistory, error) {
|
||||||
}
|
}
|
||||||
return history, nil
|
return history, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
|
func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
|
||||||
var histories []proto.SQLRunHistory
|
var histories []proto.SQLRunHistory
|
||||||
var db2 *gorm.DB
|
var db2 *gorm.DB
|
||||||
|
|
@ -152,6 +151,21 @@ func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
|
||||||
return histories, nil
|
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 {
|
func DelSQLRunHistoryByID(id uint) error {
|
||||||
var db2 *gorm.DB
|
var db2 *gorm.DB
|
||||||
if proto.Config.SERVER_SQL_LOG {
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
|
|
||||||
|
|
@ -78,9 +78,10 @@ type StructValue struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigStruct 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"`
|
MYSQL_DSN string `json:"mysql_dsn"`
|
||||||
PG_DSN string `json:"pg_dsn"`
|
PG_DSN string `json:"pg_dsn"`
|
||||||
|
SQLITE_FILE string `json:"sqlite_file"`
|
||||||
REDIS_ADDR string `json:"redis_addr"`
|
REDIS_ADDR string `json:"redis_addr"`
|
||||||
TOKEN_USE_REDIS bool `json:"token_use_redis"`
|
TOKEN_USE_REDIS bool `json:"token_use_redis"`
|
||||||
REDIS_User_PW bool `json:"redis_user_pw"` // 是否使用密码
|
REDIS_User_PW bool `json:"redis_user_pw"` // 是否使用密码
|
||||||
|
|
@ -186,6 +187,7 @@ func DefaultConfig() {
|
||||||
Config.MYSQL_DSN = MYSQL_DSN
|
Config.MYSQL_DSN = MYSQL_DSN
|
||||||
Config.PG_DSN = ""
|
Config.PG_DSN = ""
|
||||||
Config.REDIS_ADDR = REDIS_ADDR
|
Config.REDIS_ADDR = REDIS_ADDR
|
||||||
|
Config.SQLITE_FILE = ""
|
||||||
Config.TOKEN_USE_REDIS = false
|
Config.TOKEN_USE_REDIS = false
|
||||||
Config.REDIS_User_PW = false
|
Config.REDIS_User_PW = false
|
||||||
Config.REDIS_PASSWORD = REDIS_PASSWORD
|
Config.REDIS_PASSWORD = REDIS_PASSWORD
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,11 @@ func GetSQLRunHistory(req *proto.GetSQLRunHistoryReq, userID int) ([]proto.SQLRu
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if req.GET_TYPE == 0 { // 获取自己的SQL执行历史
|
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)
|
historyList, err = dao.FindDBRunHistoryByAuthID(userID)
|
||||||
|
}
|
||||||
} else if req.GET_TYPE == 1 { // 管理员获取所有SQL执行历史
|
} else if req.GET_TYPE == 1 { // 管理员获取所有SQL执行历史
|
||||||
user := GetUserByIDFromUserCenter(userID)
|
user := GetUserByIDFromUserCenter(userID)
|
||||||
if user.Role != "admin" {
|
if user.Role != "admin" {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue