/
tx.go
47 lines (39 loc) · 1.06 KB
/
tx.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
package db
import (
"context"
"gorm.io/gorm"
"github.com/wfusion/gofusion/common/infra/drivers/orm"
"github.com/wfusion/gofusion/common/utils"
)
type txOption struct {
dbName string
}
func TxUse(name string) utils.OptionFunc[txOption] {
return func(o *txOption) {
o.dbName = name
}
}
// WithinTx 事务内执行 DAL 操作
func WithinTx(ctx context.Context, cb func(ctx context.Context) (err error), opts ...utils.OptionExtender) error {
var db *DB
o := utils.ApplyOptions[useOption](opts...)
opt := utils.ApplyOptions[txOption](opts...)
if opt.dbName == "" {
db = GetCtxGormDB(ctx)
} else {
utils.IfAny(
func() bool { db = GetCtxGormDBByName(ctx, opt.dbName); return db != nil },
func() bool { db = Use(ctx, opt.dbName, AppName(o.appName)); return db != nil },
)
}
if db == nil {
panic(ErrDatabaseNotFound)
}
return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
return cb(SetCtxGormDB(ctx, &DB{
DB: &orm.DB{DB: tx},
Name: db.Name,
tableShardingPlugins: db.tableShardingPlugins,
}))
})
}