-
Notifications
You must be signed in to change notification settings - Fork 71
/
db.go
85 lines (74 loc) · 2.1 KB
/
db.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
79
80
81
82
83
84
85
package diygoapi
import (
"context"
"time"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/rs/zerolog"
)
// Datastorer is an interface for working with the Database
type Datastorer interface {
// Ping pings the DB pool.
Ping(ctx context.Context) error
// BeginTx starts a pgx.Tx using the input context
BeginTx(ctx context.Context) (pgx.Tx, error)
// RollbackTx rolls back the input pgx.Tx
RollbackTx(ctx context.Context, tx pgx.Tx, err error) error
// CommitTx commits the Tx
CommitTx(ctx context.Context, tx pgx.Tx) error
}
// DBTX interface mirrors the interface generated by https://github.com/kyleconroy/sqlc
// to allow passing a Pool or a Tx
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
// PingServicer pings the database and responds whether it is up or down
type PingServicer interface {
Ping(ctx context.Context, lgr zerolog.Logger) PingResponse
}
// PingResponse is the response struct for the PingService
type PingResponse struct {
DBUp bool `json:"db_up"`
}
// NewPgxInt4 returns a pgx/pgtype.Int4 with the input value
func NewPgxInt4(i int32) pgtype.Int4 {
return pgtype.Int4{
Int32: i,
Valid: true,
}
}
// NewPgxInt8 returns a pgx/pgtype.Int8 with the input value
func NewPgxInt8(i int64) pgtype.Int8 {
return pgtype.Int8{
Int64: i,
Valid: true,
}
}
// NewPgxText returns a pgx/pgtype.Text with the input value.
// If the input string is empty, it returns an empty pgtype.Text
func NewPgxText(s string) pgtype.Text {
if len(s) == 0 {
return pgtype.Text{}
}
return pgtype.Text{
String: s,
Valid: true,
}
}
// NewPgxTimestampTZ returns a pgx/pgtype.Timestamptz with the input value
func NewPgxTimestampTZ(t time.Time) pgtype.Timestamptz {
return pgtype.Timestamptz{
Time: t,
Valid: true,
}
}
// NewPgxDate returns a pgx/pgtype.Date with the input value
func NewPgxDate(t time.Time) pgtype.Date {
return pgtype.Date{
Time: t,
Valid: true,
}
}