bug fixed for update which includes json struct fields

This commit is contained in:
Lunny Xiao 2015-10-29 13:30:22 +08:00
parent a0e63df407
commit 8bf440560e
3 changed files with 26 additions and 14 deletions

View File

@ -1 +1 @@
xorm v0.4.4.1027 xorm v0.4.4.1029

View File

@ -332,23 +332,35 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
} else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok { } else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok {
val, _ = nulType.Value() val, _ = nulType.Value()
} else { } else {
engine.autoMapType(fieldValue) if !col.SQLType.IsJson() {
if table, ok := engine.Tables[fieldValue.Type()]; ok { engine.autoMapType(fieldValue)
if len(table.PrimaryKeys) == 1 { if table, ok := engine.Tables[fieldValue.Type()]; ok {
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) if len(table.PrimaryKeys) == 1 {
// fix non-int pk issues pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
//if pkField.Int() != 0 { // fix non-int pk issues
if pkField.IsValid() && !isZero(pkField.Interface()) { //if pkField.Int() != 0 {
val = pkField.Interface() if pkField.IsValid() && !isZero(pkField.Interface()) {
val = pkField.Interface()
} else {
continue
}
} else { } else {
continue //TODO: how to handler?
panic("not supported")
} }
} else { } else {
//TODO: how to handler? val = fieldValue.Interface()
panic("not supported")
} }
} else { } else {
val = fieldValue.Interface() 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
}
} }
} }
case reflect.Array, reflect.Slice, reflect.Map: case reflect.Array, reflect.Slice, reflect.Map:

View File

@ -17,7 +17,7 @@ import (
) )
const ( const (
Version string = "0.4.4.1027" Version string = "0.4.4.1029"
) )
func regDrvsNDialects() bool { func regDrvsNDialects() bool {