bug fixed for empty struct when update

This commit is contained in:
Lunny Xiao 2016-04-27 11:31:02 +08:00
parent 5c116a34d6
commit 56d649f413
4 changed files with 36 additions and 11 deletions

View File

@ -1 +1 @@
xorm v0.5.4.0422 xorm v0.5.4.0427

View File

@ -147,6 +147,26 @@ func isZero(k interface{}) bool {
return false return false
} }
func isStructZero(v reflect.Value) bool {
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
switch field.Kind() {
case reflect.Ptr:
field = field.Elem()
fallthrough
case reflect.Struct:
if !isStructZero(field) {
return false
}
default:
if !isZero(field.Interface()) {
return false
}
}
}
return true
}
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value { func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
var v interface{} var v interface{}
switch tp.Kind() { switch tp.Kind() {

View File

@ -352,7 +352,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
if len(table.PrimaryKeys) == 1 { if len(table.PrimaryKeys) == 1 {
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
// fix non-int pk issues // fix non-int pk issues
if pkField.IsValid() && !isZero(pkField.Interface()) { if pkField.IsValid() && (!requiredField && !isZero(pkField.Interface())) {
val = pkField.Interface() val = pkField.Interface()
} else { } else {
continue continue
@ -365,6 +365,8 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
val = fieldValue.Interface() val = fieldValue.Interface()
} }
} else { } else {
// Blank struct could not be as update data
if requiredField || !isStructZero(fieldValue) {
bytes, err := json.Marshal(fieldValue.Interface()) bytes, err := json.Marshal(fieldValue.Interface())
if err != nil { if err != nil {
panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface())) panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface()))
@ -374,6 +376,9 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
} else if col.SQLType.IsBlob() { } else if col.SQLType.IsBlob() {
val = bytes val = bytes
} }
} else {
continue
}
} }
} }
case reflect.Array, reflect.Slice, reflect.Map: case reflect.Array, reflect.Slice, reflect.Map:

View File

@ -17,7 +17,7 @@ import (
const ( const (
// Version show the xorm's version // Version show the xorm's version
Version string = "0.5.4.0422" Version string = "0.5.4.0427"
) )
func regDrvsNDialects() bool { func regDrvsNDialects() bool {