Browse Source

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

update tests

Reviewed-on: #1167
tags/v1.0.0
Lunny Xiao 3 weeks ago
parent
commit
f13883a725
3 changed files with 34 additions and 30 deletions
  1. +1
    -1
      internal/statements/statement.go
  2. +3
    -0
      internal/statements/update.go
  3. +30
    -29
      types_null_test.go

+ 1
- 1
internal/statements/statement.go View File

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


+ 3
- 0
internal/statements/update.go View File

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


+ 30
- 29
types_null_test.go View File

@@ -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"`
}

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


Loading…
Cancel
Save