fix mssql connstr bug (#1205)
This commit is contained in:
parent
17b9cc7330
commit
ff094ebc3e
|
@ -7,6 +7,7 @@ package xorm
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -544,8 +545,16 @@ type odbcDriver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *odbcDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
|
func (p *odbcDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
|
||||||
kv := strings.Split(dataSourceName, ";")
|
|
||||||
var dbName string
|
var dbName string
|
||||||
|
|
||||||
|
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 {
|
for _, c := range kv {
|
||||||
vv := strings.Split(strings.TrimSpace(c), "=")
|
vv := strings.Split(strings.TrimSpace(c), "=")
|
||||||
if len(vv) == 2 {
|
if len(vv) == 2 {
|
||||||
|
@ -555,6 +564,7 @@ func (p *odbcDriver) Parse(driverName, dataSourceName string) (*core.Uri, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if dbName == "" {
|
if dbName == "" {
|
||||||
return nil, errors.New("no db name provided")
|
return nil, errors.New("no db name provided")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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