Added new feature - on soft delete, update additional fields from model/struct in the same update operation by using "updateOnSoftDelete" field tag
This commit is contained in:
parent
e9637024d3
commit
13338381ca
@ -62,6 +62,7 @@ type Field struct {
|
|||||||
Creatable bool
|
Creatable bool
|
||||||
Updatable bool
|
Updatable bool
|
||||||
Readable bool
|
Readable bool
|
||||||
|
UpdateOnSoftDelete bool
|
||||||
AutoCreateTime TimeType
|
AutoCreateTime TimeType
|
||||||
AutoUpdateTime TimeType
|
AutoUpdateTime TimeType
|
||||||
HasDefaultValue bool
|
HasDefaultValue bool
|
||||||
@ -113,6 +114,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
|||||||
Creatable: true,
|
Creatable: true,
|
||||||
Updatable: true,
|
Updatable: true,
|
||||||
Readable: true,
|
Readable: true,
|
||||||
|
UpdateOnSoftDelete: false,
|
||||||
PrimaryKey: utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"]),
|
PrimaryKey: utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"]),
|
||||||
AutoIncrement: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
|
AutoIncrement: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
|
||||||
HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
|
HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
|
||||||
@ -329,6 +331,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, ok := field.TagSettings["UPDATEONSOFTDELETE"]; ok {
|
||||||
|
field.UpdateOnSoftDelete = true
|
||||||
|
}
|
||||||
|
|
||||||
// setup permission
|
// setup permission
|
||||||
if val, ok := field.TagSettings["-"]; ok {
|
if val, ok := field.TagSettings["-"]; ok {
|
||||||
val = strings.ToLower(strings.TrimSpace(val))
|
val = strings.ToLower(strings.TrimSpace(val))
|
||||||
|
@ -142,9 +142,28 @@ func (sd SoftDeleteDeleteClause) MergeClause(*clause.Clause) {
|
|||||||
func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *Statement) {
|
func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *Statement) {
|
||||||
if stmt.SQL.Len() == 0 && !stmt.Statement.Unscoped {
|
if stmt.SQL.Len() == 0 && !stmt.Statement.Unscoped {
|
||||||
curTime := stmt.DB.NowFunc()
|
curTime := stmt.DB.NowFunc()
|
||||||
stmt.AddClause(clause.Set{{Column: clause.Column{Name: sd.Field.DBName}, Value: curTime}})
|
|
||||||
|
var setColumns clause.Set = clause.Set{{Column: clause.Column{Name: sd.Field.DBName}, Value: curTime}}
|
||||||
stmt.SetColumn(sd.Field.DBName, curTime, true)
|
stmt.SetColumn(sd.Field.DBName, curTime, true)
|
||||||
|
|
||||||
|
modelRefVal := reflect.ValueOf(stmt.Model)
|
||||||
|
if modelRefVal.Kind() == reflect.Ptr {
|
||||||
|
modelRefVal = reflect.Indirect(modelRefVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
if stmt.Model != nil && stmt.Schema != nil {
|
||||||
|
// Add additional fields to update in the same operation!
|
||||||
|
for _, field := range stmt.Schema.Fields {
|
||||||
|
if field.UpdateOnSoftDelete {
|
||||||
|
fieldVal := modelRefVal.FieldByName(field.Name).Interface()
|
||||||
|
setColumns = append(setColumns, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: fieldVal})
|
||||||
|
stmt.SetColumn(field.DBName, fieldVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt.AddClause(setColumns)
|
||||||
|
|
||||||
if stmt.Schema != nil {
|
if stmt.Schema != nil {
|
||||||
_, queryValues := schema.GetIdentityFieldValuesMap(stmt.Context, stmt.ReflectValue, stmt.Schema.PrimaryFields)
|
_, queryValues := schema.GetIdentityFieldValuesMap(stmt.Context, stmt.ReflectValue, stmt.Schema.PrimaryFields)
|
||||||
column, values := schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)
|
column, values := schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user