-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_cross_workflow_link.go
More file actions
116 lines (103 loc) · 3.11 KB
/
pg_cross_workflow_link.go
File metadata and controls
116 lines (103 loc) · 3.11 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
package store
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
// PGCrossWorkflowLinkStore implements CrossWorkflowLinkStore backed by PostgreSQL.
type PGCrossWorkflowLinkStore struct {
pool *pgxpool.Pool
}
func (s *PGCrossWorkflowLinkStore) Create(ctx context.Context, l *CrossWorkflowLink) error {
if l.ID == uuid.Nil {
l.ID = uuid.New()
}
_, err := s.pool.Exec(ctx, `
INSERT INTO cross_workflow_links (id, source_workflow_id, target_workflow_id,
link_type, config, created_by, created_at)
VALUES ($1,$2,$3,$4,$5,$6,NOW())`,
l.ID, l.SourceWorkflowID, l.TargetWorkflowID,
l.LinkType, l.Config, l.CreatedBy)
if err != nil {
if isDuplicateError(err) {
return fmt.Errorf("%w: cross-workflow link already exists", ErrDuplicate)
}
return fmt.Errorf("insert cross-workflow link: %w", err)
}
return nil
}
func (s *PGCrossWorkflowLinkStore) Get(ctx context.Context, id uuid.UUID) (*CrossWorkflowLink, error) {
rows, err := s.pool.Query(ctx, `SELECT * FROM cross_workflow_links WHERE id = $1`, id)
if err != nil {
return nil, fmt.Errorf("query cross-workflow link: %w", err)
}
defer rows.Close()
if !rows.Next() {
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("query cross-workflow link: %w", err)
}
return nil, ErrNotFound
}
return scanCrossWorkflowLink(rows)
}
func (s *PGCrossWorkflowLinkStore) Delete(ctx context.Context, id uuid.UUID) error {
tag, err := s.pool.Exec(ctx, `DELETE FROM cross_workflow_links WHERE id = $1`, id)
if err != nil {
return fmt.Errorf("delete cross-workflow link: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrNotFound
}
return nil
}
func (s *PGCrossWorkflowLinkStore) List(ctx context.Context, f CrossWorkflowLinkFilter) ([]*CrossWorkflowLink, error) {
query := `SELECT * FROM cross_workflow_links WHERE 1=1`
args := []any{}
idx := 1
if f.SourceWorkflowID != nil {
query += fmt.Sprintf(` AND source_workflow_id = $%d`, idx)
args = append(args, *f.SourceWorkflowID)
idx++
}
if f.TargetWorkflowID != nil {
query += fmt.Sprintf(` AND target_workflow_id = $%d`, idx)
args = append(args, *f.TargetWorkflowID)
idx++
}
if f.LinkType != "" {
query += fmt.Sprintf(` AND link_type = $%d`, idx)
args = append(args, f.LinkType)
idx++
}
query += fmt.Sprintf(` ORDER BY created_at DESC LIMIT $%d OFFSET $%d`, idx, idx+1)
limit := f.Pagination.Limit
if limit <= 0 {
limit = 50
}
args = append(args, limit, f.Pagination.Offset)
rows, err := s.pool.Query(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("list cross-workflow links: %w", err)
}
defer rows.Close()
var links []*CrossWorkflowLink
for rows.Next() {
l, err := scanCrossWorkflowLink(rows)
if err != nil {
return nil, err
}
links = append(links, l)
}
return links, rows.Err()
}
func scanCrossWorkflowLink(rows pgx.Rows) (*CrossWorkflowLink, error) {
var l CrossWorkflowLink
err := rows.Scan(&l.ID, &l.SourceWorkflowID, &l.TargetWorkflowID,
&l.LinkType, &l.Config, &l.CreatedBy, &l.CreatedAt)
if err != nil {
return nil, fmt.Errorf("scan cross-workflow link: %w", err)
}
return &l, nil
}