2021-02-05 16:32:08 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Save updates an entity in the database, or inserts if it doesn't exist.
|
|
|
|
func Save(m interface{}, primaryKeys ...string) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("save: %s (panic)", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := Update(m, primaryKeys...); err == nil {
|
|
|
|
return nil
|
|
|
|
} else if err := UnscopedDb().Save(m).Error; err == nil {
|
|
|
|
return nil
|
|
|
|
} else if !strings.Contains(strings.ToLower(err.Error()), "lock") {
|
|
|
|
return err
|
|
|
|
} else if err := UnscopedDb().Save(m).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates an existing entity in the database.
|
|
|
|
func Update(m interface{}, primaryKeys ...string) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("update: %s (panic)", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
v := reflect.ValueOf(m).Elem()
|
|
|
|
|
2021-02-05 18:22:52 +01:00
|
|
|
// Abort if a primary key is zero.
|
2021-02-05 16:32:08 +01:00
|
|
|
for _, k := range primaryKeys {
|
|
|
|
if field := v.FieldByName(k); field.IsZero() {
|
|
|
|
return fmt.Errorf("key '%s' not found", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:22:52 +01:00
|
|
|
// Update all values except primary keys.
|
|
|
|
if res := UnscopedDb().Model(m).Select("*").Omit(primaryKeys...).Updates(m); res.Error != nil {
|
2021-02-05 16:32:08 +01:00
|
|
|
return res.Error
|
|
|
|
} else if res.RowsAffected == 0 {
|
|
|
|
return fmt.Errorf("no entity found for updating")
|
|
|
|
} else if res.RowsAffected > 1 {
|
|
|
|
log.Warnf("update: more than one row affected - bug?")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|