Skip to content

Commit

Permalink
fix configuration management for package gdb (#2163)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Sep 30, 2022
1 parent ceaecea commit 6cd84e8
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 72 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/gf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,22 @@ jobs:
--health-retries 10
# ClickHouse backend server.
# docker run -d --name clickhouse -p 9000:9000 -p 8123:8123 -p 9001:9001 loads/clickhouse-server:latest
clickhouse-server:
image: loads/clickhouse-server:latest
ports:
- 9000:9000
- 8123:8123
- 9001:9001

# Polaris backend server.
polaris:
image: loads/polaris-server-standalone:latest
ports:
- 8090:8090
- 8091:8091

# oracle 11g server
# Oracle 11g server
oracle-server:
image: loads/oracle-xe-11g-r2:latest
env:
Expand Down
12 changes: 8 additions & 4 deletions contrib/drivers/clickhouse/clickhouse_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 clickhouse

import (
Expand Down Expand Up @@ -123,7 +129,7 @@ func clickhouseConfigDB() gdb.DB {
User: "default",
Name: "default",
Type: "clickhouse",
Debug: true,
Debug: false,
})
gtest.AssertNil(err)
gtest.AssertNE(connect, nil)
Expand All @@ -132,9 +138,7 @@ func clickhouseConfigDB() gdb.DB {

func clickhouseLink() gdb.DB {
connect, err := gdb.New(gdb.ConfigNode{
Link: "clickhouse://default@127.0.0.1:9000,127.0.0.1:9000/default?dial_timeout=200ms&max_execution_time=60",
Type: "clickhouse",
Name: "default",
Link: "clickhouse:default:@tcp(127.0.0.1:9000)/default?dial_timeout=200ms&max_execution_time=60",
})
gtest.AssertNil(err)
gtest.AssertNE(connect, nil)
Expand Down
7 changes: 5 additions & 2 deletions contrib/drivers/mssql/mssql_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package mssql_test
import (
"context"
"fmt"

_ "github.com/denisenkom/go-mssqldb"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/database/gdb"
Expand Down Expand Up @@ -53,8 +54,10 @@ func init() {
nodeLink := gdb.ConfigNode{
Type: "mssql",
Name: "test",
Link: fmt.Sprintf("user id=%s;password=%s;server=%s;port=%s;database=%s;encrypt=disable",
node.User, node.Pass, node.Host, node.Port, node.Name),
Link: fmt.Sprintf(
"mssql:%s:%s@tcp(%s:%s)/%s?encrypt=disable",
node.User, node.Pass, node.Host, node.Port, node.Name,
),
}

nodeErr := gdb.ConfigNode{
Expand Down
8 changes: 5 additions & 3 deletions contrib/drivers/oracle/oracle_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ package oracle_test
import (
"context"
"fmt"
"strings"

"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
_ "github.com/sijms/go-ora/v2"
"strings"
)

var (
Expand Down Expand Up @@ -61,8 +62,9 @@ func init() {
nodeLink := gdb.ConfigNode{
Type: TestDbType,
Name: TestDbName,
Link: fmt.Sprintf("%s://%s:%s@%s:%s/%s",
TestDbType, TestDbUser, TestDbPass, TestDbIP, TestDbPort, TestDbName),
Link: fmt.Sprintf("%s:%s:%s@tcp(%s:%s)/%s",
TestDbType, TestDbUser, TestDbPass, TestDbIP, TestDbPort, TestDbName,
),
}

nodeErr := gdb.ConfigNode{
Expand Down
15 changes: 1 addition & 14 deletions contrib/drivers/pgsql/pgsql_z_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const (
TableSize = 10
TablePrefix = "t_"
SchemaName = "test"
TestDbUser = "postgres"
TestDbPass = "12345678"
CreateTime = "2018-10-24 10:00:00"
)

Expand All @@ -36,18 +34,7 @@ var (

func init() {
configNode = gdb.ConfigNode{
Host: "127.0.0.1",
Port: "5432",
User: TestDbUser,
Pass: TestDbPass,
Timezone: "Asia/Shanghai", // For calculating UT cases of datetime zones in convenience.
Type: "pgsql",
Role: "master",
Charset: "utf8",
Weight: 1,
MaxIdleConnCount: 10,
MaxOpenConnCount: 10,
MaxConnLifeTime: 600,
Link: `pgsql:postgres:12345678@tcp(127.0.0.1:5432)`,
}

//pgsql only permit to connect to the designation database.
Expand Down
3 changes: 2 additions & 1 deletion contrib/drivers/sqlite/sqlite_0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ func init() {

fmt.Println("init sqlite db dir: ", dbDir)

dbFilePath := gfile.Join(dbDir, "test.db")
configNode = gdb.ConfigNode{
Type: "sqlite",
Link: gfile.Join(dbDir, "test.db"),
Link: fmt.Sprintf(`sqlite::@file(%s)`, dbFilePath),
Charset: "utf8",
}
nodePrefix := configNode
Expand Down
9 changes: 6 additions & 3 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ const (
ctxKeyForDB gctx.StrKey = `CtxKeyForDB`
ctxKeyCatchSQL gctx.StrKey = `CtxKeyCatchSQL`
ctxKeyInternalProducedSQL gctx.StrKey = `CtxKeyInternalProducedSQL`

// type:[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
linkPattern = `(\w+):([\w\-]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([\w\-]*)\?{0,1}(.*)`
)

const (
Expand Down Expand Up @@ -372,9 +375,6 @@ var (

// tableFieldsMap caches the table information retrieved from database.
tableFieldsMap = gmap.NewStrAnyMap(true)

// type:[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
linkPattern = `(\w+):([\w\-]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([\w\-]*)\?{0,1}(.*)`
)

func init() {
Expand Down Expand Up @@ -426,6 +426,9 @@ func NewByGroup(group ...string) (db DB, err error) {

// newDBByConfigNode creates and returns an ORM object with given configuration node and group name.
func newDBByConfigNode(node *ConfigNode, group string) (db DB, err error) {
if node.Link != "" {
node = parseConfigNodeLink(node)
}
c := &Core{
group: group,
debug: gtype.NewBool(),
Expand Down
42 changes: 42 additions & 0 deletions database/gdb/gdb_core_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)

// Config is the configuration management object.
Expand Down Expand Up @@ -105,6 +106,9 @@ func AddConfigNode(group string, node ConfigNode) {

// parseConfigNode parses `Link` configuration syntax.
func parseConfigNode(node ConfigNode) ConfigNode {
if node.Link != "" {
node = *parseConfigNodeLink(&node)
}
if node.Link != "" && node.Type == "" {
match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.Link)
if len(match) == 3 {
Expand Down Expand Up @@ -245,3 +249,41 @@ func (c *Core) GetPrefix() string {
func (c *Core) GetSchema() string {
return c.schema
}

func parseConfigNodeLink(node *ConfigNode) *ConfigNode {
var match []string
if node.Link != "" {
match, _ = gregex.MatchString(linkPattern, node.Link)
if len(match) > 5 {
node.Type = match[1]
node.User = match[2]
node.Pass = match[3]
node.Protocol = match[4]
array := gstr.Split(match[5], ":")
if len(array) == 2 {
node.Host = array[0]
node.Port = array[1]
node.Name = match[6]
} else {
node.Name = match[5]
}
if len(match) > 6 {
node.Extra = match[7]
}
node.Link = ""
}
}
if node.Extra != "" {
if m, _ := gstr.Parse(node.Extra); len(m) > 0 {
_ = gconv.Struct(m, &node)
}
}
// Default value checks.
if node.Charset == "" {
node.Charset = defaultCharset
}
if node.Protocol == "" {
node.Protocol = defaultProtocol
}
return node
}
43 changes: 0 additions & 43 deletions database/gdb/gdb_driver_wrapper_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

"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"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)

Expand All @@ -27,9 +25,6 @@ type DriverWrapperDB struct {
// Open creates and returns an underlying sql.DB object for pgsql.
// https://pkg.go.dev/github.com/lib/pq
func (d *DriverWrapperDB) Open(config *ConfigNode) (db *sql.DB, err error) {
if config.Link != "" {
config = parseConfigNodeLink(config)
}
return d.DB.Open(config)
}

Expand Down Expand Up @@ -83,41 +78,3 @@ func (d *DriverWrapperDB) TableFields(ctx context.Context, table string, schema
}
return
}

func parseConfigNodeLink(node *ConfigNode) *ConfigNode {
var match []string
if node.Link != "" {
match, _ = gregex.MatchString(linkPattern, node.Link)
if len(match) > 5 {
node.Type = match[1]
node.User = match[2]
node.Pass = match[3]
node.Protocol = match[4]
array := gstr.Split(match[5], ":")
if len(array) == 2 {
node.Host = array[0]
node.Port = array[1]
node.Name = match[6]
} else {
node.Name = match[5]
}
if len(match) > 6 {
node.Extra = match[7]
}
node.Link = ""
}
}
if node.Extra != "" {
if m, _ := gstr.Parse(node.Extra); len(m) > 0 {
_ = gconv.Struct(m, &node)
}
}
// Default value checks.
if node.Charset == "" {
node.Charset = defaultCharset
}
if node.Protocol == "" {
node.Protocol = defaultProtocol
}
return node
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gf

const VERSION = "v2.2.0-beta"
const VERSION = "v2.2.0-beta2"
const AUTHORS = "john<john@goframe.org>"

0 comments on commit 6cd84e8

Please sign in to comment.