mymysql_driver.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. "errors"
  7. "strings"
  8. "time"
  9. "github.com/go-xorm/core"
  10. )
  11. type mymysqlDriver struct {
  12. }
  13. func (p *mymysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  14. db := &core.Uri{DbType: core.MYSQL}
  15. pd := strings.SplitN(dataSourceName, "*", 2)
  16. if len(pd) == 2 {
  17. // Parse protocol part of URI
  18. p := strings.SplitN(pd[0], ":", 2)
  19. if len(p) != 2 {
  20. return nil, errors.New("Wrong protocol part of URI")
  21. }
  22. db.Proto = p[0]
  23. options := strings.Split(p[1], ",")
  24. db.Raddr = options[0]
  25. for _, o := range options[1:] {
  26. kv := strings.SplitN(o, "=", 2)
  27. var k, v string
  28. if len(kv) == 2 {
  29. k, v = kv[0], kv[1]
  30. } else {
  31. k, v = o, "true"
  32. }
  33. switch k {
  34. case "laddr":
  35. db.Laddr = v
  36. case "timeout":
  37. to, err := time.ParseDuration(v)
  38. if err != nil {
  39. return nil, err
  40. }
  41. db.Timeout = to
  42. default:
  43. return nil, errors.New("Unknown option: " + k)
  44. }
  45. }
  46. // Remove protocol part
  47. pd = pd[1:]
  48. }
  49. // Parse database part of URI
  50. dup := strings.SplitN(pd[0], "/", 3)
  51. if len(dup) != 3 {
  52. return nil, errors.New("Wrong database part of URI")
  53. }
  54. db.DbName = dup[0]
  55. db.User = dup[1]
  56. db.Passwd = dup[2]
  57. return db, nil
  58. }