mysql_driver.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "regexp"
  7. "strings"
  8. "github.com/go-xorm/core"
  9. )
  10. type mysqlDriver struct {
  11. }
  12. func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  13. dsnPattern := regexp.MustCompile(
  14. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  15. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  16. `\/(?P<dbname>.*?)` + // /dbname
  17. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  18. matches := dsnPattern.FindStringSubmatch(dataSourceName)
  19. //tlsConfigRegister := make(map[string]*tls.Config)
  20. names := dsnPattern.SubexpNames()
  21. uri := &core.Uri{DbType: core.MYSQL}
  22. for i, match := range matches {
  23. switch names[i] {
  24. case "dbname":
  25. uri.DbName = match
  26. case "params":
  27. if len(match) > 0 {
  28. kvs := strings.Split(match, "&")
  29. for _, kv := range kvs {
  30. splits := strings.Split(kv, "=")
  31. if len(splits) == 2 {
  32. switch splits[0] {
  33. case "charset":
  34. uri.Charset = splits[1]
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. return uri, nil
  42. }