add IsZero check for custom struct column in the Update method (#2417)
Co-authored-by: tyler <tyler@mbp.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/2417 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: tylerthail2019 <tylerthail2019@noreply.gitea.com> Co-committed-by: tylerthail2019 <tylerthail2019@noreply.gitea.com>
This commit is contained in:
parent
be77a7084b
commit
34e62e9b4c
|
@ -126,6 +126,9 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value,
|
||||||
|
|
||||||
if fieldValue.CanAddr() {
|
if fieldValue.CanAddr() {
|
||||||
if structConvert, ok := fieldValue.Addr().Interface().(convert.Conversion); ok {
|
if structConvert, ok := fieldValue.Addr().Interface().(convert.Conversion); ok {
|
||||||
|
if utils.IsZero(fieldValue.Interface()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
data, err := structConvert.ToDB()
|
data, err := structConvert.ToDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
|
@ -619,3 +619,75 @@ func TestMyArray(t *testing.T) {
|
||||||
assert.True(t, has)
|
assert.True(t, has)
|
||||||
assert.EqualValues(t, v, m.Content)
|
assert.EqualValues(t, v, m.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ZDecimal struct {
|
||||||
|
value *big.Int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ZDecimal) FromDB(data []byte) error {
|
||||||
|
i, _ := strconv.ParseInt(string(data), 10, 64)
|
||||||
|
*d = ZDecimal{
|
||||||
|
value: big.NewInt(i),
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d ZDecimal) ToDB() ([]byte, error) {
|
||||||
|
return []byte(fmt.Sprintf("%d", (d.value).Int64())), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d ZDecimal) IsZero() bool {
|
||||||
|
if d.value == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return d.value.Sign() == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d ZDecimal) String() string {
|
||||||
|
if d.value == nil {
|
||||||
|
return "0"
|
||||||
|
}
|
||||||
|
return d.value.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestZDecimal(t *testing.T) {
|
||||||
|
type ZMyMoney struct {
|
||||||
|
Id int64
|
||||||
|
Account string
|
||||||
|
Amount ZDecimal
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
assertSync(t, new(ZMyMoney))
|
||||||
|
|
||||||
|
_, err := testEngine.Insert(&ZMyMoney{
|
||||||
|
Account: "test",
|
||||||
|
Amount: ZDecimal{
|
||||||
|
value: big.NewInt(10000000000000000),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
m := ZMyMoney{
|
||||||
|
Id: 1,
|
||||||
|
}
|
||||||
|
has, err := testEngine.Get(&m)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
|
||||||
|
_, err = testEngine.Update(&ZMyMoney{
|
||||||
|
Id: 1,
|
||||||
|
Account: "test2",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
m2 := ZMyMoney{
|
||||||
|
Id: 1,
|
||||||
|
}
|
||||||
|
has, err = testEngine.Get(&m2)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.Equal(t, "test2", "test2")
|
||||||
|
assert.False(t, m.Amount.IsZero())
|
||||||
|
assert.Equal(t, "10000000000000000", m.Amount.String())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue