fix mssql connstr bug (#1205)

This commit is contained in:
Lunny Xiao 2019-01-23 15:49:33 +08:00 committed by GitHub
parent 17b9cc7330
commit ff094ebc3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 7 deletions

View File

@ -7,6 +7,7 @@ package xorm
import ( import (
"errors" "errors"
"fmt" "fmt"
"net/url"
"strconv" "strconv"
"strings" "strings"
@ -544,14 +545,23 @@ 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
for _, c := range kv {
vv := strings.Split(strings.TrimSpace(c), "=") if strings.HasPrefix(dataSourceName, "sqlserver://") {
if len(vv) == 2 { u, err := url.Parse(dataSourceName)
switch strings.ToLower(vv[0]) { if err != nil {
case "database": return nil, err
dbName = vv[1] }
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]
}
} }
} }
} }

35
dialect_mssql_test.go Normal file
View File

@ -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)
}
}
}