From 8bf440560e310e6093e9c4cb40ddf2a8216725e4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 29 Oct 2015 13:30:22 +0800 Subject: [PATCH] bug fixed for update which includes json struct fields --- VERSION | 2 +- statement.go | 36 ++++++++++++++++++++++++------------ xorm.go | 2 +- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/VERSION b/VERSION index 847eb09b..78f657d6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -xorm v0.4.4.1027 +xorm v0.4.4.1029 diff --git a/statement.go b/statement.go index 47d09320..627c0bd8 100644 --- a/statement.go +++ b/statement.go @@ -332,23 +332,35 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, } else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok { val, _ = nulType.Value() } else { - engine.autoMapType(fieldValue) - if table, ok := engine.Tables[fieldValue.Type()]; ok { - if len(table.PrimaryKeys) == 1 { - pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) - // fix non-int pk issues - //if pkField.Int() != 0 { - if pkField.IsValid() && !isZero(pkField.Interface()) { - val = pkField.Interface() + if !col.SQLType.IsJson() { + engine.autoMapType(fieldValue) + if table, ok := engine.Tables[fieldValue.Type()]; ok { + if len(table.PrimaryKeys) == 1 { + pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) + // fix non-int pk issues + //if pkField.Int() != 0 { + if pkField.IsValid() && !isZero(pkField.Interface()) { + val = pkField.Interface() + } else { + continue + } } else { - continue + //TODO: how to handler? + panic("not supported") } } else { - //TODO: how to handler? - panic("not supported") + val = fieldValue.Interface() } } 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: diff --git a/xorm.go b/xorm.go index c372e2a9..a03e6cc1 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.4.1027" + Version string = "0.4.4.1029" ) func regDrvsNDialects() bool {