add local db configuration support for package gdb (#2161)

* version updates

* add local db configuration support for package gdb

* add local db configuration support for package gdb

* add local db configuration support for package gdb
This commit is contained in:
John Guo
2022-09-29 11:58:03 +08:00
committed by GitHub
parent cd5bf7c504
commit ceaeceadd9
4 changed files with 43 additions and 128 deletions

View File

@ -373,9 +373,8 @@ var (
// tableFieldsMap caches the table information retrieved from database.
tableFieldsMap = gmap.NewStrAnyMap(true)
// [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
linkPatternWithType = `(\w+):([\w\-]+):(.+?)@(.+?)\((.+?):(\d+)\)/{0,1}([\w\-]*)\?{0,1}(.*)`
linkPatternWithoutType = `([\w\-]+):(.+?)@(.+?)\((.+?):(\d+)\)/{0,1}([\w\-]*)\?{0,1}(.*)`
// type:[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
linkPattern = `(\w+):([\w\-]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([\w\-]*)\?{0,1}(.*)`
)
func init() {

View File

@ -86,31 +86,22 @@ func (d *DriverWrapperDB) TableFields(ctx context.Context, table string, schema
func parseConfigNodeLink(node *ConfigNode) *ConfigNode {
var match []string
// It firstly parses `link` using with type pattern.
match, _ = gregex.MatchString(linkPatternWithType, node.Link)
if len(match) > 6 {
node.Type = match[1]
node.User = match[2]
node.Pass = match[3]
node.Protocol = match[4]
node.Host = match[5]
node.Port = match[6]
node.Name = match[7]
if len(match) > 7 {
node.Extra = match[8]
}
node.Link = ""
} else {
// Else it parses `link` using without type pattern.
match, _ = gregex.MatchString(linkPatternWithoutType, node.Link)
if len(match) > 6 {
node.User = match[1]
node.Pass = match[2]
node.Protocol = match[3]
node.Host = match[4]
node.Port = match[5]
node.Name = match[6]
if len(match) > 7 {
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 = ""

View File

@ -141,125 +141,50 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
t.Assert(newNode.Charset, defaultCharset)
t.Assert(newNode.Protocol, `udp`)
})
}
func Test_parseConfigNodeLink_WithoutType(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
node := &ConfigNode{
Link: `root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss?loc=Local&parseTime=true&charset=latin`,
Link: `sqlite:root:CxzhD*624:27jh@file(/var/data/db.sqlite3)?local=Local&parseTime=true`,
}
newNode := parseConfigNodeLink(node)
t.Assert(newNode.Type, ``)
t.Assert(newNode.Type, `sqlite`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
t.Assert(newNode.Host, `9.135.69.119`)
t.Assert(newNode.Port, `3306`)
t.Assert(newNode.Name, `khaos_oss`)
t.Assert(newNode.Extra, `loc=Local&parseTime=true&charset=latin`)
t.Assert(newNode.Charset, `latin`)
t.Assert(newNode.Protocol, `tcp`)
t.Assert(newNode.Host, ``)
t.Assert(newNode.Port, ``)
t.Assert(newNode.Name, `/var/data/db.sqlite3`)
t.Assert(newNode.Extra, `local=Local&parseTime=true`)
t.Assert(newNode.Charset, defaultCharset)
t.Assert(newNode.Protocol, `file`)
})
gtest.C(t, func(t *gtest.T) {
node := &ConfigNode{
Link: `root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss?`,
Link: `sqlite::CxzhD*624:2@7jh@file(/var/data/db.sqlite3)`,
}
newNode := parseConfigNodeLink(node)
t.Assert(newNode.Type, ``)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
t.Assert(newNode.Host, `9.135.69.119`)
t.Assert(newNode.Port, `3306`)
t.Assert(newNode.Name, `khaos_oss`)
t.Assert(newNode.Type, `sqlite`)
t.Assert(newNode.User, ``)
t.Assert(newNode.Pass, `CxzhD*624:2@7jh`)
t.Assert(newNode.Host, ``)
t.Assert(newNode.Port, ``)
t.Assert(newNode.Name, `/var/data/db.sqlite3`)
t.Assert(newNode.Extra, ``)
t.Assert(newNode.Charset, defaultCharset)
t.Assert(newNode.Protocol, `tcp`)
t.Assert(newNode.Protocol, `file`)
})
gtest.C(t, func(t *gtest.T) {
node := &ConfigNode{
Link: `root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss`,
Link: `sqlite::@file(/var/data/db.sqlite3)`,
}
newNode := parseConfigNodeLink(node)
t.Assert(newNode.Type, ``)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
t.Assert(newNode.Host, `9.135.69.119`)
t.Assert(newNode.Port, `3306`)
t.Assert(newNode.Name, `khaos_oss`)
t.Assert(newNode.Type, `sqlite`)
t.Assert(newNode.User, ``)
t.Assert(newNode.Pass, ``)
t.Assert(newNode.Host, ``)
t.Assert(newNode.Port, ``)
t.Assert(newNode.Name, `/var/data/db.sqlite3`)
t.Assert(newNode.Extra, ``)
t.Assert(newNode.Charset, defaultCharset)
t.Assert(newNode.Protocol, `tcp`)
})
// empty database preselect.
gtest.C(t, func(t *gtest.T) {
node := &ConfigNode{
Link: `root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/?loc=Local&parseTime=true&charset=latin`,
}
newNode := parseConfigNodeLink(node)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
t.Assert(newNode.Host, `9.135.69.119`)
t.Assert(newNode.Port, `3306`)
t.Assert(newNode.Name, ``)
t.Assert(newNode.Extra, `loc=Local&parseTime=true&charset=latin`)
t.Assert(newNode.Charset, `latin`)
t.Assert(newNode.Protocol, `tcp`)
})
gtest.C(t, func(t *gtest.T) {
node := &ConfigNode{
Link: `root:CxzhD*624:27jh@tcp(9.135.69.119:3306)?loc=Local&parseTime=true&charset=latin`,
}
newNode := parseConfigNodeLink(node)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
t.Assert(newNode.Host, `9.135.69.119`)
t.Assert(newNode.Port, `3306`)
t.Assert(newNode.Name, ``)
t.Assert(newNode.Extra, `loc=Local&parseTime=true&charset=latin`)
t.Assert(newNode.Charset, `latin`)
t.Assert(newNode.Protocol, `tcp`)
})
gtest.C(t, func(t *gtest.T) {
node := &ConfigNode{
Link: `root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/`,
}
newNode := parseConfigNodeLink(node)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
t.Assert(newNode.Host, `9.135.69.119`)
t.Assert(newNode.Port, `3306`)
t.Assert(newNode.Name, ``)
t.Assert(newNode.Extra, ``)
t.Assert(newNode.Charset, defaultCharset)
t.Assert(newNode.Protocol, `tcp`)
})
gtest.C(t, func(t *gtest.T) {
node := &ConfigNode{
Link: `root:CxzhD*624:27jh@tcp(9.135.69.119:3306)`,
}
newNode := parseConfigNodeLink(node)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
t.Assert(newNode.Host, `9.135.69.119`)
t.Assert(newNode.Port, `3306`)
t.Assert(newNode.Name, ``)
t.Assert(newNode.Extra, ``)
t.Assert(newNode.Charset, defaultCharset)
t.Assert(newNode.Protocol, `tcp`)
})
// protocol.
gtest.C(t, func(t *gtest.T) {
node := &ConfigNode{
Link: `root:CxzhD*624:27jh@udp(9.135.69.119:3306)`,
}
newNode := parseConfigNodeLink(node)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
t.Assert(newNode.Host, `9.135.69.119`)
t.Assert(newNode.Port, `3306`)
t.Assert(newNode.Name, ``)
t.Assert(newNode.Extra, ``)
t.Assert(newNode.Charset, defaultCharset)
t.Assert(newNode.Protocol, `udp`)
t.Assert(newNode.Protocol, `file`)
})
}

View File

@ -1,4 +1,4 @@
package gf
const VERSION = "v2.2.0"
const VERSION = "v2.2.0-beta"
const AUTHORS = "john<john@goframe.org>"