-
Notifications
You must be signed in to change notification settings - Fork 894
/
file_update.go
701 lines (648 loc) · 16.8 KB
/
file_update.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
"github.com/facebookincubator/ent/entc/integration/ent/file"
"github.com/facebookincubator/ent/entc/integration/ent/filetype"
"github.com/facebookincubator/ent/entc/integration/ent/predicate"
"github.com/facebookincubator/ent/entc/integration/ent/user"
"github.com/facebookincubator/ent/schema/field"
)
// FileUpdate is the builder for updating File entities.
type FileUpdate struct {
config
size *int
addsize *int
name *string
user *string
clearuser bool
group *string
cleargroup bool
owner map[string]struct{}
_type map[string]struct{}
clearedOwner bool
clearedType bool
predicates []predicate.File
}
// Where adds a new predicate for the builder.
func (fu *FileUpdate) Where(ps ...predicate.File) *FileUpdate {
fu.predicates = append(fu.predicates, ps...)
return fu
}
// SetSize sets the size field.
func (fu *FileUpdate) SetSize(i int) *FileUpdate {
fu.size = &i
fu.addsize = nil
return fu
}
// SetNillableSize sets the size field if the given value is not nil.
func (fu *FileUpdate) SetNillableSize(i *int) *FileUpdate {
if i != nil {
fu.SetSize(*i)
}
return fu
}
// AddSize adds i to size.
func (fu *FileUpdate) AddSize(i int) *FileUpdate {
if fu.addsize == nil {
fu.addsize = &i
} else {
*fu.addsize += i
}
return fu
}
// SetName sets the name field.
func (fu *FileUpdate) SetName(s string) *FileUpdate {
fu.name = &s
return fu
}
// SetUser sets the user field.
func (fu *FileUpdate) SetUser(s string) *FileUpdate {
fu.user = &s
return fu
}
// SetNillableUser sets the user field if the given value is not nil.
func (fu *FileUpdate) SetNillableUser(s *string) *FileUpdate {
if s != nil {
fu.SetUser(*s)
}
return fu
}
// ClearUser clears the value of user.
func (fu *FileUpdate) ClearUser() *FileUpdate {
fu.user = nil
fu.clearuser = true
return fu
}
// SetGroup sets the group field.
func (fu *FileUpdate) SetGroup(s string) *FileUpdate {
fu.group = &s
return fu
}
// SetNillableGroup sets the group field if the given value is not nil.
func (fu *FileUpdate) SetNillableGroup(s *string) *FileUpdate {
if s != nil {
fu.SetGroup(*s)
}
return fu
}
// ClearGroup clears the value of group.
func (fu *FileUpdate) ClearGroup() *FileUpdate {
fu.group = nil
fu.cleargroup = true
return fu
}
// SetOwnerID sets the owner edge to User by id.
func (fu *FileUpdate) SetOwnerID(id string) *FileUpdate {
if fu.owner == nil {
fu.owner = make(map[string]struct{})
}
fu.owner[id] = struct{}{}
return fu
}
// SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
func (fu *FileUpdate) SetNillableOwnerID(id *string) *FileUpdate {
if id != nil {
fu = fu.SetOwnerID(*id)
}
return fu
}
// SetOwner sets the owner edge to User.
func (fu *FileUpdate) SetOwner(u *User) *FileUpdate {
return fu.SetOwnerID(u.ID)
}
// SetTypeID sets the type edge to FileType by id.
func (fu *FileUpdate) SetTypeID(id string) *FileUpdate {
if fu._type == nil {
fu._type = make(map[string]struct{})
}
fu._type[id] = struct{}{}
return fu
}
// SetNillableTypeID sets the type edge to FileType by id if the given value is not nil.
func (fu *FileUpdate) SetNillableTypeID(id *string) *FileUpdate {
if id != nil {
fu = fu.SetTypeID(*id)
}
return fu
}
// SetType sets the type edge to FileType.
func (fu *FileUpdate) SetType(f *FileType) *FileUpdate {
return fu.SetTypeID(f.ID)
}
// ClearOwner clears the owner edge to User.
func (fu *FileUpdate) ClearOwner() *FileUpdate {
fu.clearedOwner = true
return fu
}
// ClearType clears the type edge to FileType.
func (fu *FileUpdate) ClearType() *FileUpdate {
fu.clearedType = true
return fu
}
// Save executes the query and returns the number of rows/vertices matched by this operation.
func (fu *FileUpdate) Save(ctx context.Context) (int, error) {
if fu.size != nil {
if err := file.SizeValidator(*fu.size); err != nil {
return 0, fmt.Errorf("ent: validator failed for field \"size\": %v", err)
}
}
if len(fu.owner) > 1 {
return 0, errors.New("ent: multiple assignments on a unique edge \"owner\"")
}
if len(fu._type) > 1 {
return 0, errors.New("ent: multiple assignments on a unique edge \"type\"")
}
return fu.sqlSave(ctx)
}
// SaveX is like Save, but panics if an error occurs.
func (fu *FileUpdate) SaveX(ctx context.Context) int {
affected, err := fu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (fu *FileUpdate) Exec(ctx context.Context) error {
_, err := fu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (fu *FileUpdate) ExecX(ctx context.Context) {
if err := fu.Exec(ctx); err != nil {
panic(err)
}
}
func (fu *FileUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec := &sqlgraph.UpdateSpec{
Node: &sqlgraph.NodeSpec{
Table: file.Table,
Columns: file.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: file.FieldID,
},
},
}
if ps := fu.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value := fu.size; value != nil {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: *value,
Column: file.FieldSize,
})
}
if value := fu.addsize; value != nil {
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: *value,
Column: file.FieldSize,
})
}
if value := fu.name; value != nil {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: *value,
Column: file.FieldName,
})
}
if value := fu.user; value != nil {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: *value,
Column: file.FieldUser,
})
}
if fu.clearuser {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: file.FieldUser,
})
}
if value := fu.group; value != nil {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: *value,
Column: file.FieldGroup,
})
}
if fu.cleargroup {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: file.FieldGroup,
})
}
if fu.clearedOwner {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.OwnerTable,
Columns: []string{file.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: user.FieldID,
},
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := fu.owner; len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.OwnerTable,
Columns: []string{file.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: user.FieldID,
},
},
}
for k, _ := range nodes {
k, err := strconv.Atoi(k)
if err != nil {
return 0, err
}
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if fu.clearedType {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.TypeTable,
Columns: []string{file.TypeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: filetype.FieldID,
},
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := fu._type; len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.TypeTable,
Columns: []string{file.TypeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: filetype.FieldID,
},
},
}
for k, _ := range nodes {
k, err := strconv.Atoi(k)
if err != nil {
return 0, err
}
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if n, err = sqlgraph.UpdateNodes(ctx, fu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{file.Label}
} else if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return 0, err
}
return n, nil
}
// FileUpdateOne is the builder for updating a single File entity.
type FileUpdateOne struct {
config
id string
size *int
addsize *int
name *string
user *string
clearuser bool
group *string
cleargroup bool
owner map[string]struct{}
_type map[string]struct{}
clearedOwner bool
clearedType bool
}
// SetSize sets the size field.
func (fuo *FileUpdateOne) SetSize(i int) *FileUpdateOne {
fuo.size = &i
fuo.addsize = nil
return fuo
}
// SetNillableSize sets the size field if the given value is not nil.
func (fuo *FileUpdateOne) SetNillableSize(i *int) *FileUpdateOne {
if i != nil {
fuo.SetSize(*i)
}
return fuo
}
// AddSize adds i to size.
func (fuo *FileUpdateOne) AddSize(i int) *FileUpdateOne {
if fuo.addsize == nil {
fuo.addsize = &i
} else {
*fuo.addsize += i
}
return fuo
}
// SetName sets the name field.
func (fuo *FileUpdateOne) SetName(s string) *FileUpdateOne {
fuo.name = &s
return fuo
}
// SetUser sets the user field.
func (fuo *FileUpdateOne) SetUser(s string) *FileUpdateOne {
fuo.user = &s
return fuo
}
// SetNillableUser sets the user field if the given value is not nil.
func (fuo *FileUpdateOne) SetNillableUser(s *string) *FileUpdateOne {
if s != nil {
fuo.SetUser(*s)
}
return fuo
}
// ClearUser clears the value of user.
func (fuo *FileUpdateOne) ClearUser() *FileUpdateOne {
fuo.user = nil
fuo.clearuser = true
return fuo
}
// SetGroup sets the group field.
func (fuo *FileUpdateOne) SetGroup(s string) *FileUpdateOne {
fuo.group = &s
return fuo
}
// SetNillableGroup sets the group field if the given value is not nil.
func (fuo *FileUpdateOne) SetNillableGroup(s *string) *FileUpdateOne {
if s != nil {
fuo.SetGroup(*s)
}
return fuo
}
// ClearGroup clears the value of group.
func (fuo *FileUpdateOne) ClearGroup() *FileUpdateOne {
fuo.group = nil
fuo.cleargroup = true
return fuo
}
// SetOwnerID sets the owner edge to User by id.
func (fuo *FileUpdateOne) SetOwnerID(id string) *FileUpdateOne {
if fuo.owner == nil {
fuo.owner = make(map[string]struct{})
}
fuo.owner[id] = struct{}{}
return fuo
}
// SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
func (fuo *FileUpdateOne) SetNillableOwnerID(id *string) *FileUpdateOne {
if id != nil {
fuo = fuo.SetOwnerID(*id)
}
return fuo
}
// SetOwner sets the owner edge to User.
func (fuo *FileUpdateOne) SetOwner(u *User) *FileUpdateOne {
return fuo.SetOwnerID(u.ID)
}
// SetTypeID sets the type edge to FileType by id.
func (fuo *FileUpdateOne) SetTypeID(id string) *FileUpdateOne {
if fuo._type == nil {
fuo._type = make(map[string]struct{})
}
fuo._type[id] = struct{}{}
return fuo
}
// SetNillableTypeID sets the type edge to FileType by id if the given value is not nil.
func (fuo *FileUpdateOne) SetNillableTypeID(id *string) *FileUpdateOne {
if id != nil {
fuo = fuo.SetTypeID(*id)
}
return fuo
}
// SetType sets the type edge to FileType.
func (fuo *FileUpdateOne) SetType(f *FileType) *FileUpdateOne {
return fuo.SetTypeID(f.ID)
}
// ClearOwner clears the owner edge to User.
func (fuo *FileUpdateOne) ClearOwner() *FileUpdateOne {
fuo.clearedOwner = true
return fuo
}
// ClearType clears the type edge to FileType.
func (fuo *FileUpdateOne) ClearType() *FileUpdateOne {
fuo.clearedType = true
return fuo
}
// Save executes the query and returns the updated entity.
func (fuo *FileUpdateOne) Save(ctx context.Context) (*File, error) {
if fuo.size != nil {
if err := file.SizeValidator(*fuo.size); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"size\": %v", err)
}
}
if len(fuo.owner) > 1 {
return nil, errors.New("ent: multiple assignments on a unique edge \"owner\"")
}
if len(fuo._type) > 1 {
return nil, errors.New("ent: multiple assignments on a unique edge \"type\"")
}
return fuo.sqlSave(ctx)
}
// SaveX is like Save, but panics if an error occurs.
func (fuo *FileUpdateOne) SaveX(ctx context.Context) *File {
f, err := fuo.Save(ctx)
if err != nil {
panic(err)
}
return f
}
// Exec executes the query on the entity.
func (fuo *FileUpdateOne) Exec(ctx context.Context) error {
_, err := fuo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (fuo *FileUpdateOne) ExecX(ctx context.Context) {
if err := fuo.Exec(ctx); err != nil {
panic(err)
}
}
func (fuo *FileUpdateOne) sqlSave(ctx context.Context) (f *File, err error) {
_spec := &sqlgraph.UpdateSpec{
Node: &sqlgraph.NodeSpec{
Table: file.Table,
Columns: file.Columns,
ID: &sqlgraph.FieldSpec{
Value: fuo.id,
Type: field.TypeString,
Column: file.FieldID,
},
},
}
if value := fuo.size; value != nil {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: *value,
Column: file.FieldSize,
})
}
if value := fuo.addsize; value != nil {
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: *value,
Column: file.FieldSize,
})
}
if value := fuo.name; value != nil {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: *value,
Column: file.FieldName,
})
}
if value := fuo.user; value != nil {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: *value,
Column: file.FieldUser,
})
}
if fuo.clearuser {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: file.FieldUser,
})
}
if value := fuo.group; value != nil {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: *value,
Column: file.FieldGroup,
})
}
if fuo.cleargroup {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: file.FieldGroup,
})
}
if fuo.clearedOwner {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.OwnerTable,
Columns: []string{file.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: user.FieldID,
},
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := fuo.owner; len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.OwnerTable,
Columns: []string{file.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: user.FieldID,
},
},
}
for k, _ := range nodes {
k, err := strconv.Atoi(k)
if err != nil {
return nil, err
}
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if fuo.clearedType {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.TypeTable,
Columns: []string{file.TypeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: filetype.FieldID,
},
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := fuo._type; len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.TypeTable,
Columns: []string{file.TypeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: filetype.FieldID,
},
},
}
for k, _ := range nodes {
k, err := strconv.Atoi(k)
if err != nil {
return nil, err
}
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
f = &File{config: fuo.config}
_spec.Assign = f.assignValues
_spec.ScanValues = f.scanValues()
if err = sqlgraph.UpdateNode(ctx, fuo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{file.Label}
} else if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
return f, nil
}