阿巴阿巴
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/query"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetDataBaseClient 获取数据库链接
|
||||
//
|
||||
// 返回值
|
||||
// - 一个 gorm.DB 的指针
|
||||
// - 一个清理所有数据库相关数据的函数
|
||||
func GetDataBaseClient() *gorm.DB {
|
||||
dsn := "host=localhost " +
|
||||
"user=gatekeeper " +
|
||||
"dbname=crimson " +
|
||||
"sslmode=disable " +
|
||||
"port=5432 " +
|
||||
"password=crimson " +
|
||||
"connect_timeout=20 "
|
||||
|
||||
client, ero := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if ero != nil {
|
||||
fmt.Println("数据库链接建立失败")
|
||||
panic(ero)
|
||||
}
|
||||
|
||||
poolCfg, ero := client.DB()
|
||||
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
|
||||
}
|
||||
|
||||
func GetQuery(db *gorm.DB) *query.Query {
|
||||
return query.Use(db)
|
||||
}
|
||||
Reference in New Issue
Block a user