Skip to content

Commit

Permalink
Merge pull request #182 from taosdata/feat/TD-25182/3.0
Browse files Browse the repository at this point in the history
feat: move stmt field to common and add json tag
  • Loading branch information
huskar-t authored Jul 11, 2023
2 parents 68f96e2 + dbd9a06 commit e4f8e72
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 53 deletions.
52 changes: 52 additions & 0 deletions common/stmt/field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package stmt

import (
"fmt"

"github.com/taosdata/driver-go/v3/common"
"github.com/taosdata/driver-go/v3/types"
)

type StmtField struct {
Name string `json:"name"`
FieldType int8 `json:"field_type"`
Precision uint8 `json:"precision"`
Scale uint8 `json:"scale"`
Bytes int32 `json:"bytes"`
}

func (s *StmtField) GetType() (*types.ColumnType, error) {
switch s.FieldType {
case common.TSDB_DATA_TYPE_BOOL:
return &types.ColumnType{Type: types.TaosBoolType}, nil
case common.TSDB_DATA_TYPE_TINYINT:
return &types.ColumnType{Type: types.TaosTinyintType}, nil
case common.TSDB_DATA_TYPE_SMALLINT:
return &types.ColumnType{Type: types.TaosSmallintType}, nil
case common.TSDB_DATA_TYPE_INT:
return &types.ColumnType{Type: types.TaosIntType}, nil
case common.TSDB_DATA_TYPE_BIGINT:
return &types.ColumnType{Type: types.TaosBigintType}, nil
case common.TSDB_DATA_TYPE_UTINYINT:
return &types.ColumnType{Type: types.TaosUTinyintType}, nil
case common.TSDB_DATA_TYPE_USMALLINT:
return &types.ColumnType{Type: types.TaosUSmallintType}, nil
case common.TSDB_DATA_TYPE_UINT:
return &types.ColumnType{Type: types.TaosUIntType}, nil
case common.TSDB_DATA_TYPE_UBIGINT:
return &types.ColumnType{Type: types.TaosUBigintType}, nil
case common.TSDB_DATA_TYPE_FLOAT:
return &types.ColumnType{Type: types.TaosFloatType}, nil
case common.TSDB_DATA_TYPE_DOUBLE:
return &types.ColumnType{Type: types.TaosDoubleType}, nil
case common.TSDB_DATA_TYPE_BINARY:
return &types.ColumnType{Type: types.TaosBinaryType}, nil
case common.TSDB_DATA_TYPE_NCHAR:
return &types.ColumnType{Type: types.TaosNcharType}, nil
case common.TSDB_DATA_TYPE_TIMESTAMP:
return &types.ColumnType{Type: types.TaosTimestampType}, nil
case common.TSDB_DATA_TYPE_JSON:
return &types.ColumnType{Type: types.TaosJsonType}, nil
}
return nil, fmt.Errorf("unsupported type: %d, name %s", s.FieldType, s.Name)
}
3 changes: 2 additions & 1 deletion taosSql/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"unsafe"

"github.com/taosdata/driver-go/v3/common"
stmtCommon "github.com/taosdata/driver-go/v3/common/stmt"
"github.com/taosdata/driver-go/v3/errors"
"github.com/taosdata/driver-go/v3/types"
"github.com/taosdata/driver-go/v3/wrapper"
Expand All @@ -23,7 +24,7 @@ type Stmt struct {
tc *taosConn
pSql string
isInsert bool
cols []*wrapper.StmtField
cols []*stmtCommon.StmtField
//tags []*wrapper.StmtField
}

Expand Down
52 changes: 4 additions & 48 deletions wrapper/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"bytes"
"database/sql/driver"
"errors"
"fmt"
"unsafe"

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

Expand Down Expand Up @@ -586,50 +586,6 @@ func TaosStmtAffectedRowsOnce(stmt unsafe.Pointer) int {
//int32_t bytes;
//} TAOS_FIELD_E;

type StmtField struct {
Name string
FieldType int8
Precision uint8
Scale uint8
Bytes int32
}

func (s *StmtField) GetType() (*taosTypes.ColumnType, error) {
switch s.FieldType {
case common.TSDB_DATA_TYPE_BOOL:
return &taosTypes.ColumnType{Type: taosTypes.TaosBoolType}, nil
case common.TSDB_DATA_TYPE_TINYINT:
return &taosTypes.ColumnType{Type: taosTypes.TaosTinyintType}, nil
case common.TSDB_DATA_TYPE_SMALLINT:
return &taosTypes.ColumnType{Type: taosTypes.TaosSmallintType}, nil
case common.TSDB_DATA_TYPE_INT:
return &taosTypes.ColumnType{Type: taosTypes.TaosIntType}, nil
case common.TSDB_DATA_TYPE_BIGINT:
return &taosTypes.ColumnType{Type: taosTypes.TaosBigintType}, nil
case common.TSDB_DATA_TYPE_UTINYINT:
return &taosTypes.ColumnType{Type: taosTypes.TaosUTinyintType}, nil
case common.TSDB_DATA_TYPE_USMALLINT:
return &taosTypes.ColumnType{Type: taosTypes.TaosUSmallintType}, nil
case common.TSDB_DATA_TYPE_UINT:
return &taosTypes.ColumnType{Type: taosTypes.TaosUIntType}, nil
case common.TSDB_DATA_TYPE_UBIGINT:
return &taosTypes.ColumnType{Type: taosTypes.TaosUBigintType}, nil
case common.TSDB_DATA_TYPE_FLOAT:
return &taosTypes.ColumnType{Type: taosTypes.TaosFloatType}, nil
case common.TSDB_DATA_TYPE_DOUBLE:
return &taosTypes.ColumnType{Type: taosTypes.TaosDoubleType}, nil
case common.TSDB_DATA_TYPE_BINARY:
return &taosTypes.ColumnType{Type: taosTypes.TaosBinaryType}, nil
case common.TSDB_DATA_TYPE_NCHAR:
return &taosTypes.ColumnType{Type: taosTypes.TaosNcharType}, nil
case common.TSDB_DATA_TYPE_TIMESTAMP:
return &taosTypes.ColumnType{Type: taosTypes.TaosTimestampType}, nil
case common.TSDB_DATA_TYPE_JSON:
return &taosTypes.ColumnType{Type: taosTypes.TaosJsonType}, nil
}
return nil, fmt.Errorf("unsupported type: %d, name %s", s.FieldType, s.Name)
}

// TaosStmtGetTagFields DLL_EXPORT int taos_stmt_get_tag_fields(TAOS_STMT *stmt, int* fieldNum, TAOS_FIELD_E** fields);
func TaosStmtGetTagFields(stmt unsafe.Pointer) (code, num int, fields unsafe.Pointer) {
cNum := unsafe.Pointer(&num)
Expand Down Expand Up @@ -658,14 +614,14 @@ func TaosStmtGetColFields(stmt unsafe.Pointer) (code, num int, fields unsafe.Poi
return code, num, unsafe.Pointer(cField)
}

func StmtParseFields(num int, fields unsafe.Pointer) []*StmtField {
func StmtParseFields(num int, fields unsafe.Pointer) []*stmt.StmtField {
if num == 0 {
return nil
}
result := make([]*StmtField, num)
result := make([]*stmt.StmtField, num)
buf := bytes.NewBufferString("")
for i := 0; i < num; i++ {
r := &StmtField{}
r := &stmt.StmtField{}
field := *(*C.TAOS_FIELD_E)(unsafe.Pointer(uintptr(fields) + uintptr(C.sizeof_struct_TAOS_FIELD_E*C.int(i))))
for _, c := range field.name {
if c == 0 {
Expand Down
7 changes: 4 additions & 3 deletions wrapper/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/taosdata/driver-go/v3/common"
"github.com/taosdata/driver-go/v3/common/param"
"github.com/taosdata/driver-go/v3/common/parser"
stmtCommon "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 @@ -771,7 +772,7 @@ func TestGetFields(t *testing.T) {
defer TaosStmtReclaimFields(stmt, columnsP)
columns := StmtParseFields(columnCount, columnsP)
tags := StmtParseFields(tagCount, tagsP)
assert.Equal(t, []*StmtField{
assert.Equal(t, []*stmtCommon.StmtField{
{"ts", 9, 0, 0, 8},
{"c1", 1, 0, 0, 1},
{"c2", 2, 0, 0, 1},
Expand All @@ -787,7 +788,7 @@ func TestGetFields(t *testing.T) {
{"c12", 8, 0, 0, 22},
{"c13", 10, 0, 0, 82},
}, columns)
assert.Equal(t, []*StmtField{
assert.Equal(t, []*stmtCommon.StmtField{
{"tts", 9, 0, 0, 8},
{"tc1", 1, 0, 0, 1},
{"tc2", 2, 0, 0, 1},
Expand Down Expand Up @@ -863,7 +864,7 @@ func TestGetFieldsCommonTable(t *testing.T) {
}
defer TaosStmtReclaimFields(stmt, columnsP)
columns := StmtParseFields(columnCount, columnsP)
assert.Equal(t, []*StmtField{
assert.Equal(t, []*stmtCommon.StmtField{
{"ts", 9, 0, 0, 8},
{"c1", 1, 0, 0, 1},
{"c2", 2, 0, 0, 1},
Expand Down
2 changes: 1 addition & 1 deletion wrapper/tmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ func TestTMQGetTopicAssignment(t *testing.T) {
}
assert.Equal(t, 1, len(assignment))
assert.Equal(t, int64(0), assignment[0].Begin)
assert.Equal(t, int64(1), assignment[0].Offset)
assert.Equal(t, int64(0), assignment[0].Offset)
assert.GreaterOrEqual(t, assignment[0].End, end)
end = assignment[0].End
assert.Equal(t, int32(vgID), assignment[0].VGroupID)
Expand Down

0 comments on commit e4f8e72

Please sign in to comment.