[fix] 处理查询结果字段类型为 slice,struct时报错

This commit is contained in:
2023-06-21 10:03:51 +08:00
parent cd33e09d69
commit bf3032103e
2 changed files with 105 additions and 22 deletions
+36 -9
View File
@@ -4,11 +4,11 @@ import (
"testing"
)
var oDB *DB
var myConn *Connection
func init() {
oDB = Open(map[string]DBConfig{
"default": {
database := Open(map[string]DBConfig{
"mysql": {
Driver: "mysql",
Host: "localhost",
Port: "3366",
@@ -20,12 +20,14 @@ func init() {
// ParseTime: true,
},
})
myConn = database.Connection("mysql")
}
func TestScanMapSlice(t *testing.T) {
dest := []map[string]any{}
oDB.Select("select * from fsm", []any{}, &dest)
myConn.Select("select * from fsm", []any{}, &dest)
t.Log(dest)
}
@@ -34,7 +36,7 @@ func TestScanValues(t *testing.T) {
dest := []int{}
oDB.Select("select id from fsm", []any{}, &dest)
myConn.Select("select 1 as id", []any{}, &dest)
t.Log(dest)
}
@@ -43,19 +45,31 @@ func TestScanMap(t *testing.T) {
dest := map[string]any{}
oDB.Select("select * from fsm where id = 3", []any{}, &dest)
myConn.Select("select 0 as id, 'test' as code", []any{}, &dest)
t.Log(dest)
}
func TestScanStruct(t *testing.T) {
dest := struct {
Id int `db:"id"`
Code string `db:"code"`
}{}
oDB.Select("select * from fsm", []any{}, &dest)
myConn.Select(`select 0 as id, 'test' as code`, []any{}, &dest)
t.Log(dest)
}
func TestScanJsonFieldStruct(t *testing.T) {
dest := struct {
Id int `db:"id"`
Code string `db:"code"`
Map map[string]any `db:"map"`
}{}
myConn.Select(`select 0 as id, 'test' as code, '{"a":1}' as map`, []any{}, &dest)
t.Log(dest)
}
@@ -67,7 +81,20 @@ func TestScanStructSlice(t *testing.T) {
Code string `db:"code"`
}{}
oDB.Select("select * from fsm", []any{}, &dest)
myConn.Select("select * from ((select 0 as id, 'test' as code) union (select 1 as id, 'test1' as code)) as t", []any{}, &dest)
t.Log(dest)
}
func TestScanJsonFieldStructSlice(t *testing.T) {
dest := []struct {
Id int `db:"id"`
Code string `db:"code"`
Map map[string]any `db:"map"`
}{}
myConn.Select(`select * from ((select 0 as id, 'test' as code, '{"a":1}' as map) union (select 1 as id, 'test1' as code, '{"a":2}' as map)) as t`, []any{}, &dest)
t.Log(dest)
}