/
pgx.go
338 lines (282 loc) · 9.2 KB
/
pgx.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Package db provides support for access the database.
package db
import (
"context"
"database/sql"
"errors"
"fmt"
"net/url"
"strings"
"time"
"github.com/aleury/service/foundation/web"
"github.com/jackc/pgx/v5/pgconn"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)
const (
uniqueViolation = "23505"
undefinedTable = "42P01"
)
// Set of error variables for CRUD operations.
var (
ErrDBNotFound = sql.ErrNoRows
ErrDBDuplicatedEntry = errors.New("duplicated entry")
ErrUndefinedTable = errors.New("undefined table")
)
// Config is the required properties to use the database.
type Config struct {
User string
Password string
Host string
Name string
Schema string
MaxIdleConns int
MaxOpenConns int
DisableTLS bool
}
// Open knows how to open a database connection based on the configuration.
func Open(cfg Config) (*sqlx.DB, error) {
sslMode := "require"
if cfg.DisableTLS {
sslMode = "disable"
}
q := make(url.Values)
q.Set("sslmode", sslMode)
q.Set("timezone", "utc")
if cfg.Schema != "" {
q.Set("search_path", cfg.Schema)
}
u := url.URL{
Scheme: "postgres",
User: url.UserPassword(cfg.User, cfg.Password),
Host: cfg.Host,
Path: cfg.Name,
RawQuery: q.Encode(),
}
db, err := sqlx.Open("pgx", u.String())
if err != nil {
return nil, err
}
db.SetMaxIdleConns(cfg.MaxIdleConns)
db.SetMaxOpenConns(cfg.MaxOpenConns)
return db, nil
}
// StatusCheck returns nil if it can successfully talk to the database. It
// returns a non-nil error otherwise.
func StatusCheck(ctx context.Context, db *sqlx.DB) error {
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second)
defer cancel()
}
var pingError error
for attempts := 1; ; attempts++ {
pingError = db.Ping()
if pingError == nil {
break
}
time.Sleep(time.Duration(attempts) * 100 * time.Millisecond)
if ctx.Err() != nil {
return ctx.Err()
}
}
if ctx.Err() != nil {
return ctx.Err()
}
// Run a simple query to determine connectivity.
// Running this query forces a round trip through the database.
const q = `SELECT true`
var tmp bool
return db.QueryRowContext(ctx, q).Scan(&tmp)
}
// WithinTran runs passed functin and do commit/rollback at the end.
func WithinTran(ctx context.Context, log *zap.SugaredLogger, db *sqlx.DB, fn func(*sqlx.Tx) error) error {
traceID := web.GetTraceID(ctx)
log.Infow("begin tran")
tx, err := db.Beginx()
if err != nil {
return fmt.Errorf("begin tran: %w", err)
}
// We can defer the rollback since the code checks if the transaction
// has already been committed.
defer func() {
if err := tx.Rollback(); err != nil {
if errors.Is(err, sql.ErrTxDone) {
return
}
log.Errorw("unable to rollback tran", "trace_id", traceID, "ERROR", err)
}
log.Infow("rollback tran", "trace_id", traceID)
}()
if err := fn(tx); err != nil {
if pqerr, ok := err.(*pgconn.PgError); ok && pqerr.Code == uniqueViolation {
return ErrDBDuplicatedEntry
}
return fmt.Errorf("exec tran: %w", err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit tran: %w", err)
}
log.Infow("commit tran", "trace_id", traceID)
return nil
}
// ExecContext is a helper function to execute a CUD operation with
// logging and tracing.
func ExecContext(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string) error {
return NamedExecContext(ctx, log, db, query, struct{}{})
}
// NamedExecContext is a helper function to execute a CUD operation with
// logging and tracing where field replacement is necessary.
func NamedExecContext(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any) error {
q := queryString(query, data)
if _, ok := data.(struct{}); ok {
log.WithOptions(zap.AddCallerSkip(3)).Infow("database.NamedExecContext", "trace_id", web.GetTraceID(ctx), "query", q)
} else {
log.WithOptions(zap.AddCallerSkip(2)).Infow("database.NamedExecContext", "trace_id", web.GetTraceID(ctx), "query", q)
}
if _, err := sqlx.NamedExecContext(ctx, db, query, data); err != nil {
if pqerr, ok := err.(*pgconn.PgError); ok {
switch pqerr.Code {
case undefinedTable:
return ErrUndefinedTable
case uniqueViolation:
return ErrDBDuplicatedEntry
}
}
return err
}
return nil
}
// QuerySlice is a helper function for executing queries that return a
// collection of data to be unmarshalled into a slice.
func QuerySlice[T any](ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, dest *[]T) error {
return namedQuerySlice(ctx, log, db, query, struct{}{}, dest, false)
}
// NamedQuerySlice is a helper function for executing queries that return a
// collection of data to be unmarshalled into a slice where field replacement is
// necessary.
func NamedQuerySlice[T any](ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest *[]T) error {
return namedQuerySlice(ctx, log, db, query, data, dest, false)
}
// NamedQuerySliceUsingIn is a helper function for executing queries that return
// a collection of data to be unmarshalled into a slice where field replacement
// is necessary. Use this if the query has an IN clause.
func NamedQuerySliceUsingIn[T any](ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest *[]T) error {
return namedQuerySlice(ctx, log, db, query, data, dest, true)
}
func namedQuerySlice[T any](ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest *[]T, withIn bool) error {
q := queryString(query, data)
log.WithOptions(zap.AddCallerSkip(3)).Infow("database.NamedQuerySlice", "trace_id", web.GetTraceID(ctx), "query", q)
var rows *sqlx.Rows
var err error
switch withIn {
case true:
rows, err = func() (*sqlx.Rows, error) {
named, args, err := sqlx.Named(query, data)
if err != nil {
return nil, err
}
query, args, err := sqlx.In(named, args...)
if err != nil {
return nil, err
}
query = db.Rebind(query)
return db.QueryxContext(ctx, query, args...)
}()
default:
rows, err = sqlx.NamedQueryContext(ctx, db, query, data)
}
if err != nil {
if pqerr, ok := err.(*pgconn.PgError); ok && pqerr.Code == undefinedTable {
return ErrUndefinedTable
}
return err
}
defer rows.Close()
var slice []T
for rows.Next() {
v := new(T)
if err := rows.StructScan(v); err != nil {
return err
}
slice = append(slice, *v)
}
*dest = slice
return nil
}
// QueryStruct is a helper function for executing queries that return a
// single value to be unmarshalled into a struct type where field replacement is necessary.
func QueryStruct(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, dest any) error {
return namedQueryStruct(ctx, log, db, query, struct{}{}, dest, false)
}
// NamedQueryStruct is a helper function for executing queries that return a
// single value to be unmarshalled into a struct type where field replacement is necessary.
func NamedQueryStruct(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest any) error {
return namedQueryStruct(ctx, log, db, query, data, dest, false)
}
// NamedQueryStructUsingIn is a helper function for executing queries that return
// a single value to be unmarshalled into a struct type where field replacement
// is necessary. Use this if the query has an IN clause.
func NamedQueryStructUsingIn(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest any) error {
return namedQueryStruct(ctx, log, db, query, data, dest, true)
}
func namedQueryStruct(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest any, withIn bool) error {
q := queryString(query, data)
log.WithOptions(zap.AddCallerSkip(3)).Infow("database.NamedQueryStruct", "trace_id", web.GetTraceID(ctx), "query", q)
var rows *sqlx.Rows
var err error
switch withIn {
case true:
rows, err = func() (*sqlx.Rows, error) {
named, args, err := sqlx.Named(query, data)
if err != nil {
return nil, err
}
query, args, err := sqlx.In(named, args...)
if err != nil {
return nil, err
}
query = db.Rebind(query)
return db.QueryxContext(ctx, query, args...)
}()
default:
rows, err = sqlx.NamedQueryContext(ctx, db, query, data)
}
if err != nil {
if pqerr, ok := err.(*pgconn.PgError); ok && pqerr.Code == undefinedTable {
return ErrUndefinedTable
}
return err
}
defer rows.Close()
if !rows.Next() {
return ErrDBNotFound
}
if err := rows.StructScan(dest); err != nil {
return err
}
return nil
}
// queryString provides a pretty print version of the query and parameters.
func queryString(query string, args any) string {
query, params, err := sqlx.Named(query, args)
if err != nil {
return err.Error()
}
for _, param := range params {
var value string
switch v := param.(type) {
case string:
value = fmt.Sprintf("'%s'", v)
case []byte:
value = fmt.Sprintf("'%s'", string(v))
default:
value = fmt.Sprintf("%v", v)
}
query = strings.Replace(query, "?", value, 1)
}
query = strings.ReplaceAll(query, "\t", "")
query = strings.ReplaceAll(query, "\n", " ")
return strings.Trim(query, " ")
}