1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package xorm
- import (
- "regexp"
- "strings"
- "github.com/go-xorm/core"
- )
- type mysqlDriver struct {
- }
- func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
- dsnPattern := regexp.MustCompile(
- `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` +
- `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` +
- `\/(?P<dbname>.*?)` +
- `(?:\?(?P<params>[^\?]*))?$`)
- matches := dsnPattern.FindStringSubmatch(dataSourceName)
-
- names := dsnPattern.SubexpNames()
- uri := &core.Uri{DbType: core.MYSQL}
- for i, match := range matches {
- switch names[i] {
- case "dbname":
- uri.DbName = match
- case "params":
- if len(match) > 0 {
- kvs := strings.Split(match, "&")
- for _, kv := range kvs {
- splits := strings.Split(kv, "=")
- if len(splits) == 2 {
- switch splits[0] {
- case "charset":
- uri.Charset = splits[1]
- }
- }
- }
- }
- }
- }
- return uri, nil
- }
|