Debian Codegraph @

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