-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
executable file
·96 lines (74 loc) · 2.5 KB
/
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
87
88
89
90
91
92
93
94
95
96
package gorm
import (
"errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// https://gorm.io/docs/delete.html
type Email struct {
ID int
Name string
}
// Delete Hooks
func (u *User) BeforeDelete(tx *gorm.DB) (err error) {
if u.Role == "admin" {
return errors.New("admin user not allowed to delete")
}
return
}
func deleteRecord() {
db := connectMySQL()
email := Email{ID: 10}
// Email's ID is `10`
db.Delete(&email)
// DELETE from emails where id = 10;
// Delete with additional conditions
db.Where("name = ?", "jinzhu").Delete(&email)
// DELETE from emails where id = 10 AND name = "jinzhu";
}
func batchDelete() {
db := connectMySQL()
db.Where("email LIKE ?", "%jinzhu%").Delete(&Email{})
// DELETE from emails where email LIKE "%jinzhu%";
db.Delete(&Email{}, "email LIKE ?", "%jinzhu%")
// DELETE from emails where email LIKE "%jinzhu%";
}
// Return deleted data, only works for database support Returning
func returnDataFromDeleteRows() {
DB := connectMySQL()
// return all columns
var users []User
DB.Clauses(clause.Returning{}).Where("role = ?", "admin").Delete(&users)
// DELETE FROM `users` WHERE role = "admin" RETURNING *
// users => []User{{ID: 1, Name: "jinzhu", Role: "admin", Salary: 100}, {ID: 2, Name: "jinzhu.2", Role: "admin", Salary: 1000}}
// return specified columns
DB.Clauses(clause.Returning{Columns: []clause.Column{{Name: "name"}, {Name: "salary"}}}).Where("role = ?", "admin").Delete(&users)
// DELETE FROM `users` WHERE role = "admin" RETURNING `name`, `salary`
// users => []User{{ID: 0, Name: "jinzhu", Role: "", Salary: 100}, {ID: 0, Name: "jinzhu.2", Role: "", Salary: 1000}}
}
// Soft Delete
// If your model includes a gorm.DeletedAt field (which is included in gorm.Model), it will get soft delete ability automatically!
// 如果您的模型包含gorm.DeletedAt字段(该字段已包含在gorm.Model中),它将自动获得软删除功能!
func softDelete() {
db := connectMySQL()
// user's ID is `111`
user := User{ID: 111}
db.Delete(&user)
// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE id = 111;
// Batch Delete
db.Where("age = ?", 20).Delete(&User{})
// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE age = 20;
// Soft deleted records will be ignored when querying
db.Where("age = 20").Find(&user)
// SELECT * FROM users WHERE age = 20 AND deleted_at IS NULL;
}
func gormModel() {
type User3 struct {
gorm.Model // 嵌入了一些字段
Name string
}
u := User3{Name: "jinzhu"}
db := connectMySQL()
db.Create(&u)
db.Delete(&u)
}