Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(contrib/drivers/pgsql): add unix socket connection support #4028

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 john@goframe.org https://goframe.org
Copyright (c) 2017 GoFrame Team https://goframe.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
45 changes: 14 additions & 31 deletions contrib/drivers/clickhouse/clickhouse_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,26 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
)

// Open creates and returns an underlying sql.DB object for clickhouse.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source := config.Link
var source string
// clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(replaceSchemaPattern, "@$1/"+config.Name, config.Link)
} else {
// If no schema, the link is matched for replacement
dbName, _ := gregex.MatchString(replaceSchemaPattern, config.Link)
if len(dbName) > 0 {
config.Name = dbName[len(dbName)-1]
}
}
if config.Pass != "" {
source = fmt.Sprintf(
"clickhouse://%s:%s@%s:%s/%s?debug=%t",
config.User, url.PathEscape(config.Pass),
config.Host, config.Port, config.Name, config.Debug,
)
} else {
if config.Pass != "" {
source = fmt.Sprintf(
"clickhouse://%s:%s@%s:%s/%s?debug=%t",
config.User, url.PathEscape(config.Pass),
config.Host, config.Port, config.Name, config.Debug,
)
} else {
source = fmt.Sprintf(
"clickhouse://%s@%s:%s/%s?debug=%t",
config.User, config.Host, config.Port, config.Name, config.Debug,
)
}
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
source = fmt.Sprintf(
"clickhouse://%s@%s:%s/%s?debug=%t",
config.User, config.Host, config.Port, config.Name, config.Debug,
)
}
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
if db, err = sql.Open(driverName, source); err != nil {
err = gerror.WrapCodef(
Expand Down
2 changes: 1 addition & 1 deletion contrib/drivers/clickhouse/clickhouse_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func TestDriverClickhouse_Open(t *testing.T) {
// link
// DSM
// clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60
link := "clickhouse://default@127.0.0.1:9000,127.0.0.1:9000/default?dial_timeout=200ms&max_execution_time=60"
link := "clickhouse:default:@tcp(127.0.0.1:9000)/default?dial_timeout=200ms&max_execution_time=60"
db, err := gdb.New(gdb.ConfigNode{
Link: link,
Type: "clickhouse",
Expand Down
61 changes: 32 additions & 29 deletions contrib/drivers/mssql/mssql_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,16 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)

// Open creates and returns an underlying sql.DB object for mssql.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "sqlserver"
)
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`database=([\w\.\-]+)+`, "database="+config.Name, source)
}
} else {
source = fmt.Sprintf(
"user id=%s;password=%s;server=%s;port=%s;database=%s;encrypt=disable",
config.User, config.Pass, config.Host, config.Port, config.Name,
)
if config.Extra != "" {
var extraMap map[string]interface{}
if extraMap, err = gstr.Parse(config.Extra); err != nil {
return nil, err
}
for k, v := range extraMap {
source += fmt.Sprintf(`;%s=%s`, k, v)
}
}
source, err := configNodeToSource(config)
if err != nil {
return nil, err
}

underlyingDriverName := "sqlserver"
if db, err = sql.Open(underlyingDriverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
Expand All @@ -57,3 +32,31 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
}
return
}

func configNodeToSource(config *gdb.ConfigNode) (string, error) {
var source string
source = fmt.Sprintf(
"user id=%s;password=%s;server=%s;encrypt=disable",
config.User, config.Pass, config.Host,
)
if config.Name != "" {
source = fmt.Sprintf("%s;database=%s", source, config.Name)
}
if config.Port != "" {
source = fmt.Sprintf("%s;port=%s", source, config.Port)
}
if config.Extra != "" {
extraMap, err := gstr.Parse(config.Extra)
if err != nil {
return "", gerror.WrapCodef(
gcode.CodeInvalidParameter,
err,
`invalid extra configuration: %s`, config.Extra,
)
}
for k, v := range extraMap {
source += fmt.Sprintf(`;%s=%s`, k, v)
}
}
return source, nil
}
48 changes: 19 additions & 29 deletions contrib/drivers/mysql/mysql_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
)

// Open creates and returns an underlying sql.DB object for mysql.
Expand All @@ -35,36 +34,27 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
return
}

// [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
func configNodeToSource(config *gdb.ConfigNode) string {
var source string
// [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`/([\w\.\-]+)+`, "/"+config.Name, source)
}
} else {
var portStr string
if config.Port != "" {
portStr = ":" + config.Port
}
source = fmt.Sprintf(
"%s:%s@%s(%s%s)/%s?charset=%s",
config.User, config.Pass, config.Protocol, config.Host, portStr, config.Name, config.Charset,
)
if config.Timezone != "" {
if strings.Contains(config.Timezone, "/") {
config.Timezone = url.QueryEscape(config.Timezone)
}
source = fmt.Sprintf("%s&loc=%s", source, config.Timezone)
}
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
var (
source string
portStr string
)
if config.Port != "" {
portStr = ":" + config.Port
}
source = fmt.Sprintf(
"%s:%s@%s(%s%s)/%s?charset=%s",
config.User, config.Pass, config.Protocol, config.Host, portStr, config.Name, config.Charset,
)
if config.Timezone != "" {
if strings.Contains(config.Timezone, "/") {
config.Timezone = url.QueryEscape(config.Timezone)
}
source = fmt.Sprintf("%s&loc=%s", source, config.Timezone)
}
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
return source
}
32 changes: 10 additions & 22 deletions contrib/drivers/oracle/oracle_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/util/gconv"
)

Expand All @@ -35,30 +34,19 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
options["TRACE FILE"] = "oracle_trace.log"
}
// [username:[password]@]host[:port][/service_name][?param1=value1&...&paramN=valueN]
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`@(.+?)/([\w\.\-]+)+`, "@$1/"+config.Name, source)
}
} else {
if config.Extra != "" {
// fix #3226
list := strings.Split(config.Extra, "&")
for _, v := range list {
kv := strings.Split(v, "=")
if len(kv) == 2 {
options[kv[0]] = kv[1]
}
if config.Extra != "" {
// fix #3226
list := strings.Split(config.Extra, "&")
for _, v := range list {
kv := strings.Split(v, "=")
if len(kv) == 2 {
options[kv[0]] = kv[1]
}
}
source = gora.BuildUrl(
config.Host, gconv.Int(config.Port), config.Name, config.User, config.Pass, options,
)
}
source = gora.BuildUrl(
config.Host, gconv.Int(config.Port), config.Name, config.User, config.Pass, options,
)

if db, err = sql.Open(underlyingDriverName, source); err != nil {
err = gerror.WrapCodef(
Expand Down
79 changes: 38 additions & 41 deletions contrib/drivers/pgsql/pgsql_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,17 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)

// Open creates and returns an underlying sql.DB object for pgsql.
// https://pkg.go.dev/github.com/lib/pq
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "postgres"
)
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`dbname=([\w\.\-]+)+`, "dbname="+config.Name, source)
}
} else {
source = fmt.Sprintf(
"user=%s password='%s' host=%s port=%s sslmode=disable",
config.User, config.Pass, config.Host, config.Port)

if config.Name != "" {
source = fmt.Sprintf("%s dbname=%s", source, config.Name)
}

if config.Namespace != "" {
source = fmt.Sprintf("%s search_path=%s", source, config.Namespace)
}

if config.Timezone != "" {
source = fmt.Sprintf("%s timezone=%s", source, config.Timezone)
}

if config.Extra != "" {
var extraMap map[string]interface{}
if extraMap, err = gstr.Parse(config.Extra); err != nil {
return nil, err
}
for k, v := range extraMap {
source += fmt.Sprintf(` %s=%s`, k, v)
}
}
source, err := configNodeToSource(config)
if err != nil {
return nil, err
}

underlyingDriverName := "postgres"
if db, err = sql.Open(underlyingDriverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
Expand All @@ -70,3 +33,37 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
}
return
}

func configNodeToSource(config *gdb.ConfigNode) (string, error) {
var source string
source = fmt.Sprintf(
"user=%s password='%s' host=%s sslmode=disable",
config.User, config.Pass, config.Host,
)
if config.Port != "" {
source = fmt.Sprintf("%s port=%s", source, config.Port)
}
if config.Name != "" {
source = fmt.Sprintf("%s dbname=%s", source, config.Name)
}
if config.Namespace != "" {
source = fmt.Sprintf("%s search_path=%s", source, config.Namespace)
}
if config.Timezone != "" {
source = fmt.Sprintf("%s timezone=%s", source, config.Timezone)
}
if config.Extra != "" {
extraMap, err := gstr.Parse(config.Extra)
if err != nil {
return "", gerror.WrapCodef(
gcode.CodeInvalidParameter,
err,
`invalid extra configuration: %s`, config.Extra,
)
}
for k, v := range extraMap {
source += fmt.Sprintf(` %s=%s`, k, v)
}
}
return source, nil
}
10 changes: 1 addition & 9 deletions contrib/drivers/sqlite/sqlite_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,11 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source string
underlyingDriverName = "sqlite"
)
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
} else {
source = config.Name
}
source = config.Name
// It searches the source file to locate its absolute path..
if absolutePath, _ := gfile.Search(source); absolutePath != "" {
source = absolutePath
}

// Multiple PRAGMAs can be specified, e.g.:
// path/to/some.db?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)
if config.Extra != "" {
Expand Down
2 changes: 1 addition & 1 deletion contrib/drivers/sqlite/sqlite_z_unit_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_New(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
node := gdb.ConfigNode{
Type: "sqlite",
Link: gfile.Join(dbDir, "test.db"),
Name: gfile.Join(dbDir, "test.db"),
Charset: "utf8",
}
newDb, err := gdb.New(node)
Expand Down
Loading
Loading