修复数据库接口问题

This commit is contained in:
junleea 2025-08-20 20:59:34 +08:00
parent 7844e5b121
commit 96e1a96f2e
3 changed files with 8 additions and 7 deletions

View File

@ -33,7 +33,7 @@ func CreateDBRunHistory(history *proto.SQLRunHistory) (uint, error) {
return history.ID, nil return history.ID, nil
} }
func RunSQL(sql string, db_ *gorm.DB) (res map[string]interface{}, err error) { func RunSQL(sql string, db_ *gorm.DB) (res []map[string]interface{}, err error) {
var db2 *gorm.DB var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG { if proto.Config.SERVER_SQL_LOG {
db2 = db_.Debug() db2 = db_.Debug()
@ -67,7 +67,7 @@ func FindDBManageByAuthID(auth_id uint) ([]proto.DBManage, error) {
} else { } else {
db2 = DB db2 = DB
} }
res := db2.Where("auth_id = ?", auth_id).Find(&db_infos) res := db2.Where("user_id = ?", auth_id).Find(&db_infos)
if res.Error != nil { if res.Error != nil {
return nil, res.Error return nil, res.Error
} }
@ -134,7 +134,7 @@ func FindDBRunHistoryByAuthID(auth_id int) ([]proto.SQLRunHistory, error) {
} else { } else {
db2 = DB db2 = DB
} }
res := db2.Where("auth_id = ?", auth_id).Find(&histories) res := db2.Where("user_id = ?", auth_id).Find(&histories)
if res.Error != nil { if res.Error != nil {
return nil, res.Error return nil, res.Error
} }

View File

@ -36,6 +36,7 @@ func UpdateDBManageHandler(c *gin.Context) {
resp.Data = dbManage resp.Data = dbManage
} }
} }
c.JSON(http.StatusOK, resp)
} }
func GetSQLRunHistoryHandler(c *gin.Context) { func GetSQLRunHistoryHandler(c *gin.Context) {

View File

@ -5,11 +5,12 @@ import (
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"strconv"
"videoplayer/dao" "videoplayer/dao"
"videoplayer/proto" "videoplayer/proto"
) )
func RunSQL(req *proto.RunSQLRequest) (map[string]interface{}, error) { func RunSQL(req *proto.RunSQLRequest) ([]map[string]interface{}, error) {
dbmInfo, err := dao.FindDBManageByID(req.DB_ID) dbmInfo, err := dao.FindDBManageByID(req.DB_ID)
if err != nil { if err != nil {
@ -38,13 +39,13 @@ func RunSQL(req *proto.RunSQLRequest) (map[string]interface{}, error) {
func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) { func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
switch dbmInfo.DB_Type { switch dbmInfo.DB_Type {
case proto.DB_TYPE_MYSQL: // MySQL case proto.DB_TYPE_MYSQL: // MySQL
dsn := dbmInfo.DB_User + ":" + dbmInfo.DB_Password + "@tcp(" + dbmInfo.DB_IP + ":" + string(rune(dbmInfo.DB_Port)) + ")/" + dbmInfo.DB_NAME + "?charset=utf8mb4&parseTime=True&loc=Local" 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{}) db_, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
case proto.DB_TYPE_POSTGRES: // PostgreSQL case proto.DB_TYPE_POSTGRES: // PostgreSQL
dsn := "host=" + dbmInfo.DB_IP + " user=" + dbmInfo.DB_User + " password=" + dbmInfo.DB_Password + " dbname=" + dbmInfo.DB_NAME + " port=" + string(rune(dbmInfo.DB_Port)) + " sslmode=disable TimeZone=Asia/Shanghai" 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{}) db_, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil { if err != nil {
return nil, err return nil, err
@ -52,7 +53,6 @@ func GetGORMDBObject(dbmInfo *proto.DBManage) (db_ *gorm.DB, err error) {
default: default:
err = errors.New("unsupported database type") err = errors.New("unsupported database type")
} }
return db_, err return db_, err
} }