Add support custom type Nullfloat64 #1450
|
@ -84,6 +84,10 @@ func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.T
|
|||
return session.str2Time(col, string(data))
|
||||
}
|
||||
|
||||
var (
|
||||
nullFloatType = reflect.TypeOf(sql.NullFloat64{})
|
||||
)
|
||||
|
||||
// convert a db data([]byte) to a field value
|
||||
func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, data []byte) error {
|
||||
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)
|
||||
tf := session.engine.formatColTime(col, t)
|
||||
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() {
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -21,3 +22,24 @@ func TestClose(t *testing.T) {
|
|||
sess2.Close()
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user