check driver.Valuer response, and skip the column if nil #1167

Merged
koron merged 1 commits from koron/valuer-update-by-nil-always into master 2020-03-10 03:01:36 +00:00
3 changed files with 35 additions and 31 deletions

View File

@ -838,7 +838,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
continue continue
} else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok { } else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok {
val, _ = valNul.Value() val, _ = valNul.Value()
if val == nil { if val == nil && !requiredField {
continue continue
} }
} else { } else {

View File

@ -186,6 +186,9 @@ func (statement *Statement) BuildUpdates(bean interface{},
val = dialects.FormatColumnTime(statement.dialect, statement.defaultTimeZone, col, t) val = dialects.FormatColumnTime(statement.dialect, statement.defaultTimeZone, col, t)
} else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok { } else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok {
val, _ = nulType.Value() val, _ = nulType.Value()
if val == nil && !requiredField {
continue
}
} else { } else {
if !col.SQLType.IsJson() { if !col.SQLType.IsJson() {
table, err := statement.tagParser.ParseWithCache(fieldValue) table, err := statement.tagParser.ParseWithCache(fieldValue)

View File

@ -22,6 +22,7 @@ type NullType struct {
Age sql.NullInt64 Age sql.NullInt64
Height sql.NullFloat64 Height sql.NullFloat64
IsMan sql.NullBool `xorm:"null"` IsMan sql.NullBool `xorm:"null"`
Nil driver.Valuer
CustomStruct CustomStruct `xorm:"varchar(64) null"` CustomStruct CustomStruct `xorm:"varchar(64) null"`
} }
@ -72,42 +73,42 @@ func TestNullStructInsert(t *testing.T) {
assert.NoError(t, prepareEngine()) assert.NoError(t, prepareEngine())
assertSync(t, new(NullType)) assertSync(t, new(NullType))
if true { item1 := new(NullType)
item := new(NullType) _, err := testEngine.Insert(item1)
_, err := testEngine.Insert(item) assert.NoError(t, err)
assert.NoError(t, err) assert.EqualValues(t, 1, item1.Id)
assert.EqualValues(t, 1, item.Id)
}
if true { item := NullType{
Name: sql.NullString{String: "haolei", Valid: true},
Age: sql.NullInt64{Int64: 34, Valid: true},
Height: sql.NullFloat64{Float64: 1.72, Valid: true},
IsMan: sql.NullBool{Bool: true, Valid: true},
Nil: nil,
}
_, err = testEngine.Insert(&item)
assert.NoError(t, err)
assert.EqualValues(t, 2, item.Id)
items := []NullType{}
for i := 0; i < 5; i++ {
item := NullType{ item := NullType{
Name: sql.NullString{String: "haolei", Valid: true}, Name: sql.NullString{String: "haolei_" + fmt.Sprint(i+1), Valid: true},
Age: sql.NullInt64{Int64: 34, Valid: true}, Age: sql.NullInt64{Int64: 30 + int64(i), Valid: true},
Height: sql.NullFloat64{Float64: 1.72, Valid: true}, Height: sql.NullFloat64{Float64: 1.5 + 1.1*float64(i), Valid: true},
IsMan: sql.NullBool{Bool: true, Valid: true}, IsMan: sql.NullBool{Bool: true, Valid: true},
CustomStruct: CustomStruct{i, i + 1, i + 2},
Nil: nil,
} }
_, err := testEngine.Insert(&item) items = append(items, item)
assert.NoError(t, err)
assert.EqualValues(t, 2, item.Id)
} }
if true { _, err = testEngine.Insert(&items)
items := []NullType{} assert.NoError(t, err)
for i := 0; i < 5; i++ {
item := NullType{
Name: sql.NullString{String: "haolei_" + fmt.Sprint(i+1), Valid: true},
Age: sql.NullInt64{Int64: 30 + int64(i), Valid: true},
Height: sql.NullFloat64{Float64: 1.5 + 1.1*float64(i), Valid: true},
IsMan: sql.NullBool{Bool: true, Valid: true},
CustomStruct: CustomStruct{i, i + 1, i + 2},
}
items = append(items, item) items = make([]NullType, 0, 7)
} err = testEngine.Find(&items)
assert.NoError(t, err)
_, err := testEngine.Insert(&items) assert.EqualValues(t, 7, len(items))
assert.NoError(t, err)
}
} }
func TestNullStructUpdate(t *testing.T) { func TestNullStructUpdate(t *testing.T) {