Skip to content

Commit

Permalink
Fix: Handle extended chars in SQL instance names (denisenkom#138)
Browse files Browse the repository at this point in the history
* Fix: Handle extended chars in SQL instance names
  • Loading branch information
shueybubbles authored Jul 25, 2023
1 parent d3fae84 commit 7804e0c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## 1.5.0

### Features

### Bug fixes

* Handle extended character in SQL instance names for browser lookup (#122)

## 1.4.0

### Features

* Adds UnmarshalJSON interface for UniqueIdentifier (#126)

### Bug fixes

* Fixes MarshalText prototype for UniqueIdentifier

## 1.2.0

### Features
Expand Down
21 changes: 20 additions & 1 deletion protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ func (t tcpDialer) ParseBrowserData(data msdsn.BrowserData, p *msdsn.Config) err
// for the instance and discover its port.
ok := len(data) > 0
strport := ""
inst := ""
if ok {
p.Instance = strings.ToUpper(p.Instance)
strport, ok = data[p.Instance]["tcp"]
instanceName := stringForInstanceNameComparison(p.Instance)
for _, i := range data {
inst, ok = i["InstanceName"]
if ok && stringForInstanceNameComparison(inst) == instanceName {
strport, ok = i["tcp"]
break
}
ok = false
}
}
if !ok {
f := "no instance matching '%v' returned from host '%v'"
Expand All @@ -40,6 +49,16 @@ func (t tcpDialer) ParseBrowserData(data msdsn.BrowserData, p *msdsn.Config) err
return nil
}

// SQL returns ASCII encoded instance names with \x## escaped UTF16 code points.
// We use QuoteToASCII to normalize strings like TJUTVÅ
// SQL returns 0xc5 as the byte value for Å while the UTF8 bytes in a Go string are [195 133]
// QuoteToASCII returns "TJUTV\u00c5" for both
func stringForInstanceNameComparison(inst string) (instanceName string) {
instanceName = strings.Replace(strconv.QuoteToASCII(inst), `\u00`, `\x`, -1)
instanceName = strings.Replace(instanceName, `\u`, `\x`, -1)
return
}

func (t tcpDialer) DialConnection(ctx context.Context, p *msdsn.Config) (conn net.Conn, err error) {
return nil, fmt.Errorf("tcp dialer requires a Connector instance")
}
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "fmt"

// Update this variable with the release tag before pushing the tag
// This value is written to the prelogin and login7 packets during a new connection
const driverVersion = "v1.4.0"
const driverVersion = "v1.5.0"

func getDriverVersion(ver string) uint32 {
var majorVersion uint32
Expand Down

0 comments on commit 7804e0c

Please sign in to comment.