[fix] 修复 converting NULL to string is unsupported 错误
This commit is contained in:
parent
28d9dcbd4d
commit
5740d11240
35
scan.go
35
scan.go
@ -4,7 +4,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -115,6 +114,9 @@ func sStructSlice(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
case reflect.Slice, reflect.Struct, reflect.Map:
|
case reflect.Slice, reflect.Struct, reflect.Map:
|
||||||
scanArgs[i] = &[]uint8{}
|
scanArgs[i] = &[]uint8{}
|
||||||
jFields[i] = v.Field(f)
|
jFields[i] = v.Field(f)
|
||||||
|
case reflect.String:
|
||||||
|
scanArgs[i] = &sql.NullString{}
|
||||||
|
jFields[i] = v.Field(f)
|
||||||
default:
|
default:
|
||||||
scanArgs[i] = v.Field(f).Addr().Interface()
|
scanArgs[i] = v.Field(f).Addr().Interface()
|
||||||
}
|
}
|
||||||
@ -129,17 +131,18 @@ func sStructSlice(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
|
|
||||||
// json 字符串 反序列化
|
// json 字符串 反序列化
|
||||||
for i, rv := range jFields {
|
for i, rv := range jFields {
|
||||||
|
switch rv.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
rv.SetString(scanArgs[i].(*sql.NullString).String)
|
||||||
|
default:
|
||||||
|
if data := *scanArgs[i].(*[]uint8); len(data) > 0 {
|
||||||
v := rv.Addr().Interface()
|
v := rv.Addr().Interface()
|
||||||
data := *scanArgs[i].(*[]uint8)
|
|
||||||
|
|
||||||
if len(data) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(data, &v); err != nil {
|
if err := json.Unmarshal(data, &v); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if itemIsPtr {
|
if itemIsPtr {
|
||||||
realDest.Set(reflect.Append(realDest, vp))
|
realDest.Set(reflect.Append(realDest, vp))
|
||||||
@ -185,8 +188,9 @@ func sStruct(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
case reflect.Slice, reflect.Struct, reflect.Map:
|
case reflect.Slice, reflect.Struct, reflect.Map:
|
||||||
scanArgs[i] = &[]uint8{}
|
scanArgs[i] = &[]uint8{}
|
||||||
jFields[i] = v.Field(f)
|
jFields[i] = v.Field(f)
|
||||||
|
case reflect.String:
|
||||||
fmt.Printf("123 %+#v \n", v.Field(f))
|
scanArgs[i] = &sql.NullString{}
|
||||||
|
jFields[i] = v.Field(f)
|
||||||
default:
|
default:
|
||||||
scanArgs[i] = v.Field(f).Addr().Interface()
|
scanArgs[i] = v.Field(f).Addr().Interface()
|
||||||
}
|
}
|
||||||
@ -201,17 +205,18 @@ func sStruct(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
|
|
||||||
// json 字符串 反序列化
|
// json 字符串 反序列化
|
||||||
for i, rv := range jFields {
|
for i, rv := range jFields {
|
||||||
|
switch rv.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
rv.SetString(scanArgs[i].(*sql.NullString).String)
|
||||||
|
default:
|
||||||
|
if data := *scanArgs[i].(*[]uint8); len(data) > 0 {
|
||||||
v := rv.Addr().Interface()
|
v := rv.Addr().Interface()
|
||||||
data := *scanArgs[i].(*[]uint8)
|
|
||||||
|
|
||||||
if len(data) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(data, &v); err != nil {
|
if err := json.Unmarshal(data, &v); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if realDest.Kind() == reflect.Ptr {
|
if realDest.Kind() == reflect.Ptr {
|
||||||
realDest.Set(vp)
|
realDest.Set(vp)
|
||||||
|
@ -70,7 +70,7 @@ func TestScanJsonFieldStruct(t *testing.T) {
|
|||||||
Arr []int64 `db:"arr"`
|
Arr []int64 `db:"arr"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
myConn.Select(`select 0 as id, 'test' as code, '{"a":1}' as map, '[3, 4]' as arr`, []any{}, &dest)
|
myConn.Select(`select 0 as id, NULL as code, '{"a":1}' as map, '[3, 4]' as arr`, []any{}, &dest)
|
||||||
|
|
||||||
t.Log(dest)
|
t.Log(dest)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user