Skip to content

Commit f13883a

Browse files
committed
check driver.Valuer response, and skip the column if nil (go-xorm#1167)
update tests Reviewed-on: https://gitea.com/xorm/xorm/pulls/1167
1 parent 67cf427 commit f13883a

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

internal/statements/statement.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
838838
continue
839839
} else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok {
840840
val, _ = valNul.Value()
841-
if val == nil {
841+
if val == nil && !requiredField {
842842
continue
843843
}
844844
} else {

internal/statements/update.go

+3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ func (statement *Statement) BuildUpdates(bean interface{},
186186
val = dialects.FormatColumnTime(statement.dialect, statement.defaultTimeZone, col, t)
187187
} else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok {
188188
val, _ = nulType.Value()
189+
if val == nil && !requiredField {
190+
continue
191+
}
189192
} else {
190193
if !col.SQLType.IsJson() {
191194
table, err := statement.tagParser.ParseWithCache(fieldValue)

types_null_test.go

+30-29
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type NullType struct {
2222
Age sql.NullInt64
2323
Height sql.NullFloat64
2424
IsMan sql.NullBool `xorm:"null"`
25+
Nil driver.Valuer
2526
CustomStruct CustomStruct `xorm:"varchar(64) null"`
2627
}
2728

@@ -72,42 +73,42 @@ func TestNullStructInsert(t *testing.T) {
7273
assert.NoError(t, prepareEngine())
7374
assertSync(t, new(NullType))
7475

75-
if true {
76-
item := new(NullType)
77-
_, err := testEngine.Insert(item)
78-
assert.NoError(t, err)
79-
assert.EqualValues(t, 1, item.Id)
76+
item1 := new(NullType)
77+
_, err := testEngine.Insert(item1)
78+
assert.NoError(t, err)
79+
assert.EqualValues(t, 1, item1.Id)
80+
81+
item := NullType{
82+
Name: sql.NullString{String: "haolei", Valid: true},
83+
Age: sql.NullInt64{Int64: 34, Valid: true},
84+
Height: sql.NullFloat64{Float64: 1.72, Valid: true},
85+
IsMan: sql.NullBool{Bool: true, Valid: true},
86+
Nil: nil,
8087
}
88+
_, err = testEngine.Insert(&item)
89+
assert.NoError(t, err)
90+
assert.EqualValues(t, 2, item.Id)
8191

82-
if true {
92+
items := []NullType{}
93+
for i := 0; i < 5; i++ {
8394
item := NullType{
84-
Name: sql.NullString{String: "haolei", Valid: true},
85-
Age: sql.NullInt64{Int64: 34, Valid: true},
86-
Height: sql.NullFloat64{Float64: 1.72, Valid: true},
87-
IsMan: sql.NullBool{Bool: true, Valid: true},
95+
Name: sql.NullString{String: "haolei_" + fmt.Sprint(i+1), Valid: true},
96+
Age: sql.NullInt64{Int64: 30 + int64(i), Valid: true},
97+
Height: sql.NullFloat64{Float64: 1.5 + 1.1*float64(i), Valid: true},
98+
IsMan: sql.NullBool{Bool: true, Valid: true},
99+
CustomStruct: CustomStruct{i, i + 1, i + 2},
100+
Nil: nil,
88101
}
89-
_, err := testEngine.Insert(&item)
90-
assert.NoError(t, err)
91-
assert.EqualValues(t, 2, item.Id)
102+
items = append(items, item)
92103
}
93104

94-
if true {
95-
items := []NullType{}
96-
for i := 0; i < 5; i++ {
97-
item := NullType{
98-
Name: sql.NullString{String: "haolei_" + fmt.Sprint(i+1), Valid: true},
99-
Age: sql.NullInt64{Int64: 30 + int64(i), Valid: true},
100-
Height: sql.NullFloat64{Float64: 1.5 + 1.1*float64(i), Valid: true},
101-
IsMan: sql.NullBool{Bool: true, Valid: true},
102-
CustomStruct: CustomStruct{i, i + 1, i + 2},
103-
}
104-
105-
items = append(items, item)
106-
}
105+
_, err = testEngine.Insert(&items)
106+
assert.NoError(t, err)
107107

108-
_, err := testEngine.Insert(&items)
109-
assert.NoError(t, err)
110-
}
108+
items = make([]NullType, 0, 7)
109+
err = testEngine.Find(&items)
110+
assert.NoError(t, err)
111+
assert.EqualValues(t, 7, len(items))
111112
}
112113

113114
func TestNullStructUpdate(t *testing.T) {

0 commit comments

Comments
 (0)