/ ent / subscription_create.go
subscription_create.go
  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"
 12  	"entgo.io/ent/dialect/sql"
 13  	"entgo.io/ent/dialect/sql/sqlgraph"
 14  	"entgo.io/ent/schema/field"
 15  	"github.com/google/uuid"
 16  	"github.com/mrusme/journalist/ent/feed"
 17  	"github.com/mrusme/journalist/ent/subscription"
 18  	"github.com/mrusme/journalist/ent/user"
 19  )
 20  
 21  // SubscriptionCreate is the builder for creating a Subscription entity.
 22  type SubscriptionCreate struct {
 23  	config
 24  	mutation *SubscriptionMutation
 25  	hooks    []Hook
 26  	conflict []sql.ConflictOption
 27  }
 28  
 29  // SetUserID sets the "user_id" field.
 30  func (sc *SubscriptionCreate) SetUserID(u uuid.UUID) *SubscriptionCreate {
 31  	sc.mutation.SetUserID(u)
 32  	return sc
 33  }
 34  
 35  // SetFeedID sets the "feed_id" field.
 36  func (sc *SubscriptionCreate) SetFeedID(u uuid.UUID) *SubscriptionCreate {
 37  	sc.mutation.SetFeedID(u)
 38  	return sc
 39  }
 40  
 41  // SetName sets the "name" field.
 42  func (sc *SubscriptionCreate) SetName(s string) *SubscriptionCreate {
 43  	sc.mutation.SetName(s)
 44  	return sc
 45  }
 46  
 47  // SetGroup sets the "group" field.
 48  func (sc *SubscriptionCreate) SetGroup(s string) *SubscriptionCreate {
 49  	sc.mutation.SetGroup(s)
 50  	return sc
 51  }
 52  
 53  // SetCreatedAt sets the "created_at" field.
 54  func (sc *SubscriptionCreate) SetCreatedAt(t time.Time) *SubscriptionCreate {
 55  	sc.mutation.SetCreatedAt(t)
 56  	return sc
 57  }
 58  
 59  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
 60  func (sc *SubscriptionCreate) SetNillableCreatedAt(t *time.Time) *SubscriptionCreate {
 61  	if t != nil {
 62  		sc.SetCreatedAt(*t)
 63  	}
 64  	return sc
 65  }
 66  
 67  // SetID sets the "id" field.
 68  func (sc *SubscriptionCreate) SetID(u uuid.UUID) *SubscriptionCreate {
 69  	sc.mutation.SetID(u)
 70  	return sc
 71  }
 72  
 73  // SetNillableID sets the "id" field if the given value is not nil.
 74  func (sc *SubscriptionCreate) SetNillableID(u *uuid.UUID) *SubscriptionCreate {
 75  	if u != nil {
 76  		sc.SetID(*u)
 77  	}
 78  	return sc
 79  }
 80  
 81  // SetUser sets the "user" edge to the User entity.
 82  func (sc *SubscriptionCreate) SetUser(u *User) *SubscriptionCreate {
 83  	return sc.SetUserID(u.ID)
 84  }
 85  
 86  // SetFeed sets the "feed" edge to the Feed entity.
 87  func (sc *SubscriptionCreate) SetFeed(f *Feed) *SubscriptionCreate {
 88  	return sc.SetFeedID(f.ID)
 89  }
 90  
 91  // Mutation returns the SubscriptionMutation object of the builder.
 92  func (sc *SubscriptionCreate) Mutation() *SubscriptionMutation {
 93  	return sc.mutation
 94  }
 95  
 96  // Save creates the Subscription in the database.
 97  func (sc *SubscriptionCreate) Save(ctx context.Context) (*Subscription, error) {
 98  	sc.defaults()
 99  	return withHooks(ctx, sc.sqlSave, sc.mutation, sc.hooks)
100  }
101  
102  // SaveX calls Save and panics if Save returns an error.
103  func (sc *SubscriptionCreate) SaveX(ctx context.Context) *Subscription {
104  	v, err := sc.Save(ctx)
105  	if err != nil {
106  		panic(err)
107  	}
108  	return v
109  }
110  
111  // Exec executes the query.
112  func (sc *SubscriptionCreate) Exec(ctx context.Context) error {
113  	_, err := sc.Save(ctx)
114  	return err
115  }
116  
117  // ExecX is like Exec, but panics if an error occurs.
118  func (sc *SubscriptionCreate) ExecX(ctx context.Context) {
119  	if err := sc.Exec(ctx); err != nil {
120  		panic(err)
121  	}
122  }
123  
124  // defaults sets the default values of the builder before save.
125  func (sc *SubscriptionCreate) defaults() {
126  	if _, ok := sc.mutation.CreatedAt(); !ok {
127  		v := subscription.DefaultCreatedAt()
128  		sc.mutation.SetCreatedAt(v)
129  	}
130  	if _, ok := sc.mutation.ID(); !ok {
131  		v := subscription.DefaultID()
132  		sc.mutation.SetID(v)
133  	}
134  }
135  
136  // check runs all checks and user-defined validators on the builder.
137  func (sc *SubscriptionCreate) check() error {
138  	if _, ok := sc.mutation.UserID(); !ok {
139  		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "Subscription.user_id"`)}
140  	}
141  	if _, ok := sc.mutation.FeedID(); !ok {
142  		return &ValidationError{Name: "feed_id", err: errors.New(`ent: missing required field "Subscription.feed_id"`)}
143  	}
144  	if _, ok := sc.mutation.Name(); !ok {
145  		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Subscription.name"`)}
146  	}
147  	if v, ok := sc.mutation.Name(); ok {
148  		if err := subscription.NameValidator(v); err != nil {
149  			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Subscription.name": %w`, err)}
150  		}
151  	}
152  	if _, ok := sc.mutation.Group(); !ok {
153  		return &ValidationError{Name: "group", err: errors.New(`ent: missing required field "Subscription.group"`)}
154  	}
155  	if v, ok := sc.mutation.Group(); ok {
156  		if err := subscription.GroupValidator(v); err != nil {
157  			return &ValidationError{Name: "group", err: fmt.Errorf(`ent: validator failed for field "Subscription.group": %w`, err)}
158  		}
159  	}
160  	if _, ok := sc.mutation.CreatedAt(); !ok {
161  		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Subscription.created_at"`)}
162  	}
163  	if _, ok := sc.mutation.UserID(); !ok {
164  		return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "Subscription.user"`)}
165  	}
166  	if _, ok := sc.mutation.FeedID(); !ok {
167  		return &ValidationError{Name: "feed", err: errors.New(`ent: missing required edge "Subscription.feed"`)}
168  	}
169  	return nil
170  }
171  
172  func (sc *SubscriptionCreate) sqlSave(ctx context.Context) (*Subscription, error) {
173  	if err := sc.check(); err != nil {
174  		return nil, err
175  	}
176  	_node, _spec := sc.createSpec()
177  	if err := sqlgraph.CreateNode(ctx, sc.driver, _spec); err != nil {
178  		if sqlgraph.IsConstraintError(err) {
179  			err = &ConstraintError{msg: err.Error(), wrap: err}
180  		}
181  		return nil, err
182  	}
183  	if _spec.ID.Value != nil {
184  		if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
185  			_node.ID = *id
186  		} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
187  			return nil, err
188  		}
189  	}
190  	sc.mutation.id = &_node.ID
191  	sc.mutation.done = true
192  	return _node, nil
193  }
194  
195  func (sc *SubscriptionCreate) createSpec() (*Subscription, *sqlgraph.CreateSpec) {
196  	var (
197  		_node = &Subscription{config: sc.config}
198  		_spec = sqlgraph.NewCreateSpec(subscription.Table, sqlgraph.NewFieldSpec(subscription.FieldID, field.TypeUUID))
199  	)
200  	_spec.OnConflict = sc.conflict
201  	if id, ok := sc.mutation.ID(); ok {
202  		_node.ID = id
203  		_spec.ID.Value = &id
204  	}
205  	if value, ok := sc.mutation.Name(); ok {
206  		_spec.SetField(subscription.FieldName, field.TypeString, value)
207  		_node.Name = value
208  	}
209  	if value, ok := sc.mutation.Group(); ok {
210  		_spec.SetField(subscription.FieldGroup, field.TypeString, value)
211  		_node.Group = value
212  	}
213  	if value, ok := sc.mutation.CreatedAt(); ok {
214  		_spec.SetField(subscription.FieldCreatedAt, field.TypeTime, value)
215  		_node.CreatedAt = value
216  	}
217  	if nodes := sc.mutation.UserIDs(); len(nodes) > 0 {
218  		edge := &sqlgraph.EdgeSpec{
219  			Rel:     sqlgraph.M2O,
220  			Inverse: false,
221  			Table:   subscription.UserTable,
222  			Columns: []string{subscription.UserColumn},
223  			Bidi:    false,
224  			Target: &sqlgraph.EdgeTarget{
225  				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
226  			},
227  		}
228  		for _, k := range nodes {
229  			edge.Target.Nodes = append(edge.Target.Nodes, k)
230  		}
231  		_node.UserID = nodes[0]
232  		_spec.Edges = append(_spec.Edges, edge)
233  	}
234  	if nodes := sc.mutation.FeedIDs(); len(nodes) > 0 {
235  		edge := &sqlgraph.EdgeSpec{
236  			Rel:     sqlgraph.M2O,
237  			Inverse: false,
238  			Table:   subscription.FeedTable,
239  			Columns: []string{subscription.FeedColumn},
240  			Bidi:    false,
241  			Target: &sqlgraph.EdgeTarget{
242  				IDSpec: sqlgraph.NewFieldSpec(feed.FieldID, field.TypeUUID),
243  			},
244  		}
245  		for _, k := range nodes {
246  			edge.Target.Nodes = append(edge.Target.Nodes, k)
247  		}
248  		_node.FeedID = nodes[0]
249  		_spec.Edges = append(_spec.Edges, edge)
250  	}
251  	return _node, _spec
252  }
253  
254  // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
255  // of the `INSERT` statement. For example:
256  //
257  //	client.Subscription.Create().
258  //		SetUserID(v).
259  //		OnConflict(
260  //			// Update the row with the new values
261  //			// the was proposed for insertion.
262  //			sql.ResolveWithNewValues(),
263  //		).
264  //		// Override some of the fields with custom
265  //		// update values.
266  //		Update(func(u *ent.SubscriptionUpsert) {
267  //			SetUserID(v+v).
268  //		}).
269  //		Exec(ctx)
270  func (sc *SubscriptionCreate) OnConflict(opts ...sql.ConflictOption) *SubscriptionUpsertOne {
271  	sc.conflict = opts
272  	return &SubscriptionUpsertOne{
273  		create: sc,
274  	}
275  }
276  
277  // OnConflictColumns calls `OnConflict` and configures the columns
278  // as conflict target. Using this option is equivalent to using:
279  //
280  //	client.Subscription.Create().
281  //		OnConflict(sql.ConflictColumns(columns...)).
282  //		Exec(ctx)
283  func (sc *SubscriptionCreate) OnConflictColumns(columns ...string) *SubscriptionUpsertOne {
284  	sc.conflict = append(sc.conflict, sql.ConflictColumns(columns...))
285  	return &SubscriptionUpsertOne{
286  		create: sc,
287  	}
288  }
289  
290  type (
291  	// SubscriptionUpsertOne is the builder for "upsert"-ing
292  	//  one Subscription node.
293  	SubscriptionUpsertOne struct {
294  		create *SubscriptionCreate
295  	}
296  
297  	// SubscriptionUpsert is the "OnConflict" setter.
298  	SubscriptionUpsert struct {
299  		*sql.UpdateSet
300  	}
301  )
302  
303  // SetUserID sets the "user_id" field.
304  func (u *SubscriptionUpsert) SetUserID(v uuid.UUID) *SubscriptionUpsert {
305  	u.Set(subscription.FieldUserID, v)
306  	return u
307  }
308  
309  // UpdateUserID sets the "user_id" field to the value that was provided on create.
310  func (u *SubscriptionUpsert) UpdateUserID() *SubscriptionUpsert {
311  	u.SetExcluded(subscription.FieldUserID)
312  	return u
313  }
314  
315  // SetFeedID sets the "feed_id" field.
316  func (u *SubscriptionUpsert) SetFeedID(v uuid.UUID) *SubscriptionUpsert {
317  	u.Set(subscription.FieldFeedID, v)
318  	return u
319  }
320  
321  // UpdateFeedID sets the "feed_id" field to the value that was provided on create.
322  func (u *SubscriptionUpsert) UpdateFeedID() *SubscriptionUpsert {
323  	u.SetExcluded(subscription.FieldFeedID)
324  	return u
325  }
326  
327  // SetName sets the "name" field.
328  func (u *SubscriptionUpsert) SetName(v string) *SubscriptionUpsert {
329  	u.Set(subscription.FieldName, v)
330  	return u
331  }
332  
333  // UpdateName sets the "name" field to the value that was provided on create.
334  func (u *SubscriptionUpsert) UpdateName() *SubscriptionUpsert {
335  	u.SetExcluded(subscription.FieldName)
336  	return u
337  }
338  
339  // SetGroup sets the "group" field.
340  func (u *SubscriptionUpsert) SetGroup(v string) *SubscriptionUpsert {
341  	u.Set(subscription.FieldGroup, v)
342  	return u
343  }
344  
345  // UpdateGroup sets the "group" field to the value that was provided on create.
346  func (u *SubscriptionUpsert) UpdateGroup() *SubscriptionUpsert {
347  	u.SetExcluded(subscription.FieldGroup)
348  	return u
349  }
350  
351  // SetCreatedAt sets the "created_at" field.
352  func (u *SubscriptionUpsert) SetCreatedAt(v time.Time) *SubscriptionUpsert {
353  	u.Set(subscription.FieldCreatedAt, v)
354  	return u
355  }
356  
357  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
358  func (u *SubscriptionUpsert) UpdateCreatedAt() *SubscriptionUpsert {
359  	u.SetExcluded(subscription.FieldCreatedAt)
360  	return u
361  }
362  
363  // UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
364  // Using this option is equivalent to using:
365  //
366  //	client.Subscription.Create().
367  //		OnConflict(
368  //			sql.ResolveWithNewValues(),
369  //			sql.ResolveWith(func(u *sql.UpdateSet) {
370  //				u.SetIgnore(subscription.FieldID)
371  //			}),
372  //		).
373  //		Exec(ctx)
374  func (u *SubscriptionUpsertOne) UpdateNewValues() *SubscriptionUpsertOne {
375  	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
376  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
377  		if _, exists := u.create.mutation.ID(); exists {
378  			s.SetIgnore(subscription.FieldID)
379  		}
380  	}))
381  	return u
382  }
383  
384  // Ignore sets each column to itself in case of conflict.
385  // Using this option is equivalent to using:
386  //
387  //	client.Subscription.Create().
388  //	    OnConflict(sql.ResolveWithIgnore()).
389  //	    Exec(ctx)
390  func (u *SubscriptionUpsertOne) Ignore() *SubscriptionUpsertOne {
391  	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
392  	return u
393  }
394  
395  // DoNothing configures the conflict_action to `DO NOTHING`.
396  // Supported only by SQLite and PostgreSQL.
397  func (u *SubscriptionUpsertOne) DoNothing() *SubscriptionUpsertOne {
398  	u.create.conflict = append(u.create.conflict, sql.DoNothing())
399  	return u
400  }
401  
402  // Update allows overriding fields `UPDATE` values. See the SubscriptionCreate.OnConflict
403  // documentation for more info.
404  func (u *SubscriptionUpsertOne) Update(set func(*SubscriptionUpsert)) *SubscriptionUpsertOne {
405  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
406  		set(&SubscriptionUpsert{UpdateSet: update})
407  	}))
408  	return u
409  }
410  
411  // SetUserID sets the "user_id" field.
412  func (u *SubscriptionUpsertOne) SetUserID(v uuid.UUID) *SubscriptionUpsertOne {
413  	return u.Update(func(s *SubscriptionUpsert) {
414  		s.SetUserID(v)
415  	})
416  }
417  
418  // UpdateUserID sets the "user_id" field to the value that was provided on create.
419  func (u *SubscriptionUpsertOne) UpdateUserID() *SubscriptionUpsertOne {
420  	return u.Update(func(s *SubscriptionUpsert) {
421  		s.UpdateUserID()
422  	})
423  }
424  
425  // SetFeedID sets the "feed_id" field.
426  func (u *SubscriptionUpsertOne) SetFeedID(v uuid.UUID) *SubscriptionUpsertOne {
427  	return u.Update(func(s *SubscriptionUpsert) {
428  		s.SetFeedID(v)
429  	})
430  }
431  
432  // UpdateFeedID sets the "feed_id" field to the value that was provided on create.
433  func (u *SubscriptionUpsertOne) UpdateFeedID() *SubscriptionUpsertOne {
434  	return u.Update(func(s *SubscriptionUpsert) {
435  		s.UpdateFeedID()
436  	})
437  }
438  
439  // SetName sets the "name" field.
440  func (u *SubscriptionUpsertOne) SetName(v string) *SubscriptionUpsertOne {
441  	return u.Update(func(s *SubscriptionUpsert) {
442  		s.SetName(v)
443  	})
444  }
445  
446  // UpdateName sets the "name" field to the value that was provided on create.
447  func (u *SubscriptionUpsertOne) UpdateName() *SubscriptionUpsertOne {
448  	return u.Update(func(s *SubscriptionUpsert) {
449  		s.UpdateName()
450  	})
451  }
452  
453  // SetGroup sets the "group" field.
454  func (u *SubscriptionUpsertOne) SetGroup(v string) *SubscriptionUpsertOne {
455  	return u.Update(func(s *SubscriptionUpsert) {
456  		s.SetGroup(v)
457  	})
458  }
459  
460  // UpdateGroup sets the "group" field to the value that was provided on create.
461  func (u *SubscriptionUpsertOne) UpdateGroup() *SubscriptionUpsertOne {
462  	return u.Update(func(s *SubscriptionUpsert) {
463  		s.UpdateGroup()
464  	})
465  }
466  
467  // SetCreatedAt sets the "created_at" field.
468  func (u *SubscriptionUpsertOne) SetCreatedAt(v time.Time) *SubscriptionUpsertOne {
469  	return u.Update(func(s *SubscriptionUpsert) {
470  		s.SetCreatedAt(v)
471  	})
472  }
473  
474  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
475  func (u *SubscriptionUpsertOne) UpdateCreatedAt() *SubscriptionUpsertOne {
476  	return u.Update(func(s *SubscriptionUpsert) {
477  		s.UpdateCreatedAt()
478  	})
479  }
480  
481  // Exec executes the query.
482  func (u *SubscriptionUpsertOne) Exec(ctx context.Context) error {
483  	if len(u.create.conflict) == 0 {
484  		return errors.New("ent: missing options for SubscriptionCreate.OnConflict")
485  	}
486  	return u.create.Exec(ctx)
487  }
488  
489  // ExecX is like Exec, but panics if an error occurs.
490  func (u *SubscriptionUpsertOne) ExecX(ctx context.Context) {
491  	if err := u.create.Exec(ctx); err != nil {
492  		panic(err)
493  	}
494  }
495  
496  // Exec executes the UPSERT query and returns the inserted/updated ID.
497  func (u *SubscriptionUpsertOne) ID(ctx context.Context) (id uuid.UUID, err error) {
498  	if u.create.driver.Dialect() == dialect.MySQL {
499  		// In case of "ON CONFLICT", there is no way to get back non-numeric ID
500  		// fields from the database since MySQL does not support the RETURNING clause.
501  		return id, errors.New("ent: SubscriptionUpsertOne.ID is not supported by MySQL driver. Use SubscriptionUpsertOne.Exec instead")
502  	}
503  	node, err := u.create.Save(ctx)
504  	if err != nil {
505  		return id, err
506  	}
507  	return node.ID, nil
508  }
509  
510  // IDX is like ID, but panics if an error occurs.
511  func (u *SubscriptionUpsertOne) IDX(ctx context.Context) uuid.UUID {
512  	id, err := u.ID(ctx)
513  	if err != nil {
514  		panic(err)
515  	}
516  	return id
517  }
518  
519  // SubscriptionCreateBulk is the builder for creating many Subscription entities in bulk.
520  type SubscriptionCreateBulk struct {
521  	config
522  	err      error
523  	builders []*SubscriptionCreate
524  	conflict []sql.ConflictOption
525  }
526  
527  // Save creates the Subscription entities in the database.
528  func (scb *SubscriptionCreateBulk) Save(ctx context.Context) ([]*Subscription, error) {
529  	if scb.err != nil {
530  		return nil, scb.err
531  	}
532  	specs := make([]*sqlgraph.CreateSpec, len(scb.builders))
533  	nodes := make([]*Subscription, len(scb.builders))
534  	mutators := make([]Mutator, len(scb.builders))
535  	for i := range scb.builders {
536  		func(i int, root context.Context) {
537  			builder := scb.builders[i]
538  			builder.defaults()
539  			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
540  				mutation, ok := m.(*SubscriptionMutation)
541  				if !ok {
542  					return nil, fmt.Errorf("unexpected mutation type %T", m)
543  				}
544  				if err := builder.check(); err != nil {
545  					return nil, err
546  				}
547  				builder.mutation = mutation
548  				var err error
549  				nodes[i], specs[i] = builder.createSpec()
550  				if i < len(mutators)-1 {
551  					_, err = mutators[i+1].Mutate(root, scb.builders[i+1].mutation)
552  				} else {
553  					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
554  					spec.OnConflict = scb.conflict
555  					// Invoke the actual operation on the latest mutation in the chain.
556  					if err = sqlgraph.BatchCreate(ctx, scb.driver, spec); err != nil {
557  						if sqlgraph.IsConstraintError(err) {
558  							err = &ConstraintError{msg: err.Error(), wrap: err}
559  						}
560  					}
561  				}
562  				if err != nil {
563  					return nil, err
564  				}
565  				mutation.id = &nodes[i].ID
566  				mutation.done = true
567  				return nodes[i], nil
568  			})
569  			for i := len(builder.hooks) - 1; i >= 0; i-- {
570  				mut = builder.hooks[i](mut)
571  			}
572  			mutators[i] = mut
573  		}(i, ctx)
574  	}
575  	if len(mutators) > 0 {
576  		if _, err := mutators[0].Mutate(ctx, scb.builders[0].mutation); err != nil {
577  			return nil, err
578  		}
579  	}
580  	return nodes, nil
581  }
582  
583  // SaveX is like Save, but panics if an error occurs.
584  func (scb *SubscriptionCreateBulk) SaveX(ctx context.Context) []*Subscription {
585  	v, err := scb.Save(ctx)
586  	if err != nil {
587  		panic(err)
588  	}
589  	return v
590  }
591  
592  // Exec executes the query.
593  func (scb *SubscriptionCreateBulk) Exec(ctx context.Context) error {
594  	_, err := scb.Save(ctx)
595  	return err
596  }
597  
598  // ExecX is like Exec, but panics if an error occurs.
599  func (scb *SubscriptionCreateBulk) ExecX(ctx context.Context) {
600  	if err := scb.Exec(ctx); err != nil {
601  		panic(err)
602  	}
603  }
604  
605  // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
606  // of the `INSERT` statement. For example:
607  //
608  //	client.Subscription.CreateBulk(builders...).
609  //		OnConflict(
610  //			// Update the row with the new values
611  //			// the was proposed for insertion.
612  //			sql.ResolveWithNewValues(),
613  //		).
614  //		// Override some of the fields with custom
615  //		// update values.
616  //		Update(func(u *ent.SubscriptionUpsert) {
617  //			SetUserID(v+v).
618  //		}).
619  //		Exec(ctx)
620  func (scb *SubscriptionCreateBulk) OnConflict(opts ...sql.ConflictOption) *SubscriptionUpsertBulk {
621  	scb.conflict = opts
622  	return &SubscriptionUpsertBulk{
623  		create: scb,
624  	}
625  }
626  
627  // OnConflictColumns calls `OnConflict` and configures the columns
628  // as conflict target. Using this option is equivalent to using:
629  //
630  //	client.Subscription.Create().
631  //		OnConflict(sql.ConflictColumns(columns...)).
632  //		Exec(ctx)
633  func (scb *SubscriptionCreateBulk) OnConflictColumns(columns ...string) *SubscriptionUpsertBulk {
634  	scb.conflict = append(scb.conflict, sql.ConflictColumns(columns...))
635  	return &SubscriptionUpsertBulk{
636  		create: scb,
637  	}
638  }
639  
640  // SubscriptionUpsertBulk is the builder for "upsert"-ing
641  // a bulk of Subscription nodes.
642  type SubscriptionUpsertBulk struct {
643  	create *SubscriptionCreateBulk
644  }
645  
646  // UpdateNewValues updates the mutable fields using the new values that
647  // were set on create. Using this option is equivalent to using:
648  //
649  //	client.Subscription.Create().
650  //		OnConflict(
651  //			sql.ResolveWithNewValues(),
652  //			sql.ResolveWith(func(u *sql.UpdateSet) {
653  //				u.SetIgnore(subscription.FieldID)
654  //			}),
655  //		).
656  //		Exec(ctx)
657  func (u *SubscriptionUpsertBulk) UpdateNewValues() *SubscriptionUpsertBulk {
658  	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
659  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
660  		for _, b := range u.create.builders {
661  			if _, exists := b.mutation.ID(); exists {
662  				s.SetIgnore(subscription.FieldID)
663  			}
664  		}
665  	}))
666  	return u
667  }
668  
669  // Ignore sets each column to itself in case of conflict.
670  // Using this option is equivalent to using:
671  //
672  //	client.Subscription.Create().
673  //		OnConflict(sql.ResolveWithIgnore()).
674  //		Exec(ctx)
675  func (u *SubscriptionUpsertBulk) Ignore() *SubscriptionUpsertBulk {
676  	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
677  	return u
678  }
679  
680  // DoNothing configures the conflict_action to `DO NOTHING`.
681  // Supported only by SQLite and PostgreSQL.
682  func (u *SubscriptionUpsertBulk) DoNothing() *SubscriptionUpsertBulk {
683  	u.create.conflict = append(u.create.conflict, sql.DoNothing())
684  	return u
685  }
686  
687  // Update allows overriding fields `UPDATE` values. See the SubscriptionCreateBulk.OnConflict
688  // documentation for more info.
689  func (u *SubscriptionUpsertBulk) Update(set func(*SubscriptionUpsert)) *SubscriptionUpsertBulk {
690  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
691  		set(&SubscriptionUpsert{UpdateSet: update})
692  	}))
693  	return u
694  }
695  
696  // SetUserID sets the "user_id" field.
697  func (u *SubscriptionUpsertBulk) SetUserID(v uuid.UUID) *SubscriptionUpsertBulk {
698  	return u.Update(func(s *SubscriptionUpsert) {
699  		s.SetUserID(v)
700  	})
701  }
702  
703  // UpdateUserID sets the "user_id" field to the value that was provided on create.
704  func (u *SubscriptionUpsertBulk) UpdateUserID() *SubscriptionUpsertBulk {
705  	return u.Update(func(s *SubscriptionUpsert) {
706  		s.UpdateUserID()
707  	})
708  }
709  
710  // SetFeedID sets the "feed_id" field.
711  func (u *SubscriptionUpsertBulk) SetFeedID(v uuid.UUID) *SubscriptionUpsertBulk {
712  	return u.Update(func(s *SubscriptionUpsert) {
713  		s.SetFeedID(v)
714  	})
715  }
716  
717  // UpdateFeedID sets the "feed_id" field to the value that was provided on create.
718  func (u *SubscriptionUpsertBulk) UpdateFeedID() *SubscriptionUpsertBulk {
719  	return u.Update(func(s *SubscriptionUpsert) {
720  		s.UpdateFeedID()
721  	})
722  }
723  
724  // SetName sets the "name" field.
725  func (u *SubscriptionUpsertBulk) SetName(v string) *SubscriptionUpsertBulk {
726  	return u.Update(func(s *SubscriptionUpsert) {
727  		s.SetName(v)
728  	})
729  }
730  
731  // UpdateName sets the "name" field to the value that was provided on create.
732  func (u *SubscriptionUpsertBulk) UpdateName() *SubscriptionUpsertBulk {
733  	return u.Update(func(s *SubscriptionUpsert) {
734  		s.UpdateName()
735  	})
736  }
737  
738  // SetGroup sets the "group" field.
739  func (u *SubscriptionUpsertBulk) SetGroup(v string) *SubscriptionUpsertBulk {
740  	return u.Update(func(s *SubscriptionUpsert) {
741  		s.SetGroup(v)
742  	})
743  }
744  
745  // UpdateGroup sets the "group" field to the value that was provided on create.
746  func (u *SubscriptionUpsertBulk) UpdateGroup() *SubscriptionUpsertBulk {
747  	return u.Update(func(s *SubscriptionUpsert) {
748  		s.UpdateGroup()
749  	})
750  }
751  
752  // SetCreatedAt sets the "created_at" field.
753  func (u *SubscriptionUpsertBulk) SetCreatedAt(v time.Time) *SubscriptionUpsertBulk {
754  	return u.Update(func(s *SubscriptionUpsert) {
755  		s.SetCreatedAt(v)
756  	})
757  }
758  
759  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
760  func (u *SubscriptionUpsertBulk) UpdateCreatedAt() *SubscriptionUpsertBulk {
761  	return u.Update(func(s *SubscriptionUpsert) {
762  		s.UpdateCreatedAt()
763  	})
764  }
765  
766  // Exec executes the query.
767  func (u *SubscriptionUpsertBulk) Exec(ctx context.Context) error {
768  	if u.create.err != nil {
769  		return u.create.err
770  	}
771  	for i, b := range u.create.builders {
772  		if len(b.conflict) != 0 {
773  			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the SubscriptionCreateBulk instead", i)
774  		}
775  	}
776  	if len(u.create.conflict) == 0 {
777  		return errors.New("ent: missing options for SubscriptionCreateBulk.OnConflict")
778  	}
779  	return u.create.Exec(ctx)
780  }
781  
782  // ExecX is like Exec, but panics if an error occurs.
783  func (u *SubscriptionUpsertBulk) ExecX(ctx context.Context) {
784  	if err := u.create.Exec(ctx); err != nil {
785  		panic(err)
786  	}
787  }