-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontract.go
76 lines (66 loc) · 2.22 KB
/
contract.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
// Package dbtools contains logic for database transaction, using the retry
// library.
package dbtools
import (
"context"
"database/sql"
"errors"
"time"
"github.com/arsham/retry/v3"
"github.com/jackc/pgx/v5"
)
// ErrEmptyDatabase is returned when no database connection is set.
var ErrEmptyDatabase = errors.New("no database connection is set")
// Pool is the contract for beginning a transaction with a pgxpool db
// connection.
//
//go:generate mockery --name Pool --filename pool_mock.go
type Pool interface {
Begin(ctx context.Context) (pgx.Tx, error)
}
//nolint:unused,deadcode // only used for mocking.
//go:generate mockery --name pgxTx --filename pgx_tx_mock.go --structname PGXTx
type pgxTx interface {
pgx.Tx
}
// Tx is a transaction began with sql.DB.
//
//go:generate mockery --name Tx --filename tx_mock.go
type Tx interface {
Commit() error
Exec(query string, args ...any) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
Prepare(query string) (*sql.Stmt, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRow(query string, args ...any) *sql.Row
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
Rollback() error
Stmt(stmt *sql.Stmt) *sql.Stmt
StmtContext(ctx context.Context, stmt *sql.Stmt) *sql.Stmt
}
// A ConfigFunc function sets up a Transaction.
type ConfigFunc func(*PGX)
// WithRetry sets the retrier. The default retrier tries only once.
func WithRetry(r retry.Retry) ConfigFunc {
return func(p *PGX) {
p.loop = r
}
}
// Retry sets the retry strategy. If you want to pass a Retry object you can
// use the WithRetry function instead.
func Retry(attempts int, delay time.Duration) ConfigFunc {
return func(p *PGX) {
p.loop.Attempts = attempts
p.loop.Delay = delay
}
}
// GracePeriod sets the context timeout when doing a rollback. This context
// needs to be different from the context user is giving as the user's context
// might be cancelled. The default value is 30s.
func GracePeriod(delay time.Duration) ConfigFunc {
return func(p *PGX) {
p.gracePeriod = delay
}
}