// Copyright 2019-present Facebook Inc. 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 ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/customid/ent/schema" "entgo.io/ent/entc/integration/customid/ent/valuescan" "entgo.io/ent/schema/field" ) // ValueScanCreate is the builder for creating a ValueScan entity. type ValueScanCreate struct { config mutation *ValueScanMutation hooks []Hook conflict []sql.ConflictOption } // SetName sets the "name" field. func (_c *ValueScanCreate) SetName(v string) *ValueScanCreate { _c.mutation.SetName(v) return _c } // SetID sets the "id" field. func (_c *ValueScanCreate) SetID(v schema.ValueScanID) *ValueScanCreate { _c.mutation.SetID(v) return _c } // Mutation returns the ValueScanMutation object of the builder. func (_c *ValueScanCreate) Mutation() *ValueScanMutation { return _c.mutation } // Save creates the ValueScan in the database. func (_c *ValueScanCreate) Save(ctx context.Context) (*ValueScan, error) { return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *ValueScanCreate) SaveX(ctx context.Context) *ValueScan { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *ValueScanCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *ValueScanCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (_c *ValueScanCreate) check() error { if _, ok := _c.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "ValueScan.name"`)} } return nil } func (_c *ValueScanCreate) sqlSave(ctx context.Context) (*ValueScan, error) { if err := _c.check(); err != nil { return nil, err } _node, _spec, err := _c.createSpec() if err != nil { return nil, err } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } if _spec.ID.Value != nil { sv, ok := _spec.ID.Value.(field.ValueScanner) if !ok { sv = valuescan.ValueScanner.ID.ScanValue() if err := sv.Scan(_spec.ID.Value); err != nil { return nil, err } } if value, err := valuescan.ValueScanner.ID.FromValue(sv); err != nil { return nil, err } else { _node.ID = value } } _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *ValueScanCreate) createSpec() (*ValueScan, *sqlgraph.CreateSpec, error) { var ( _node = &ValueScan{config: _c.config} _spec = sqlgraph.NewCreateSpec(valuescan.Table, sqlgraph.NewFieldSpec(valuescan.FieldID, field.TypeInt)) ) _spec.OnConflict = _c.conflict if id, ok := _c.mutation.ID(); ok { _node.ID = id vv, err := valuescan.ValueScanner.ID.Value(id) if err != nil { return nil, nil, err } _spec.ID.Value = vv } if value, ok := _c.mutation.Name(); ok { _spec.SetField(valuescan.FieldName, field.TypeString, value) _node.Name = value } return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.ValueScan.Create(). // SetName(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.ValueScanUpsert) { // SetName(v+v). // }). // Exec(ctx) func (_c *ValueScanCreate) OnConflict(opts ...sql.ConflictOption) *ValueScanUpsertOne { _c.conflict = opts return &ValueScanUpsertOne{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.ValueScan.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *ValueScanCreate) OnConflictColumns(columns ...string) *ValueScanUpsertOne { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &ValueScanUpsertOne{ create: _c, } } type ( // ValueScanUpsertOne is the builder for "upsert"-ing // one ValueScan node. ValueScanUpsertOne struct { create *ValueScanCreate } // ValueScanUpsert is the "OnConflict" setter. ValueScanUpsert struct { *sql.UpdateSet } ) // SetName sets the "name" field. func (u *ValueScanUpsert) SetName(v string) *ValueScanUpsert { u.Set(valuescan.FieldName, v) return u } // UpdateName sets the "name" field to the value that was provided on create. func (u *ValueScanUpsert) UpdateName() *ValueScanUpsert { u.SetExcluded(valuescan.FieldName) return u } // UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field. // Using this option is equivalent to using: // // client.ValueScan.Create(). // OnConflict( // sql.ResolveWithNewValues(), // sql.ResolveWith(func(u *sql.UpdateSet) { // u.SetIgnore(valuescan.FieldID) // }), // ). // Exec(ctx) func (u *ValueScanUpsertOne) UpdateNewValues() *ValueScanUpsertOne { 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.ID(); exists { s.SetIgnore(valuescan.FieldID) } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.ValueScan.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *ValueScanUpsertOne) Ignore() *ValueScanUpsertOne { 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 *ValueScanUpsertOne) DoNothing() *ValueScanUpsertOne { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the ValueScanCreate.OnConflict // documentation for more info. func (u *ValueScanUpsertOne) Update(set func(*ValueScanUpsert)) *ValueScanUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&ValueScanUpsert{UpdateSet: update}) })) return u } // SetName sets the "name" field. func (u *ValueScanUpsertOne) SetName(v string) *ValueScanUpsertOne { return u.Update(func(s *ValueScanUpsert) { s.SetName(v) }) } // UpdateName sets the "name" field to the value that was provided on create. func (u *ValueScanUpsertOne) UpdateName() *ValueScanUpsertOne { return u.Update(func(s *ValueScanUpsert) { s.UpdateName() }) } // Exec executes the query. func (u *ValueScanUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { return errors.New("ent: missing options for ValueScanCreate.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *ValueScanUpsertOne) 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 *ValueScanUpsertOne) ID(ctx context.Context) (id schema.ValueScanID, 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 *ValueScanUpsertOne) IDX(ctx context.Context) schema.ValueScanID { id, err := u.ID(ctx) if err != nil { panic(err) } return id } // ValueScanCreateBulk is the builder for creating many ValueScan entities in bulk. type ValueScanCreateBulk struct { config err error builders []*ValueScanCreate conflict []sql.ConflictOption } // Save creates the ValueScan entities in the database. func (_c *ValueScanCreateBulk) Save(ctx context.Context) ([]*ValueScan, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*ValueScan, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.builders[i] var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*ValueScanMutation) 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], err = builder.createSpec() if err != nil { return nil, err } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { spec := &sqlgraph.BatchCreateSpec{Nodes: specs} spec.OnConflict = _c.conflict // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, _c.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 { sv, ok := specs[i].ID.Value.(field.ValueScanner) if !ok { sv = valuescan.ValueScanner.ID.ScanValue() if err := sv.Scan(specs[i].ID.Value); err != nil { return nil, err } } if id, err := valuescan.ValueScanner.ID.FromValue(sv); err != nil { return nil, err } else { nodes[i].ID = 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, _c.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (_c *ValueScanCreateBulk) SaveX(ctx context.Context) []*ValueScan { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *ValueScanCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *ValueScanCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.ValueScan.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.ValueScanUpsert) { // SetName(v+v). // }). // Exec(ctx) func (_c *ValueScanCreateBulk) OnConflict(opts ...sql.ConflictOption) *ValueScanUpsertBulk { _c.conflict = opts return &ValueScanUpsertBulk{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.ValueScan.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *ValueScanCreateBulk) OnConflictColumns(columns ...string) *ValueScanUpsertBulk { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &ValueScanUpsertBulk{ create: _c, } } // ValueScanUpsertBulk is the builder for "upsert"-ing // a bulk of ValueScan nodes. type ValueScanUpsertBulk struct { create *ValueScanCreateBulk } // UpdateNewValues updates the mutable fields using the new values that // were set on create. Using this option is equivalent to using: // // client.ValueScan.Create(). // OnConflict( // sql.ResolveWithNewValues(), // sql.ResolveWith(func(u *sql.UpdateSet) { // u.SetIgnore(valuescan.FieldID) // }), // ). // Exec(ctx) func (u *ValueScanUpsertBulk) UpdateNewValues() *ValueScanUpsertBulk { 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.ID(); exists { s.SetIgnore(valuescan.FieldID) } } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.ValueScan.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *ValueScanUpsertBulk) Ignore() *ValueScanUpsertBulk { 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 *ValueScanUpsertBulk) DoNothing() *ValueScanUpsertBulk { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the ValueScanCreateBulk.OnConflict // documentation for more info. func (u *ValueScanUpsertBulk) Update(set func(*ValueScanUpsert)) *ValueScanUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&ValueScanUpsert{UpdateSet: update}) })) return u } // SetName sets the "name" field. func (u *ValueScanUpsertBulk) SetName(v string) *ValueScanUpsertBulk { return u.Update(func(s *ValueScanUpsert) { s.SetName(v) }) } // UpdateName sets the "name" field to the value that was provided on create. func (u *ValueScanUpsertBulk) UpdateName() *ValueScanUpsertBulk { return u.Update(func(s *ValueScanUpsert) { s.UpdateName() }) } // Exec executes the query. func (u *ValueScanUpsertBulk) 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 ValueScanCreateBulk instead", i) } } if len(u.create.conflict) == 0 { return errors.New("ent: missing options for ValueScanCreateBulk.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *ValueScanUpsertBulk) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } }