/
instance_profiles.mapper.go
216 lines (167 loc) · 6.19 KB
/
instance_profiles.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 lxd-generate - DO NOT EDIT!
import (
"context"
"database/sql"
"fmt"
"github.com/canonical/lxd/lxd/db/query"
"github.com/canonical/lxd/shared/api"
)
var _ = api.ServerEnvironment{}
var instanceProfileObjects = RegisterStmt(`
SELECT instances_profiles.instance_id, instances_profiles.profile_id, instances_profiles.apply_order
FROM instances_profiles
ORDER BY instances_profiles.instance_id, instances_profiles.apply_order
`)
var instanceProfileObjectsByProfileID = RegisterStmt(`
SELECT instances_profiles.instance_id, instances_profiles.profile_id, instances_profiles.apply_order
FROM instances_profiles
WHERE ( instances_profiles.profile_id = ? )
ORDER BY instances_profiles.instance_id, instances_profiles.apply_order
`)
var instanceProfileObjectsByInstanceID = RegisterStmt(`
SELECT instances_profiles.instance_id, instances_profiles.profile_id, instances_profiles.apply_order
FROM instances_profiles
WHERE ( instances_profiles.instance_id = ? )
ORDER BY instances_profiles.instance_id, instances_profiles.apply_order
`)
var instanceProfileCreate = RegisterStmt(`
INSERT INTO instances_profiles (instance_id, profile_id, apply_order)
VALUES (?, ?, ?)
`)
var instanceProfileDeleteByInstanceID = RegisterStmt(`
DELETE FROM instances_profiles WHERE instance_id = ?
`)
// GetProfileInstances returns all available Instances for the Profile.
// generator: instance_profile GetMany
func GetProfileInstances(ctx context.Context, tx *sql.Tx, profileID int) ([]Instance, error) {
var err error
// Result slice.
objects := make([]InstanceProfile, 0)
sqlStmt, err := Stmt(tx, instanceProfileObjectsByProfileID)
if err != nil {
return nil, fmt.Errorf("Failed to get \"instanceProfileObjectsByProfileID\" prepared statement: %w", err)
}
args := []any{profileID}
// Select.
objects, err = getInstanceProfiles(ctx, sqlStmt, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"instances_profiles\" table: %w", err)
}
result := make([]Instance, len(objects))
for i, object := range objects {
instance, err := GetInstances(ctx, tx, InstanceFilter{ID: &object.InstanceID})
if err != nil {
return nil, err
}
result[i] = instance[0]
}
return result, nil
}
// instanceProfileColumns 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 InstanceProfile entity.
func instanceProfileColumns() string {
return "instances_profiles.instance_id, instances_profiles.profile_id, instances_profiles.apply_order"
}
// getInstanceProfiles can be used to run handwritten sql.Stmts to return a slice of objects.
func getInstanceProfiles(ctx context.Context, stmt *sql.Stmt, args ...any) ([]InstanceProfile, error) {
objects := make([]InstanceProfile, 0)
dest := func(scan func(dest ...any) error) error {
i := InstanceProfile{}
err := scan(&i.InstanceID, &i.ProfileID, &i.ApplyOrder)
if err != nil {
return err
}
objects = append(objects, i)
return nil
}
err := query.SelectObjects(ctx, stmt, dest, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"instances_profiles\" table: %w", err)
}
return objects, nil
}
// getInstanceProfilesRaw can be used to run handwritten query strings to return a slice of objects.
func getInstanceProfilesRaw(ctx context.Context, tx *sql.Tx, sql string, args ...any) ([]InstanceProfile, error) {
objects := make([]InstanceProfile, 0)
dest := func(scan func(dest ...any) error) error {
i := InstanceProfile{}
err := scan(&i.InstanceID, &i.ProfileID, &i.ApplyOrder)
if err != nil {
return err
}
objects = append(objects, i)
return nil
}
err := query.Scan(ctx, tx, sql, dest, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"instances_profiles\" table: %w", err)
}
return objects, nil
}
// GetInstanceProfiles returns all available Profiles for the Instance.
// generator: instance_profile GetMany
func GetInstanceProfiles(ctx context.Context, tx *sql.Tx, instanceID int) ([]Profile, error) {
var err error
// Result slice.
objects := make([]InstanceProfile, 0)
sqlStmt, err := Stmt(tx, instanceProfileObjectsByInstanceID)
if err != nil {
return nil, fmt.Errorf("Failed to get \"instanceProfileObjectsByInstanceID\" prepared statement: %w", err)
}
args := []any{instanceID}
// Select.
objects, err = getInstanceProfiles(ctx, sqlStmt, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"instances_profiles\" table: %w", err)
}
result := make([]Profile, len(objects))
for i, object := range objects {
profile, err := GetProfiles(ctx, tx, ProfileFilter{ID: &object.ProfileID})
if err != nil {
return nil, err
}
result[i] = profile[0]
}
return result, nil
}
// CreateInstanceProfiles adds a new instance_profile to the database.
// generator: instance_profile Create
func CreateInstanceProfiles(ctx context.Context, tx *sql.Tx, objects []InstanceProfile) error {
for _, object := range objects {
args := make([]any, 3)
// Populate the statement arguments.
args[0] = object.InstanceID
args[1] = object.ProfileID
args[2] = object.ApplyOrder
// Prepared statement to use.
stmt, err := Stmt(tx, instanceProfileCreate)
if err != nil {
return fmt.Errorf("Failed to get \"instanceProfileCreate\" prepared statement: %w", err)
}
// Execute the statement.
_, err = stmt.Exec(args...)
if err != nil {
return fmt.Errorf("Failed to create \"instances_profiles\" entry: %w", err)
}
}
return nil
}
// DeleteInstanceProfiles deletes the instance_profile matching the given key parameters.
// generator: instance_profile DeleteMany
func DeleteInstanceProfiles(ctx context.Context, tx *sql.Tx, instanceID int) error {
stmt, err := Stmt(tx, instanceProfileDeleteByInstanceID)
if err != nil {
return fmt.Errorf("Failed to get \"instanceProfileDeleteByInstanceID\" prepared statement: %w", err)
}
result, err := stmt.Exec(int(instanceID))
if err != nil {
return fmt.Errorf("Delete \"instances_profiles\" entry failed: %w", err)
}
_, err = result.RowsAffected()
if err != nil {
return fmt.Errorf("Fetch affected rows: %w", err)
}
return nil
}