-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_runner.go
30 lines (26 loc) · 1013 Bytes
/
query_runner.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
package pgxtransactor
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
// PgxQueryRunner represents query runner behavior.
//
//go:generate mockery --name=PgxQueryRunner --case underscore --output=./mocks
type PgxQueryRunner interface {
Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) pgx.Row
Begin(ctx context.Context) (pgx.Tx, error)
SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
CopyFrom(ctx context.Context, ident pgx.Identifier, cls []string, src pgx.CopyFromSource) (int64, error)
}
// Runner extracts query runner from context, if pgx.Tx is injected into context
// it is returned and pgxpool.Pool otherwise.
func (e *PgxPool) Runner(ctx context.Context) PgxQueryRunner {
tx := extractTx(ctx)
if tx != nil {
return tx
}
return e.pool
}