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/syncstatus"
15)
16
17// SyncStatusCreate is the builder for creating a SyncStatus entity.
18type SyncStatusCreate struct {
19 config
20 mutation *SyncStatusMutation
21 hooks []Hook
22 conflict []sql.ConflictOption
23}
24
25// SetKey sets the "key" field.
26func (ssc *SyncStatusCreate) SetKey(s string) *SyncStatusCreate {
27 ssc.mutation.SetKey(s)
28 return ssc
29}
30
31// SetLastOperationTime sets the "last_operation_time" field.
32func (ssc *SyncStatusCreate) SetLastOperationTime(t time.Time) *SyncStatusCreate {
33 ssc.mutation.SetLastOperationTime(t)
34 return ssc
35}
36
37// SetLastSyncTime sets the "last_sync_time" field.
38func (ssc *SyncStatusCreate) SetLastSyncTime(t time.Time) *SyncStatusCreate {
39 ssc.mutation.SetLastSyncTime(t)
40 return ssc
41}
42
43// Mutation returns the SyncStatusMutation object of the builder.
44func (ssc *SyncStatusCreate) Mutation() *SyncStatusMutation {
45 return ssc.mutation
46}
47
48// Save creates the SyncStatus in the database.
49func (ssc *SyncStatusCreate) Save(ctx context.Context) (*SyncStatus, error) {
50 return withHooks(ctx, ssc.sqlSave, ssc.mutation, ssc.hooks)
51}
52
53// SaveX calls Save and panics if Save returns an error.
54func (ssc *SyncStatusCreate) SaveX(ctx context.Context) *SyncStatus {
55 v, err := ssc.Save(ctx)
56 if err != nil {
57 panic(err)
58 }
59 return v
60}
61
62// Exec executes the query.
63func (ssc *SyncStatusCreate) Exec(ctx context.Context) error {
64 _, err := ssc.Save(ctx)
65 return err
66}
67
68// ExecX is like Exec, but panics if an error occurs.
69func (ssc *SyncStatusCreate) ExecX(ctx context.Context) {
70 if err := ssc.Exec(ctx); err != nil {
71 panic(err)
72 }
73}
74
75// check runs all checks and user-defined validators on the builder.
76func (ssc *SyncStatusCreate) check() error {
77 if _, ok := ssc.mutation.Key(); !ok {
78 return &ValidationError{Name: "key", err: errors.New(`ent: missing required field "SyncStatus.key"`)}
79 }
80 if _, ok := ssc.mutation.LastOperationTime(); !ok {
81 return &ValidationError{Name: "last_operation_time", err: errors.New(`ent: missing required field "SyncStatus.last_operation_time"`)}
82 }
83 if _, ok := ssc.mutation.LastSyncTime(); !ok {
84 return &ValidationError{Name: "last_sync_time", err: errors.New(`ent: missing required field "SyncStatus.last_sync_time"`)}
85 }
86 return nil
87}
88
89func (ssc *SyncStatusCreate) sqlSave(ctx context.Context) (*SyncStatus, error) {
90 if err := ssc.check(); err != nil {
91 return nil, err
92 }
93 _node, _spec := ssc.createSpec()
94 if err := sqlgraph.CreateNode(ctx, ssc.driver, _spec); err != nil {
95 if sqlgraph.IsConstraintError(err) {
96 err = &ConstraintError{msg: err.Error(), wrap: err}
97 }
98 return nil, err
99 }
100 id := _spec.ID.Value.(int64)
101 _node.ID = int(id)
102 ssc.mutation.id = &_node.ID
103 ssc.mutation.done = true
104 return _node, nil
105}
106
107func (ssc *SyncStatusCreate) createSpec() (*SyncStatus, *sqlgraph.CreateSpec) {
108 var (
109 _node = &SyncStatus{config: ssc.config}
110 _spec = sqlgraph.NewCreateSpec(syncstatus.Table, sqlgraph.NewFieldSpec(syncstatus.FieldID, field.TypeInt))
111 )
112 _spec.OnConflict = ssc.conflict
113 if value, ok := ssc.mutation.Key(); ok {
114 _spec.SetField(syncstatus.FieldKey, field.TypeString, value)
115 _node.Key = value
116 }
117 if value, ok := ssc.mutation.LastOperationTime(); ok {
118 _spec.SetField(syncstatus.FieldLastOperationTime, field.TypeTime, value)
119 _node.LastOperationTime = value
120 }
121 if value, ok := ssc.mutation.LastSyncTime(); ok {
122 _spec.SetField(syncstatus.FieldLastSyncTime, field.TypeTime, value)
123 _node.LastSyncTime = value
124 }
125 return _node, _spec
126}
127
128// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
129// of the `INSERT` statement. For example:
130//
131// client.SyncStatus.Create().
132// SetKey(v).
133// OnConflict(
134// // Update the row with the new values
135// // the was proposed for insertion.
136// sql.ResolveWithNewValues(),
137// ).
138// // Override some of the fields with custom
139// // update values.
140// Update(func(u *ent.SyncStatusUpsert) {
141// SetKey(v+v).
142// }).
143// Exec(ctx)
144func (ssc *SyncStatusCreate) OnConflict(opts ...sql.ConflictOption) *SyncStatusUpsertOne {
145 ssc.conflict = opts
146 return &SyncStatusUpsertOne{
147 create: ssc,
148 }
149}
150
151// OnConflictColumns calls `OnConflict` and configures the columns
152// as conflict target. Using this option is equivalent to using:
153//
154// client.SyncStatus.Create().
155// OnConflict(sql.ConflictColumns(columns...)).
156// Exec(ctx)
157func (ssc *SyncStatusCreate) OnConflictColumns(columns ...string) *SyncStatusUpsertOne {
158 ssc.conflict = append(ssc.conflict, sql.ConflictColumns(columns...))
159 return &SyncStatusUpsertOne{
160 create: ssc,
161 }
162}
163
164type (
165 // SyncStatusUpsertOne is the builder for "upsert"-ing
166 // one SyncStatus node.
167 SyncStatusUpsertOne struct {
168 create *SyncStatusCreate
169 }
170
171 // SyncStatusUpsert is the "OnConflict" setter.
172 SyncStatusUpsert struct {
173 *sql.UpdateSet
174 }
175)
176
177// SetKey sets the "key" field.
178func (u *SyncStatusUpsert) SetKey(v string) *SyncStatusUpsert {
179 u.Set(syncstatus.FieldKey, v)
180 return u
181}
182
183// UpdateKey sets the "key" field to the value that was provided on create.
184func (u *SyncStatusUpsert) UpdateKey() *SyncStatusUpsert {
185 u.SetExcluded(syncstatus.FieldKey)
186 return u
187}
188
189// SetLastOperationTime sets the "last_operation_time" field.
190func (u *SyncStatusUpsert) SetLastOperationTime(v time.Time) *SyncStatusUpsert {
191 u.Set(syncstatus.FieldLastOperationTime, v)
192 return u
193}
194
195// UpdateLastOperationTime sets the "last_operation_time" field to the value that was provided on create.
196func (u *SyncStatusUpsert) UpdateLastOperationTime() *SyncStatusUpsert {
197 u.SetExcluded(syncstatus.FieldLastOperationTime)
198 return u
199}
200
201// SetLastSyncTime sets the "last_sync_time" field.
202func (u *SyncStatusUpsert) SetLastSyncTime(v time.Time) *SyncStatusUpsert {
203 u.Set(syncstatus.FieldLastSyncTime, v)
204 return u
205}
206
207// UpdateLastSyncTime sets the "last_sync_time" field to the value that was provided on create.
208func (u *SyncStatusUpsert) UpdateLastSyncTime() *SyncStatusUpsert {
209 u.SetExcluded(syncstatus.FieldLastSyncTime)
210 return u
211}
212
213// UpdateNewValues updates the mutable fields using the new values that were set on create.
214// Using this option is equivalent to using:
215//
216// client.SyncStatus.Create().
217// OnConflict(
218// sql.ResolveWithNewValues(),
219// ).
220// Exec(ctx)
221func (u *SyncStatusUpsertOne) UpdateNewValues() *SyncStatusUpsertOne {
222 u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
223 return u
224}
225
226// Ignore sets each column to itself in case of conflict.
227// Using this option is equivalent to using:
228//
229// client.SyncStatus.Create().
230// OnConflict(sql.ResolveWithIgnore()).
231// Exec(ctx)
232func (u *SyncStatusUpsertOne) Ignore() *SyncStatusUpsertOne {
233 u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
234 return u
235}
236
237// DoNothing configures the conflict_action to `DO NOTHING`.
238// Supported only by SQLite and PostgreSQL.
239func (u *SyncStatusUpsertOne) DoNothing() *SyncStatusUpsertOne {
240 u.create.conflict = append(u.create.conflict, sql.DoNothing())
241 return u
242}
243
244// Update allows overriding fields `UPDATE` values. See the SyncStatusCreate.OnConflict
245// documentation for more info.
246func (u *SyncStatusUpsertOne) Update(set func(*SyncStatusUpsert)) *SyncStatusUpsertOne {
247 u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
248 set(&SyncStatusUpsert{UpdateSet: update})
249 }))
250 return u
251}
252
253// SetKey sets the "key" field.
254func (u *SyncStatusUpsertOne) SetKey(v string) *SyncStatusUpsertOne {
255 return u.Update(func(s *SyncStatusUpsert) {
256 s.SetKey(v)
257 })
258}
259
260// UpdateKey sets the "key" field to the value that was provided on create.
261func (u *SyncStatusUpsertOne) UpdateKey() *SyncStatusUpsertOne {
262 return u.Update(func(s *SyncStatusUpsert) {
263 s.UpdateKey()
264 })
265}
266
267// SetLastOperationTime sets the "last_operation_time" field.
268func (u *SyncStatusUpsertOne) SetLastOperationTime(v time.Time) *SyncStatusUpsertOne {
269 return u.Update(func(s *SyncStatusUpsert) {
270 s.SetLastOperationTime(v)
271 })
272}
273
274// UpdateLastOperationTime sets the "last_operation_time" field to the value that was provided on create.
275func (u *SyncStatusUpsertOne) UpdateLastOperationTime() *SyncStatusUpsertOne {
276 return u.Update(func(s *SyncStatusUpsert) {
277 s.UpdateLastOperationTime()
278 })
279}
280
281// SetLastSyncTime sets the "last_sync_time" field.
282func (u *SyncStatusUpsertOne) SetLastSyncTime(v time.Time) *SyncStatusUpsertOne {
283 return u.Update(func(s *SyncStatusUpsert) {
284 s.SetLastSyncTime(v)
285 })
286}
287
288// UpdateLastSyncTime sets the "last_sync_time" field to the value that was provided on create.
289func (u *SyncStatusUpsertOne) UpdateLastSyncTime() *SyncStatusUpsertOne {
290 return u.Update(func(s *SyncStatusUpsert) {
291 s.UpdateLastSyncTime()
292 })
293}
294
295// Exec executes the query.
296func (u *SyncStatusUpsertOne) Exec(ctx context.Context) error {
297 if len(u.create.conflict) == 0 {
298 return errors.New("ent: missing options for SyncStatusCreate.OnConflict")
299 }
300 return u.create.Exec(ctx)
301}
302
303// ExecX is like Exec, but panics if an error occurs.
304func (u *SyncStatusUpsertOne) ExecX(ctx context.Context) {
305 if err := u.create.Exec(ctx); err != nil {
306 panic(err)
307 }
308}
309
310// Exec executes the UPSERT query and returns the inserted/updated ID.
311func (u *SyncStatusUpsertOne) ID(ctx context.Context) (id int, err error) {
312 node, err := u.create.Save(ctx)
313 if err != nil {
314 return id, err
315 }
316 return node.ID, nil
317}
318
319// IDX is like ID, but panics if an error occurs.
320func (u *SyncStatusUpsertOne) IDX(ctx context.Context) int {
321 id, err := u.ID(ctx)
322 if err != nil {
323 panic(err)
324 }
325 return id
326}
327
328// SyncStatusCreateBulk is the builder for creating many SyncStatus entities in bulk.
329type SyncStatusCreateBulk struct {
330 config
331 err error
332 builders []*SyncStatusCreate
333 conflict []sql.ConflictOption
334}
335
336// Save creates the SyncStatus entities in the database.
337func (sscb *SyncStatusCreateBulk) Save(ctx context.Context) ([]*SyncStatus, error) {
338 if sscb.err != nil {
339 return nil, sscb.err
340 }
341 specs := make([]*sqlgraph.CreateSpec, len(sscb.builders))
342 nodes := make([]*SyncStatus, len(sscb.builders))
343 mutators := make([]Mutator, len(sscb.builders))
344 for i := range sscb.builders {
345 func(i int, root context.Context) {
346 builder := sscb.builders[i]
347 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
348 mutation, ok := m.(*SyncStatusMutation)
349 if !ok {
350 return nil, fmt.Errorf("unexpected mutation type %T", m)
351 }
352 if err := builder.check(); err != nil {
353 return nil, err
354 }
355 builder.mutation = mutation
356 var err error
357 nodes[i], specs[i] = builder.createSpec()
358 if i < len(mutators)-1 {
359 _, err = mutators[i+1].Mutate(root, sscb.builders[i+1].mutation)
360 } else {
361 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
362 spec.OnConflict = sscb.conflict
363 // Invoke the actual operation on the latest mutation in the chain.
364 if err = sqlgraph.BatchCreate(ctx, sscb.driver, spec); err != nil {
365 if sqlgraph.IsConstraintError(err) {
366 err = &ConstraintError{msg: err.Error(), wrap: err}
367 }
368 }
369 }
370 if err != nil {
371 return nil, err
372 }
373 mutation.id = &nodes[i].ID
374 if specs[i].ID.Value != nil {
375 id := specs[i].ID.Value.(int64)
376 nodes[i].ID = int(id)
377 }
378 mutation.done = true
379 return nodes[i], nil
380 })
381 for i := len(builder.hooks) - 1; i >= 0; i-- {
382 mut = builder.hooks[i](mut)
383 }
384 mutators[i] = mut
385 }(i, ctx)
386 }
387 if len(mutators) > 0 {
388 if _, err := mutators[0].Mutate(ctx, sscb.builders[0].mutation); err != nil {
389 return nil, err
390 }
391 }
392 return nodes, nil
393}
394
395// SaveX is like Save, but panics if an error occurs.
396func (sscb *SyncStatusCreateBulk) SaveX(ctx context.Context) []*SyncStatus {
397 v, err := sscb.Save(ctx)
398 if err != nil {
399 panic(err)
400 }
401 return v
402}
403
404// Exec executes the query.
405func (sscb *SyncStatusCreateBulk) Exec(ctx context.Context) error {
406 _, err := sscb.Save(ctx)
407 return err
408}
409
410// ExecX is like Exec, but panics if an error occurs.
411func (sscb *SyncStatusCreateBulk) ExecX(ctx context.Context) {
412 if err := sscb.Exec(ctx); err != nil {
413 panic(err)
414 }
415}
416
417// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
418// of the `INSERT` statement. For example:
419//
420// client.SyncStatus.CreateBulk(builders...).
421// OnConflict(
422// // Update the row with the new values
423// // the was proposed for insertion.
424// sql.ResolveWithNewValues(),
425// ).
426// // Override some of the fields with custom
427// // update values.
428// Update(func(u *ent.SyncStatusUpsert) {
429// SetKey(v+v).
430// }).
431// Exec(ctx)
432func (sscb *SyncStatusCreateBulk) OnConflict(opts ...sql.ConflictOption) *SyncStatusUpsertBulk {
433 sscb.conflict = opts
434 return &SyncStatusUpsertBulk{
435 create: sscb,
436 }
437}
438
439// OnConflictColumns calls `OnConflict` and configures the columns
440// as conflict target. Using this option is equivalent to using:
441//
442// client.SyncStatus.Create().
443// OnConflict(sql.ConflictColumns(columns...)).
444// Exec(ctx)
445func (sscb *SyncStatusCreateBulk) OnConflictColumns(columns ...string) *SyncStatusUpsertBulk {
446 sscb.conflict = append(sscb.conflict, sql.ConflictColumns(columns...))
447 return &SyncStatusUpsertBulk{
448 create: sscb,
449 }
450}
451
452// SyncStatusUpsertBulk is the builder for "upsert"-ing
453// a bulk of SyncStatus nodes.
454type SyncStatusUpsertBulk struct {
455 create *SyncStatusCreateBulk
456}
457
458// UpdateNewValues updates the mutable fields using the new values that
459// were set on create. Using this option is equivalent to using:
460//
461// client.SyncStatus.Create().
462// OnConflict(
463// sql.ResolveWithNewValues(),
464// ).
465// Exec(ctx)
466func (u *SyncStatusUpsertBulk) UpdateNewValues() *SyncStatusUpsertBulk {
467 u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
468 return u
469}
470
471// Ignore sets each column to itself in case of conflict.
472// Using this option is equivalent to using:
473//
474// client.SyncStatus.Create().
475// OnConflict(sql.ResolveWithIgnore()).
476// Exec(ctx)
477func (u *SyncStatusUpsertBulk) Ignore() *SyncStatusUpsertBulk {
478 u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
479 return u
480}
481
482// DoNothing configures the conflict_action to `DO NOTHING`.
483// Supported only by SQLite and PostgreSQL.
484func (u *SyncStatusUpsertBulk) DoNothing() *SyncStatusUpsertBulk {
485 u.create.conflict = append(u.create.conflict, sql.DoNothing())
486 return u
487}
488
489// Update allows overriding fields `UPDATE` values. See the SyncStatusCreateBulk.OnConflict
490// documentation for more info.
491func (u *SyncStatusUpsertBulk) Update(set func(*SyncStatusUpsert)) *SyncStatusUpsertBulk {
492 u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
493 set(&SyncStatusUpsert{UpdateSet: update})
494 }))
495 return u
496}
497
498// SetKey sets the "key" field.
499func (u *SyncStatusUpsertBulk) SetKey(v string) *SyncStatusUpsertBulk {
500 return u.Update(func(s *SyncStatusUpsert) {
501 s.SetKey(v)
502 })
503}
504
505// UpdateKey sets the "key" field to the value that was provided on create.
506func (u *SyncStatusUpsertBulk) UpdateKey() *SyncStatusUpsertBulk {
507 return u.Update(func(s *SyncStatusUpsert) {
508 s.UpdateKey()
509 })
510}
511
512// SetLastOperationTime sets the "last_operation_time" field.
513func (u *SyncStatusUpsertBulk) SetLastOperationTime(v time.Time) *SyncStatusUpsertBulk {
514 return u.Update(func(s *SyncStatusUpsert) {
515 s.SetLastOperationTime(v)
516 })
517}
518
519// UpdateLastOperationTime sets the "last_operation_time" field to the value that was provided on create.
520func (u *SyncStatusUpsertBulk) UpdateLastOperationTime() *SyncStatusUpsertBulk {
521 return u.Update(func(s *SyncStatusUpsert) {
522 s.UpdateLastOperationTime()
523 })
524}
525
526// SetLastSyncTime sets the "last_sync_time" field.
527func (u *SyncStatusUpsertBulk) SetLastSyncTime(v time.Time) *SyncStatusUpsertBulk {
528 return u.Update(func(s *SyncStatusUpsert) {
529 s.SetLastSyncTime(v)
530 })
531}
532
533// UpdateLastSyncTime sets the "last_sync_time" field to the value that was provided on create.
534func (u *SyncStatusUpsertBulk) UpdateLastSyncTime() *SyncStatusUpsertBulk {
535 return u.Update(func(s *SyncStatusUpsert) {
536 s.UpdateLastSyncTime()
537 })
538}
539
540// Exec executes the query.
541func (u *SyncStatusUpsertBulk) Exec(ctx context.Context) error {
542 if u.create.err != nil {
543 return u.create.err
544 }
545 for i, b := range u.create.builders {
546 if len(b.conflict) != 0 {
547 return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the SyncStatusCreateBulk instead", i)
548 }
549 }
550 if len(u.create.conflict) == 0 {
551 return errors.New("ent: missing options for SyncStatusCreateBulk.OnConflict")
552 }
553 return u.create.Exec(ctx)
554}
555
556// ExecX is like Exec, but panics if an error occurs.
557func (u *SyncStatusUpsertBulk) ExecX(ctx context.Context) {
558 if err := u.create.Exec(ctx); err != nil {
559 panic(err)
560 }
561}