Skip to content

Commit

Permalink
Merge pull request #310 from taosdata/feat/xftan/TD-33175
Browse files Browse the repository at this point in the history
feat: rename taos_stmt2_get_fields
  • Loading branch information
zitsen authored Dec 18, 2024
2 parents a721473 + 2cd6f2c commit c5c3dac
Show file tree
Hide file tree
Showing 12 changed files with 1,740 additions and 496 deletions.
76 changes: 75 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,78 @@ jobs:
with:
files: ./coverage.txt
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_ORG_TOKEN }}
CODECOV_TOKEN: ${{ secrets.CODECOV_ORG_TOKEN }}

test_asan:
runs-on: ubuntu-latest
needs: build
strategy:
matrix:
go: [ 'stable' ]
name: Go-ASAN-${{ matrix.go }}
steps:
- name: get cache server by pr
if: github.event_name == 'pull_request'
id: get-cache-server-pr
uses: actions/cache@v4
with:
path: server.tar.gz
key: ${{ runner.os }}-build-${{ github.base_ref }}-${{ needs.build.outputs.commit_id }}
restore-keys: |
${{ runner.os }}-build-${{ github.base_ref }}-
- name: get cache server by push
if: github.event_name == 'push'
id: get-cache-server-push
uses: actions/cache@v4
with:
path: server.tar.gz
key: ${{ runner.os }}-build-${{ github.ref_name }}-${{ needs.build.outputs.commit_id }}
restore-keys: |
${{ runner.os }}-build-${{ github.ref_name }}-
- name: cache server manually
if: github.event_name == 'workflow_dispatch'
id: get-cache-server-manually
uses: actions/cache@v4
with:
path: server.tar.gz
key: ${{ runner.os }}-build-${{ inputs.tbBranch }}-${{ needs.build.outputs.commit_id }}
restore-keys: |
${{ runner.os }}-build-${{ inputs.tbBranch }}-
- name: install
run: |
tar -zxvf server.tar.gz
cd release && sudo sh install.sh
- name: checkout
uses: actions/checkout@v4

- name: copy taos cfg
run: |
sudo mkdir -p /etc/taos
sudo cp ./.github/workflows/taos.cfg /etc/taos/taos.cfg
sudo cp ./.github/workflows/taosadapter.toml /etc/taos/taosadapter.toml
- name: shell
run: |
cat >start.sh<<EOF
ulimit -n 65535 && TAOS_SUPPORT_VNODES=256 taosd
EOF
- name: taosd
run: nohup sudo sh ./start.sh &

- name: start taosadapter
run: sudo taosadapter &

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache-dependency-path: go.sum

- name: Test
run: sudo go test -v -asan ./...
18 changes: 16 additions & 2 deletions af/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,22 @@ func TestFastInsertWithSetSubTableName(t *testing.T) {
params []*param2.Param
bindType *param2.ColumnType
}{
{"set_table_name_sub_int", "1,'int'", "ts timestamp, `value` int", "?, ?", []*param2.Param{param2.NewParam(1).AddTimestamp(now, common.PrecisionMicroSecond), param2.NewParam(1).AddInt(1)}, param2.NewColumnType(2).AddTimestamp().AddInt()},
{"set_table_name_sub_nchar", "2,'nchar'", "ts timestamp, `value` nchar(8)", "?, ?", []*param2.Param{param2.NewParam(1).AddTimestamp(time.Now(), common.PrecisionMicroSecond), param2.NewParam(1).AddNchar("ttt")}, param2.NewColumnType(2).AddTimestamp().AddNchar(1)},
{
sTableName: "set_table_name_sub_int",
tags: "1,'int'",
tbType: "ts timestamp, `value` int",
pos: "?, ?",
params: []*param2.Param{param2.NewParam(1).AddTimestamp(now, common.PrecisionMicroSecond), param2.NewParam(1).AddInt(1)},
bindType: param2.NewColumnType(2).AddTimestamp().AddInt(),
},
{
sTableName: "set_table_name_sub_nchar",
tags: "2,'nchar'",
tbType: "ts timestamp, `value` nchar(8)",
pos: "?, ?",
params: []*param2.Param{param2.NewParam(1).AddTimestamp(time.Now(), common.PrecisionMicroSecond), param2.NewParam(1).AddNchar("ttt")},
bindType: param2.NewColumnType(2).AddTimestamp().AddNchar(5),
},
} {
tbName := fmt.Sprintf("test_fast_insert_with_sub_table_name_%02d", i)
tbType := tc.tbType
Expand Down
30 changes: 12 additions & 18 deletions af/stmt2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
)

type Stmt2 struct {
stmt2 unsafe.Pointer
isInsert *bool
colFields []*stmt.StmtField
tagFields []*stmt.StmtField
stmt2 unsafe.Pointer
isInsert *bool
fields []*stmt.Stmt2AllField
//colFields []*stmt.StmtField
//tagFields []*stmt.StmtField
caller *Stmt2CallBackCaller
affectedRows int
queryResult unsafe.Pointer
Expand Down Expand Up @@ -102,21 +103,14 @@ func (s *Stmt2) Prepare(sql string) error {
}
s.isInsert = &isInsert
if !isInsert {
s.tagFields = nil
s.colFields = nil
s.fields = nil
} else {
colFields, err := s.getFields(stmt.TAOS_FIELD_COL)
fields, err := s.getFields()
if err != nil {
s.isInsert = nil
return fmt.Errorf("get stmt2 col fields error:%s, sql:%s", err.Error(), sql)
}
tagFields, err := s.getFields(stmt.TAOS_FIELD_TAG)
if err != nil {
s.isInsert = nil
return fmt.Errorf("get stmt2 tag fields error:%s, sql:%s", err.Error(), sql)
}
s.colFields = colFields
s.tagFields = tagFields
s.fields = fields
}
return nil
}
Expand Down Expand Up @@ -148,7 +142,7 @@ func (s *Stmt2) Bind(params []*stmt.TaosStmt2BindData) error {
}
locker.Lock()
defer locker.Unlock()
err := wrapper.TaosStmt2BindParam(s.stmt2, *s.isInsert, params, s.colFields, s.tagFields, -1)
err := wrapper.TaosStmt2BindParam(s.stmt2, *s.isInsert, params, s.fields, -1)
return err
}

Expand Down Expand Up @@ -211,8 +205,8 @@ func (s *Stmt2) Close() error {
return nil
}

func (s *Stmt2) getFields(fieldType int) ([]*stmt.StmtField, error) {
code, count, cFields := wrapper.TaosStmt2GetFields(s.stmt2, fieldType)
func (s *Stmt2) getFields() ([]*stmt.Stmt2AllField, error) {
code, count, cFields := wrapper.TaosStmt2GetFields(s.stmt2)
if code != 0 {
return nil, s.stmt2Err(code)
}
Expand All @@ -222,7 +216,7 @@ func (s *Stmt2) getFields(fieldType int) ([]*stmt.StmtField, error) {
if count == 0 {
return nil, nil
}
fields := wrapper.StmtParseFields(count, cFields)
fields := wrapper.Stmt2ParseAllFields(count, cFields)
return fields, nil
}

Expand Down
46 changes: 46 additions & 0 deletions af/stmt2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,49 @@ func TestStmt2(t *testing.T) {
assert.ErrorIs(t, err, io.EOF)

}

func TestStmt2_Prepare(t *testing.T) {
conn, err := Open("", "root", "taosdata", "", 0)
if !assert.NoError(t, err) {
return
}
stmt2 := conn.Stmt2(0x123456789, false)
if stmt2 == nil {
t.Errorf("Expected stmt to be not nil")
return
}
defer func() {
err = stmt2.Close()
assert.NoError(t, err)
}()
_, err = conn.Exec("create database if not exists stmt2_prepare_wrong_test")
if !assert.NoError(t, err) {
return
}
defer func() {
_, err = conn.Exec("drop database if exists stmt2_prepare_wrong_test")
assert.NoError(t, err)
}()
_, err = conn.Exec("use stmt2_prepare_wrong_test")
if !assert.NoError(t, err) {
return
}
err = stmt2.Prepare("insert into not_exist_table values(?,?,?)")
assert.Error(t, err)
_, err = conn.Exec("create table t (ts timestamp, b int, c int)")
assert.NoError(t, err)
err = stmt2.Prepare("")
assert.NoError(t, err)
err = stmt2.Bind([]*stmt.TaosStmt2BindData{
{
Cols: [][]driver.Value{
{time.Now()},
{int32(1)},
{int32(2)},
},
},
})
assert.Error(t, err)
err = stmt2.Prepare("insert into t values(?,?,?)")
assert.Error(t, err)
}
26 changes: 22 additions & 4 deletions common/stmt/stmt2.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ const (
BindDataIsNullOffset = BindDataNumOffset + 4
)

func MarshalStmt2Binary(bindData []*TaosStmt2BindData, isInsert bool, colType, tagType []*StmtField) ([]byte, error) {
func MarshalStmt2Binary(bindData []*TaosStmt2BindData, isInsert bool, fields []*Stmt2AllField) ([]byte, error) {
var colType []*Stmt2AllField
var tagType []*Stmt2AllField
for i := 0; i < len(fields); i++ {
if fields[i].BindType == TAOS_FIELD_COL {
colType = append(colType, fields[i])
} else if fields[i].BindType == TAOS_FIELD_TAG {
tagType = append(tagType, fields[i])
}
}
// count
count := len(bindData)
if count == 0 {
Expand Down Expand Up @@ -215,7 +224,7 @@ func getBindDataHeaderLength(num int, needLength bool) int {
return length
}

func generateBindColData(data []driver.Value, colType *StmtField, tmpBuffer *bytes.Buffer) ([]byte, error) {
func generateBindColData(data []driver.Value, colType *Stmt2AllField, tmpBuffer *bytes.Buffer) ([]byte, error) {
num := len(data)
tmpBuffer.Reset()
needLength := needLength(colType.FieldType)
Expand Down Expand Up @@ -448,7 +457,7 @@ func checkAllNull(data []driver.Value) bool {
}

func generateBindQueryData(data driver.Value) ([]byte, error) {
var colType = 0
var colType uint32
var haveLength = false
var length = 0
var buf []byte
Expand Down Expand Up @@ -526,7 +535,7 @@ func generateBindQueryData(data driver.Value) ([]byte, error) {
totalLength := len(buf) + headerLength
dataBuf := make([]byte, totalLength)
// type
binary.LittleEndian.PutUint32(dataBuf[BindDataTypeOffset:], uint32(colType))
binary.LittleEndian.PutUint32(dataBuf[BindDataTypeOffset:], colType)
// num
binary.LittleEndian.PutUint32(dataBuf[BindDataNumOffset:], 1)
// is null
Expand Down Expand Up @@ -578,3 +587,12 @@ func needLength(colType int8) bool {
}
return false
}

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

0 comments on commit c5c3dac

Please sign in to comment.