Skip to content

Commit

Permalink
check driver.Valuer response, and skip the column if nil (go-xorm#1167)
Browse files Browse the repository at this point in the history
update tests

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1167
  • Loading branch information
lunny committed Mar 10, 2020
1 parent 67cf427 commit f13883a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/statements/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
continue
} else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok {
val, _ = valNul.Value()
if val == nil {
if val == nil && !requiredField {
continue
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions internal/statements/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ func (statement *Statement) BuildUpdates(bean interface{},
val = dialects.FormatColumnTime(statement.dialect, statement.defaultTimeZone, col, t)
} else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok {
val, _ = nulType.Value()
if val == nil && !requiredField {
continue
}
} else {
if !col.SQLType.IsJson() {
table, err := statement.tagParser.ParseWithCache(fieldValue)
Expand Down
59 changes: 30 additions & 29 deletions types_null_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type NullType struct {
Age sql.NullInt64
Height sql.NullFloat64
IsMan sql.NullBool `xorm:"null"`
Nil driver.Valuer
CustomStruct CustomStruct `xorm:"varchar(64) null"`
}

Expand Down Expand Up @@ -72,42 +73,42 @@ func TestNullStructInsert(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(NullType))

if true {
item := new(NullType)
_, err := testEngine.Insert(item)
assert.NoError(t, err)
assert.EqualValues(t, 1, item.Id)
item1 := new(NullType)
_, err := testEngine.Insert(item1)
assert.NoError(t, err)
assert.EqualValues(t, 1, item1.Id)

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)

if true {
items := []NullType{}
for i := 0; i < 5; i++ {
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},
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},
Nil: nil,
}
_, err := testEngine.Insert(&item)
assert.NoError(t, err)
assert.EqualValues(t, 2, item.Id)
items = append(items, item)
}

if true {
items := []NullType{}
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)
}
_, err = testEngine.Insert(&items)
assert.NoError(t, err)

_, err := testEngine.Insert(&items)
assert.NoError(t, err)
}
items = make([]NullType, 0, 7)
err = testEngine.Find(&items)
assert.NoError(t, err)
assert.EqualValues(t, 7, len(items))
}

func TestNullStructUpdate(t *testing.T) {
Expand Down

0 comments on commit f13883a

Please sign in to comment.