github.com/wfusion/gofusion@v1.1.14/db/callbacks/soft_delete.go (about) 1 package callbacks 2 3 import ( 4 "gorm.io/gorm" 5 "gorm.io/gorm/clause" 6 "gorm.io/gorm/utils" 7 8 "github.com/wfusion/gofusion/db/softdelete" 9 10 comUtl "github.com/wfusion/gofusion/common/utils" 11 ) 12 13 func SoftDelete(db *gorm.DB) { 14 // update callback 15 comUtl.MustSuccess(db.Callback().Update().Replace("gorm:update", func(db *gorm.DB) { 16 if db.Error != nil { 17 return 18 } 19 20 BuildUpdateSQL(db) 21 checkMissingWhereConditions(db) 22 23 if !db.DryRun && db.Error == nil { 24 if ok, mode := hasReturning(db, utils.Contains(db.Callback().Update().Clauses, "RETURNING")); ok { 25 if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil { 26 dest := db.Statement.Dest 27 db.Statement.Dest = db.Statement.ReflectValue.Addr().Interface() 28 gorm.Scan(rows, db, mode) 29 db.Statement.Dest = dest 30 _ = db.AddError(rows.Close()) 31 } 32 } else { 33 result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) 34 35 if db.AddError(err) == nil { 36 db.RowsAffected, _ = result.RowsAffected() 37 } 38 } 39 } 40 })) 41 42 // delete callback 43 comUtl.MustSuccess(db.Callback().Delete().Replace("gorm:delete", func(db *gorm.DB) { 44 if db.Error != nil { 45 return 46 } 47 48 BuildDeleteSQL(db) 49 checkMissingWhereConditions(db) 50 51 if !db.DryRun && db.Error == nil { 52 ok, mode := hasReturning(db, utils.Contains(db.Callback().Delete().Clauses, "RETURNING")) 53 if !ok { 54 result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) 55 if db.AddError(err) == nil { 56 db.RowsAffected, _ = result.RowsAffected() 57 } 58 59 return 60 } 61 62 if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil { 63 gorm.Scan(rows, db, mode) 64 _ = db.AddError(rows.Close()) 65 } 66 } 67 })) 68 69 return 70 } 71 72 func checkMissingWhereConditions(db *gorm.DB) { 73 if !db.AllowGlobalUpdate && db.Error == nil { 74 where, withCondition := db.Statement.Clauses["WHERE"] 75 if withCondition { 76 if softdelete.IsClausesWithSoftDelete(db.Statement.Clauses) { 77 whereClause, _ := where.Expression.(clause.Where) 78 withCondition = len(whereClause.Exprs) > 1 79 } 80 } 81 if !withCondition { 82 _ = db.AddError(gorm.ErrMissingWhereClause) 83 } 84 return 85 } 86 }