好像有戏

This commit is contained in:
des
2026-01-22 00:12:24 +08:00
parent 27c89d66f7
commit d09d9d9f1a
4 changed files with 54 additions and 42 deletions

View File

@@ -13,7 +13,7 @@ import (
// 返回值
// - 一个 gorm.DB 的指针
// - 一个清理所有数据库相关数据的函数
func GetDataBaseClient() (*gorm.DB, func(), error) {
func GetDataBaseClient() *gorm.DB {
dsn := "host=localhost " +
"user=gatekeeper " +
"dbname=crimson " +
@@ -25,28 +25,28 @@ func GetDataBaseClient() (*gorm.DB, func(), error) {
client, ero := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if ero != nil {
fmt.Println("数据库链接建立失败")
return nil, nil, ero
panic(ero)
}
poolCfg, ero := client.DB()
if ero != nil {
fmt.Println("获取数据库链接池失败")
return nil, nil, ero
panic(ero)
}
cleanPoolCfg := func() {
ero := poolCfg.Close()
if ero != nil {
fmt.Println("清理过程中出现错误")
panic(ero)
}
}
//cleanPoolCfg := func() {
// ero := poolCfg.Close()
// if ero != nil {
// fmt.Println("清理过程中出现错误")
// panic(ero)
// }
//}
poolCfg.SetConnMaxIdleTime(3)
poolCfg.SetMaxOpenConns(10)
fmt.Println("构建数据库链接")
return client, cleanPoolCfg, nil
return client
}
func GetQuery(db *gorm.DB) *query.Query {