-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
178 lines (147 loc) · 3.44 KB
/
transaction.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package db
/*
命令模式
*/
// Command 执行数据库操作的命令接口
type Command interface {
// Exec 执行insert、update、delete命令
Exec() error
// Undo 回滚命令
Undo()
// SetDb 设置关联的数据库
setDb(db Db)
}
// Transaction Db事务实现,事务接口的调用顺序为begin -> exec -> exec > ... -> commit
type Transaction struct {
name string
db Db
cmds []Command
}
func NewTransaction(name string, db Db) *Transaction {
return &Transaction{
name: name,
db: db,
cmds: nil,
}
}
// Begin 开启一个事务
func (t *Transaction) Begin() {
t.cmds = make([]Command, 0)
}
// Exec 在事务中执行命令,先缓存到cmds队列中,等commit时再执行
func (t *Transaction) Exec(cmd Command) error {
if t.cmds == nil {
return ErrTransactionNotBegin
}
cmd.setDb(t.db)
t.cmds = append(t.cmds, cmd)
return nil
}
// Commit 提交事务,执行队列中的命令,如果有命令失败,则回滚后返回错误
func (t *Transaction) Commit() error {
history := &cmdHistory{history: make([]Command, 0, len(t.cmds))}
for _, cmd := range t.cmds {
if err := cmd.Exec(); err != nil {
history.rollback()
return err
}
history.add(cmd)
}
return nil
}
/*
备忘录模式
*/
// cmdHistory 命令执行历史
type cmdHistory struct {
history []Command
}
func (c *cmdHistory) add(cmd Command) {
c.history = append(c.history, cmd)
}
func (c *cmdHistory) rollback() {
for i := len(c.history) - 1; i >= 0; i-- {
c.history[i].Undo()
}
}
// InsertCmd 插入命令
type InsertCmd struct {
db Db
tableName string
primaryKey interface{}
newRecord interface{}
}
func NewInsertCmd(tableName string) *InsertCmd {
return &InsertCmd{tableName: tableName}
}
func (i *InsertCmd) WithPrimaryKey(primaryKey interface{}) *InsertCmd {
i.primaryKey = primaryKey
return i
}
func (i *InsertCmd) WithRecord(record interface{}) *InsertCmd {
i.newRecord = record
return i
}
func (i *InsertCmd) Exec() error {
return i.db.Insert(i.tableName, i.primaryKey, i.newRecord)
}
func (i *InsertCmd) Undo() {
i.db.Delete(i.tableName, i.primaryKey)
}
func (i *InsertCmd) setDb(db Db) {
i.db = db
}
// UpdateCmd 更新命令
type UpdateCmd struct {
db Db
tableName string
primaryKey interface{}
newRecord interface{}
oldRecord interface{}
}
func NewUpdateCmd(tableName string) *UpdateCmd {
return &UpdateCmd{tableName: tableName}
}
func (u *UpdateCmd) WithPrimaryKey(primaryKey interface{}) *UpdateCmd {
u.primaryKey = primaryKey
return u
}
func (u *UpdateCmd) WithRecord(record interface{}) *UpdateCmd {
u.newRecord = record
return u
}
func (u *UpdateCmd) Exec() error {
if err := u.db.Query(u.tableName, u.primaryKey, u.oldRecord); err != nil {
return err
}
return u.db.Update(u.tableName, u.primaryKey, u.newRecord)
}
func (u *UpdateCmd) Undo() {
u.db.Update(u.tableName, u.primaryKey, u.oldRecord)
}
func (u *UpdateCmd) setDb(db Db) {
u.db = db
}
// DeleteCmd 删除命令
type DeleteCmd struct {
db Db
tableName string
primaryKey interface{}
oldRecord interface{}
}
func NewDeleteCmd(tableName string) *DeleteCmd {
return &DeleteCmd{tableName: tableName}
}
func (d *DeleteCmd) WithPrimaryKey(primaryKey interface{}) *DeleteCmd {
d.primaryKey = primaryKey
return d
}
func (d *DeleteCmd) Exec() error {
return d.db.Delete(d.tableName, d.primaryKey)
}
func (d *DeleteCmd) Undo() {
d.db.Insert(d.tableName, d.primaryKey, d.oldRecord)
}
func (d *DeleteCmd) setDb(db Db) {
d.db = db
}