diff --git a/.github/workflows/gf.yml b/.github/workflows/gf.yml index 9e8a7c36a9c..66a3109124d 100644 --- a/.github/workflows/gf.yml +++ b/.github/workflows/gf.yml @@ -94,6 +94,7 @@ 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: @@ -101,13 +102,14 @@ jobs: - 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: diff --git a/contrib/drivers/clickhouse/clickhouse_test.go b/contrib/drivers/clickhouse/clickhouse_test.go index f94fd2d06d7..e04714544cd 100644 --- a/contrib/drivers/clickhouse/clickhouse_test.go +++ b/contrib/drivers/clickhouse/clickhouse_test.go @@ -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 ( @@ -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) @@ -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) diff --git a/contrib/drivers/mssql/mssql_init_test.go b/contrib/drivers/mssql/mssql_init_test.go index 97c6d0e5338..c9145c09581 100644 --- a/contrib/drivers/mssql/mssql_init_test.go +++ b/contrib/drivers/mssql/mssql_init_test.go @@ -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" @@ -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{ diff --git a/contrib/drivers/oracle/oracle_init_test.go b/contrib/drivers/oracle/oracle_init_test.go index b16c8cb9e50..5d19b06021b 100644 --- a/contrib/drivers/oracle/oracle_init_test.go +++ b/contrib/drivers/oracle/oracle_init_test.go @@ -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 ( @@ -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{ diff --git a/contrib/drivers/pgsql/pgsql_z_init_test.go b/contrib/drivers/pgsql/pgsql_z_init_test.go index acc2d6c5039..6ea57983baf 100644 --- a/contrib/drivers/pgsql/pgsql_z_init_test.go +++ b/contrib/drivers/pgsql/pgsql_z_init_test.go @@ -23,8 +23,6 @@ const ( TableSize = 10 TablePrefix = "t_" SchemaName = "test" - TestDbUser = "postgres" - TestDbPass = "12345678" CreateTime = "2018-10-24 10:00:00" ) @@ -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. diff --git a/contrib/drivers/sqlite/sqlite_0_test.go b/contrib/drivers/sqlite/sqlite_0_test.go index 4406072fb40..22152f16559 100644 --- a/contrib/drivers/sqlite/sqlite_0_test.go +++ b/contrib/drivers/sqlite/sqlite_0_test.go @@ -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 diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 9705e3316b1..6dae140af02 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -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&...¶mN=valueN] + linkPattern = `(\w+):([\w\-]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([\w\-]*)\?{0,1}(.*)` ) const ( @@ -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&...¶mN=valueN] - linkPattern = `(\w+):([\w\-]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([\w\-]*)\?{0,1}(.*)` ) func init() { @@ -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(), diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index 1c5bfc59be3..3ee78853faf 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -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. @@ -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 { @@ -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 +} diff --git a/database/gdb/gdb_driver_wrapper_db.go b/database/gdb/gdb_driver_wrapper_db.go index 3cbac566291..a29bb2226c6 100644 --- a/database/gdb/gdb_driver_wrapper_db.go +++ b/database/gdb/gdb_driver_wrapper_db.go @@ -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" ) @@ -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) } @@ -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 -} diff --git a/version.go b/version.go index 6c10583e593..7899d150ee9 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package gf -const VERSION = "v2.2.0-beta" +const VERSION = "v2.2.0-beta2" const AUTHORS = "john"