diff --git a/src/connector/go/src/taosSql/dsn.go b/src/connector/go/src/taosSql/dsn.go index be31462f1ddffb89d091a40517287afe0a0bc445..c68ae1725cb730c770325a1b3603416b8c2ac1f6 100755 --- a/src/connector/go/src/taosSql/dsn.go +++ b/src/connector/go/src/taosSql/dsn.go @@ -18,39 +18,41 @@ package taosSql import ( "errors" "net/url" + "strconv" "strings" "time" ) 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)") - errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name") + 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") ) // Config is a configuration parsed from a DSN string. // If a new Config is created instead of being parsed from a DSN string, // the NewConfig function should be used, which sets default values. type config struct { - user string // Username - passwd string // Password (requires User) - net string // Network type - addr string // Network address (requires Net) - port int - dbName string // Database name - params map[string]string // Connection parameters - loc *time.Location // Location for time.Time values - columnsWithAlias bool // Prepend table alias to column names - interpolateParams bool // Interpolate placeholders into query string - parseTime bool // Parse time values to time.Time + user string // Username + passwd string // Password (requires User) + net string // Network type + addr string // Network address (requires Net) + port int + dbName string // Database name + params map[string]string // Connection parameters + loc *time.Location // Location for time.Time values + columnsWithAlias bool // Prepend table alias to column names + interpolateParams bool // Interpolate placeholders into query string + parseTime bool // Parse time values to time.Time } // NewConfig creates a new Config and sets default values. func newConfig() *config { return &config{ - loc: time.UTC, - interpolateParams: true, - parseTime: true, + loc: time.UTC, + interpolateParams: true, + parseTime: true, } } @@ -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 } - diff --git a/src/connector/go/src/taosSql/taosSqlCgo.go b/src/connector/go/src/taosSql/taosSqlCgo.go index 78835d5639e4c1199947e7ef8a1efb28494df0a7..e91f6bfc49781d003338a969c9cecc5b32b92d54 100755 --- a/src/connector/go/src/taosSql/taosSqlCgo.go +++ b/src/connector/go/src/taosSql/taosSqlCgo.go @@ -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)) diff --git a/tests/examples/go/src/taosapp/taosapp.go b/tests/examples/go/src/taosapp/taosapp.go index 75f780e61f8f3772f754ac818e8b1778dda5f2d7..5c7b92f6fbc22d8f5a784409b797891a19829417 100755 --- a/tests/examples/go/src/taosapp/taosapp.go +++ b/tests/examples/go/src/taosapp/taosapp.go @@ -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) } @@ -302,4 +302,4 @@ func checkErr(err error) { if err != nil { panic(err) } -} \ No newline at end of file +}