This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
forked from go-gourd/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.go
76 lines (61 loc) · 1.85 KB
/
postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package mysql
import (
"errors"
"fmt"
"github.com/go-gourd/gourd/config"
"github.com/go-gourd/gourd/logger"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"time"
)
var dbs = make(map[string]*gorm.DB)
type LogWriter struct{}
func (w LogWriter) Printf(format string, args ...any) {
logger.Warnf(format, args...)
}
// GetDb 获取数据库连接
func GetDb(name string) (*gorm.DB, error) {
if _, ok := dbs[name]; ok {
return dbs[name], nil
}
dbConf := config.GetDbConfig()
if _, ok := dbConf[name]; !ok {
return nil, errors.New("Database config '" + name + "' does not exist.")
}
conf := dbConf[name]
//判断配置数据库类型
if conf.Type != "postgres" {
return nil, errors.New("Database config '" + name + "' type is not sqlserver.")
}
dsnParam := ""
if conf.Param != "" {
dsnParam = "?" + conf.Param
}
dsnF := "host=%s user=%s password=%s dbname=%s port=%d %s"
dsn := fmt.Sprintf(dsnF, conf.Host, conf.User, conf.Pass, conf.Database, conf.Port, dsnParam)
// 慢日志阈值
slowLogTime := conf.SlowLogTime
if slowLogTime == 0 {
slowLogTime = 60000 //默认1分钟
}
newLogger := gormLogger.New(
LogWriter{},
gormLogger.Config{
SlowThreshold: time.Duration(slowLogTime) * time.Millisecond, // 慢 SQL 阈值
LogLevel: gormLogger.Warn, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误
Colorful: false, // 禁用彩色打印
},
)
// 连接数据库
newDb, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: newLogger,
})
if err != nil {
logger.Error("cannot establish db connection: %w" + err.Error())
return nil, err
}
dbs[name] = newDb
return newDb, nil
}