forked from gilcrest/diygoapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
103 lines (91 loc) · 2.4 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package diygoapi
import (
"context"
"database/sql"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
// 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"`
}
// NewNullString returns a null if s is empty, otherwise it returns
// the string which was input
func NewNullString(s string) sql.NullString {
if len(s) == 0 {
return sql.NullString{}
}
return sql.NullString{
String: s,
Valid: true,
}
}
// NewNullTime returns a null if t is the zero value for time.Time,
// otherwise it returns the time which was input
func NewNullTime(t time.Time) sql.NullTime {
if t.IsZero() {
return sql.NullTime{}
}
return sql.NullTime{
Time: t,
Valid: true,
}
}
// NewNullInt64 returns a null if i == 0, otherwise it returns
// the int64 which was input.
func NewNullInt64(i int64) sql.NullInt64 {
if i == 0 {
return sql.NullInt64{}
}
return sql.NullInt64{
Int64: i,
Valid: true,
}
}
// NewNullInt32 returns a null if i == 0, otherwise it returns
// the int32 which was input.
func NewNullInt32(i int32) sql.NullInt32 {
if i == 0 {
return sql.NullInt32{}
}
return sql.NullInt32{
Int32: i,
Valid: true,
}
}
// NewNullUUID returns a null if i == uuid.Nil, otherwise it returns
// the int32 which was input.
func NewNullUUID(i uuid.UUID) uuid.NullUUID {
if i == uuid.Nil {
return uuid.NullUUID{}
}
return uuid.NullUUID{
UUID: i,
Valid: true,
}
}