Refactor Model with slice data

This commit is contained in:
Jinzhu 2022-07-07 15:39:29 +08:00
parent 9fd73ae4f1
commit b13d1757fa
1 changed files with 11 additions and 11 deletions

View File

@ -158,21 +158,21 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
switch stmt.ReflectValue.Kind() { switch stmt.ReflectValue.Kind() {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
if size := stmt.ReflectValue.Len(); size > 0 { if size := stmt.ReflectValue.Len(); size > 0 {
var primaryKeyExprs []clause.Expression var isZero bool
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
exprs := make([]clause.Expression, len(stmt.Schema.PrimaryFields)) for _, field := range stmt.Schema.PrimaryFields {
var notZero bool _, isZero = field.ValueOf(stmt.Context, stmt.ReflectValue.Index(i))
for idx, field := range stmt.Schema.PrimaryFields { if !isZero {
value, isZero := field.ValueOf(stmt.Context, stmt.ReflectValue.Index(i)) break
exprs[idx] = clause.Eq{Column: field.DBName, Value: value} }
notZero = notZero || !isZero
}
if notZero {
primaryKeyExprs = append(primaryKeyExprs, clause.And(exprs...))
} }
} }
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.And(clause.Or(primaryKeyExprs...))}}) if !isZero {
_, primaryValues := schema.GetIdentityFieldValuesMap(stmt.Context, stmt.ReflectValue, stmt.Schema.PrimaryFields)
column, values := schema.ToQueryValues("", stmt.Schema.PrimaryFieldDBNames, primaryValues)
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
}
} }
case reflect.Struct: case reflect.Struct:
for _, field := range stmt.Schema.PrimaryFields { for _, field := range stmt.Schema.PrimaryFields {