[Fixed dsn parsing for network addr and port by caeret. #214]

This commit is contained in:
huili 2019-08-01 10:59:16 +08:00
parent 52ad125f54
commit b0971e8f0e
3 changed files with 31 additions and 23 deletions

View File

@ -18,6 +18,7 @@ package taosSql
import (
"errors"
"net/url"
"strconv"
"strings"
"time"
)
@ -25,6 +26,7 @@ import (
var (
errInvalidDSNUnescaped = errors.New("invalid DSN: did you forget to escape a param value?")
errInvalidDSNAddr = errors.New("invalid DSN: network address not terminated (missing closing brace)")
errInvalidDSNPort = errors.New("invalid DSN: network port is not a valid number")
errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
)
@ -100,7 +102,15 @@ func parseDSN(dsn string) (cfg *config, err error) {
}
return nil, errInvalidDSNAddr
}
cfg.addr = dsn[k+1 : i-1]
strs := strings.Split(dsn[k+1:i-1], ":")
if len(strs) == 1 {
return nil, errInvalidDSNAddr
}
cfg.addr = strs[0]
cfg.port, err = strconv.Atoi(strs[1])
if err != nil {
return nil, errInvalidDSNPort
}
break
}
}
@ -190,4 +200,3 @@ func parseDSNParams(cfg *config, params string) (err error) {
return
}

View File

@ -32,9 +32,8 @@ import (
func (mc *taosConn) taosConnect(ip, user, pass, db string, port int) (taos unsafe.Pointer, err error){
cuser := C.CString(user)
cpass := C.CString(pass)
cip := C.CString(ip) // TODO: Addr : x.x.x.x:port, must process to ip and port format
cip := C.CString(ip)
cdb := C.CString("")
port = 0
defer C.free(unsafe.Pointer(cip))
defer C.free(unsafe.Pointer(cuser))
defer C.free(unsafe.Pointer(cpass))

View File

@ -29,7 +29,7 @@ func main() {
fmt.Printf("\n======== start demo test ========\n")
// open connect to taos server
db, err := sql.Open(taosDriverName, "root:taosdata@/tcp(127.0.0.1)/demodb")
db, err := sql.Open(taosDriverName, "root:taosdata@/tcp(127.0.0.1:0)/demodb")
if err != nil {
log.Fatalf("Open database error: %s\n", err)
}