diff --git a/VERSION b/VERSION index ed14c179..7038453c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -xorm v0.5.4.0422 +xorm v0.5.4.0427 diff --git a/helpers.go b/helpers.go index 946153d9..617e8d7a 100644 --- a/helpers.go +++ b/helpers.go @@ -147,6 +147,26 @@ func isZero(k interface{}) bool { 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 { var v interface{} switch tp.Kind() { diff --git a/statement.go b/statement.go index 66151ebe..3dd5c60c 100644 --- a/statement.go +++ b/statement.go @@ -352,7 +352,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, if len(table.PrimaryKeys) == 1 { pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) // fix non-int pk issues - if pkField.IsValid() && !isZero(pkField.Interface()) { + if pkField.IsValid() && (!requiredField && !isZero(pkField.Interface())) { val = pkField.Interface() } else { continue @@ -365,14 +365,19 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, val = fieldValue.Interface() } } else { - bytes, err := json.Marshal(fieldValue.Interface()) - if err != nil { - panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface())) - } - if col.SQLType.IsText() { - val = string(bytes) - } else if col.SQLType.IsBlob() { - val = bytes + // Blank struct could not be as update data + if requiredField || !isStructZero(fieldValue) { + bytes, err := json.Marshal(fieldValue.Interface()) + if err != nil { + panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface())) + } + if col.SQLType.IsText() { + val = string(bytes) + } else if col.SQLType.IsBlob() { + val = bytes + } + } else { + continue } } } diff --git a/xorm.go b/xorm.go index 2235472d..0fdae547 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( const ( // Version show the xorm's version - Version string = "0.5.4.0422" + Version string = "0.5.4.0427" ) func regDrvsNDialects() bool {