-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathctx.go
55 lines (43 loc) Β· 1.26 KB
/
ctx.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
package csql
import (
"context"
"database/sql"
"github.com/gocopper/copper/cerrors"
"github.com/jmoiron/sqlx"
)
type ctxKey string
const connCtxKey = ctxKey("csql/*sqlx.Tx")
// CtxWithTx creates a context with a new database transaction. Any queries run using Querier will be run within
// this transaction.
func CtxWithTx(parentCtx context.Context, db *sql.DB, dialect string) (context.Context, *sql.Tx, error) {
tx, err := sqlx.NewDb(db, dialect).Beginx()
if err != nil {
return nil, nil, cerrors.New(err, "failed to begin db transaction", map[string]interface{}{
"dialect": dialect,
})
}
return context.WithValue(parentCtx, connCtxKey, tx), tx.Tx, nil
}
// TxFromCtx returns an existing transaction from the context. This method should be called with context created
// using CtxWithTx.
func TxFromCtx(ctx context.Context) (*sql.Tx, error) {
tx, err := txFromCtx(ctx)
if err != nil {
return nil, err
}
return tx.Tx, nil
}
func txFromCtx(ctx context.Context) (*sqlx.Tx, error) {
tx, ok := ctx.Value(connCtxKey).(*sqlx.Tx)
if !ok {
return nil, cerrors.New(nil, "no database transaction in the context", nil)
}
return tx, nil
}
func mustTxFromCtx(ctx context.Context) *sqlx.Tx {
tx, err := txFromCtx(ctx)
if err != nil {
panic(err)
}
return tx
}