Skip to content

Commit

Permalink
[TD-5071]<enhance>: explicit errno in TaosError type
Browse files Browse the repository at this point in the history
  • Loading branch information
zitsen committed Jul 6, 2021
1 parent 49fbe22 commit b62c90b
Show file tree
Hide file tree
Showing 12 changed files with 495 additions and 78 deletions.
20 changes: 6 additions & 14 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ kind: pipeline
name: default

steps:
- name: tdengine
image: alpine/git
commands:
- git clone -b develop https://github.com/taosdata/TDengine.git _tdengine
- name: test
image: golang
commands:
- apt-get update
- apt-get install -y cmake build-essential gcc
- mkdir _tdengine/build
- cd _tdengine/build
- cmake ..
- make
- make install
- echo "firstEp $(hostname):6030" > /etc/taos/taos.cfg
- echo "fqdn $(hostname)" >> /etc/taos/taos.cfg
- wget https://www.taosdata.com/assets-download/TDengine-server-2.1.3.2-Linux-x64.tar.gz
- tar xvf TDengine-server-2.1.3.2-Linux-x64.tar.gz
- cd TDengine-server-2.1.3.2/
- printf "\n\n"|./install.sh
- nohup taosd &
- cd ../../
- cd ../
- go mod tidy
- go test -v ./...
15 changes: 4 additions & 11 deletions taosSql/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@ import "C"
import (
"context"
"database/sql/driver"
"errors"
"strconv"
"strings"
"time"
"unsafe"
)

var (
errInvalidConn = errors.New("invalid connection")
errConnNoExist = errors.New("no existent connection ")
)

type taosConn struct {
taos unsafe.Pointer
result unsafe.Pointer
Expand All @@ -56,14 +50,13 @@ func (res *taosSqlResult) RowsAffected() (int64, error) {
}

func (mc *taosConn) Begin() (driver.Tx, error) {
return nil, errors.New("taosSql does not support transaction")
return nil, &TaosError{Code: 0, ErrStr: "taosSql does not support transaction"}
}

func (mc *taosConn) Close() (err error) {
if mc.taos == nil {
return errConnNoExist
if mc.taos != nil {
mc.taos_close()
}
mc.taos_close()
return nil
}

Expand Down Expand Up @@ -277,7 +270,7 @@ func (mc *taosConn) Ping(ctx context.Context) (err error) {

// BeginTx implements driver.ConnBeginTx interface
func (mc *taosConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return nil, errors.New("taosSql does not support transaction")
return nil, &TaosError{Code: 0, ErrStr: "taosSql does not support transaction"}
}

func (mc *taosConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
Expand Down
7 changes: 3 additions & 4 deletions taosSql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package taosSql

const (
timeFormat = "2006-01-02 15:04:05"
maxTaosSqlLen = 65380
defaultBufSize = maxTaosSqlLen + 32
timeFormat = "2006-01-02 15:04:05"
maxTaosSqlLen = 65380
defaultBufSize = maxTaosSqlLen + 32
)

type fieldType byte
Expand Down Expand Up @@ -48,4 +48,3 @@ const (
statusInTransReadonly
statusSessionStateChanged
)

7 changes: 3 additions & 4 deletions taosSql/ctaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import "C"

import (
"database/sql/driver"
"errors"
"io"
"log"
"time"
Expand Down Expand Up @@ -113,8 +112,8 @@ func (res *taosRes) errstr() string {
}

// int taos_errno(TAOS_RES *tres);
func (res *taosRes) errno() int {
return int(C.taos_errno(res.ref))
func (res *taosRes) errno() int32 {
return int32(C.taos_errno(res.ref))
}

// call after fetchRow
Expand Down Expand Up @@ -387,7 +386,7 @@ func (stmt *taosStmt) useResult() (res *taosRes) {
func (res *taosRes) Next(values []driver.Value) (err error) {
fields := res.fetchFields()
if len(values) != len(fields) {
err = errors.New("values and fields length not match")
err = &TaosError{Code: 0, ErrStr: "values and fields length not match"}
return
}

Expand Down
34 changes: 16 additions & 18 deletions taosSql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package taosSql

import (
"database/sql/driver"
"errors"
"fmt"
"time"
"unsafe"
Expand Down Expand Up @@ -49,8 +48,8 @@ type taosTopic struct {
}

func getError() error {
if errno := getErrno(); errno < 0 {
return errors.New(tstrerror(errno))
if errno := getErrno(); errno != 0 {
return &TaosError{Code: errno & 0xffff, ErrStr: tstrerror(errno)}
}
return nil
}
Expand Down Expand Up @@ -86,7 +85,7 @@ func (db *taosDB) Subscribe(restart bool, name string, sql string, interval time
if err != nil {
return
} else {
err = errors.New("failed to subscribe")
err = &TaosError{Code: 0, ErrStr: "failed to subscribe"}
return
}
}
Expand All @@ -99,29 +98,29 @@ func (db *taosDB) execute(sql string, params []driver.Value) (res *taosRes, err
if err = getError(); err != nil {
return
} else {
err = errors.New("failed to init stmt")
err = &TaosError{Code: 0, ErrStr: "failed to init stmt"}
return
}
}

defer stmt.close()
if rc := stmt.prepare(sql); rc < 0 {
err = errors.New(tstrerror(rc))
if rc := stmt.prepare(sql); rc != 0 {
err = &TaosError{Code: rc & 0xffff, ErrStr: tstrerror(rc)}
return
}

if rc := stmt.bindParam(params); rc < 0 {
err = errors.New(tstrerror(rc))
err = &TaosError{Code: rc & 0xffff, ErrStr: tstrerror(rc)}
return
}
if isInsert := stmt.isInsert(); isInsert == 1 {
if rc := stmt.addBatch(); rc < 0 {
err = errors.New(tstrerror(rc))
if rc := stmt.addBatch(); rc != 0 {
err = &TaosError{Code: rc & 0xffff, ErrStr: tstrerror(rc)}
return
}
}
if rc := stmt.execute(); rc < 0 {
err = errors.New(tstrerror(rc))
err = &TaosError{Code: rc & 0xffff, ErrStr: tstrerror(rc)}
return
}
res = stmt.useResult()
Expand All @@ -140,18 +139,18 @@ func (db *taosDB) Exec(sql string, params ...driver.Value) (result driver.Result

if res == nil {
if err = getError(); err == nil {
err = fmt.Errorf("failed to exec: %s", sql)
err = &TaosError{Code: 0, ErrStr: fmt.Sprintf("failed to exec: %s", sql)}
}
return
}
defer res.freeResult()
if errno := res.errno(); errno != 0 {
err = errors.New(res.errstr())
err = &TaosError{Code: errno & 0xffff, ErrStr: res.errstr()}
return
}
rowsAffected := res.affectedRows()
if errno := res.errno(); errno != 0 {
err = errors.New(res.errstr())
err = &TaosError{Code: errno & 0xffff, ErrStr: res.errstr()}
return
}
result = driver.RowsAffected(rowsAffected)
Expand All @@ -170,13 +169,13 @@ func (db *taosDB) Query(sql string, params ...driver.Value) (rows driver.Rows, e
}
if res == nil {
if err = getError(); err == nil {
err = errors.New("failed to query")
err = &TaosError{Code: 0, ErrStr: "failed to query"}
}
return
}
errno := res.errno()
if errno != 0 {
err = errors.New(res.errstr())
err = &TaosError{Code: errno & 0xffff, ErrStr: res.errstr()}
return
}
rows = res
Expand All @@ -187,8 +186,7 @@ func (sub *taosTopic) Consume() (rows driver.Rows, err error) {
res := sub.consume()
errno := res.errno()
if errno != 0 {
msg := res.errstr()
err = errors.New(msg)
err = &TaosError{Code: errno & 0xffff, ErrStr: res.errstr()}
return
}
sub.res = res
Expand Down
15 changes: 7 additions & 8 deletions taosSql/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
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)")
errInvalidDSNPort = errors.New("invalid DSN: network port is not a valid number")
errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
errInvalidDSNUnescaped = &TaosError{Code: 0, ErrStr: "invalid DSN: did you forget to escape a param value?"}
errInvalidDSNAddr = &TaosError{Code: 0, ErrStr: "invalid DSN: network address not terminated (missing closing brace)"}
errInvalidDSNPort = &TaosError{Code: 0, ErrStr: "invalid DSN: network port is not a valid number"}
errInvalidDSNNoSlash = &TaosError{Code: 0, ErrStr: "invalid DSN: missing the slash separating the database name"}
)

// Config is a configuration parsed from a DSN string.
Expand Down Expand Up @@ -155,15 +154,15 @@ func parseDSNParams(cfg *config, params string) (err error) {
var isBool bool
cfg.columnsWithAlias, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
return &TaosError{Code: 0, ErrStr: "invalid bool value: " + value}
}

// Enable client side placeholder substitution
case "interpolateParams":
var isBool bool
cfg.interpolateParams, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
return &TaosError{Code: 0, ErrStr: "invalid bool value: " + value}
}

// Time Location
Expand All @@ -181,7 +180,7 @@ func parseDSNParams(cfg *config, params string) (err error) {
var isBool bool
cfg.parseTime, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
return &TaosError{Code: 0, ErrStr: "invalid bool value: " + value}
}

default:
Expand Down
Loading

0 comments on commit b62c90b

Please sign in to comment.