feat: 增强 PostgreSQL 配置,添加连接生命周期和日志配置选项
This commit is contained in:
65
templates/project/providers/postgres/config.go.tpl
Executable file → Normal file
65
templates/project/providers/postgres/config.go.tpl
Executable file → Normal file
@@ -2,9 +2,12 @@ package postgres
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/opt"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "Database"
|
||||
@@ -31,6 +34,42 @@ type Config struct {
|
||||
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 (m Config) GormSlowThreshold() time.Duration {
|
||||
if m.SlowThresholdMs == 0 {
|
||||
return 200 * time.Millisecond // 默认200ms
|
||||
}
|
||||
return time.Duration(m.SlowThresholdMs) * time.Millisecond
|
||||
}
|
||||
|
||||
func (m Config) GormLogLevel() logger.LogLevel {
|
||||
switch m.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 (m *Config) checkDefault() {
|
||||
@@ -64,16 +103,34 @@ func (m *Config) checkDefault() {
|
||||
}
|
||||
|
||||
func (m *Config) EmptyDsn() string {
|
||||
// 基本 DSN
|
||||
dsnTpl := "host=%s user=%s password=%s port=%d dbname=%s sslmode=%s TimeZone=%s"
|
||||
m.checkDefault()
|
||||
|
||||
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Port, m.Database, m.SslMode, m.TimeZone)
|
||||
base := fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Port, m.Database, m.SslMode, m.TimeZone)
|
||||
// 附加可选参数
|
||||
extras := ""
|
||||
if m.UseSearchPath && m.Schema != "" {
|
||||
extras += " search_path=" + m.Schema
|
||||
}
|
||||
if m.ApplicationName != "" {
|
||||
extras += " application_name=" + strconv.Quote(m.ApplicationName)
|
||||
}
|
||||
return base + extras
|
||||
}
|
||||
|
||||
// DSN connection dsn
|
||||
func (m *Config) DSN() string {
|
||||
// 基本 DSN
|
||||
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
|
||||
m.checkDefault()
|
||||
|
||||
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
|
||||
base := fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
|
||||
// 附加可选参数
|
||||
extras := ""
|
||||
if m.UseSearchPath && m.Schema != "" {
|
||||
extras += " search_path=" + m.Schema
|
||||
}
|
||||
if m.ApplicationName != "" {
|
||||
extras += " application_name=" + strconv.Quote(m.ApplicationName)
|
||||
}
|
||||
return base + extras
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user