-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpostgres.go
More file actions
504 lines (465 loc) · 15.2 KB
/
postgres.go
File metadata and controls
504 lines (465 loc) · 15.2 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package postgres
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"time"
"github.com/henvic/pgtools"
"github.com/henvic/pgxtutorial/database"
"github.com/henvic/pgxtutorial/inventory"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
)
// DB handles database communication with PostgreSQL.
type DB struct {
// pool for accessing Postgres database.PGX
pool *pgxpool.Pool
// log is a log for the operations.
log *slog.Logger
}
// NewDB creates a DB.
func NewDB(pool *pgxpool.Pool, logger *slog.Logger) DB {
return DB{
pool: pool,
log: logger,
}
}
// TransactionContext returns a copy of the parent context which begins a transaction
// to PostgreSQL.
//
// Once the transaction is over, you must call db.Commit(ctx) to make the changes effective.
// This might live in the go-pkg/postgres package later for the sake of code reuse.
func (db DB) TransactionContext(ctx context.Context) (context.Context, error) {
tx, err := db.conn(ctx).Begin(ctx)
if err != nil {
return nil, err
}
return context.WithValue(ctx, txCtx{}, tx), nil
}
// Commit transaction from context.
func (db DB) Commit(ctx context.Context) error {
if tx, ok := ctx.Value(txCtx{}).(pgx.Tx); ok && tx != nil {
return tx.Commit(ctx)
}
return errors.New("context has no transaction")
}
// Rollback transaction from context.
func (db DB) Rollback(ctx context.Context) error {
if tx, ok := ctx.Value(txCtx{}).(pgx.Tx); ok && tx != nil {
return tx.Rollback(ctx)
}
return errors.New("context has no transaction")
}
// WithAcquire returns a copy of the parent context which acquires a connection
// to PostgreSQL from pgxpool to make sure commands executed in series reuse the
// same database connection.
//
// To release the connection back to the pool, you must call postgres.Release(ctx).
//
// Example:
// dbCtx := db.WithAcquire(ctx)
// defer postgres.Release(dbCtx)
func (db DB) WithAcquire(ctx context.Context) (dbCtx context.Context, err error) {
if _, ok := ctx.Value(connCtx{}).(*pgxpool.Conn); ok {
panic("context already has a connection acquired")
}
res, err := db.pool.Acquire(ctx)
if err != nil {
return nil, err
}
return context.WithValue(ctx, connCtx{}, res), nil
}
// Release PostgreSQL connection acquired by context back to the pool.
func (db DB) Release(ctx context.Context) {
if res, ok := ctx.Value(connCtx{}).(*pgxpool.Conn); ok && res != nil {
res.Release()
}
}
// txCtx key.
type txCtx struct{}
// connCtx key.
type connCtx struct{}
// conn returns a PostgreSQL transaction if one exists.
// If not, returns a connection if a connection has been acquired by calling WithAcquire.
// Otherwise, it returns *pgxpool.Pool which acquires the connection and closes it immediately after a SQL command is executed.
func (db DB) conn(ctx context.Context) database.PGXQuerier {
if tx, ok := ctx.Value(txCtx{}).(pgx.Tx); ok && tx != nil {
return tx
}
if res, ok := ctx.Value(connCtx{}).(*pgxpool.Conn); ok && res != nil {
return res
}
return db.pool
}
var _ inventory.DB = (*DB)(nil) // Check if methods expected by inventory.DB are implemented correctly.
// CreateProduct creates a new product.
func (db DB) CreateProduct(ctx context.Context, params inventory.CreateProductParams) error {
const sql = `INSERT INTO product ("id", "name", "description", "price") VALUES ($1, $2, $3, $4);`
switch _, err := db.conn(ctx).Exec(ctx, sql, params.ID, params.Name, params.Description, params.Price); {
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
return err
case err != nil:
if sqlErr := db.productPgError(err); sqlErr != nil {
return sqlErr
}
db.log.Error("cannot create product on database", slog.Any("error", err))
return errors.New("cannot create product on database")
}
return nil
}
func (db DB) productPgError(err error) error {
var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) {
return nil
}
if pgErr.Code == pgerrcode.UniqueViolation {
return errors.New("product already exists")
}
if pgErr.Code == pgerrcode.CheckViolation {
switch pgErr.ConstraintName {
case "product_id_check":
return errors.New("invalid product ID")
case "product_name_check":
return errors.New("invalid product name")
case "product_price_check":
return errors.New("invalid price")
}
}
return nil
}
// ErrProductNotFound is returned when a product is not found.
var ErrProductNotFound = errors.New("product not found")
// UpdateProduct updates an existing product.
func (db DB) UpdateProduct(ctx context.Context, params inventory.UpdateProductParams) error {
const sql = `UPDATE "product" SET
"name" = COALESCE($1, "name"),
"description" = COALESCE($2, "description"),
"price" = COALESCE($3, "price"),
"modified_at" = now()
WHERE id = $4`
ct, err := db.conn(ctx).Exec(ctx, sql,
params.Name,
params.Description,
params.Price,
params.ID)
if err == context.Canceled || err == context.DeadlineExceeded {
return err
}
if err != nil {
if sqlErr := db.productPgError(err); sqlErr != nil {
return sqlErr
}
db.log.Error("cannot update product on database", slog.Any("error", err))
return errors.New("cannot update product on database")
}
if ct.RowsAffected() == 0 {
return ErrProductNotFound
}
return nil
}
// product table.
type product struct {
ID string
Name string
Description string
Price int
CreatedAt time.Time
ModifiedAt time.Time
}
func (p *product) dto() *inventory.Product {
return &inventory.Product{
ID: p.ID,
Name: p.Name,
Description: p.Description,
Price: p.Price,
CreatedAt: p.CreatedAt,
ModifiedAt: p.ModifiedAt,
}
}
// GetProduct returns a product.
func (db DB) GetProduct(ctx context.Context, id string) (*inventory.Product, error) {
var p product
// The following pgtools.Wildcard() call returns:
// "id","product_id","reviewer_id","title","description","score","created_at","modified_at"
sql := fmt.Sprintf(`SELECT %s FROM "product" WHERE id = $1 LIMIT 1`, pgtools.Wildcard(p)) // #nosec G201
rows, err := db.conn(ctx).Query(ctx, sql, id)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil, err
}
if err == nil {
p, err = pgx.CollectOneRow(rows, pgx.RowToStructByPos[product])
}
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
if err != nil {
db.log.Error("cannot get product from database",
slog.Any("id", id),
slog.Any("error", err),
)
return nil, errors.New("cannot get product from database")
}
return p.dto(), nil
}
// SearchProducts returns a list of products.
func (db DB) SearchProducts(ctx context.Context, params inventory.SearchProductsParams) (*inventory.SearchProductsResponse, error) {
var (
args = []any{"%" + params.QueryString + "%"}
w = []string{"name LIKE $1"}
)
if params.MinPrice != 0 {
args = append(args, params.MinPrice)
w = append(w, fmt.Sprintf(`"price" >= $%d`, len(args)))
}
if params.MaxPrice != 0 {
args = append(args, params.MaxPrice)
w = append(w, fmt.Sprintf(`"price" <= $%d`, len(args)))
}
where := strings.Join(w, " AND ")
sqlTotal := fmt.Sprintf(`SELECT COUNT(*) AS total FROM "product" WHERE %s`, where) // #nosec G201
resp := inventory.SearchProductsResponse{
Items: []*inventory.Product{},
}
switch err := db.conn(ctx).QueryRow(ctx, sqlTotal, args...).Scan(&resp.Total); {
case err == context.Canceled || err == context.DeadlineExceeded:
return nil, err
case err != nil:
db.log.Error("cannot get product count from the database", slog.Any("error", err))
return nil, errors.New("cannot get product")
}
// Once the count query was made, add pagination args and query the results of the current page.
sql := fmt.Sprintf(`SELECT * FROM "product" WHERE %s ORDER BY "id" DESC`, where) // #nosec G201
if params.Pagination.Limit != 0 {
args = append(args, params.Pagination.Limit)
sql += fmt.Sprintf(` LIMIT $%d`, len(args))
}
if params.Pagination.Offset != 0 {
args = append(args, params.Pagination.Offset)
sql += fmt.Sprintf(` OFFSET $%d`, len(args))
}
rows, err := db.conn(ctx).Query(ctx, sql, args...)
if err == context.Canceled || err == context.DeadlineExceeded {
return nil, err
}
var products []product
if err == nil {
products, err = pgx.CollectRows(rows, pgx.RowToStructByPos[product])
}
if err != nil {
db.log.Error("cannot get products from the database", slog.Any("error", err))
return nil, errors.New("cannot get products")
}
for _, p := range products {
resp.Items = append(resp.Items, p.dto())
}
return &resp, nil
}
// DeleteProduct from the database.
func (db DB) DeleteProduct(ctx context.Context, id string) error {
switch _, err := db.conn(ctx).Exec(ctx, `DELETE FROM "product" WHERE "id" = $1`, id); {
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
return err
case err != nil:
db.log.Error("cannot delete product from database", slog.Any("error", err))
return errors.New("cannot delete product from database")
}
return nil
}
// CreateProductReview for a given product.
func (db DB) CreateProductReview(ctx context.Context, params inventory.CreateProductReviewDBParams) error {
const sql = `
INSERT INTO review (
"id", "product_id", "reviewer_id",
"title", "description", "score"
)
VALUES (
$1, $2, $3,
$4, $5, $6
);`
switch _, err := db.conn(ctx).Exec(ctx, sql,
params.ID, params.ProductID, params.ReviewerID,
params.Title, params.Description, params.Score); {
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
return err
case err != nil:
if sqlErr := db.productReviewPgError(err); sqlErr != nil {
return sqlErr
}
db.log.Error("cannot create review on database", slog.Any("error", err))
return errors.New("cannot create review on database")
}
return nil
}
func (db DB) productReviewPgError(err error) error {
var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) {
return nil
}
if pgErr.Code == pgerrcode.UniqueViolation {
return errors.New("product review already exists")
}
if pgErr.Code == pgerrcode.ForeignKeyViolation && pgErr.ConstraintName == "review_product_id_fkey" {
return inventory.ErrCreateReviewNoProduct
}
if pgErr.Code == pgerrcode.CheckViolation {
switch pgErr.ConstraintName {
case "review_id_check":
return errors.New("invalid product review ID")
case "review_title_check":
return errors.New("invalid title")
case "review_score_check":
return errors.New("invalid score")
}
}
return nil
}
// ErrReviewNotFound is returned when a review is not found.
var ErrReviewNotFound = errors.New("product review not found")
// UpdateProductReview for a given product.
func (db DB) UpdateProductReview(ctx context.Context, params inventory.UpdateProductReviewParams) error {
const sql = `UPDATE "review" SET
"title" = COALESCE($1, "title"),
"score" = COALESCE($2, "score"),
"description" = COALESCE($3, "description"),
"modified_at" = now()
WHERE id = $4`
switch ct, err := db.conn(ctx).Exec(ctx, sql, params.Title, params.Score, params.Description, params.ID); {
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
return err
case err != nil:
if sqlErr := db.productReviewPgError(err); sqlErr != nil {
return sqlErr
}
db.log.Error("cannot update review on database", slog.Any("error", err))
return errors.New("cannot update review on database")
default:
if ct.RowsAffected() == 0 {
return ErrReviewNotFound
}
return nil
}
}
// review table.
type review struct {
ID string
ProductID string
ReviewerID string
Score int
Title string
Description string
CreatedAt time.Time
ModifiedAt time.Time
}
func (r *review) dto() *inventory.ProductReview {
return &inventory.ProductReview{
ID: r.ID,
ProductID: r.ProductID,
ReviewerID: r.ReviewerID,
Score: r.Score,
Title: r.Title,
Description: r.Description,
CreatedAt: r.CreatedAt,
ModifiedAt: r.ModifiedAt,
}
}
// GetProductReview gets a specific review.
func (db DB) GetProductReview(ctx context.Context, id string) (*inventory.ProductReview, error) {
// The following pgtools.Wildcard() call returns:
// "id","product_id","reviewer_id","title","description","score","created_at","modified_at"
var r review
sql := fmt.Sprintf(`SELECT %s FROM "review" WHERE id = $1 LIMIT 1`, pgtools.Wildcard(r)) // #nosec G201
rows, err := db.conn(ctx).Query(ctx, sql, id)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil, err
}
if err == nil {
r, err = pgx.CollectOneRow(rows, pgx.RowToStructByPos[review])
}
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
if err != nil {
db.log.Error("cannot get product review from database",
slog.Any("id", id),
slog.Any("error", err))
return nil, errors.New("cannot get product review from database")
}
return r.dto(), nil
}
// GetProductReviews gets reviews for a given product or from a given user.
func (db DB) GetProductReviews(ctx context.Context, params inventory.ProductReviewsParams) (*inventory.ProductReviewsResponse, error) {
var (
args []any
where []string
)
if params.ProductID != "" {
args = append(args, params.ProductID)
where = append(where, fmt.Sprintf(`"product_id" = $%d`, len(args)))
}
if params.ReviewerID != "" {
args = append(args, params.ReviewerID)
where = append(where, fmt.Sprintf(`"reviewer_id" = $%d`, len(args)))
}
sql := fmt.Sprintf(`SELECT %s FROM "review"`, pgtools.Wildcard(review{})) // #nosec G201
sqlTotal := `SELECT COUNT(*) AS total FROM "review"`
if len(where) > 0 {
w := " WHERE " + strings.Join(where, " AND ") // #nosec G202
sql += w
sqlTotal += w
}
resp := &inventory.ProductReviewsResponse{
Reviews: []*inventory.ProductReview{},
}
err := db.conn(ctx).QueryRow(ctx, sqlTotal, args...).Scan(&resp.Total)
if err == context.Canceled || err == context.DeadlineExceeded {
return nil, err
}
if err != nil {
db.log.Error("cannot get reviews count from the database", slog.Any("error", err))
return nil, errors.New("cannot get reviews")
}
// Once the count query was made, add pagination args and query the results of the current page.
sql += ` ORDER BY "created_at" DESC`
if params.Pagination.Limit != 0 {
args = append(args, params.Pagination.Limit)
sql += fmt.Sprintf(` LIMIT $%d`, len(args))
}
if params.Pagination.Offset != 0 {
args = append(args, params.Pagination.Offset)
sql += fmt.Sprintf(` OFFSET $%d`, len(args))
}
rows, err := db.conn(ctx).Query(ctx, sql, args...)
if err == context.Canceled || err == context.DeadlineExceeded {
return nil, err
}
var reviews []review
if err == nil {
reviews, err = pgx.CollectRows(rows, pgx.RowToStructByPos[review])
}
if err != nil {
db.log.Error("cannot get reviews from database", slog.Any("error", err))
return nil, errors.New("cannot get reviews")
}
for _, r := range reviews {
resp.Reviews = append(resp.Reviews, r.dto())
}
return resp, nil
}
// DeleteProductReview from the database.
func (db DB) DeleteProductReview(ctx context.Context, id string) error {
switch _, err := db.conn(ctx).Exec(ctx, `DELETE FROM "review" WHERE id = $1`, id); {
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
return err
case err != nil:
db.log.Error("cannot delete review from database",
slog.Any("id", id),
slog.Any("error", err),
)
return errors.New("cannot delete review from database")
}
return nil
}