generated from dogmatiq/template-go
/
process.go
160 lines (144 loc) · 3.04 KB
/
process.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
package postgres
import (
"context"
"database/sql"
"github.com/dogmatiq/infix/internal/x/sqlx"
"github.com/dogmatiq/infix/persistence"
)
// InsertProcessInstance inserts a process instance.
//
// It returns false if the row already exists.
func (driver) InsertProcessInstance(
ctx context.Context,
tx *sql.Tx,
ak string,
inst persistence.ProcessInstance,
) (_ bool, err error) {
defer sqlx.Recover(&err)
res := sqlx.Exec(
ctx,
tx,
`INSERT INTO infix.process_instance (
app_key,
handler_key,
instance_id,
media_type,
data
) VALUES (
$1, $2, $3, $4, $5
) ON CONFLICT (app_key, handler_key, instance_id) DO NOTHING`,
ak,
inst.HandlerKey,
inst.InstanceID,
inst.Packet.MediaType,
inst.Packet.Data,
)
n, err := res.RowsAffected()
return n == 1, err
}
// UpdateProcessInstance updates a process instance.
//
// It returns false if the row does not exist or inst.Revision is not current.
func (driver) UpdateProcessInstance(
ctx context.Context,
tx *sql.Tx,
ak string,
inst persistence.ProcessInstance,
) (_ bool, err error) {
defer sqlx.Recover(&err)
return sqlx.TryExecRow(
ctx,
tx,
`UPDATE infix.process_instance SET
revision = revision + 1,
media_type = $1,
data = $2
WHERE app_key = $3
AND handler_key = $4
AND instance_id = $5
AND revision = $6`,
inst.Packet.MediaType,
inst.Packet.Data,
ak,
inst.HandlerKey,
inst.InstanceID,
inst.Revision,
), nil
}
// DeleteProcessInstance deletes a process instance.
//
// It returns false if the row does not exist or inst.Revision is not current.
func (driver) DeleteProcessInstance(
ctx context.Context,
tx *sql.Tx,
ak string,
inst persistence.ProcessInstance,
) (_ bool, err error) {
defer sqlx.Recover(&err)
return sqlx.TryExecRow(
ctx,
tx,
`DELETE FROM infix.process_instance
WHERE app_key = $1
AND handler_key = $2
AND instance_id = $3
AND revision = $4`,
ak,
inst.HandlerKey,
inst.InstanceID,
inst.Revision,
), nil
}
// SelectProcessInstance selects a process instance's data.
func (driver) SelectProcessInstance(
ctx context.Context,
db *sql.DB,
ak, hk, id string,
) (persistence.ProcessInstance, error) {
row := db.QueryRowContext(
ctx,
`SELECT
revision,
media_type,
data
FROM infix.process_instance
WHERE app_key = $1
AND handler_key = $2
AND instance_id = $3`,
ak,
hk,
id,
)
inst := persistence.ProcessInstance{
HandlerKey: hk,
InstanceID: id,
}
err := row.Scan(
&inst.Revision,
&inst.Packet.MediaType,
&inst.Packet.Data,
)
if err == sql.ErrNoRows {
err = nil
}
if len(inst.Packet.Data) == 0 {
inst.Packet.Data = nil
}
return inst, err
}
// createOffsetSchema creates the schema elements for processes.
func createProcessSchema(ctx context.Context, db *sql.DB) {
sqlx.Exec(
ctx,
db,
`CREATE TABLE infix.process_instance (
app_key TEXT NOT NULL,
handler_key TEXT NOT NULL,
instance_id TEXT NOT NULL,
revision BIGINT NOT NULL DEFAULT 1,
media_type TEXT NOT NULL,
data BYTEA,
PRIMARY KEY (app_key, handler_key, instance_id)
)`,
)
}