tdengine-mapper-go/README.md

77 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# tdengine-mapper-go
> Golang 的 struct 与 TDengine 表进行映射
### struct 标签
```
db : 表的列名, 值为 "" 或 "-" 表示忽略
taos : taos的标签目前只有 [tag], 表示当前是 超级表的 TAG 字段
```
### struct 接口
```go
type SuperTableNamer interface {
SuperTableName() string // 获取超级表的表名 可选
}
type TableNamer interface {
TableName() string // 当前表的表名
}
```
`TableNamer` 是必须要实现的接口
`SuperTableName` 是可选实现的,如果没有 实现,就会当普通表进行处理
### 示例
```go
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)
}
------------------------------------------------------
INSERT INTO
`user_张三` USING `super_user` (`name`) TAGS ('张三') (`age`) VALUES (18)
`user_李四` USING `super_user` (`name`) TAGS ('李四') (`age`) VALUES (20)
```