-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
78 lines (61 loc) · 1.73 KB
/
conn.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
77
78
package pg
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type Conn interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
type Object interface {
FromRow(pgx.Row) error
}
type Objects interface {
AddFromRow(pgx.Row) error
}
func Exec(conn Conn, query string, args ...interface{}) (err error) {
ctx := context.Background()
_, err = conn.Exec(ctx, query, args...)
return
}
func Exec2(conn Conn, query string, args ...interface{}) (int64, error) {
ctx := context.Background()
tag, err := conn.Exec(ctx, query, args...)
if err != nil {
return -1, err
}
return tag.RowsAffected(), nil
}
func Query(conn Conn, query string, args ...interface{}) (pgx.Rows, error) {
ctx := context.Background()
return conn.Query(ctx, query, args...)
}
func QueryRow(conn Conn, query string, args ...interface{}) pgx.Row {
ctx := context.Background()
return conn.QueryRow(ctx, query, args...)
}
func QueryObject(conn Conn, obj Object, query string, args ...interface{}) error {
ctx := context.Background()
row := conn.QueryRow(ctx, query, args...)
return obj.FromRow(row)
}
func QueryObjects(conn Conn, objs Objects, query string, args ...interface{}) error {
ctx := context.Background()
rows, err := conn.Query(ctx, query, args...)
if err != nil {
return fmt.Errorf("cannot execute query: %w", err)
}
defer rows.Close()
for rows.Next() {
if err := objs.AddFromRow(rows); err != nil {
return fmt.Errorf("cannot read row: %w", err)
}
}
if err := rows.Err(); err != nil {
return fmt.Errorf("cannot read query response: %w", err)
}
return nil
}