-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
config.mapper.go
216 lines (170 loc) · 5.85 KB
/
config.mapper.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
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
//go:build linux && cgo && !agent
package cluster
// The code below was generated by incus-generate - DO NOT EDIT!
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/lxc/incus/internal/server/db/query"
"github.com/lxc/incus/shared/api"
)
var _ = api.ServerEnvironment{}
const configObjects = `SELECT %s_config.id, %s_config.%s_id, %s_config.key, %s_config.value
FROM %s_config
ORDER BY %s_config.id`
const configCreate = `INSERT INTO %s_config (%s_id, key, value)
VALUES (?, ?, ?)`
const configDelete = `DELETE FROM %s_config WHERE %s_id = ?`
// configColumns returns a string of column names to be used with a SELECT statement for the entity.
// Use this function when building statements to retrieve database entries matching the Config entity.
func configColumns() string {
return "%s_config.id, %s_config.%s_id, %s_config.key, %s_config.value"
}
// getConfig can be used to run handwritten sql.Stmts to return a slice of objects.
func getConfig(ctx context.Context, stmt *sql.Stmt, parent string, args ...any) ([]Config, error) {
objects := make([]Config, 0)
dest := func(scan func(dest ...any) error) error {
c := Config{}
err := scan(&c.ID, &c.ReferenceID, &c.Key, &c.Value)
if err != nil {
return err
}
objects = append(objects, c)
return nil
}
err := query.SelectObjects(ctx, stmt, dest, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"%s_config\" table: %w", parent, err)
}
return objects, nil
}
// getConfigRaw can be used to run handwritten query strings to return a slice of objects.
func getConfigRaw(ctx context.Context, tx *sql.Tx, sql string, parent string, args ...any) ([]Config, error) {
objects := make([]Config, 0)
dest := func(scan func(dest ...any) error) error {
c := Config{}
err := scan(&c.ID, &c.ReferenceID, &c.Key, &c.Value)
if err != nil {
return err
}
objects = append(objects, c)
return nil
}
err := query.Scan(ctx, tx, sql, dest, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"%s_config\" table: %w", parent, err)
}
return objects, nil
}
// GetConfig returns all available config.
// generator: config GetMany
func GetConfig(ctx context.Context, tx *sql.Tx, parent string, filters ...ConfigFilter) (map[int]map[string]string, error) {
var err error
// Result slice.
objects := make([]Config, 0)
configObjectsLocal := strings.Replace(configObjects, "%s_id", fmt.Sprintf("%s_id", parent), -1)
fillParent := make([]any, strings.Count(configObjectsLocal, "%s"))
for i := range fillParent {
fillParent[i] = strings.Replace(parent, "_", "s_", -1) + "s"
}
queryStr := fmt.Sprintf(configObjectsLocal, fillParent...)
queryParts := strings.SplitN(queryStr, "ORDER BY", 2)
args := []any{}
for i, filter := range filters {
var cond string
if i == 0 {
cond = " WHERE ( %s )"
} else {
cond = " OR ( %s )"
}
entries := []string{}
if filter.Key != nil {
entries = append(entries, "key = ?")
args = append(args, filter.Key)
}
if filter.Value != nil {
entries = append(entries, "value = ?")
args = append(args, filter.Value)
}
if len(entries) == 0 {
return nil, fmt.Errorf("Cannot filter on empty ConfigFilter")
}
queryParts[0] += fmt.Sprintf(cond, strings.Join(entries, " AND "))
}
queryStr = strings.Join(queryParts, " ORDER BY")
// Select.
objects, err = getConfigRaw(ctx, tx, queryStr, parent, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"%s_config\" table: %w", parent, err)
}
resultMap := map[int]map[string]string{}
for _, object := range objects {
_, ok := resultMap[object.ReferenceID]
if !ok {
resultMap[object.ReferenceID] = map[string]string{}
}
resultMap[object.ReferenceID][object.Key] = object.Value
}
return resultMap, nil
}
// CreateConfig adds a new config to the database.
// generator: config Create
func CreateConfig(ctx context.Context, tx *sql.Tx, parent string, object Config) error {
// An empty value means we are unsetting this key, so just return.
if object.Value == "" {
return nil
}
configCreateLocal := strings.Replace(configCreate, "%s_id", fmt.Sprintf("%s_id", parent), -1)
fillParent := make([]any, strings.Count(configCreateLocal, "%s"))
for i := range fillParent {
fillParent[i] = strings.Replace(parent, "_", "s_", -1) + "s"
}
queryStr := fmt.Sprintf(configCreateLocal, fillParent...)
_, err := tx.ExecContext(ctx, queryStr, object.ReferenceID, object.Key, object.Value)
if err != nil {
return fmt.Errorf("Insert failed for \"%s_config\" table: %w", parent, err)
}
return nil
}
// UpdateConfig updates the config matching the given key parameters.
// generator: config Update
func UpdateConfig(ctx context.Context, tx *sql.Tx, parent string, referenceID int, config map[string]string) error {
// Delete current entry.
err := DeleteConfig(ctx, tx, parent, referenceID)
if err != nil {
return err
}
// Insert new entries.
for key, value := range config {
object := Config{
ReferenceID: referenceID,
Key: key,
Value: value,
}
err = CreateConfig(ctx, tx, parent, object)
if err != nil {
return err
}
}
return nil
}
// DeleteConfig deletes the config matching the given key parameters.
// generator: config DeleteMany
func DeleteConfig(ctx context.Context, tx *sql.Tx, parent string, referenceID int) error {
configDeleteLocal := strings.Replace(configDelete, "%s_id", fmt.Sprintf("%s_id", parent), -1)
fillParent := make([]any, strings.Count(configDeleteLocal, "%s"))
for i := range fillParent {
fillParent[i] = strings.Replace(parent, "_", "s_", -1) + "s"
}
queryStr := fmt.Sprintf(configDeleteLocal, fillParent...)
result, err := tx.ExecContext(ctx, queryStr, referenceID)
if err != nil {
return fmt.Errorf("Delete entry for \"%s_config\" failed: %w", parent, err)
}
_, err = result.RowsAffected()
if err != nil {
return fmt.Errorf("Fetch affected rows: %w", err)
}
return nil
}