-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
certificate_projects.mapper.go
202 lines (157 loc) · 5.79 KB
/
certificate_projects.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
//go:build linux && cgo && !agent
package cluster
// The code below was generated by incus-generate - DO NOT EDIT!
import (
"context"
"database/sql"
"fmt"
"github.com/lxc/incus/internal/server/db/query"
"github.com/lxc/incus/shared/api"
)
var _ = api.ServerEnvironment{}
var certificateProjectObjects = RegisterStmt(`
SELECT certificates_projects.certificate_id, certificates_projects.project_id
FROM certificates_projects
ORDER BY certificates_projects.certificate_id
`)
var certificateProjectObjectsByCertificateID = RegisterStmt(`
SELECT certificates_projects.certificate_id, certificates_projects.project_id
FROM certificates_projects
WHERE ( certificates_projects.certificate_id = ? )
ORDER BY certificates_projects.certificate_id
`)
var certificateProjectCreate = RegisterStmt(`
INSERT INTO certificates_projects (certificate_id, project_id)
VALUES (?, ?)
`)
var certificateProjectDeleteByCertificateID = RegisterStmt(`
DELETE FROM certificates_projects WHERE certificate_id = ?
`)
// certificateProjectColumns 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 CertificateProject entity.
func certificateProjectColumns() string {
return "certificates_projects.certificate_id, certificates_projects.project_id"
}
// getCertificateProjects can be used to run handwritten sql.Stmts to return a slice of objects.
func getCertificateProjects(ctx context.Context, stmt *sql.Stmt, args ...any) ([]CertificateProject, error) {
objects := make([]CertificateProject, 0)
dest := func(scan func(dest ...any) error) error {
c := CertificateProject{}
err := scan(&c.CertificateID, &c.ProjectID)
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 \"certificates_projects\" table: %w", err)
}
return objects, nil
}
// getCertificateProjectsRaw can be used to run handwritten query strings to return a slice of objects.
func getCertificateProjectsRaw(ctx context.Context, tx *sql.Tx, sql string, args ...any) ([]CertificateProject, error) {
objects := make([]CertificateProject, 0)
dest := func(scan func(dest ...any) error) error {
c := CertificateProject{}
err := scan(&c.CertificateID, &c.ProjectID)
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 \"certificates_projects\" table: %w", err)
}
return objects, nil
}
// GetCertificateProjects returns all available Projects for the Certificate.
// generator: certificate_project GetMany
func GetCertificateProjects(ctx context.Context, tx *sql.Tx, certificateID int) ([]Project, error) {
var err error
// Result slice.
objects := make([]CertificateProject, 0)
sqlStmt, err := Stmt(tx, certificateProjectObjectsByCertificateID)
if err != nil {
return nil, fmt.Errorf("Failed to get \"certificateProjectObjectsByCertificateID\" prepared statement: %w", err)
}
args := []any{certificateID}
// Select.
objects, err = getCertificateProjects(ctx, sqlStmt, args...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"certificates_projects\" table: %w", err)
}
result := make([]Project, len(objects))
for i, object := range objects {
project, err := GetProjects(ctx, tx, ProjectFilter{ID: &object.ProjectID})
if err != nil {
return nil, err
}
result[i] = project[0]
}
return result, nil
}
// DeleteCertificateProjects deletes the certificate_project matching the given key parameters.
// generator: certificate_project DeleteMany
func DeleteCertificateProjects(ctx context.Context, tx *sql.Tx, certificateID int) error {
stmt, err := Stmt(tx, certificateProjectDeleteByCertificateID)
if err != nil {
return fmt.Errorf("Failed to get \"certificateProjectDeleteByCertificateID\" prepared statement: %w", err)
}
result, err := stmt.Exec(int(certificateID))
if err != nil {
return fmt.Errorf("Delete \"certificates_projects\" entry failed: %w", err)
}
_, err = result.RowsAffected()
if err != nil {
return fmt.Errorf("Fetch affected rows: %w", err)
}
return nil
}
// CreateCertificateProjects adds a new certificate_project to the database.
// generator: certificate_project Create
func CreateCertificateProjects(ctx context.Context, tx *sql.Tx, objects []CertificateProject) error {
for _, object := range objects {
args := make([]any, 2)
// Populate the statement arguments.
args[0] = object.CertificateID
args[1] = object.ProjectID
// Prepared statement to use.
stmt, err := Stmt(tx, certificateProjectCreate)
if err != nil {
return fmt.Errorf("Failed to get \"certificateProjectCreate\" prepared statement: %w", err)
}
// Execute the statement.
_, err = stmt.Exec(args...)
if err != nil {
return fmt.Errorf("Failed to create \"certificates_projects\" entry: %w", err)
}
}
return nil
}
// UpdateCertificateProjects updates the certificate_project matching the given key parameters.
// generator: certificate_project Update
func UpdateCertificateProjects(ctx context.Context, tx *sql.Tx, certificateID int, projectNames []string) error {
// Delete current entry.
err := DeleteCertificateProjects(ctx, tx, certificateID)
if err != nil {
return err
}
// Get new entry IDs.
certificateProjects := make([]CertificateProject, 0, len(projectNames))
for _, entry := range projectNames {
refID, err := GetProjectID(ctx, tx, entry)
if err != nil {
return err
}
certificateProjects = append(certificateProjects, CertificateProject{CertificateID: certificateID, ProjectID: int(refID)})
}
err = CreateCertificateProjects(ctx, tx, certificateProjects)
if err != nil {
return err
}
return nil
}