add support custom type Nullfloat64 (#1450)

This commit is contained in:
Lunny Xiao 2019-10-05 21:10:55 +08:00 committed by GitHub
parent 57a49c6c23
commit 33fc33b2f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -84,6 +84,10 @@ func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.T
return session.str2Time(col, string(data)) return session.str2Time(col, string(data))
} }
var (
nullFloatType = reflect.TypeOf(sql.NullFloat64{})
)
// convert a db data([]byte) to a field value // convert a db data([]byte) to a field value
func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, data []byte) error { func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, data []byte) error {
if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok { if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
@ -583,6 +587,12 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
t := fieldValue.Convert(core.TimeType).Interface().(time.Time) t := fieldValue.Convert(core.TimeType).Interface().(time.Time)
tf := session.engine.formatColTime(col, t) tf := session.engine.formatColTime(col, t)
return tf, nil return tf, nil
} else if fieldType.ConvertibleTo(nullFloatType) {
t := fieldValue.Convert(nullFloatType).Interface().(sql.NullFloat64)
if !t.Valid {
return nil, nil
}
return t.Float64, nil
} }
if !col.SQLType.IsJson() { if !col.SQLType.IsJson() {

View File

@ -5,6 +5,7 @@
package xorm package xorm
import ( import (
"database/sql"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -21,3 +22,24 @@ func TestClose(t *testing.T) {
sess2.Close() sess2.Close()
assert.True(t, sess2.IsClosed()) assert.True(t, sess2.IsClosed())
} }
func TestNullFloatStruct(t *testing.T) {
type MyNullFloat64 sql.NullFloat64
type MyNullFloatStruct struct {
Uuid string
Amount MyNullFloat64
}
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.Sync2(new(MyNullFloatStruct)))
_, err := testEngine.Insert(&MyNullFloatStruct{
Uuid: "111111",
Amount: MyNullFloat64(sql.NullFloat64{
Float64: 0.1111,
Valid: true,
}),
})
assert.NoError(t, err)
}