Skip to content

Commit

Permalink
Merge pull request #200 from taosdata/enh/sunpeng/TD-25691-add-interf…
Browse files Browse the repository at this point in the history
…ace-required-by-ws

enh: add api required by ws
  • Loading branch information
huskar-t authored Sep 28, 2023
2 parents 5ab5921 + bba9cc3 commit 9b9dab2
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
cd TDengine
mkdir debug
cd debug
cmake .. -DBUILD_JDBC=false -DBUILD_TEST=off -DBUILD_HTTP=false -DVERNUMBER=3.9.9.9 -DCMAKE_C_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache
cmake .. -DBUILD_TEST=off -DBUILD_HTTP=false -DVERNUMBER=3.9.9.9 -DCMAKE_C_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache
make -j 4
- name: package
Expand Down
13 changes: 13 additions & 0 deletions wrapper/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/taosdata/driver-go/v3/common"
"github.com/taosdata/driver-go/v3/common/stmt"
taosError "github.com/taosdata/driver-go/v3/errors"
taosTypes "github.com/taosdata/driver-go/v3/types"
)

Expand Down Expand Up @@ -709,3 +710,15 @@ func StmtParseFields(num int, fields unsafe.Pointer) []*stmt.StmtField {
func TaosStmtReclaimFields(stmt unsafe.Pointer, fields unsafe.Pointer) {
C.taos_stmt_reclaim_fields(stmt, (*C.TAOS_FIELD_E)(fields))
}

// TaosStmtGetParam DLL_EXPORT int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes)
func TaosStmtGetParam(stmt unsafe.Pointer, idx int) (dataType int, dataLength int, err error) {
code := C.taos_stmt_get_param(stmt, C.int(idx), (*C.int)(unsafe.Pointer(&dataType)), (*C.int)(unsafe.Pointer(&dataLength)))
if code != 0 {
err = &taosError.TaosError{
Code: int32(code),
ErrStr: TaosStmtErrStr(stmt),
}
}
return
}
43 changes: 43 additions & 0 deletions wrapper/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,3 +1145,46 @@ func TestTaosStmtSetTags(t *testing.T) {
assert.Equal(t, int32(102), data[0][2].(int32))
assert.Equal(t, []byte(`{"a":"b"}`), data[0][3].([]byte))
}

func TestTaosStmtGetParam(t *testing.T) {
conn, err := TaosConnect("", "root", "taosdata", "", 0)
assert.NoError(t, err)
defer TaosClose(conn)

err = exec(conn, "drop database if exists test_stmt_get_param")
assert.NoError(t, err)
err = exec(conn, "create database if not exists test_stmt_get_param")
assert.NoError(t, err)
defer exec(conn, "drop database if exists test_stmt_get_param")

err = exec(conn,
"create table if not exists test_stmt_get_param.stb(ts TIMESTAMP,current float,voltage int,phase float) TAGS (groupid int,location varchar(24))")
assert.NoError(t, err)

stmt := TaosStmtInit(conn)
assert.NotNilf(t, stmt, "failed to init stmt")
defer TaosStmtClose(stmt)

code := TaosStmtPrepare(stmt, "insert into test_stmt_get_param.tb_0 using test_stmt_get_param.stb tags(?,?) values (?,?,?,?)")
assert.Equal(t, 0, code, TaosStmtErrStr(stmt))

dt, dl, err := TaosStmtGetParam(stmt, 0) // ts
assert.NoError(t, err)
assert.Equal(t, 9, dt)
assert.Equal(t, 8, dl)

dt, dl, err = TaosStmtGetParam(stmt, 1) // current
assert.NoError(t, err)
assert.Equal(t, 6, dt)
assert.Equal(t, 4, dl)

dt, dl, err = TaosStmtGetParam(stmt, 2) // voltage
assert.NoError(t, err)
assert.Equal(t, 4, dt)
assert.Equal(t, 4, dl)

dt, dl, err = TaosStmtGetParam(stmt, 3) // phase
assert.NoError(t, err)
assert.Equal(t, 6, dt)
assert.Equal(t, 4, dl)
}
21 changes: 21 additions & 0 deletions wrapper/taosc.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,24 @@ func TaosGetTablesVgID(conn unsafe.Pointer, db string, tables []string) (vgIDs [
func TaosSetConnMode(conn unsafe.Pointer, mode int, value int) int {
return int(C.taos_set_conn_mode(conn, C.int(mode), C.int(value)))
}

// TaosGetCurrentDB DLL_EXPORT int taos_get_current_db(TAOS *taos, char *database, int len, int *required)
func TaosGetCurrentDB(conn unsafe.Pointer) (db string, err error) {
cDb := C.CString(db)
defer C.free(unsafe.Pointer(cDb))
var required int

code := C.taos_get_current_db(conn, cDb, C.int(193), (*C.int)(unsafe.Pointer(&required)))
if code != 0 {
err = errors.NewError(int(code), TaosErrorStr(nil))
}
db = C.GoString(cDb)

return
}

// TaosGetServerInfo DLL_EXPORT const char *taos_get_server_info(TAOS *taos)
func TaosGetServerInfo(conn unsafe.Pointer) string {
info := C.taos_get_server_info(conn)
return C.GoString(info)
}
25 changes: 25 additions & 0 deletions wrapper/taosc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,28 @@ func TestTaosSetConnMode(t *testing.T) {
t.Errorf("TaosSetConnMode() error code= %d, msg: %s", code, TaosErrorStr(nil))
}
}

func TestTaosGetCurrentDB(t *testing.T) {
conn, err := TaosConnect("", "root", "taosdata", "", 0)
assert.NoError(t, err)
defer TaosClose(conn)
dbName := "current_db_test"
_ = exec(conn, fmt.Sprintf("drop database if exists %s", dbName))
err = exec(conn, fmt.Sprintf("create database %s", dbName))
assert.NoError(t, err)
defer func() {
_ = exec(conn, fmt.Sprintf("drop database if exists %s", dbName))
}()
_ = exec(conn, fmt.Sprintf("use %s", dbName))
db, err := TaosGetCurrentDB(conn)
assert.NoError(t, err)
assert.Equal(t, dbName, db)
}

func TestTaosGetServerInfo(t *testing.T) {
conn, err := TaosConnect("", "root", "taosdata", "", 0)
assert.NoError(t, err)
defer TaosClose(conn)
info := TaosGetServerInfo(conn)
assert.NotEmpty(t, info)
}

0 comments on commit 9b9dab2

Please sign in to comment.