tdengine-mapper-go/mapping_test.go

127 lines
2.6 KiB
Go
Raw Permalink Normal View History

package td_builder
import (
"database/sql"
"fmt"
"testing"
"time"
)
type TaosTAG struct {
DevId string `db:"dev_id" taos:"tag"`
DevType string `db:"dev_type" taos:"tag"`
DataType string `db:"data_type" taos:"tag"`
Alias string `db:"alias"`
}
type TaosDevice struct {
*TaosTAG
Ts time.Time `db:"ts"` // 时间戳
Ts2 *time.Time `db:"ts2"` // 时间戳
LoadUnitId string `db:"load_unit_id" taos:"tag"` // 负载单体 id
NullInt *int `db:"null_int"`
DefString string `db:"def_string"`
PInt *int `db:"p_int"`
NullInt64 sql.NullInt64 `db:"null_int64"`
NullInt32 sql.NullInt32 `db:"null_int32"`
}
func (s *TaosDevice) SuperTableName() string {
return "super_device"
}
func (s *TaosDevice) TableName() string {
return "device_" + s.DevId
}
type TaosUser struct {
*TaosTAG
Name string `db:"name" taos:"tag"`
Ts time.Time `db:"ts"` // 时间戳
// 体重
Weight int `db:"weight"`
}
func (s *TaosUser) SuperTableName() string {
return "super_user"
}
func (s *TaosUser) TableName() string {
return "user_" + s.DevId
}
func TestBuilderInsert(t *testing.T) {
tdMapping := NewTdMapping()
p := 1
data := []any{
&TaosDevice{
TaosTAG: &TaosTAG{
DevId: "设备ID",
DevType: "测试设备",
DataType: "测试数据",
},
Ts: time.Now(),
LoadUnitId: "负载单体ID",
PInt: &p,
NullInt32: sql.NullInt32{Int32: 32, Valid: true},
},
&TaosUser{
TaosTAG: &TaosTAG{
DevId: "User001",
DevType: "User类型",
DataType: "User数据类型001",
},
Ts: time.Now(),
Name: "张三",
}, &TaosUser{
TaosTAG: &TaosTAG{
DevId: "User002",
DevType: "User类型",
DataType: "User数据类型002",
},
Ts: time.Now(),
Name: "李四",
Weight: 110,
}, &TaosUser{
TaosTAG: &TaosTAG{
DevId: "User002",
DevType: "User类型",
DataType: "User数据类型002",
},
Name: "李四",
Ts: time.Now(),
Weight: 100,
},
}
insertSql, err := tdMapping.ToInsertSQL(data...)
if err != nil {
t.Fatal(err)
}
fmt.Println(insertSql)
}
type User struct {
Name string `db:"name" taos:"tag"`
Age int `db:"age"`
}
func (u *User) TableName() string {
return "user_" + u.Name
}
func (u *User) SuperTableName() string {
return "super_user"
}
func TestSimpleInsert(t *testing.T) {
tdMapping := NewTdMapping()
data := []any{
&User{Name: "张三", Age: 18},
&User{Name: "李四", Age: 20},
}
insertSql, err := tdMapping.ToInsertSQL(data...)
if err != nil {
t.Fatal(err)
}
fmt.Println(insertSql)
}