mysql_driver.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/xormplus/core"
  9. )
  10. // func init() {
  11. // core.RegisterDriver("mysql", &mysqlDriver{})
  12. // }
  13. type mysqlDriver struct {
  14. }
  15. func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  16. dsnPattern := regexp.MustCompile(
  17. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  18. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  19. `\/(?P<dbname>.*?)` + // /dbname
  20. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  21. matches := dsnPattern.FindStringSubmatch(dataSourceName)
  22. //tlsConfigRegister := make(map[string]*tls.Config)
  23. names := dsnPattern.SubexpNames()
  24. uri := &core.Uri{DbType: core.MYSQL}
  25. for i, match := range matches {
  26. switch names[i] {
  27. case "dbname":
  28. uri.DbName = match
  29. case "params":
  30. if len(match) > 0 {
  31. kvs := strings.Split(match, "&")
  32. for _, kv := range kvs {
  33. splits := strings.Split(kv, "=")
  34. if len(splits) == 2 {
  35. switch splits[0] {
  36. case "charset":
  37. uri.Charset = splits[1]
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. return uri, nil
  45. }