[feat] 移除对 db 库的直接依赖
This commit is contained in:
+61
-30
@@ -5,8 +5,6 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type MockDBConfig struct {
|
||||
@@ -56,7 +54,6 @@ type DBConfig struct {
|
||||
Synchronous int
|
||||
Cache string
|
||||
BusyTimeout int
|
||||
Raw func(*sqlite3.SQLiteConn) error
|
||||
}
|
||||
|
||||
SQLServer struct {
|
||||
@@ -142,33 +139,77 @@ func (c *DBConfig) toMySQLDSN() string {
|
||||
|
||||
// toPostgreSQLDSN 生成 PostgreSQL DSN
|
||||
func (c *DBConfig) toPostgreSQLDSN() string {
|
||||
params := url.Values{}
|
||||
if c.Host != "" {
|
||||
params.Add("host", c.Host)
|
||||
} else if len(c.WriteHosts) > 0 {
|
||||
params.Add("host", c.WriteHosts[0])
|
||||
} else if len(c.ReadHosts) > 0 {
|
||||
params.Add("host", c.ReadHosts[0])
|
||||
// 构建连接字符串参数列表
|
||||
var params []string
|
||||
|
||||
// 确定主机地址
|
||||
host := c.Host
|
||||
if host == "" && len(c.WriteHosts) > 0 {
|
||||
host = c.WriteHosts[0]
|
||||
}
|
||||
if c.Port == "" {
|
||||
c.Port = "5432" // 默认 PostgreSQL 端口
|
||||
if host == "" && len(c.ReadHosts) > 0 {
|
||||
host = c.ReadHosts[0]
|
||||
}
|
||||
params.Add("port", c.Port)
|
||||
params.Add("dbname", c.Database)
|
||||
params.Add("user", c.Username)
|
||||
if host != "" {
|
||||
params = append(params, fmt.Sprintf("host=%s", host))
|
||||
}
|
||||
|
||||
// 设置端口
|
||||
port := c.Port
|
||||
if port == "" {
|
||||
port = "5432" // 默认 PostgreSQL 端口
|
||||
}
|
||||
params = append(params, fmt.Sprintf("port=%s", port))
|
||||
|
||||
// 添加用户名
|
||||
if c.Username != "" {
|
||||
params = append(params, fmt.Sprintf("user=%s", c.Username))
|
||||
}
|
||||
|
||||
// 添加密码
|
||||
if c.Password != "" {
|
||||
params.Add("password", c.Password)
|
||||
params = append(params, fmt.Sprintf("password=%s", c.Password))
|
||||
}
|
||||
|
||||
// 添加数据库名
|
||||
if c.Database != "" {
|
||||
params = append(params, fmt.Sprintf("dbname=%s", c.Database))
|
||||
}
|
||||
|
||||
// 添加 SSL 模式
|
||||
if c.PostgreSQL.Sslmode != "" {
|
||||
params.Add("sslmode", c.PostgreSQL.Sslmode)
|
||||
params = append(params, fmt.Sprintf("sslmode=%s", c.PostgreSQL.Sslmode))
|
||||
}
|
||||
|
||||
// 添加连接超时
|
||||
if c.PostgreSQL.ConnectTimeout > 0 {
|
||||
params.Add("connect_timeout", fmt.Sprintf("%d", c.PostgreSQL.ConnectTimeout))
|
||||
params = append(params, fmt.Sprintf("connect_timeout=%d", c.PostgreSQL.ConnectTimeout))
|
||||
}
|
||||
|
||||
// 添加应用名称
|
||||
if c.PostgreSQL.ApplicationName != "" {
|
||||
params.Add("application_name", c.PostgreSQL.ApplicationName)
|
||||
params = append(params, fmt.Sprintf("application_name=%s", c.PostgreSQL.ApplicationName))
|
||||
}
|
||||
return "postgres://" + c.Username + ":" + url.QueryEscape(c.Password) + "@" + params.Get("host") + ":" + c.Port + "/" + c.Database + "?" + params.Encode()
|
||||
|
||||
// 添加搜索路径
|
||||
if c.PostgreSQL.SearchPath != "" {
|
||||
params = append(params, fmt.Sprintf("search_path=%s", c.PostgreSQL.SearchPath))
|
||||
}
|
||||
|
||||
// 用空格连接所有参数
|
||||
return joinParams(params, " ")
|
||||
}
|
||||
|
||||
// joinParams 连接参数列表
|
||||
func joinParams(params []string, sep string) string {
|
||||
if len(params) == 0 {
|
||||
return ""
|
||||
}
|
||||
result := params[0]
|
||||
for i := 1; i < len(params); i++ {
|
||||
result += sep + params[i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// toSQLiteDSN 生成 SQLite DSN
|
||||
@@ -388,16 +429,6 @@ func WithSQLiteFile(file string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// SQLite 注册函数
|
||||
func WithSQLiteRaw(raw func(*sqlite3.SQLiteConn) error) Option {
|
||||
return func(c *DBConfig) {
|
||||
if c.Driver != "sqlite3" {
|
||||
panic("WithSQLiteRegFn is only valid for sqlite3 driver")
|
||||
}
|
||||
c.SQLite.Raw = raw
|
||||
}
|
||||
}
|
||||
|
||||
func WithSQLiteJournal(journal string) Option {
|
||||
return func(c *DBConfig) {
|
||||
if c.Driver != "sqlite3" {
|
||||
|
||||
Reference in New Issue
Block a user