Compare commits
No commits in common. "master" and "v0.0.9" have entirely different histories.
49
mapping.go
49
mapping.go
@ -340,52 +340,3 @@ func (b *Mapper) scanRow(target reflect.Value, rows *sql.Rows) error {
|
|||||||
// 扫描数据
|
// 扫描数据
|
||||||
return rows.Scan(dest...)
|
return rows.Scan(dest...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScanRowsToMap 扫描多行数据到map
|
|
||||||
// @param rows 数据库返回的行数据
|
|
||||||
// @return 返回map数组和错误
|
|
||||||
func (b *Mapper) ScanRowsToMap(rows *sql.Rows) ([]map[string]any, error) {
|
|
||||||
return b.ScanRowsToMapWithContext(context.Background(), rows)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ScanRowsToMapWithContext 扫描多行数据到map
|
|
||||||
// @param ctx 上下文
|
|
||||||
// @param rows 数据库返回的行数据
|
|
||||||
// @return 返回map数组和错误
|
|
||||||
func (b *Mapper) ScanRowsToMapWithContext(ctx context.Context, rows *sql.Rows) ([]map[string]any, error) {
|
|
||||||
columns, err := rows.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
result := make([]map[string]any, 0)
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
// 检查上下文是否已取消
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return nil, ctx.Err()
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建一个切片来存储列的值
|
|
||||||
values := make([]any, len(columns))
|
|
||||||
for i := range values {
|
|
||||||
values[i] = new(any)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 扫描行数据到切片中
|
|
||||||
if err := rows.Scan(values...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建 结果映射
|
|
||||||
rowMap := make(map[string]any, len(columns))
|
|
||||||
for i := range values {
|
|
||||||
column := columns[i]
|
|
||||||
value := *(values[i].(*any))
|
|
||||||
rowMap[column] = value
|
|
||||||
}
|
|
||||||
result = append(result, rowMap)
|
|
||||||
}
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
//_ "github.com/taosdata/driver-go/v3/taosWS"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -196,47 +195,3 @@ func TestScanRows(t *testing.T) {
|
|||||||
//marshal, _ := json.MarshalIndent(sdArray, "", " ")
|
//marshal, _ := json.MarshalIndent(sdArray, "", " ")
|
||||||
//fmt.Println(len(sdArray), string(marshal))
|
//fmt.Println(len(sdArray), string(marshal))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestScanRowsToMap(t *testing.T) {
|
|
||||||
/*
|
|
||||||
文档参考: https://docs.taosdata.com/reference/connector/go/#websocket-%E8%BF%9E%E6%8E%A5
|
|
||||||
|
|
||||||
go get github.com/taosdata/driver-go/v3
|
|
||||||
|
|
||||||
import _ "github.com/taosdata/driver-go/v3/taosWS"
|
|
||||||
|
|
||||||
超级表创建
|
|
||||||
CREATE STABLE `super_dev` (`ts` TIMESTAMP , `app_sn` VARCHAR(500) , `ct` INT ) TAGS (`dev_id` VARCHAR(50), `dev_type` VARCHAR(50))
|
|
||||||
|
|
||||||
批量插入
|
|
||||||
INSERT INTO
|
|
||||||
`dev_SN001` USING `super_dev` (`dev_id`,`dev_type`) TAGS ('SN001','模拟设备') (`ts`,`app_sn`,`ct`)
|
|
||||||
VALUES ('2024-09-18T16:22:17+08:00','a0001',1),('2024-09-18T16:22:18+08:00','a0002',2)
|
|
||||||
`dev_SN002` USING `super_dev` (`dev_id`,`dev_type`) TAGS ('SN002','模拟设备') (`ts`,`app_sn`,`ct`)
|
|
||||||
VALUES ('2024-09-18T16:22:17+08:00','a0003',3)
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
var taosUri = "root:taosdata@localhost:6041/test"
|
|
||||||
db, err := sql.Open("taosWS", taosUri)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err := db.Query("select * from super_dev order by ts desc limit 100")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer func() { _ = rows.Close() }()
|
|
||||||
|
|
||||||
tdMapper := NewMapper()
|
|
||||||
|
|
||||||
toMap, err := tdMapper.ScanRowsToMap(rows)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
indent, _ := json.MarshalIndent(toMap, "", " ")
|
|
||||||
fmt.Println(string(indent))
|
|
||||||
fmt.Println("len:", len(toMap))
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user