github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/xorm/mymysql_driver.go (about)

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