Repository navigation
Expand file tree
/
Copy pathmultiserverskus.go
More file actions
63 lines (50 loc) · 1.57 KB
/
Copy pathmultiserverskus.go
File metadata and controls
63 lines (50 loc) · 1.57 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
package database
import (
"context"
_ "embed"
"errors"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type MultiServerSkus struct {
*pgxpool.Pool
}
var (
//go:embed sql/multi_server_skus/schema.sql
multiServerSkusSchema string
//go:embed sql/multi_server_skus/get_permitted_server_count.sql
multiServerSkusGetPermittedServerCount string
//go:embed sql/multi_server_skus/insert.sql
multiServerSkusInsert string
//go:embed sql/multi_server_skus/delete.sql
multiServerSkusDelete string
)
func newMultiServerSkusTable(db *pgxpool.Pool) *MultiServerSkus {
return &MultiServerSkus{
db,
}
}
func (MultiServerSkus) Schema() string {
return multiServerSkusSchema
}
// Upsert inserts or updates a multi-server SKU entry within a transaction.
func (m *MultiServerSkus) Upsert(ctx context.Context, tx pgx.Tx, skuId uuid.UUID, serversPermitted int) error {
_, err := tx.Exec(ctx, multiServerSkusInsert, skuId, serversPermitted)
return err
}
// DeleteBySku removes a multi-server SKU entry by its SKU ID within a transaction.
func (m *MultiServerSkus) DeleteBySku(ctx context.Context, tx pgx.Tx, skuId uuid.UUID) error {
_, err := tx.Exec(ctx, multiServerSkusDelete, skuId)
return err
}
func (m *MultiServerSkus) GetPermittedServerCount(ctx context.Context, tx pgx.Tx, skuId uuid.UUID) (int, bool, error) {
var count int
if err := tx.QueryRow(ctx, multiServerSkusGetPermittedServerCount, skuId).Scan(&count); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return 0, false, nil
}
return 0, false, err
}
return count, true, nil
}