153 lines
3.7 KiB
Go
153 lines
3.7 KiB
Go
package postgres
|
||
|
||
import (
|
||
"fmt"
|
||
"math"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"go.ipao.vip/atom/container"
|
||
"go.ipao.vip/atom/opt"
|
||
"gorm.io/gorm/logger"
|
||
)
|
||
|
||
const DefaultPrefix = "Database"
|
||
|
||
func DefaultProvider() container.ProviderContainer {
|
||
return container.ProviderContainer{
|
||
Provider: Provide,
|
||
Options: []opt.Option{
|
||
opt.Prefix(DefaultPrefix),
|
||
},
|
||
}
|
||
}
|
||
|
||
type Config struct {
|
||
Username string
|
||
Password string
|
||
Database string
|
||
Schema string
|
||
Host string
|
||
Port uint
|
||
SslMode string
|
||
TimeZone string
|
||
Prefix string // 表前缀
|
||
Singular bool // 是否开启全局禁用复数,true表示开启
|
||
MaxIdleConns int // 空闲中的最大连接数
|
||
MaxOpenConns int // 打开到数据库的最大连接数
|
||
// 可选:连接生命周期配置(0 表示不设置)
|
||
ConnMaxLifetimeSeconds uint
|
||
ConnMaxIdleTimeSeconds uint
|
||
|
||
// 可选:GORM 日志与行为配置
|
||
LogLevel string // silent|error|warn|info(默认info)
|
||
SlowThresholdMs uint // 慢查询阈值(毫秒)默认200
|
||
ParameterizedQueries bool // 占位符输出,便于日志安全与查询归并
|
||
PrepareStmt bool // 预编译语句缓存
|
||
SkipDefaultTransaction bool // 跳过默认事务
|
||
|
||
// 可选:DSN 增强
|
||
UseSearchPath bool // 在 DSN 中附带 search_path
|
||
ApplicationName string // application_name
|
||
}
|
||
|
||
func (config Config) GormSlowThreshold() time.Duration {
|
||
if config.SlowThresholdMs == 0 {
|
||
return 200 * time.Millisecond // 默认200ms
|
||
}
|
||
if config.SlowThresholdMs > math.MaxInt64/uint(time.Millisecond) {
|
||
return time.Duration(math.MaxInt64)
|
||
}
|
||
|
||
return time.Duration(config.SlowThresholdMs) * time.Millisecond
|
||
}
|
||
|
||
func (config Config) GormLogLevel() logger.LogLevel {
|
||
switch config.LogLevel {
|
||
case "silent":
|
||
return logger.Silent
|
||
case "error":
|
||
return logger.Error
|
||
case "warn":
|
||
return logger.Warn
|
||
case "info", "":
|
||
return logger.Info
|
||
default:
|
||
return logger.Info
|
||
}
|
||
}
|
||
|
||
func (config *Config) checkDefault() {
|
||
if config.MaxIdleConns == 0 {
|
||
config.MaxIdleConns = 10
|
||
}
|
||
|
||
if config.MaxOpenConns == 0 {
|
||
config.MaxOpenConns = 100
|
||
}
|
||
|
||
if config.Username == "" {
|
||
config.Username = "postgres"
|
||
}
|
||
|
||
if config.SslMode == "" {
|
||
config.SslMode = "disable"
|
||
} else {
|
||
config.SslMode = strings.ToLower(strings.TrimSpace(config.SslMode))
|
||
}
|
||
|
||
if config.TimeZone == "" {
|
||
config.TimeZone = "Asia/Shanghai"
|
||
}
|
||
|
||
if config.Port == 0 {
|
||
config.Port = 5432
|
||
}
|
||
|
||
if config.Schema == "" {
|
||
config.Schema = "public"
|
||
}
|
||
}
|
||
|
||
func (config *Config) EmptyDsn() string {
|
||
// 基本 DSN
|
||
dsnTpl := "host=%s user=%s password=%s port=%d dbname=%s sslmode=%s TimeZone=%s"
|
||
config.checkDefault()
|
||
base := fmt.Sprintf(dsnTpl, config.Host, config.Username, config.Password, config.Port, config.Database, config.SslMode, config.TimeZone)
|
||
// 附加可选参数
|
||
extras := ""
|
||
if config.UseSearchPath && config.Schema != "" {
|
||
extras += " search_path=" + config.Schema
|
||
}
|
||
if config.ApplicationName != "" {
|
||
extras += " application_name=" + strconv.Quote(config.ApplicationName)
|
||
}
|
||
|
||
return base + extras
|
||
}
|
||
|
||
// DSN connection dsn
|
||
func (config *Config) DSN() string {
|
||
// 基本 DSN
|
||
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
|
||
config.checkDefault()
|
||
base := fmt.Sprintf(dsnTpl, config.Host, config.Username, config.Password, config.Database, config.Port, config.SslMode, config.TimeZone)
|
||
// 附加可选参数
|
||
extras := ""
|
||
if config.UseSearchPath && config.Schema != "" {
|
||
extras += " search_path=" + config.Schema
|
||
}
|
||
if config.ApplicationName != "" {
|
||
extras += " application_name=" + strconv.Quote(config.ApplicationName)
|
||
}
|
||
|
||
return base + extras
|
||
}
|
||
|
||
func (config *Config) IsTLSEnabled() bool {
|
||
mode := strings.ToLower(strings.TrimSpace(config.SslMode))
|
||
|
||
return mode != "" && mode != "disable"
|
||
}
|