From 6a9993179866d7a63ef6750a2fc167aa83bb66eb Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 13 Sep 2024 16:50:59 +0800 Subject: [PATCH] fix(database/gdb): #3755 error parsing database link without port number (#3772) --- database/gdb/gdb_core_config.go | 16 +++++++++++----- database/gdb/gdb_z_mysql_internal_test.go | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index d649f8998..fdec90f2e 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -276,12 +276,18 @@ func parseConfigNodeLink(node *ConfigNode) *ConfigNode { node.Pass = match[3] node.Protocol = match[4] array := gstr.Split(match[5], ":") - if len(array) == 2 && node.Protocol != "file" { - node.Host = array[0] - node.Port = array[1] - node.Name = match[6] - } else { + 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] diff --git a/database/gdb/gdb_z_mysql_internal_test.go b/database/gdb/gdb_z_mysql_internal_test.go index abf82b6a1..2f6947555 100644 --- a/database/gdb/gdb_z_mysql_internal_test.go +++ b/database/gdb/gdb_z_mysql_internal_test.go @@ -232,6 +232,22 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { t.Assert(newNode.Charset, defaultCharset) t.Assert(newNode.Protocol, `tcp`) }) + // #3755 + gtest.C(t, func(t *gtest.T) { + node := &ConfigNode{ + Link: "mysql:user:pwd@tcp(rdsid.mysql.rds.aliyuncs.com)/dbname?charset=utf8&loc=Local", + } + newNode := parseConfigNodeLink(node) + t.Assert(newNode.Type, `mysql`) + t.Assert(newNode.User, `user`) + t.Assert(newNode.Pass, `pwd`) + t.Assert(newNode.Host, `rdsid.mysql.rds.aliyuncs.com`) + t.Assert(newNode.Port, ``) + t.Assert(newNode.Name, `dbname`) + t.Assert(newNode.Extra, `charset=utf8&loc=Local`) + t.Assert(newNode.Charset, `utf8`) + t.Assert(newNode.Protocol, `tcp`) + }) } func Test_Func_doQuoteWord(t *testing.T) {