mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package pgsql
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"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 {
|
|
if config.Name != "" {
|
|
source = fmt.Sprintf(
|
|
"user=%s password=%s host=%s port=%s dbname=%s sslmode=disable",
|
|
config.User, config.Pass, config.Host, config.Port, config.Name,
|
|
)
|
|
} else {
|
|
source = fmt.Sprintf(
|
|
"user=%s password=%s host=%s port=%s sslmode=disable",
|
|
config.User, config.Pass, config.Host, config.Port,
|
|
)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
if db, err = sql.Open(underlyingDriverName, source); err != nil {
|
|
err = gerror.WrapCodef(
|
|
gcode.CodeDbOperationError, err,
|
|
`sql.Open failed for driver "%s" by source "%s"`, underlyingDriverName, source,
|
|
)
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|