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