diff --git a/LICENSE b/LICENSE index 0c20e2aaa..953bd19ac 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/contrib/drivers/clickhouse/clickhouse_open.go b/contrib/drivers/clickhouse/clickhouse_open.go index 15eaf147b..7cf076d09 100644 --- a/contrib/drivers/clickhouse/clickhouse_open.go +++ b/contrib/drivers/clickhouse/clickhouse_open.go @@ -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( diff --git a/contrib/drivers/clickhouse/clickhouse_z_unit_test.go b/contrib/drivers/clickhouse/clickhouse_z_unit_test.go index a4eb48487..caf82f77a 100644 --- a/contrib/drivers/clickhouse/clickhouse_z_unit_test.go +++ b/contrib/drivers/clickhouse/clickhouse_z_unit_test.go @@ -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", diff --git a/contrib/drivers/mssql/mssql_open.go b/contrib/drivers/mssql/mssql_open.go index 9555dc855..6e571e4e4 100644 --- a/contrib/drivers/mssql/mssql_open.go +++ b/contrib/drivers/mssql/mssql_open.go @@ -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, @@ -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 +} diff --git a/contrib/drivers/mysql/mysql_open.go b/contrib/drivers/mysql/mysql_open.go index cd5f74b11..fdf3595cb 100644 --- a/contrib/drivers/mysql/mysql_open.go +++ b/contrib/drivers/mysql/mysql_open.go @@ -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. @@ -35,36 +34,27 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) { return } +// [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN] func configNodeToSource(config *gdb.ConfigNode) string { - var source string - // [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=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 } diff --git a/contrib/drivers/oracle/oracle_open.go b/contrib/drivers/oracle/oracle_open.go index b720bb9cc..c41505e29 100644 --- a/contrib/drivers/oracle/oracle_open.go +++ b/contrib/drivers/oracle/oracle_open.go @@ -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" ) @@ -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&...¶mN=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( diff --git a/contrib/drivers/pgsql/pgsql_open.go b/contrib/drivers/pgsql/pgsql_open.go index 21d3dce13..87e0e5092 100644 --- a/contrib/drivers/pgsql/pgsql_open.go +++ b/contrib/drivers/pgsql/pgsql_open.go @@ -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, @@ -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 +} diff --git a/contrib/drivers/sqlite/sqlite_open.go b/contrib/drivers/sqlite/sqlite_open.go index 401b4b694..0f084292c 100644 --- a/contrib/drivers/sqlite/sqlite_open.go +++ b/contrib/drivers/sqlite/sqlite_open.go @@ -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 != "" { diff --git a/contrib/drivers/sqlite/sqlite_z_unit_core_test.go b/contrib/drivers/sqlite/sqlite_z_unit_core_test.go index 1c151472f..cb592db6e 100644 --- a/contrib/drivers/sqlite/sqlite_z_unit_core_test.go +++ b/contrib/drivers/sqlite/sqlite_z_unit_core_test.go @@ -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) diff --git a/contrib/drivers/sqlitecgo/sqlitecgo_open.go b/contrib/drivers/sqlitecgo/sqlitecgo_open.go index 7c014d69d..1e2e4b985 100644 --- a/contrib/drivers/sqlitecgo/sqlitecgo_open.go +++ b/contrib/drivers/sqlitecgo/sqlitecgo_open.go @@ -34,14 +34,7 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) { source string underlyingDriverName = "sqlite3" ) - 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 diff --git a/contrib/drivers/sqlitecgo/sqlitecgo_z_unit_core_test.go b/contrib/drivers/sqlitecgo/sqlitecgo_z_unit_core_test.go index fa2fe2c71..31b0bc460 100644 --- a/contrib/drivers/sqlitecgo/sqlitecgo_z_unit_core_test.go +++ b/contrib/drivers/sqlitecgo/sqlitecgo_z_unit_core_test.go @@ -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) diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 18de920a7..da52e7526 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -638,8 +638,8 @@ const ( ctxKeyCatchSQL gctx.StrKey = `CtxKeyCatchSQL` ctxKeyInternalProducedSQL gctx.StrKey = `CtxKeyInternalProducedSQL` - // type:[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN] - linkPattern = `(\w+):([\w\-\$]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([^\?]*)\?{0,1}(.*)` + linkPattern = `(\w+):([\w\-\$]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([^\?]*)\?{0,1}(.*)` + linkPatternDescription = `type:username:password@protocol(host:port)/dbname?param1=value1&...¶mN=valueN` ) type ctxTimeoutType int @@ -843,7 +843,10 @@ func NewByGroup(group ...string) (db DB, err error) { // So all db type configurations in the same group should be the same. func newDBByConfigNode(node *ConfigNode, group string) (db DB, err error) { if node.Link != "" { - node = parseConfigNodeLink(node) + node, err = parseConfigNodeLink(node) + if err != nil { + return + } } c := &Core{ group: group, diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index fdec90f2e..291ade995 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -10,6 +10,8 @@ import ( "sync" "time" + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/glog" "github.com/gogf/gf/v2/text/gregex" @@ -73,42 +75,64 @@ func init() { // SetConfig sets the global configuration for package. // It will overwrite the old configuration of package. -func SetConfig(config Config) { +func SetConfig(config Config) error { defer instances.Clear() configs.Lock() defer configs.Unlock() + for k, nodes := range config { for i, node := range nodes { - nodes[i] = parseConfigNode(node) + parsedNode, err := parseConfigNode(node) + if err != nil { + return err + } + nodes[i] = parsedNode } config[k] = nodes } configs.config = config + return nil } // SetConfigGroup sets the configuration for given group. -func SetConfigGroup(group string, nodes ConfigGroup) { +func SetConfigGroup(group string, nodes ConfigGroup) error { defer instances.Clear() configs.Lock() defer configs.Unlock() + for i, node := range nodes { - nodes[i] = parseConfigNode(node) + parsedNode, err := parseConfigNode(node) + if err != nil { + return err + } + nodes[i] = parsedNode } configs.config[group] = nodes + return nil } // AddConfigNode adds one node configuration to configuration of given group. -func AddConfigNode(group string, node ConfigNode) { +func AddConfigNode(group string, node ConfigNode) error { defer instances.Clear() configs.Lock() defer configs.Unlock() - configs.config[group] = append(configs.config[group], parseConfigNode(node)) + + parsedNode, err := parseConfigNode(node) + if err != nil { + return err + } + configs.config[group] = append(configs.config[group], parsedNode) + return nil } // parseConfigNode parses `Link` configuration syntax. -func parseConfigNode(node ConfigNode) ConfigNode { +func parseConfigNode(node ConfigNode) (ConfigNode, error) { if node.Link != "" { - node = *parseConfigNodeLink(&node) + parsedLinkNode, err := parseConfigNodeLink(&node) + if err != nil { + return node, err + } + node = *parsedLinkNode } if node.Link != "" && node.Type == "" { match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.Link) @@ -117,17 +141,17 @@ func parseConfigNode(node ConfigNode) ConfigNode { node.Link = gstr.Trim(match[2]) } } - return node + return node, nil } // AddDefaultConfigNode adds one node configuration to configuration of default group. -func AddDefaultConfigNode(node ConfigNode) { - AddConfigNode(DefaultGroupName, node) +func AddDefaultConfigNode(node ConfigNode) error { + return AddConfigNode(DefaultGroupName, node) } // AddDefaultConfigGroup adds multiple node configurations to configuration of default group. -func AddDefaultConfigGroup(nodes ConfigGroup) { - SetConfigGroup(DefaultGroupName, nodes) +func AddDefaultConfigGroup(nodes ConfigGroup) error { + return SetConfigGroup(DefaultGroupName, nodes) } // GetConfig retrieves and returns the configuration of given group. @@ -266,34 +290,39 @@ func (c *Core) GetSchema() string { return schema } -func parseConfigNodeLink(node *ConfigNode) *ConfigNode { +func parseConfigNodeLink(node *ConfigNode) (*ConfigNode, error) { 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 node.Protocol == "file" { - node.Name = match[5] - } else { - if len(array) == 2 { - // link with port. - node.Host = array[0] - node.Port = array[1] - } else { - // link without port. - node.Host = array[0] - } - node.Name = match[6] - } - if len(match) > 6 && match[7] != "" { - node.Extra = match[7] - } - node.Link = "" + if len(match) <= 5 { + return nil, gerror.NewCodef( + gcode.CodeInvalidParameter, + `invalid link configuration: %s, shuold be pattern like: %s`, + node.Link, linkPatternDescription, + ) } + node.Type = match[1] + node.User = match[2] + node.Pass = match[3] + node.Protocol = match[4] + array := gstr.Split(match[5], ":") + if node.Protocol == "file" { + node.Name = match[5] + } else { + if len(array) == 2 { + // link with port. + node.Host = array[0] + node.Port = array[1] + } else { + // link without port. + node.Host = array[0] + } + node.Name = match[6] + } + if len(match) > 6 && match[7] != "" { + node.Extra = match[7] + } + node.Link = "" } if node.Extra != "" { if m, _ := gstr.Parse(node.Extra); len(m) > 0 { @@ -307,5 +336,5 @@ func parseConfigNodeLink(node *ConfigNode) *ConfigNode { if node.Protocol == "" { node.Protocol = defaultProtocol } - return node + return node, nil } diff --git a/database/gdb/gdb_z_mysql_internal_test.go b/database/gdb/gdb_z_mysql_internal_test.go index a2428dbee..b2ec6e83b 100644 --- a/database/gdb/gdb_z_mysql_internal_test.go +++ b/database/gdb/gdb_z_mysql_internal_test.go @@ -53,7 +53,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss?loc=Local&parseTime=true&charset=latin`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -68,7 +69,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss?`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -83,7 +85,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -99,7 +102,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/?loc=Local&parseTime=true&charset=latin`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -114,7 +118,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)?loc=Local&parseTime=true&charset=latin`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -129,7 +134,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -144,7 +150,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -160,7 +167,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `mysql:root:CxzhD*624:27jh@udp(9.135.69.119:3306)`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -175,7 +183,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `sqlite:root:CxzhD*624:27jh@file(/var/data/db.sqlite3)?local=Local&parseTime=true`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `sqlite`) t.Assert(newNode.User, `root`) t.Assert(newNode.Pass, `CxzhD*624:27jh`) @@ -190,7 +199,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `sqlite::CxzhD*624:2@7jh@file(/var/data/db.sqlite3)`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `sqlite`) t.Assert(newNode.User, ``) t.Assert(newNode.Pass, `CxzhD*624:2@7jh`) @@ -205,7 +215,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `sqlite::@file(/var/data/db.sqlite3)`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `sqlite`) t.Assert(newNode.User, ``) t.Assert(newNode.Pass, ``) @@ -221,7 +232,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: `pgsql:BASIC$xxxx:123456@tcp(xxxx.hologres.aliyuncs.com:80)/xxx`, } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `pgsql`) t.Assert(newNode.User, `BASIC$xxxx`) t.Assert(newNode.Pass, `123456`) @@ -237,7 +249,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: "mysql:user:pwd@tcp(rdsid.mysql.rds.aliyuncs.com)/dbname?charset=utf8&loc=Local", } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `user`) t.Assert(newNode.Pass, `pwd`) @@ -253,7 +266,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { node := &ConfigNode{ Link: "mysql:username:password@unix(/tmp/mysql.sock)/dbname", } - newNode := parseConfigNodeLink(node) + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) t.Assert(newNode.Type, `mysql`) t.Assert(newNode.User, `username`) t.Assert(newNode.Pass, `password`) diff --git a/frame/gins/gins_database.go b/frame/gins/gins_database.go index ece4ed70e..ef8e07aaf 100644 --- a/frame/gins/gins_database.go +++ b/frame/gins/gins_database.go @@ -91,7 +91,9 @@ func Database(name ...string) gdb.DB { if len(cg) > 0 { if gdb.GetConfig(group) == nil { intlog.Printf(ctx, "add configuration for group: %s, %#v", g, cg) - gdb.SetConfigGroup(g, cg) + if err := gdb.SetConfigGroup(g, cg); err != nil { + panic(err) + } } else { intlog.Printf(ctx, "ignore configuration as it already exists for group: %s, %#v", g, cg) intlog.Printf(ctx, "%s, %#v", g, cg) @@ -108,7 +110,9 @@ func Database(name ...string) gdb.DB { if len(cg) > 0 { if gdb.GetConfig(group) == nil { intlog.Printf(ctx, "add configuration for group: %s, %#v", gdb.DefaultGroupName, cg) - gdb.SetConfigGroup(gdb.DefaultGroupName, cg) + if err := gdb.SetConfigGroup(gdb.DefaultGroupName, cg); err != nil { + panic(err) + } } else { intlog.Printf( ctx,