plc.directory mirror
1// Code generated by ent, DO NOT EDIT.
2
3package ent
4
5import (
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 "tangled.sh/seiso.moe/aletheia.directory/ent/operation"
15 "tangled.sh/seiso.moe/aletheia.directory/ent/predicate"
16 "tangled.sh/seiso.moe/aletheia.directory/pkg/plc"
17)
18
19// OperationUpdate is the builder for updating Operation entities.
20type OperationUpdate struct {
21 config
22 hooks []Hook
23 mutation *OperationMutation
24}
25
26// Where appends a list predicates to the OperationUpdate builder.
27func (ou *OperationUpdate) Where(ps ...predicate.Operation) *OperationUpdate {
28 ou.mutation.Where(ps...)
29 return ou
30}
31
32// SetDid sets the "did" field.
33func (ou *OperationUpdate) SetDid(s string) *OperationUpdate {
34 ou.mutation.SetDid(s)
35 return ou
36}
37
38// SetNillableDid sets the "did" field if the given value is not nil.
39func (ou *OperationUpdate) SetNillableDid(s *string) *OperationUpdate {
40 if s != nil {
41 ou.SetDid(*s)
42 }
43 return ou
44}
45
46// SetOperation sets the "operation" field.
47func (ou *OperationUpdate) SetOperation(po plc.PLCOperation) *OperationUpdate {
48 ou.mutation.SetOperation(po)
49 return ou
50}
51
52// SetNillableOperation sets the "operation" field if the given value is not nil.
53func (ou *OperationUpdate) SetNillableOperation(po *plc.PLCOperation) *OperationUpdate {
54 if po != nil {
55 ou.SetOperation(*po)
56 }
57 return ou
58}
59
60// SetCid sets the "cid" field.
61func (ou *OperationUpdate) SetCid(s string) *OperationUpdate {
62 ou.mutation.SetCid(s)
63 return ou
64}
65
66// SetNillableCid sets the "cid" field if the given value is not nil.
67func (ou *OperationUpdate) SetNillableCid(s *string) *OperationUpdate {
68 if s != nil {
69 ou.SetCid(*s)
70 }
71 return ou
72}
73
74// SetNullified sets the "nullified" field.
75func (ou *OperationUpdate) SetNullified(b bool) *OperationUpdate {
76 ou.mutation.SetNullified(b)
77 return ou
78}
79
80// SetNillableNullified sets the "nullified" field if the given value is not nil.
81func (ou *OperationUpdate) SetNillableNullified(b *bool) *OperationUpdate {
82 if b != nil {
83 ou.SetNullified(*b)
84 }
85 return ou
86}
87
88// SetCreatedAt sets the "created_at" field.
89func (ou *OperationUpdate) SetCreatedAt(t time.Time) *OperationUpdate {
90 ou.mutation.SetCreatedAt(t)
91 return ou
92}
93
94// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
95func (ou *OperationUpdate) SetNillableCreatedAt(t *time.Time) *OperationUpdate {
96 if t != nil {
97 ou.SetCreatedAt(*t)
98 }
99 return ou
100}
101
102// Mutation returns the OperationMutation object of the builder.
103func (ou *OperationUpdate) Mutation() *OperationMutation {
104 return ou.mutation
105}
106
107// Save executes the query and returns the number of nodes affected by the update operation.
108func (ou *OperationUpdate) Save(ctx context.Context) (int, error) {
109 return withHooks(ctx, ou.sqlSave, ou.mutation, ou.hooks)
110}
111
112// SaveX is like Save, but panics if an error occurs.
113func (ou *OperationUpdate) SaveX(ctx context.Context) int {
114 affected, err := ou.Save(ctx)
115 if err != nil {
116 panic(err)
117 }
118 return affected
119}
120
121// Exec executes the query.
122func (ou *OperationUpdate) Exec(ctx context.Context) error {
123 _, err := ou.Save(ctx)
124 return err
125}
126
127// ExecX is like Exec, but panics if an error occurs.
128func (ou *OperationUpdate) ExecX(ctx context.Context) {
129 if err := ou.Exec(ctx); err != nil {
130 panic(err)
131 }
132}
133
134// check runs all checks and user-defined validators on the builder.
135func (ou *OperationUpdate) check() error {
136 if v, ok := ou.mutation.Did(); ok {
137 if err := operation.DidValidator(v); err != nil {
138 return &ValidationError{Name: "did", err: fmt.Errorf(`ent: validator failed for field "Operation.did": %w`, err)}
139 }
140 }
141 if v, ok := ou.mutation.Cid(); ok {
142 if err := operation.CidValidator(v); err != nil {
143 return &ValidationError{Name: "cid", err: fmt.Errorf(`ent: validator failed for field "Operation.cid": %w`, err)}
144 }
145 }
146 return nil
147}
148
149func (ou *OperationUpdate) sqlSave(ctx context.Context) (n int, err error) {
150 if err := ou.check(); err != nil {
151 return n, err
152 }
153 _spec := sqlgraph.NewUpdateSpec(operation.Table, operation.Columns, sqlgraph.NewFieldSpec(operation.FieldID, field.TypeInt))
154 if ps := ou.mutation.predicates; len(ps) > 0 {
155 _spec.Predicate = func(selector *sql.Selector) {
156 for i := range ps {
157 ps[i](selector)
158 }
159 }
160 }
161 if value, ok := ou.mutation.Did(); ok {
162 _spec.SetField(operation.FieldDid, field.TypeString, value)
163 }
164 if value, ok := ou.mutation.Operation(); ok {
165 _spec.SetField(operation.FieldOperation, field.TypeJSON, value)
166 }
167 if value, ok := ou.mutation.Cid(); ok {
168 _spec.SetField(operation.FieldCid, field.TypeString, value)
169 }
170 if value, ok := ou.mutation.Nullified(); ok {
171 _spec.SetField(operation.FieldNullified, field.TypeBool, value)
172 }
173 if value, ok := ou.mutation.CreatedAt(); ok {
174 _spec.SetField(operation.FieldCreatedAt, field.TypeTime, value)
175 }
176 if n, err = sqlgraph.UpdateNodes(ctx, ou.driver, _spec); err != nil {
177 if _, ok := err.(*sqlgraph.NotFoundError); ok {
178 err = &NotFoundError{operation.Label}
179 } else if sqlgraph.IsConstraintError(err) {
180 err = &ConstraintError{msg: err.Error(), wrap: err}
181 }
182 return 0, err
183 }
184 ou.mutation.done = true
185 return n, nil
186}
187
188// OperationUpdateOne is the builder for updating a single Operation entity.
189type OperationUpdateOne struct {
190 config
191 fields []string
192 hooks []Hook
193 mutation *OperationMutation
194}
195
196// SetDid sets the "did" field.
197func (ouo *OperationUpdateOne) SetDid(s string) *OperationUpdateOne {
198 ouo.mutation.SetDid(s)
199 return ouo
200}
201
202// SetNillableDid sets the "did" field if the given value is not nil.
203func (ouo *OperationUpdateOne) SetNillableDid(s *string) *OperationUpdateOne {
204 if s != nil {
205 ouo.SetDid(*s)
206 }
207 return ouo
208}
209
210// SetOperation sets the "operation" field.
211func (ouo *OperationUpdateOne) SetOperation(po plc.PLCOperation) *OperationUpdateOne {
212 ouo.mutation.SetOperation(po)
213 return ouo
214}
215
216// SetNillableOperation sets the "operation" field if the given value is not nil.
217func (ouo *OperationUpdateOne) SetNillableOperation(po *plc.PLCOperation) *OperationUpdateOne {
218 if po != nil {
219 ouo.SetOperation(*po)
220 }
221 return ouo
222}
223
224// SetCid sets the "cid" field.
225func (ouo *OperationUpdateOne) SetCid(s string) *OperationUpdateOne {
226 ouo.mutation.SetCid(s)
227 return ouo
228}
229
230// SetNillableCid sets the "cid" field if the given value is not nil.
231func (ouo *OperationUpdateOne) SetNillableCid(s *string) *OperationUpdateOne {
232 if s != nil {
233 ouo.SetCid(*s)
234 }
235 return ouo
236}
237
238// SetNullified sets the "nullified" field.
239func (ouo *OperationUpdateOne) SetNullified(b bool) *OperationUpdateOne {
240 ouo.mutation.SetNullified(b)
241 return ouo
242}
243
244// SetNillableNullified sets the "nullified" field if the given value is not nil.
245func (ouo *OperationUpdateOne) SetNillableNullified(b *bool) *OperationUpdateOne {
246 if b != nil {
247 ouo.SetNullified(*b)
248 }
249 return ouo
250}
251
252// SetCreatedAt sets the "created_at" field.
253func (ouo *OperationUpdateOne) SetCreatedAt(t time.Time) *OperationUpdateOne {
254 ouo.mutation.SetCreatedAt(t)
255 return ouo
256}
257
258// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
259func (ouo *OperationUpdateOne) SetNillableCreatedAt(t *time.Time) *OperationUpdateOne {
260 if t != nil {
261 ouo.SetCreatedAt(*t)
262 }
263 return ouo
264}
265
266// Mutation returns the OperationMutation object of the builder.
267func (ouo *OperationUpdateOne) Mutation() *OperationMutation {
268 return ouo.mutation
269}
270
271// Where appends a list predicates to the OperationUpdate builder.
272func (ouo *OperationUpdateOne) Where(ps ...predicate.Operation) *OperationUpdateOne {
273 ouo.mutation.Where(ps...)
274 return ouo
275}
276
277// Select allows selecting one or more fields (columns) of the returned entity.
278// The default is selecting all fields defined in the entity schema.
279func (ouo *OperationUpdateOne) Select(field string, fields ...string) *OperationUpdateOne {
280 ouo.fields = append([]string{field}, fields...)
281 return ouo
282}
283
284// Save executes the query and returns the updated Operation entity.
285func (ouo *OperationUpdateOne) Save(ctx context.Context) (*Operation, error) {
286 return withHooks(ctx, ouo.sqlSave, ouo.mutation, ouo.hooks)
287}
288
289// SaveX is like Save, but panics if an error occurs.
290func (ouo *OperationUpdateOne) SaveX(ctx context.Context) *Operation {
291 node, err := ouo.Save(ctx)
292 if err != nil {
293 panic(err)
294 }
295 return node
296}
297
298// Exec executes the query on the entity.
299func (ouo *OperationUpdateOne) Exec(ctx context.Context) error {
300 _, err := ouo.Save(ctx)
301 return err
302}
303
304// ExecX is like Exec, but panics if an error occurs.
305func (ouo *OperationUpdateOne) ExecX(ctx context.Context) {
306 if err := ouo.Exec(ctx); err != nil {
307 panic(err)
308 }
309}
310
311// check runs all checks and user-defined validators on the builder.
312func (ouo *OperationUpdateOne) check() error {
313 if v, ok := ouo.mutation.Did(); ok {
314 if err := operation.DidValidator(v); err != nil {
315 return &ValidationError{Name: "did", err: fmt.Errorf(`ent: validator failed for field "Operation.did": %w`, err)}
316 }
317 }
318 if v, ok := ouo.mutation.Cid(); ok {
319 if err := operation.CidValidator(v); err != nil {
320 return &ValidationError{Name: "cid", err: fmt.Errorf(`ent: validator failed for field "Operation.cid": %w`, err)}
321 }
322 }
323 return nil
324}
325
326func (ouo *OperationUpdateOne) sqlSave(ctx context.Context) (_node *Operation, err error) {
327 if err := ouo.check(); err != nil {
328 return _node, err
329 }
330 _spec := sqlgraph.NewUpdateSpec(operation.Table, operation.Columns, sqlgraph.NewFieldSpec(operation.FieldID, field.TypeInt))
331 id, ok := ouo.mutation.ID()
332 if !ok {
333 return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Operation.id" for update`)}
334 }
335 _spec.Node.ID.Value = id
336 if fields := ouo.fields; len(fields) > 0 {
337 _spec.Node.Columns = make([]string, 0, len(fields))
338 _spec.Node.Columns = append(_spec.Node.Columns, operation.FieldID)
339 for _, f := range fields {
340 if !operation.ValidColumn(f) {
341 return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
342 }
343 if f != operation.FieldID {
344 _spec.Node.Columns = append(_spec.Node.Columns, f)
345 }
346 }
347 }
348 if ps := ouo.mutation.predicates; len(ps) > 0 {
349 _spec.Predicate = func(selector *sql.Selector) {
350 for i := range ps {
351 ps[i](selector)
352 }
353 }
354 }
355 if value, ok := ouo.mutation.Did(); ok {
356 _spec.SetField(operation.FieldDid, field.TypeString, value)
357 }
358 if value, ok := ouo.mutation.Operation(); ok {
359 _spec.SetField(operation.FieldOperation, field.TypeJSON, value)
360 }
361 if value, ok := ouo.mutation.Cid(); ok {
362 _spec.SetField(operation.FieldCid, field.TypeString, value)
363 }
364 if value, ok := ouo.mutation.Nullified(); ok {
365 _spec.SetField(operation.FieldNullified, field.TypeBool, value)
366 }
367 if value, ok := ouo.mutation.CreatedAt(); ok {
368 _spec.SetField(operation.FieldCreatedAt, field.TypeTime, value)
369 }
370 _node = &Operation{config: ouo.config}
371 _spec.Assign = _node.assignValues
372 _spec.ScanValues = _node.scanValues
373 if err = sqlgraph.UpdateNode(ctx, ouo.driver, _spec); err != nil {
374 if _, ok := err.(*sqlgraph.NotFoundError); ok {
375 err = &NotFoundError{operation.Label}
376 } else if sqlgraph.IsConstraintError(err) {
377 err = &ConstraintError{msg: err.Error(), wrap: err}
378 }
379 return nil, err
380 }
381 ouo.mutation.done = true
382 return _node, nil
383}