-
Notifications
You must be signed in to change notification settings - Fork 3
/
pig.go
65 lines (53 loc) · 1.16 KB
/
pig.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
// Package pig – simple pgx wrapper to execute and scan query results.
package pig
import (
"context"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
// Conn connection interface.
type Conn interface {
BeginFunc(context.Context, func(pgx.Tx) error) error
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
}
// Handler to execute transaction.
type Handler func(*Ex) error
// Pig pgx wrapper.
type Pig struct {
conn Conn
}
// Conn returns pgx connection.
func (p *Pig) Conn() Conn {
return p.conn
}
// Query returns new query executor.
func (p *Pig) Query(options ...Option) *Ex {
return &Ex{
ex: p.conn,
options: p.options(options...),
}
}
// Tx returns new transaction.
func (p *Pig) Tx(options ...Option) *Tx {
return &Tx{
conn: p.conn,
options: p.options(options...),
}
}
func (p *Pig) options(options ...Option) Options {
var o Options
for _, opt := range options {
opt(&o)
}
if o.Context == nil {
o.Context = context.Background()
}
return o
}
// New returns new pig instance.
func New(conn Conn) *Pig {
return &Pig{
conn: conn,
}
}