-
Notifications
You must be signed in to change notification settings - Fork 3
/
ex.go
55 lines (43 loc) · 1.26 KB
/
ex.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 pig
import (
"context"
"github.com/georgysavva/scany/pgxscan"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/pkg/errors"
)
type executable interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
}
// Ex to execute queries.
type Ex struct {
ex executable
options Options
}
// Exec query and return affected rows.
func (e *Ex) Exec(sql string, args ...interface{}) (int64, error) {
t, err := e.ex.Exec(e.options.Context, sql, args...)
if err != nil {
return 0, errors.Wrap(err, "pig: execute query")
}
return t.RowsAffected(), nil
}
// Get single record.
func (e *Ex) Get(dst interface{}, sql string, args ...interface{}) error {
rows, err := e.ex.Query(e.options.Context, sql, args...)
if err != nil {
return errors.Wrap(err, "pig: get one result row")
}
err = pgxscan.ScanOne(dst, rows)
return errors.WithStack(err)
}
// Select multiple records.
func (e *Ex) Select(dst interface{}, sql string, args ...interface{}) error {
rows, err := e.ex.Query(e.options.Context, sql, args...)
if err != nil {
return errors.Wrap(err, "pig: select multiple result row")
}
err = pgxscan.ScanAll(dst, rows)
return errors.WithStack(err)
}