-
Notifications
You must be signed in to change notification settings - Fork 2
/
transaction.go
119 lines (110 loc) · 2.75 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
package sq
import (
"context"
"database/sql"
xerr "github.com/goclub/error"
"github.com/jmoiron/sqlx"
)
type T struct {
Core *sqlx.Tx
db *Database
}
func (tx *T) getCore() (core StoragerCore) {
return tx.Core
}
func (tx *T) getSQLChecker() (sqlChecker SQLChecker) {
return tx.db.getSQLChecker()
}
func newTx(tx *sqlx.Tx, db *Database) *T {
return &T{tx, db}
}
// TxResult
// tx.Commit() commit transaction
// tx.Rollback() rollback transaction , rollbackNoError = true
// tx.Error(err) rollback transaction , rollbackNoError = false, err = err
type TxResult struct {
isCommit bool
withError error
}
func (T) Commit() TxResult {
return TxResult{
isCommit: true,
}
}
func (T) Rollback() TxResult {
return TxResult{
isCommit: false,
}
}
func (T) RollbackWithError(err error) TxResult {
return TxResult{
isCommit: false,
withError: xerr.WithStack(err),
}
}
// Error same RollbackWithError
func (T) Error(err error) TxResult {
return TxResult{
isCommit: false,
withError: xerr.WithStack(err),
}
}
// 给 TxResult 增加 Error 接口是为了避出现类似 tx.Rollback() 前面没有 return 的错误
func (result TxResult) Error() string {
if result.withError != nil {
return result.withError.Error()
}
if result.isCommit {
return "goclub/sql: result commit"
} else {
return "goclub/sql: result rollback"
}
}
func (db *Database) Begin(ctx context.Context, level sql.IsolationLevel, handle func(tx *T) TxResult) (rollbackNoError bool, err error) {
return db.BeginOpt(ctx, sql.TxOptions{
Isolation: level,
ReadOnly: false,
}, handle)
}
func (db *Database) BeginOpt(ctx context.Context, opt sql.TxOptions, handle func(tx *T) TxResult) (rollbackNoError bool, err error) {
coreTx, err := db.Core.BeginTxx(ctx, &opt)
if err != nil {
return
}
tx := newTx(coreTx, db)
txResult := handle(tx)
if txResult.isCommit {
err = tx.Core.Commit()
if err != nil {
err = xerr.WithStack(err)
return
}
return
} else {
err = tx.Core.Rollback()
if err != nil {
err = xerr.WithStack(err)
return
}
if txResult.withError != nil {
err = txResult.withError
return
}
rollbackNoError = true
return
}
}
const (
LevelDefault sql.IsolationLevel = sql.LevelDefault
LevelReadUncommitted sql.IsolationLevel = sql.LevelReadUncommitted
LevelReadCommitted sql.IsolationLevel = sql.LevelReadCommitted
LevelWriteCommitted sql.IsolationLevel = sql.LevelWriteCommitted
LevelRepeatableRead sql.IsolationLevel = sql.LevelRepeatableRead
LevelSnapshot sql.IsolationLevel = sql.LevelSnapshot
LevelSerializable sql.IsolationLevel = sql.LevelSerializable
LevelLinearizable sql.IsolationLevel = sql.LevelLinearizable
)
const (
RC sql.IsolationLevel = sql.LevelReadCommitted
RR sql.IsolationLevel = sql.LevelRepeatableRead
)