fix mssql connstr bug (#1205)
This commit is contained in:
parent
17b9cc7330
commit
ff094ebc3e
|
@ -7,6 +7,7 @@ package xorm
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -544,14 +545,23 @@ type odbcDriver struct {
|
|||
}
|
||||
|
||||
func (p *odbcDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
|
||||
kv := strings.Split(dataSourceName, ";")
|
||||
var dbName string
|
||||
for _, c := range kv {
|
||||
vv := strings.Split(strings.TrimSpace(c), "=")
|
||||
if len(vv) == 2 {
|
||||
switch strings.ToLower(vv[0]) {
|
||||
case "database":
|
||||
dbName = vv[1]
|
||||
|
||||
if strings.HasPrefix(dataSourceName, "sqlserver://") {
|
||||
u, err := url.Parse(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dbName = u.Query().Get("database")
|
||||
} else {
|
||||
kv := strings.Split(dataSourceName, ";")
|
||||
for _, c := range kv {
|
||||
vv := strings.Split(strings.TrimSpace(c), "=")
|
||||
if len(vv) == 2 {
|
||||
switch strings.ToLower(vv[0]) {
|
||||
case "database":
|
||||
dbName = vv[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2019 The Xorm Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package xorm
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/go-xorm/core"
|
||||
)
|
||||
|
||||
func TestParseMSSQL(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
expected string
|
||||
valid bool
|
||||
}{
|
||||
{"sqlserver://sa:yourStrong(!)Password@localhost:1433?database=db&connection+timeout=30", "db", true},
|
||||
{"server=localhost;user id=sa;password=yourStrong(!)Password;database=db", "db", true},
|
||||
}
|
||||
|
||||
driver := core.QueryDriver("mssql")
|
||||
|
||||
for _, test := range tests {
|
||||
uri, err := driver.Parse("mssql", test.in)
|
||||
|
||||
if err != nil && test.valid {
|
||||
t.Errorf("%q got unexpected error: %s", test.in, err)
|
||||
} else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
|
||||
t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue