Debian Codegraph @

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