Debian Codegraph @

ent/attestationpolicy_update.go

raw
1 // Code generated by ent, DO NOT EDIT.
2
3 package ent
4
5 import (
6 "context"
7 "errors"
8 "fmt"
9
10 "entgo.io/ent/dialect/sql"
11 "entgo.io/ent/dialect/sql/sqlgraph"
12 "entgo.io/ent/schema/field"
13 "github.com/google/uuid"
14 "github.com/in-toto/archivista/ent/attestationpolicy"
15 "github.com/in-toto/archivista/ent/predicate"
16 "github.com/in-toto/archivista/ent/statement"
17 )
18
19 // AttestationPolicyUpdate is the builder for updating AttestationPolicy entities.
20 type AttestationPolicyUpdate struct {
21 config
22 hooks []Hook
23 mutation *AttestationPolicyMutation
24 }
25
26 // Where appends a list predicates to the AttestationPolicyUpdate builder.
27 func (_u *AttestationPolicyUpdate) Where(ps ...predicate.AttestationPolicy) *AttestationPolicyUpdate {
28 _u.mutation.Where(ps...)
29 return _u
30 }
31
32 // SetName sets the "name" field.
33 func (_u *AttestationPolicyUpdate) SetName(v string) *AttestationPolicyUpdate {
34 _u.mutation.SetName(v)
35 return _u
36 }
37
38 // SetNillableName sets the "name" field if the given value is not nil.
39 func (_u *AttestationPolicyUpdate) SetNillableName(v *string) *AttestationPolicyUpdate {
40 if v != nil {
41 _u.SetName(*v)
42 }
43 return _u
44 }
45
46 // SetStatementID sets the "statement" edge to the Statement entity by ID.
47 func (_u *AttestationPolicyUpdate) SetStatementID(id uuid.UUID) *AttestationPolicyUpdate {
48 _u.mutation.SetStatementID(id)
49 return _u
50 }
51
52 // SetNillableStatementID sets the "statement" edge to the Statement entity by ID if the given value is not nil.
53 func (_u *AttestationPolicyUpdate) SetNillableStatementID(id *uuid.UUID) *AttestationPolicyUpdate {
54 if id != nil {
55 _u = _u.SetStatementID(*id)
56 }
57 return _u
58 }
59
60 // SetStatement sets the "statement" edge to the Statement entity.
61 func (_u *AttestationPolicyUpdate) SetStatement(v *Statement) *AttestationPolicyUpdate {
62 return _u.SetStatementID(v.ID)
63 }
64
65 // Mutation returns the AttestationPolicyMutation object of the builder.
66 func (_u *AttestationPolicyUpdate) Mutation() *AttestationPolicyMutation {
67 return _u.mutation
68 }
69
70 // ClearStatement clears the "statement" edge to the Statement entity.
71 func (_u *AttestationPolicyUpdate) ClearStatement() *AttestationPolicyUpdate {
72 _u.mutation.ClearStatement()
73 return _u
74 }
75
76 // Save executes the query and returns the number of nodes affected by the update operation.
77 func (_u *AttestationPolicyUpdate) Save(ctx context.Context) (int, error) {
78 return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
79 }
80
81 // SaveX is like Save, but panics if an error occurs.
82 func (_u *AttestationPolicyUpdate) SaveX(ctx context.Context) int {
83 affected, err := _u.Save(ctx)
84 if err != nil {
85 panic(err)
86 }
87 return affected
88 }
89
90 // Exec executes the query.
91 func (_u *AttestationPolicyUpdate) Exec(ctx context.Context) error {
92 _, err := _u.Save(ctx)
93 return err
94 }
95
96 // ExecX is like Exec, but panics if an error occurs.
97 func (_u *AttestationPolicyUpdate) ExecX(ctx context.Context) {
98 if err := _u.Exec(ctx); err != nil {
99 panic(err)
100 }
101 }
102
103 // check runs all checks and user-defined validators on the builder.
104 func (_u *AttestationPolicyUpdate) check() error {
105 if v, ok := _u.mutation.Name(); ok {
106 if err := attestationpolicy.NameValidator(v); err != nil {
107 return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "AttestationPolicy.name": %w`, err)}
108 }
109 }
110 return nil
111 }
112
113 func (_u *AttestationPolicyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
114 if err := _u.check(); err != nil {
115 return _node, err
116 }
117 _spec := sqlgraph.NewUpdateSpec(attestationpolicy.Table, attestationpolicy.Columns, sqlgraph.NewFieldSpec(attestationpolicy.FieldID, field.TypeUUID))
118 if ps := _u.mutation.predicates; len(ps) > 0 {
119 _spec.Predicate = func(selector *sql.Selector) {
120 for i := range ps {
121 ps[i](selector)
122 }
123 }
124 }
125 if value, ok := _u.mutation.Name(); ok {
126 _spec.SetField(attestationpolicy.FieldName, field.TypeString, value)
127 }
128 if _u.mutation.StatementCleared() {
129 edge := &sqlgraph.EdgeSpec{
130 Rel: sqlgraph.O2O,
131 Inverse: true,
132 Table: attestationpolicy.StatementTable,
133 Columns: []string{attestationpolicy.StatementColumn},
134 Bidi: false,
135 Target: &sqlgraph.EdgeTarget{
136 IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
137 },
138 }
139 _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
140 }
141 if nodes := _u.mutation.StatementIDs(); len(nodes) > 0 {
142 edge := &sqlgraph.EdgeSpec{
143 Rel: sqlgraph.O2O,
144 Inverse: true,
145 Table: attestationpolicy.StatementTable,
146 Columns: []string{attestationpolicy.StatementColumn},
147 Bidi: false,
148 Target: &sqlgraph.EdgeTarget{
149 IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
150 },
151 }
152 for _, k := range nodes {
153 edge.Target.Nodes = append(edge.Target.Nodes, k)
154 }
155 _spec.Edges.Add = append(_spec.Edges.Add, edge)
156 }
157 if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
158 if _, ok := err.(*sqlgraph.NotFoundError); ok {
159 err = &NotFoundError{attestationpolicy.Label}
160 } else if sqlgraph.IsConstraintError(err) {
161 err = &ConstraintError{msg: err.Error(), wrap: err}
162 }
163 return 0, err
164 }
165 _u.mutation.done = true
166 return _node, nil
167 }
168
169 // AttestationPolicyUpdateOne is the builder for updating a single AttestationPolicy entity.
170 type AttestationPolicyUpdateOne struct {
171 config
172 fields []string
173 hooks []Hook
174 mutation *AttestationPolicyMutation
175 }
176
177 // SetName sets the "name" field.
178 func (_u *AttestationPolicyUpdateOne) SetName(v string) *AttestationPolicyUpdateOne {
179 _u.mutation.SetName(v)
180 return _u
181 }
182
183 // SetNillableName sets the "name" field if the given value is not nil.
184 func (_u *AttestationPolicyUpdateOne) SetNillableName(v *string) *AttestationPolicyUpdateOne {
185 if v != nil {
186 _u.SetName(*v)
187 }
188 return _u
189 }
190
191 // SetStatementID sets the "statement" edge to the Statement entity by ID.
192 func (_u *AttestationPolicyUpdateOne) SetStatementID(id uuid.UUID) *AttestationPolicyUpdateOne {
193 _u.mutation.SetStatementID(id)
194 return _u
195 }
196
197 // SetNillableStatementID sets the "statement" edge to the Statement entity by ID if the given value is not nil.
198 func (_u *AttestationPolicyUpdateOne) SetNillableStatementID(id *uuid.UUID) *AttestationPolicyUpdateOne {
199 if id != nil {
200 _u = _u.SetStatementID(*id)
201 }
202 return _u
203 }
204
205 // SetStatement sets the "statement" edge to the Statement entity.
206 func (_u *AttestationPolicyUpdateOne) SetStatement(v *Statement) *AttestationPolicyUpdateOne {
207 return _u.SetStatementID(v.ID)
208 }
209
210 // Mutation returns the AttestationPolicyMutation object of the builder.
211 func (_u *AttestationPolicyUpdateOne) Mutation() *AttestationPolicyMutation {
212 return _u.mutation
213 }
214
215 // ClearStatement clears the "statement" edge to the Statement entity.
216 func (_u *AttestationPolicyUpdateOne) ClearStatement() *AttestationPolicyUpdateOne {
217 _u.mutation.ClearStatement()
218 return _u
219 }
220
221 // Where appends a list predicates to the AttestationPolicyUpdate builder.
222 func (_u *AttestationPolicyUpdateOne) Where(ps ...predicate.AttestationPolicy) *AttestationPolicyUpdateOne {
223 _u.mutation.Where(ps...)
224 return _u
225 }
226
227 // Select allows selecting one or more fields (columns) of the returned entity.
228 // The default is selecting all fields defined in the entity schema.
229 func (_u *AttestationPolicyUpdateOne) Select(field string, fields ...string) *AttestationPolicyUpdateOne {
230 _u.fields = append([]string{field}, fields...)
231 return _u
232 }
233
234 // Save executes the query and returns the updated AttestationPolicy entity.
235 func (_u *AttestationPolicyUpdateOne) Save(ctx context.Context) (*AttestationPolicy, error) {
236 return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
237 }
238
239 // SaveX is like Save, but panics if an error occurs.
240 func (_u *AttestationPolicyUpdateOne) SaveX(ctx context.Context) *AttestationPolicy {
241 node, err := _u.Save(ctx)
242 if err != nil {
243 panic(err)
244 }
245 return node
246 }
247
248 // Exec executes the query on the entity.
249 func (_u *AttestationPolicyUpdateOne) Exec(ctx context.Context) error {
250 _, err := _u.Save(ctx)
251 return err
252 }
253
254 // ExecX is like Exec, but panics if an error occurs.
255 func (_u *AttestationPolicyUpdateOne) ExecX(ctx context.Context) {
256 if err := _u.Exec(ctx); err != nil {
257 panic(err)
258 }
259 }
260
261 // check runs all checks and user-defined validators on the builder.
262 func (_u *AttestationPolicyUpdateOne) check() error {
263 if v, ok := _u.mutation.Name(); ok {
264 if err := attestationpolicy.NameValidator(v); err != nil {
265 return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "AttestationPolicy.name": %w`, err)}
266 }
267 }
268 return nil
269 }
270
271 func (_u *AttestationPolicyUpdateOne) sqlSave(ctx context.Context) (_node *AttestationPolicy, err error) {
272 if err := _u.check(); err != nil {
273 return _node, err
274 }
275 _spec := sqlgraph.NewUpdateSpec(attestationpolicy.Table, attestationpolicy.Columns, sqlgraph.NewFieldSpec(attestationpolicy.FieldID, field.TypeUUID))
276 id, ok := _u.mutation.ID()
277 if !ok {
278 return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AttestationPolicy.id" for update`)}
279 }
280 _spec.Node.ID.Value = id
281 if fields := _u.fields; len(fields) > 0 {
282 _spec.Node.Columns = make([]string, 0, len(fields))
283 _spec.Node.Columns = append(_spec.Node.Columns, attestationpolicy.FieldID)
284 for _, f := range fields {
285 if !attestationpolicy.ValidColumn(f) {
286 return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
287 }
288 if f != attestationpolicy.FieldID {
289 _spec.Node.Columns = append(_spec.Node.Columns, f)
290 }
291 }
292 }
293 if ps := _u.mutation.predicates; len(ps) > 0 {
294 _spec.Predicate = func(selector *sql.Selector) {
295 for i := range ps {
296 ps[i](selector)
297 }
298 }
299 }
300 if value, ok := _u.mutation.Name(); ok {
301 _spec.SetField(attestationpolicy.FieldName, field.TypeString, value)
302 }
303 if _u.mutation.StatementCleared() {
304 edge := &sqlgraph.EdgeSpec{
305 Rel: sqlgraph.O2O,
306 Inverse: true,
307 Table: attestationpolicy.StatementTable,
308 Columns: []string{attestationpolicy.StatementColumn},
309 Bidi: false,
310 Target: &sqlgraph.EdgeTarget{
311 IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
312 },
313 }
314 _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
315 }
316 if nodes := _u.mutation.StatementIDs(); len(nodes) > 0 {
317 edge := &sqlgraph.EdgeSpec{
318 Rel: sqlgraph.O2O,
319 Inverse: true,
320 Table: attestationpolicy.StatementTable,
321 Columns: []string{attestationpolicy.StatementColumn},
322 Bidi: false,
323 Target: &sqlgraph.EdgeTarget{
324 IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
325 },
326 }
327 for _, k := range nodes {
328 edge.Target.Nodes = append(edge.Target.Nodes, k)
329 }
330 _spec.Edges.Add = append(_spec.Edges.Add, edge)
331 }
332 _node = &AttestationPolicy{config: _u.config}
333 _spec.Assign = _node.assignValues
334 _spec.ScanValues = _node.scanValues
335 if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
336 if _, ok := err.(*sqlgraph.NotFoundError); ok {
337 err = &NotFoundError{attestationpolicy.Label}
338 } else if sqlgraph.IsConstraintError(err) {
339 err = &ConstraintError{msg: err.Error(), wrap: err}
340 }
341 return nil, err
342 }
343 _u.mutation.done = true
344 return _node, nil
345 }