-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
103 lines (87 loc) · 2.5 KB
/
update.go
File metadata and controls
103 lines (87 loc) · 2.5 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
package debefix
import (
"context"
"errors"
)
// UpdateQuery returns the list of rows that should be updated.
type UpdateQuery interface {
Rows(ctx context.Context, resolvedData *ResolvedData) ([]UpdateData, error)
}
// UpdateAction do an update on a row returned by UpdateQuery.
type UpdateAction interface {
UpdateRow(ctx context.Context, resolvedData *ResolvedData, tableID TableID, row *Row) error
}
// UpdateData represents one row of one table to be updated.
type UpdateData struct {
TableID TableID
KeyFields []string
Row *Row
}
// Update is an updated query and its corresponding action.
type Update struct {
Query UpdateQuery
Action UpdateAction
}
// UpdateQueryQueryRow wraps a QueryRow in an UpdateQuery.
type UpdateQueryQueryRow struct {
QueryRow QueryRow
KeyFields []string
}
// UpdateQueryQueryRows wraps a QueryRows in an UpdateQuery.
type UpdateQueryQueryRows struct {
QueryRows QueryRows
KeyFields []string
}
// UpdateQueryRow wraps a QueryRow in an UpdateQuery.
func UpdateQueryRow(queryRow QueryRow, keyFields []string) *UpdateQueryQueryRow {
return &UpdateQueryQueryRow{
QueryRow: queryRow,
KeyFields: keyFields,
}
}
// UpdateQueryRows wraps a QueryRows in an UpdateQuery.
func UpdateQueryRows(queryRows QueryRows, keyFields []string) *UpdateQueryQueryRows {
return &UpdateQueryQueryRows{
QueryRows: queryRows,
KeyFields: keyFields,
}
}
func (u UpdateQueryQueryRow) Rows(ctx context.Context, resolvedData *ResolvedData) ([]UpdateData, error) {
row, err := u.QueryRow.QueryRow(&resolvedData.Data)
if err != nil {
if errors.Is(err, ErrNotFound) {
return nil, nil
}
return nil, err
}
return []UpdateData{{
TableID: row.TableID,
KeyFields: u.KeyFields,
Row: row.Row,
}}, nil
}
func (u UpdateQueryQueryRows) Rows(ctx context.Context, resolvedData *ResolvedData) ([]UpdateData, error) {
rows, err := u.QueryRows.QueryRows(&resolvedData.Data)
if err != nil {
return nil, err
}
var ret []UpdateData
for _, row := range rows {
ret = append(ret, UpdateData{
TableID: row.TableID,
KeyFields: u.KeyFields,
Row: row.Row,
})
}
return ret, nil
}
// UpdateActionSetValues is an update action which sets the field values in Values to the row being updated.
type UpdateActionSetValues struct {
Values Values
}
func (u UpdateActionSetValues) UpdateRow(ctx context.Context, resolvedData *ResolvedData, tableID TableID, row *Row) error {
for fieldName, fieldValue := range u.Values.All {
row.Values.Set(fieldName, fieldValue)
}
return nil
}