Browse Source

add support custom type Nullfloat64 (#1450)

tags/v0.8.0
Lunny Xiao GitHub 2 months ago
parent
commit
33fc33b2f5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions
  1. +10
    -0
      session_convert.go
  2. +22
    -0
      session_test.go

+ 10
- 0
session_convert.go View File

@@ -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() {


+ 22
- 0
session_test.go View File

@@ -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…
Cancel
Save