| 1 |
package callbacks |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"gorm.io/gorm" |
| 8 |
"gorm.io/gorm/clause" |
| 9 |
"gorm.io/gorm/schema" |
| 10 |
"gorm.io/gorm/utils" |
| 11 |
) |
| 12 |
|
| 13 |
func BeforeDelete(db *gorm.DB) { |
| 14 |
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && db.Statement.Schema.BeforeDelete { |
| 15 |
callMethod(db, func(value interface{}, tx *gorm.DB) bool { |
| 16 |
if i, ok := value.(BeforeDeleteInterface); ok { |
| 17 |
db.AddError(i.BeforeDelete(tx)) |
| 18 |
return true |
| 19 |
} |
| 20 |
|
| 21 |
return false |
| 22 |
}) |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func DeleteBeforeAssociations(db *gorm.DB) { |
| 27 |
if db.Error == nil && db.Statement.Schema != nil { |
| 28 |
selectColumns, restricted := db.Statement.SelectAndOmitColumns(true, false) |
| 29 |
if !restricted { |
| 30 |
return |
| 31 |
} |
| 32 |
|
| 33 |
for column, v := range selectColumns { |
| 34 |
if !v { |
| 35 |
continue |
| 36 |
} |
| 37 |
|
| 38 |
rel, ok := db.Statement.Schema.Relationships.Relations[column] |
| 39 |
if !ok { |
| 40 |
continue |
| 41 |
} |
| 42 |
|
| 43 |
switch rel.Type { |
| 44 |
case schema.HasOne, schema.HasMany: |
| 45 |
queryConds := rel.ToQueryConditions(db.Statement.Context, db.Statement.ReflectValue) |
| 46 |
modelValue := reflect.New(rel.FieldSchema.ModelType).Interface() |
| 47 |
tx := db.Session(&gorm.Session{NewDB: true}).Model(modelValue) |
| 48 |
withoutConditions := false |
| 49 |
if db.Statement.Unscoped { |
| 50 |
tx = tx.Unscoped() |
| 51 |
} |
| 52 |
|
| 53 |
if len(db.Statement.Selects) > 0 { |
| 54 |
selects := make([]string, 0, len(db.Statement.Selects)) |
| 55 |
for _, s := range db.Statement.Selects { |
| 56 |
if s == clause.Associations { |
| 57 |
selects = append(selects, s) |
| 58 |
} else if columnPrefix := column + "."; strings.HasPrefix(s, columnPrefix) { |
| 59 |
selects = append(selects, strings.TrimPrefix(s, columnPrefix)) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if len(selects) > 0 { |
| 64 |
tx = tx.Select(selects) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
for _, cond := range queryConds { |
| 69 |
if c, ok := cond.(clause.IN); ok && len(c.Values) == 0 { |
| 70 |
withoutConditions = true |
| 71 |
break |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if !withoutConditions && db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil { |
| 76 |
return |
| 77 |
} |
| 78 |
case schema.Many2Many: |
| 79 |
var ( |
| 80 |
queryConds = make([]clause.Expression, 0, len(rel.References)) |
| 81 |
foreignFields = make([]*schema.Field, 0, len(rel.References)) |
| 82 |
relForeignKeys = make([]string, 0, len(rel.References)) |
| 83 |
modelValue = reflect.New(rel.JoinTable.ModelType).Interface() |
| 84 |
table = rel.JoinTable.Table |
| 85 |
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table) |
| 86 |
) |
| 87 |
|
| 88 |
for _, ref := range rel.References { |
| 89 |
if ref.OwnPrimaryKey { |
| 90 |
foreignFields = append(foreignFields, ref.PrimaryKey) |
| 91 |
relForeignKeys = append(relForeignKeys, ref.ForeignKey.DBName) |
| 92 |
} else if ref.PrimaryValue != "" { |
| 93 |
queryConds = append(queryConds, clause.Eq{ |
| 94 |
Column: clause.Column{Table: rel.JoinTable.Table, Name: ref.ForeignKey.DBName}, |
| 95 |
Value: ref.PrimaryValue, |
| 96 |
}) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
_, foreignValues := schema.GetIdentityFieldValuesMap(db.Statement.Context, db.Statement.ReflectValue, foreignFields) |
| 101 |
column, values := schema.ToQueryValues(table, relForeignKeys, foreignValues) |
| 102 |
queryConds = append(queryConds, clause.IN{Column: column, Values: values}) |
| 103 |
|
| 104 |
if db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil { |
| 105 |
return |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func Delete(config *Config) func(db *gorm.DB) { |
| 114 |
supportReturning := utils.Contains(config.DeleteClauses, "RETURNING") |
| 115 |
|
| 116 |
return func(db *gorm.DB) { |
| 117 |
if db.Error != nil { |
| 118 |
return |
| 119 |
} |
| 120 |
|
| 121 |
if db.Statement.Schema != nil { |
| 122 |
for _, c := range db.Statement.Schema.DeleteClauses { |
| 123 |
db.Statement.AddClause(c) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if db.Statement.SQL.Len() == 0 { |
| 128 |
db.Statement.SQL.Grow(100) |
| 129 |
db.Statement.AddClauseIfNotExists(clause.Delete{}) |
| 130 |
|
| 131 |
if db.Statement.Schema != nil { |
| 132 |
_, queryValues := schema.GetIdentityFieldValuesMap(db.Statement.Context, db.Statement.ReflectValue, db.Statement.Schema.PrimaryFields) |
| 133 |
column, values := schema.ToQueryValues(db.Statement.Table, db.Statement.Schema.PrimaryFieldDBNames, queryValues) |
| 134 |
|
| 135 |
if len(values) > 0 { |
| 136 |
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}}) |
| 137 |
} |
| 138 |
|
| 139 |
if db.Statement.ReflectValue.CanAddr() && db.Statement.Dest != db.Statement.Model && db.Statement.Model != nil { |
| 140 |
_, queryValues = schema.GetIdentityFieldValuesMap(db.Statement.Context, reflect.ValueOf(db.Statement.Model), db.Statement.Schema.PrimaryFields) |
| 141 |
column, values = schema.ToQueryValues(db.Statement.Table, db.Statement.Schema.PrimaryFieldDBNames, queryValues) |
| 142 |
|
| 143 |
if len(values) > 0 { |
| 144 |
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}}) |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
db.Statement.AddClauseIfNotExists(clause.From{}) |
| 150 |
|
| 151 |
db.Statement.Build(db.Statement.BuildClauses...) |
| 152 |
} |
| 153 |
|
| 154 |
checkMissingWhereConditions(db) |
| 155 |
|
| 156 |
if !db.DryRun && db.Error == nil { |
| 157 |
ok, mode := hasReturning(db, supportReturning) |
| 158 |
if !ok { |
| 159 |
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) |
| 160 |
|
| 161 |
if db.AddError(err) == nil { |
| 162 |
db.RowsAffected, _ = result.RowsAffected() |
| 163 |
|
| 164 |
if db.Statement.Result != nil { |
| 165 |
db.Statement.Result.Result = result |
| 166 |
db.Statement.Result.RowsAffected = db.RowsAffected |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return |
| 171 |
} |
| 172 |
|
| 173 |
if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil { |
| 174 |
gorm.Scan(rows, db, mode) |
| 175 |
|
| 176 |
if db.Statement.Result != nil { |
| 177 |
db.Statement.Result.RowsAffected = db.RowsAffected |
| 178 |
} |
| 179 |
db.AddError(rows.Close()) |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func AfterDelete(db *gorm.DB) { |
| 186 |
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && db.Statement.Schema.AfterDelete { |
| 187 |
callMethod(db, func(value interface{}, tx *gorm.DB) bool { |
| 188 |
if i, ok := value.(AfterDeleteInterface); ok { |
| 189 |
db.AddError(i.AfterDelete(tx)) |
| 190 |
return true |
| 191 |
} |
| 192 |
return false |
| 193 |
}) |
| 194 |
} |
| 195 |
} |