-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.go
More file actions
58 lines (48 loc) · 1.68 KB
/
transaction.go
File metadata and controls
58 lines (48 loc) · 1.68 KB
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
package daogext
import (
dgctx "github.com/darwinOrg/go-common/context"
"github.com/rolandhe/daog"
txrequest "github.com/rolandhe/daog/tx"
)
func Readonly(ctx *dgctx.DgContext, workFn func(tc *daog.TransContext) error) error {
return daog.AutoTrans(func() (*daog.TransContext, error) {
return newReadonlyTransContext(ctx)
}, workFn)
}
func ReadonlyWithResult[T any](ctx *dgctx.DgContext, workFn func(tc *daog.TransContext) (T, error)) (T, error) {
return daog.AutoTransWithResult(func() (*daog.TransContext, error) {
return newReadonlyTransContext(ctx)
}, workFn)
}
func Write(ctx *dgctx.DgContext, workFn func(tc *daog.TransContext) error) error {
return daog.AutoTrans(func() (*daog.TransContext, error) {
return newWriteTransContext(ctx)
}, workFn)
}
func WriteWithResult[T any](ctx *dgctx.DgContext, workFn func(tc *daog.TransContext) (T, error)) (T, error) {
return daog.AutoTransWithResult(func() (*daog.TransContext, error) {
return newWriteTransContext(ctx)
}, workFn)
}
func newReadonlyTransContext(ctx *dgctx.DgContext) (*daog.TransContext, error) {
return newTransContextWithRequestStyle(ctx, txrequest.RequestReadonly)
}
func newWriteTransContext(ctx *dgctx.DgContext) (*daog.TransContext, error) {
tc, err := newTransContextWithRequestStyle(ctx, txrequest.RequestWrite)
if tc != nil {
tc.ExtInfo = map[string]any{
"op_id": ctx.OpId,
}
}
return tc, err
}
func newTransContextWithRequestStyle(ctx *dgctx.DgContext, requestStyle txrequest.RequestStyle) (*daog.TransContext, error) {
tc, err := daog.NewTransContext(dataSource, requestStyle, ctx.TraceId)
if err != nil {
return tc, err
}
if tc.LogSQL && ctx.NotLogSQL {
tc.LogSQL = false
}
return tc, err
}