bug fixed for update empty slice, map

This commit is contained in:
Lunny Xiao 2015-12-02 14:54:58 +08:00
parent b36f8ed87e
commit aac04b323f
1 changed files with 8 additions and 11 deletions

View File

@ -182,7 +182,7 @@ func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
return statement return statement
} }
// Auto generating conditions according a struct // Auto generating update columnes and values according a struct
func buildUpdates(engine *Engine, table *core.Table, bean interface{}, func buildUpdates(engine *Engine, table *core.Table, bean interface{},
includeVersion bool, includeUpdated bool, includeNil bool, includeVersion bool, includeUpdated bool, includeNil bool,
includeAutoIncr bool, allUseBool bool, useAllCols bool, includeAutoIncr bool, allUseBool bool, useAllCols bool,
@ -211,10 +211,6 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
continue continue
} }
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
continue
}
fieldValuePtr, err := col.ValueOf(bean) fieldValuePtr, err := col.ValueOf(bean)
if err != nil { if err != nil {
engine.LogError(err) engine.LogError(err)
@ -338,7 +334,6 @@ 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.Int() != 0 {
if pkField.IsValid() && !isZero(pkField.Interface()) { if pkField.IsValid() && !isZero(pkField.Interface()) {
val = pkField.Interface() val = pkField.Interface()
} else { } else {
@ -364,12 +359,14 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
} }
} }
case reflect.Array, reflect.Slice, reflect.Map: case reflect.Array, reflect.Slice, reflect.Map:
if !requiredField {
if fieldValue == reflect.Zero(fieldType) { if fieldValue == reflect.Zero(fieldType) {
continue continue
} }
if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 { if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 {
continue continue
} }
}
if col.SQLType.IsText() { if col.SQLType.IsText() {
bytes, err := json.Marshal(fieldValue.Interface()) bytes, err := json.Marshal(fieldValue.Interface())