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