// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/cloudreve/Cloudreve/v4/ent/giftcode" "github.com/cloudreve/Cloudreve/v4/ent/user" ) // GiftCodeCreate is the builder for creating a GiftCode entity. type GiftCodeCreate struct { config mutation *GiftCodeMutation hooks []Hook conflict []sql.ConflictOption } // SetCreatedAt sets the "created_at" field. func (gcc *GiftCodeCreate) SetCreatedAt(t time.Time) *GiftCodeCreate { gcc.mutation.SetCreatedAt(t) return gcc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (gcc *GiftCodeCreate) SetNillableCreatedAt(t *time.Time) *GiftCodeCreate { if t != nil { gcc.SetCreatedAt(*t) } return gcc } // SetUpdatedAt sets the "updated_at" field. func (gcc *GiftCodeCreate) SetUpdatedAt(t time.Time) *GiftCodeCreate { gcc.mutation.SetUpdatedAt(t) return gcc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (gcc *GiftCodeCreate) SetNillableUpdatedAt(t *time.Time) *GiftCodeCreate { if t != nil { gcc.SetUpdatedAt(*t) } return gcc } // SetDeletedAt sets the "deleted_at" field. func (gcc *GiftCodeCreate) SetDeletedAt(t time.Time) *GiftCodeCreate { gcc.mutation.SetDeletedAt(t) return gcc } // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil. func (gcc *GiftCodeCreate) SetNillableDeletedAt(t *time.Time) *GiftCodeCreate { if t != nil { gcc.SetDeletedAt(*t) } return gcc } // SetCode sets the "code" field. func (gcc *GiftCodeCreate) SetCode(s string) *GiftCodeCreate { gcc.mutation.SetCode(s) return gcc } // SetType sets the "type" field. func (gcc *GiftCodeCreate) SetType(gi giftcode.Type) *GiftCodeCreate { gcc.mutation.SetType(gi) return gcc } // SetAmount sets the "amount" field. func (gcc *GiftCodeCreate) SetAmount(i int64) *GiftCodeCreate { gcc.mutation.SetAmount(i) return gcc } // SetDuration sets the "duration" field. func (gcc *GiftCodeCreate) SetDuration(i int64) *GiftCodeCreate { gcc.mutation.SetDuration(i) return gcc } // SetNillableDuration sets the "duration" field if the given value is not nil. func (gcc *GiftCodeCreate) SetNillableDuration(i *int64) *GiftCodeCreate { if i != nil { gcc.SetDuration(*i) } return gcc } // SetUsedByID sets the "used_by_id" field. func (gcc *GiftCodeCreate) SetUsedByID(i int) *GiftCodeCreate { gcc.mutation.SetUsedByID(i) return gcc } // SetNillableUsedByID sets the "used_by_id" field if the given value is not nil. func (gcc *GiftCodeCreate) SetNillableUsedByID(i *int) *GiftCodeCreate { if i != nil { gcc.SetUsedByID(*i) } return gcc } // SetUsedAt sets the "used_at" field. func (gcc *GiftCodeCreate) SetUsedAt(t time.Time) *GiftCodeCreate { gcc.mutation.SetUsedAt(t) return gcc } // SetNillableUsedAt sets the "used_at" field if the given value is not nil. func (gcc *GiftCodeCreate) SetNillableUsedAt(t *time.Time) *GiftCodeCreate { if t != nil { gcc.SetUsedAt(*t) } return gcc } // SetDes sets the "des" field. func (gcc *GiftCodeCreate) SetDes(s string) *GiftCodeCreate { gcc.mutation.SetDes(s) return gcc } // SetNillableDes sets the "des" field if the given value is not nil. func (gcc *GiftCodeCreate) SetNillableDes(s *string) *GiftCodeCreate { if s != nil { gcc.SetDes(*s) } return gcc } // SetRedeemerID sets the "redeemer" edge to the User entity by ID. func (gcc *GiftCodeCreate) SetRedeemerID(id int) *GiftCodeCreate { gcc.mutation.SetRedeemerID(id) return gcc } // SetNillableRedeemerID sets the "redeemer" edge to the User entity by ID if the given value is not nil. func (gcc *GiftCodeCreate) SetNillableRedeemerID(id *int) *GiftCodeCreate { if id != nil { gcc = gcc.SetRedeemerID(*id) } return gcc } // SetRedeemer sets the "redeemer" edge to the User entity. func (gcc *GiftCodeCreate) SetRedeemer(u *User) *GiftCodeCreate { return gcc.SetRedeemerID(u.ID) } // Mutation returns the GiftCodeMutation object of the builder. func (gcc *GiftCodeCreate) Mutation() *GiftCodeMutation { return gcc.mutation } // Save creates the GiftCode in the database. func (gcc *GiftCodeCreate) Save(ctx context.Context) (*GiftCode, error) { if err := gcc.defaults(); err != nil { return nil, err } return withHooks(ctx, gcc.sqlSave, gcc.mutation, gcc.hooks) } // SaveX calls Save and panics if Save returns an error. func (gcc *GiftCodeCreate) SaveX(ctx context.Context) *GiftCode { v, err := gcc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (gcc *GiftCodeCreate) Exec(ctx context.Context) error { _, err := gcc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (gcc *GiftCodeCreate) ExecX(ctx context.Context) { if err := gcc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (gcc *GiftCodeCreate) defaults() error { if _, ok := gcc.mutation.CreatedAt(); !ok { if giftcode.DefaultCreatedAt == nil { return fmt.Errorf("ent: uninitialized giftcode.DefaultCreatedAt (forgotten import ent/runtime?)") } v := giftcode.DefaultCreatedAt() gcc.mutation.SetCreatedAt(v) } if _, ok := gcc.mutation.UpdatedAt(); !ok { if giftcode.DefaultUpdatedAt == nil { return fmt.Errorf("ent: uninitialized giftcode.DefaultUpdatedAt (forgotten import ent/runtime?)") } v := giftcode.DefaultUpdatedAt() gcc.mutation.SetUpdatedAt(v) } return nil } // check runs all checks and user-defined validators on the builder. func (gcc *GiftCodeCreate) check() error { if _, ok := gcc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "GiftCode.created_at"`)} } if _, ok := gcc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "GiftCode.updated_at"`)} } if _, ok := gcc.mutation.Code(); !ok { return &ValidationError{Name: "code", err: errors.New(`ent: missing required field "GiftCode.code"`)} } if _, ok := gcc.mutation.GetType(); !ok { return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "GiftCode.type"`)} } if v, ok := gcc.mutation.GetType(); ok { if err := giftcode.TypeValidator(v); err != nil { return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "GiftCode.type": %w`, err)} } } if _, ok := gcc.mutation.Amount(); !ok { return &ValidationError{Name: "amount", err: errors.New(`ent: missing required field "GiftCode.amount"`)} } return nil } func (gcc *GiftCodeCreate) sqlSave(ctx context.Context) (*GiftCode, error) { if err := gcc.check(); err != nil { return nil, err } _node, _spec := gcc.createSpec() if err := sqlgraph.CreateNode(ctx, gcc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } id := _spec.ID.Value.(int64) _node.ID = int(id) gcc.mutation.id = &_node.ID gcc.mutation.done = true return _node, nil } func (gcc *GiftCodeCreate) createSpec() (*GiftCode, *sqlgraph.CreateSpec) { var ( _node = &GiftCode{config: gcc.config} _spec = sqlgraph.NewCreateSpec(giftcode.Table, sqlgraph.NewFieldSpec(giftcode.FieldID, field.TypeInt)) ) if id, ok := gcc.mutation.ID(); ok { _node.ID = id id64 := int64(id) _spec.ID.Value = id64 } _spec.OnConflict = gcc.conflict if value, ok := gcc.mutation.CreatedAt(); ok { _spec.SetField(giftcode.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := gcc.mutation.UpdatedAt(); ok { _spec.SetField(giftcode.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if value, ok := gcc.mutation.DeletedAt(); ok { _spec.SetField(giftcode.FieldDeletedAt, field.TypeTime, value) _node.DeletedAt = &value } if value, ok := gcc.mutation.Code(); ok { _spec.SetField(giftcode.FieldCode, field.TypeString, value) _node.Code = value } if value, ok := gcc.mutation.GetType(); ok { _spec.SetField(giftcode.FieldType, field.TypeEnum, value) _node.Type = value } if value, ok := gcc.mutation.Amount(); ok { _spec.SetField(giftcode.FieldAmount, field.TypeInt64, value) _node.Amount = value } if value, ok := gcc.mutation.Duration(); ok { _spec.SetField(giftcode.FieldDuration, field.TypeInt64, value) _node.Duration = value } if value, ok := gcc.mutation.UsedAt(); ok { _spec.SetField(giftcode.FieldUsedAt, field.TypeTime, value) _node.UsedAt = &value } if value, ok := gcc.mutation.Des(); ok { _spec.SetField(giftcode.FieldDes, field.TypeString, value) _node.Des = value } if nodes := gcc.mutation.RedeemerIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: giftcode.RedeemerTable, Columns: []string{giftcode.RedeemerColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.UsedByID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.GiftCode.Create(). // SetCreatedAt(v). // OnConflict( // // Update the row with the new values // // the was proposed for insertion. // sql.ResolveWithNewValues(), // ). // // Override some of the fields with custom // // update values. // Update(func(u *ent.GiftCodeUpsert) { // SetCreatedAt(v+v). // }). // Exec(ctx) func (gcc *GiftCodeCreate) OnConflict(opts ...sql.ConflictOption) *GiftCodeUpsertOne { gcc.conflict = opts return &GiftCodeUpsertOne{ create: gcc, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.GiftCode.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (gcc *GiftCodeCreate) OnConflictColumns(columns ...string) *GiftCodeUpsertOne { gcc.conflict = append(gcc.conflict, sql.ConflictColumns(columns...)) return &GiftCodeUpsertOne{ create: gcc, } } type ( // GiftCodeUpsertOne is the builder for "upsert"-ing // one GiftCode node. GiftCodeUpsertOne struct { create *GiftCodeCreate } // GiftCodeUpsert is the "OnConflict" setter. GiftCodeUpsert struct { *sql.UpdateSet } ) // SetUpdatedAt sets the "updated_at" field. func (u *GiftCodeUpsert) SetUpdatedAt(v time.Time) *GiftCodeUpsert { u.Set(giftcode.FieldUpdatedAt, v) return u } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateUpdatedAt() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldUpdatedAt) return u } // SetDeletedAt sets the "deleted_at" field. func (u *GiftCodeUpsert) SetDeletedAt(v time.Time) *GiftCodeUpsert { u.Set(giftcode.FieldDeletedAt, v) return u } // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateDeletedAt() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldDeletedAt) return u } // ClearDeletedAt clears the value of the "deleted_at" field. func (u *GiftCodeUpsert) ClearDeletedAt() *GiftCodeUpsert { u.SetNull(giftcode.FieldDeletedAt) return u } // SetCode sets the "code" field. func (u *GiftCodeUpsert) SetCode(v string) *GiftCodeUpsert { u.Set(giftcode.FieldCode, v) return u } // UpdateCode sets the "code" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateCode() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldCode) return u } // SetType sets the "type" field. func (u *GiftCodeUpsert) SetType(v giftcode.Type) *GiftCodeUpsert { u.Set(giftcode.FieldType, v) return u } // UpdateType sets the "type" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateType() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldType) return u } // SetAmount sets the "amount" field. func (u *GiftCodeUpsert) SetAmount(v int64) *GiftCodeUpsert { u.Set(giftcode.FieldAmount, v) return u } // UpdateAmount sets the "amount" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateAmount() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldAmount) return u } // AddAmount adds v to the "amount" field. func (u *GiftCodeUpsert) AddAmount(v int64) *GiftCodeUpsert { u.Add(giftcode.FieldAmount, v) return u } // SetDuration sets the "duration" field. func (u *GiftCodeUpsert) SetDuration(v int64) *GiftCodeUpsert { u.Set(giftcode.FieldDuration, v) return u } // UpdateDuration sets the "duration" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateDuration() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldDuration) return u } // AddDuration adds v to the "duration" field. func (u *GiftCodeUpsert) AddDuration(v int64) *GiftCodeUpsert { u.Add(giftcode.FieldDuration, v) return u } // ClearDuration clears the value of the "duration" field. func (u *GiftCodeUpsert) ClearDuration() *GiftCodeUpsert { u.SetNull(giftcode.FieldDuration) return u } // SetUsedByID sets the "used_by_id" field. func (u *GiftCodeUpsert) SetUsedByID(v int) *GiftCodeUpsert { u.Set(giftcode.FieldUsedByID, v) return u } // UpdateUsedByID sets the "used_by_id" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateUsedByID() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldUsedByID) return u } // ClearUsedByID clears the value of the "used_by_id" field. func (u *GiftCodeUpsert) ClearUsedByID() *GiftCodeUpsert { u.SetNull(giftcode.FieldUsedByID) return u } // SetUsedAt sets the "used_at" field. func (u *GiftCodeUpsert) SetUsedAt(v time.Time) *GiftCodeUpsert { u.Set(giftcode.FieldUsedAt, v) return u } // UpdateUsedAt sets the "used_at" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateUsedAt() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldUsedAt) return u } // ClearUsedAt clears the value of the "used_at" field. func (u *GiftCodeUpsert) ClearUsedAt() *GiftCodeUpsert { u.SetNull(giftcode.FieldUsedAt) return u } // SetDes sets the "des" field. func (u *GiftCodeUpsert) SetDes(v string) *GiftCodeUpsert { u.Set(giftcode.FieldDes, v) return u } // UpdateDes sets the "des" field to the value that was provided on create. func (u *GiftCodeUpsert) UpdateDes() *GiftCodeUpsert { u.SetExcluded(giftcode.FieldDes) return u } // ClearDes clears the value of the "des" field. func (u *GiftCodeUpsert) ClearDes() *GiftCodeUpsert { u.SetNull(giftcode.FieldDes) return u } // UpdateNewValues updates the mutable fields using the new values that were set on create. // Using this option is equivalent to using: // // client.GiftCode.Create(). // OnConflict( // sql.ResolveWithNewValues(), // ). // Exec(ctx) func (u *GiftCodeUpsertOne) UpdateNewValues() *GiftCodeUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) { if _, exists := u.create.mutation.CreatedAt(); exists { s.SetIgnore(giftcode.FieldCreatedAt) } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.GiftCode.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *GiftCodeUpsertOne) Ignore() *GiftCodeUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) return u } // DoNothing configures the conflict_action to `DO NOTHING`. // Supported only by SQLite and PostgreSQL. func (u *GiftCodeUpsertOne) DoNothing() *GiftCodeUpsertOne { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the GiftCodeCreate.OnConflict // documentation for more info. func (u *GiftCodeUpsertOne) Update(set func(*GiftCodeUpsert)) *GiftCodeUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&GiftCodeUpsert{UpdateSet: update}) })) return u } // SetUpdatedAt sets the "updated_at" field. func (u *GiftCodeUpsertOne) SetUpdatedAt(v time.Time) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetUpdatedAt(v) }) } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateUpdatedAt() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateUpdatedAt() }) } // SetDeletedAt sets the "deleted_at" field. func (u *GiftCodeUpsertOne) SetDeletedAt(v time.Time) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetDeletedAt(v) }) } // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateDeletedAt() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateDeletedAt() }) } // ClearDeletedAt clears the value of the "deleted_at" field. func (u *GiftCodeUpsertOne) ClearDeletedAt() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.ClearDeletedAt() }) } // SetCode sets the "code" field. func (u *GiftCodeUpsertOne) SetCode(v string) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetCode(v) }) } // UpdateCode sets the "code" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateCode() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateCode() }) } // SetType sets the "type" field. func (u *GiftCodeUpsertOne) SetType(v giftcode.Type) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetType(v) }) } // UpdateType sets the "type" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateType() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateType() }) } // SetAmount sets the "amount" field. func (u *GiftCodeUpsertOne) SetAmount(v int64) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetAmount(v) }) } // AddAmount adds v to the "amount" field. func (u *GiftCodeUpsertOne) AddAmount(v int64) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.AddAmount(v) }) } // UpdateAmount sets the "amount" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateAmount() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateAmount() }) } // SetDuration sets the "duration" field. func (u *GiftCodeUpsertOne) SetDuration(v int64) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetDuration(v) }) } // AddDuration adds v to the "duration" field. func (u *GiftCodeUpsertOne) AddDuration(v int64) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.AddDuration(v) }) } // UpdateDuration sets the "duration" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateDuration() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateDuration() }) } // ClearDuration clears the value of the "duration" field. func (u *GiftCodeUpsertOne) ClearDuration() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.ClearDuration() }) } // SetUsedByID sets the "used_by_id" field. func (u *GiftCodeUpsertOne) SetUsedByID(v int) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetUsedByID(v) }) } // UpdateUsedByID sets the "used_by_id" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateUsedByID() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateUsedByID() }) } // ClearUsedByID clears the value of the "used_by_id" field. func (u *GiftCodeUpsertOne) ClearUsedByID() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.ClearUsedByID() }) } // SetUsedAt sets the "used_at" field. func (u *GiftCodeUpsertOne) SetUsedAt(v time.Time) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetUsedAt(v) }) } // UpdateUsedAt sets the "used_at" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateUsedAt() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateUsedAt() }) } // ClearUsedAt clears the value of the "used_at" field. func (u *GiftCodeUpsertOne) ClearUsedAt() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.ClearUsedAt() }) } // SetDes sets the "des" field. func (u *GiftCodeUpsertOne) SetDes(v string) *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.SetDes(v) }) } // UpdateDes sets the "des" field to the value that was provided on create. func (u *GiftCodeUpsertOne) UpdateDes() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.UpdateDes() }) } // ClearDes clears the value of the "des" field. func (u *GiftCodeUpsertOne) ClearDes() *GiftCodeUpsertOne { return u.Update(func(s *GiftCodeUpsert) { s.ClearDes() }) } // Exec executes the query. func (u *GiftCodeUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { return errors.New("ent: missing options for GiftCodeCreate.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *GiftCodeUpsertOne) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } } // Exec executes the UPSERT query and returns the inserted/updated ID. func (u *GiftCodeUpsertOne) ID(ctx context.Context) (id int, err error) { node, err := u.create.Save(ctx) if err != nil { return id, err } return node.ID, nil } // IDX is like ID, but panics if an error occurs. func (u *GiftCodeUpsertOne) IDX(ctx context.Context) int { id, err := u.ID(ctx) if err != nil { panic(err) } return id } func (m *GiftCodeCreate) SetRawID(t int) *GiftCodeCreate { m.mutation.SetRawID(t) return m } // GiftCodeCreateBulk is the builder for creating many GiftCode entities in bulk. type GiftCodeCreateBulk struct { config err error builders []*GiftCodeCreate conflict []sql.ConflictOption } // Save creates the GiftCode entities in the database. func (gccb *GiftCodeCreateBulk) Save(ctx context.Context) ([]*GiftCode, error) { if gccb.err != nil { return nil, gccb.err } specs := make([]*sqlgraph.CreateSpec, len(gccb.builders)) nodes := make([]*GiftCode, len(gccb.builders)) mutators := make([]Mutator, len(gccb.builders)) for i := range gccb.builders { func(i int, root context.Context) { builder := gccb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*GiftCodeMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation var err error nodes[i], specs[i] = builder.createSpec() if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, gccb.builders[i+1].mutation) } else { spec := &sqlgraph.BatchCreateSpec{Nodes: specs} spec.OnConflict = gccb.conflict // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, gccb.driver, spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } } } if err != nil { return nil, err } mutation.id = &nodes[i].ID if specs[i].ID.Value != nil { id := specs[i].ID.Value.(int64) nodes[i].ID = int(id) } mutation.done = true return nodes[i], nil }) for i := len(builder.hooks) - 1; i >= 0; i-- { mut = builder.hooks[i](mut) } mutators[i] = mut }(i, ctx) } if len(mutators) > 0 { if _, err := mutators[0].Mutate(ctx, gccb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (gccb *GiftCodeCreateBulk) SaveX(ctx context.Context) []*GiftCode { v, err := gccb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (gccb *GiftCodeCreateBulk) Exec(ctx context.Context) error { _, err := gccb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (gccb *GiftCodeCreateBulk) ExecX(ctx context.Context) { if err := gccb.Exec(ctx); err != nil { panic(err) } } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.GiftCode.CreateBulk(builders...). // OnConflict( // // Update the row with the new values // // the was proposed for insertion. // sql.ResolveWithNewValues(), // ). // // Override some of the fields with custom // // update values. // Update(func(u *ent.GiftCodeUpsert) { // SetCreatedAt(v+v). // }). // Exec(ctx) func (gccb *GiftCodeCreateBulk) OnConflict(opts ...sql.ConflictOption) *GiftCodeUpsertBulk { gccb.conflict = opts return &GiftCodeUpsertBulk{ create: gccb, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.GiftCode.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (gccb *GiftCodeCreateBulk) OnConflictColumns(columns ...string) *GiftCodeUpsertBulk { gccb.conflict = append(gccb.conflict, sql.ConflictColumns(columns...)) return &GiftCodeUpsertBulk{ create: gccb, } } // GiftCodeUpsertBulk is the builder for "upsert"-ing // a bulk of GiftCode nodes. type GiftCodeUpsertBulk struct { create *GiftCodeCreateBulk } // UpdateNewValues updates the mutable fields using the new values that // were set on create. Using this option is equivalent to using: // // client.GiftCode.Create(). // OnConflict( // sql.ResolveWithNewValues(), // ). // Exec(ctx) func (u *GiftCodeUpsertBulk) UpdateNewValues() *GiftCodeUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) { for _, b := range u.create.builders { if _, exists := b.mutation.CreatedAt(); exists { s.SetIgnore(giftcode.FieldCreatedAt) } } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.GiftCode.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *GiftCodeUpsertBulk) Ignore() *GiftCodeUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) return u } // DoNothing configures the conflict_action to `DO NOTHING`. // Supported only by SQLite and PostgreSQL. func (u *GiftCodeUpsertBulk) DoNothing() *GiftCodeUpsertBulk { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the GiftCodeCreateBulk.OnConflict // documentation for more info. func (u *GiftCodeUpsertBulk) Update(set func(*GiftCodeUpsert)) *GiftCodeUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&GiftCodeUpsert{UpdateSet: update}) })) return u } // SetUpdatedAt sets the "updated_at" field. func (u *GiftCodeUpsertBulk) SetUpdatedAt(v time.Time) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetUpdatedAt(v) }) } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateUpdatedAt() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateUpdatedAt() }) } // SetDeletedAt sets the "deleted_at" field. func (u *GiftCodeUpsertBulk) SetDeletedAt(v time.Time) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetDeletedAt(v) }) } // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateDeletedAt() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateDeletedAt() }) } // ClearDeletedAt clears the value of the "deleted_at" field. func (u *GiftCodeUpsertBulk) ClearDeletedAt() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.ClearDeletedAt() }) } // SetCode sets the "code" field. func (u *GiftCodeUpsertBulk) SetCode(v string) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetCode(v) }) } // UpdateCode sets the "code" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateCode() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateCode() }) } // SetType sets the "type" field. func (u *GiftCodeUpsertBulk) SetType(v giftcode.Type) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetType(v) }) } // UpdateType sets the "type" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateType() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateType() }) } // SetAmount sets the "amount" field. func (u *GiftCodeUpsertBulk) SetAmount(v int64) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetAmount(v) }) } // AddAmount adds v to the "amount" field. func (u *GiftCodeUpsertBulk) AddAmount(v int64) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.AddAmount(v) }) } // UpdateAmount sets the "amount" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateAmount() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateAmount() }) } // SetDuration sets the "duration" field. func (u *GiftCodeUpsertBulk) SetDuration(v int64) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetDuration(v) }) } // AddDuration adds v to the "duration" field. func (u *GiftCodeUpsertBulk) AddDuration(v int64) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.AddDuration(v) }) } // UpdateDuration sets the "duration" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateDuration() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateDuration() }) } // ClearDuration clears the value of the "duration" field. func (u *GiftCodeUpsertBulk) ClearDuration() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.ClearDuration() }) } // SetUsedByID sets the "used_by_id" field. func (u *GiftCodeUpsertBulk) SetUsedByID(v int) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetUsedByID(v) }) } // UpdateUsedByID sets the "used_by_id" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateUsedByID() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateUsedByID() }) } // ClearUsedByID clears the value of the "used_by_id" field. func (u *GiftCodeUpsertBulk) ClearUsedByID() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.ClearUsedByID() }) } // SetUsedAt sets the "used_at" field. func (u *GiftCodeUpsertBulk) SetUsedAt(v time.Time) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetUsedAt(v) }) } // UpdateUsedAt sets the "used_at" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateUsedAt() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateUsedAt() }) } // ClearUsedAt clears the value of the "used_at" field. func (u *GiftCodeUpsertBulk) ClearUsedAt() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.ClearUsedAt() }) } // SetDes sets the "des" field. func (u *GiftCodeUpsertBulk) SetDes(v string) *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.SetDes(v) }) } // UpdateDes sets the "des" field to the value that was provided on create. func (u *GiftCodeUpsertBulk) UpdateDes() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.UpdateDes() }) } // ClearDes clears the value of the "des" field. func (u *GiftCodeUpsertBulk) ClearDes() *GiftCodeUpsertBulk { return u.Update(func(s *GiftCodeUpsert) { s.ClearDes() }) } // Exec executes the query. func (u *GiftCodeUpsertBulk) Exec(ctx context.Context) error { if u.create.err != nil { return u.create.err } for i, b := range u.create.builders { if len(b.conflict) != 0 { return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the GiftCodeCreateBulk instead", i) } } if len(u.create.conflict) == 0 { return errors.New("ent: missing options for GiftCodeCreateBulk.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *GiftCodeUpsertBulk) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } }