// Code generated by ent, DO NOT EDIT. package db import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/openmeterio/openmeter/pkg/framework/entutils/testutils/ent2/db/example2" ) // Example2Create is the builder for creating a Example2 entity. type Example2Create struct { config mutation *Example2Mutation hooks []Hook conflict []sql.ConflictOption } // SetCreatedAt sets the "created_at" field. func (_c *Example2Create) SetCreatedAt(v time.Time) *Example2Create { _c.mutation.SetCreatedAt(v) return _c } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (_c *Example2Create) SetNillableCreatedAt(v *time.Time) *Example2Create { if v != nil { _c.SetCreatedAt(*v) } return _c } // SetUpdatedAt sets the "updated_at" field. func (_c *Example2Create) SetUpdatedAt(v time.Time) *Example2Create { _c.mutation.SetUpdatedAt(v) return _c } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (_c *Example2Create) SetNillableUpdatedAt(v *time.Time) *Example2Create { if v != nil { _c.SetUpdatedAt(*v) } return _c } // SetDeletedAt sets the "deleted_at" field. func (_c *Example2Create) SetDeletedAt(v time.Time) *Example2Create { _c.mutation.SetDeletedAt(v) return _c } // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil. func (_c *Example2Create) SetNillableDeletedAt(v *time.Time) *Example2Create { if v != nil { _c.SetDeletedAt(*v) } return _c } // SetExampleValue2 sets the "example_value_2" field. func (_c *Example2Create) SetExampleValue2(v string) *Example2Create { _c.mutation.SetExampleValue2(v) return _c } // SetID sets the "id" field. func (_c *Example2Create) SetID(v string) *Example2Create { _c.mutation.SetID(v) return _c } // Mutation returns the Example2Mutation object of the builder. func (_c *Example2Create) Mutation() *Example2Mutation { return _c.mutation } // Save creates the Example2 in the database. func (_c *Example2Create) Save(ctx context.Context) (*Example2, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *Example2Create) SaveX(ctx context.Context) *Example2 { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *Example2Create) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *Example2Create) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (_c *Example2Create) defaults() { if _, ok := _c.mutation.CreatedAt(); !ok { v := example2.DefaultCreatedAt() _c.mutation.SetCreatedAt(v) } if _, ok := _c.mutation.UpdatedAt(); !ok { v := example2.DefaultUpdatedAt() _c.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (_c *Example2Create) check() error { if _, ok := _c.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "Example2.created_at"`)} } if _, ok := _c.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`db: missing required field "Example2.updated_at"`)} } if _, ok := _c.mutation.ExampleValue2(); !ok { return &ValidationError{Name: "example_value_2", err: errors.New(`db: missing required field "Example2.example_value_2"`)} } return nil } func (_c *Example2Create) sqlSave(ctx context.Context) (*Example2, error) { if err := _c.check(); err != nil { return nil, err } _node, _spec := _c.createSpec() 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 { if id, ok := _spec.ID.Value.(string); ok { _node.ID = id } else { return nil, fmt.Errorf("unexpected Example2.ID type: %T", _spec.ID.Value) } } _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *Example2Create) createSpec() (*Example2, *sqlgraph.CreateSpec) { var ( _node = &Example2{config: _c.config} _spec = sqlgraph.NewCreateSpec(example2.Table, sqlgraph.NewFieldSpec(example2.FieldID, field.TypeString)) ) _spec.OnConflict = _c.conflict if id, ok := _c.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := _c.mutation.CreatedAt(); ok { _spec.SetField(example2.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := _c.mutation.UpdatedAt(); ok { _spec.SetField(example2.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if value, ok := _c.mutation.DeletedAt(); ok { _spec.SetField(example2.FieldDeletedAt, field.TypeTime, value) _node.DeletedAt = &value } if value, ok := _c.mutation.ExampleValue2(); ok { _spec.SetField(example2.FieldExampleValue2, field.TypeString, value) _node.ExampleValue2 = value } return _node, _spec } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.Example2.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.Example2Upsert) { // SetCreatedAt(v+v). // }). // Exec(ctx) func (_c *Example2Create) OnConflict(opts ...sql.ConflictOption) *Example2UpsertOne { _c.conflict = opts return &Example2UpsertOne{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.Example2.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *Example2Create) OnConflictColumns(columns ...string) *Example2UpsertOne { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &Example2UpsertOne{ create: _c, } } type ( // Example2UpsertOne is the builder for "upsert"-ing // one Example2 node. Example2UpsertOne struct { create *Example2Create } // Example2Upsert is the "OnConflict" setter. Example2Upsert struct { *sql.UpdateSet } ) // SetUpdatedAt sets the "updated_at" field. func (u *Example2Upsert) SetUpdatedAt(v time.Time) *Example2Upsert { u.Set(example2.FieldUpdatedAt, v) return u } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *Example2Upsert) UpdateUpdatedAt() *Example2Upsert { u.SetExcluded(example2.FieldUpdatedAt) return u } // SetDeletedAt sets the "deleted_at" field. func (u *Example2Upsert) SetDeletedAt(v time.Time) *Example2Upsert { u.Set(example2.FieldDeletedAt, v) return u } // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. func (u *Example2Upsert) UpdateDeletedAt() *Example2Upsert { u.SetExcluded(example2.FieldDeletedAt) return u } // ClearDeletedAt clears the value of the "deleted_at" field. func (u *Example2Upsert) ClearDeletedAt() *Example2Upsert { u.SetNull(example2.FieldDeletedAt) return u } // SetExampleValue2 sets the "example_value_2" field. func (u *Example2Upsert) SetExampleValue2(v string) *Example2Upsert { u.Set(example2.FieldExampleValue2, v) return u } // UpdateExampleValue2 sets the "example_value_2" field to the value that was provided on create. func (u *Example2Upsert) UpdateExampleValue2() *Example2Upsert { u.SetExcluded(example2.FieldExampleValue2) 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.Example2.Create(). // OnConflict( // sql.ResolveWithNewValues(), // sql.ResolveWith(func(u *sql.UpdateSet) { // u.SetIgnore(example2.FieldID) // }), // ). // Exec(ctx) func (u *Example2UpsertOne) UpdateNewValues() *Example2UpsertOne { 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(example2.FieldID) } if _, exists := u.create.mutation.CreatedAt(); exists { s.SetIgnore(example2.FieldCreatedAt) } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.Example2.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *Example2UpsertOne) Ignore() *Example2UpsertOne { 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 *Example2UpsertOne) DoNothing() *Example2UpsertOne { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the Example2Create.OnConflict // documentation for more info. func (u *Example2UpsertOne) Update(set func(*Example2Upsert)) *Example2UpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&Example2Upsert{UpdateSet: update}) })) return u } // SetUpdatedAt sets the "updated_at" field. func (u *Example2UpsertOne) SetUpdatedAt(v time.Time) *Example2UpsertOne { return u.Update(func(s *Example2Upsert) { s.SetUpdatedAt(v) }) } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *Example2UpsertOne) UpdateUpdatedAt() *Example2UpsertOne { return u.Update(func(s *Example2Upsert) { s.UpdateUpdatedAt() }) } // SetDeletedAt sets the "deleted_at" field. func (u *Example2UpsertOne) SetDeletedAt(v time.Time) *Example2UpsertOne { return u.Update(func(s *Example2Upsert) { s.SetDeletedAt(v) }) } // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. func (u *Example2UpsertOne) UpdateDeletedAt() *Example2UpsertOne { return u.Update(func(s *Example2Upsert) { s.UpdateDeletedAt() }) } // ClearDeletedAt clears the value of the "deleted_at" field. func (u *Example2UpsertOne) ClearDeletedAt() *Example2UpsertOne { return u.Update(func(s *Example2Upsert) { s.ClearDeletedAt() }) } // SetExampleValue2 sets the "example_value_2" field. func (u *Example2UpsertOne) SetExampleValue2(v string) *Example2UpsertOne { return u.Update(func(s *Example2Upsert) { s.SetExampleValue2(v) }) } // UpdateExampleValue2 sets the "example_value_2" field to the value that was provided on create. func (u *Example2UpsertOne) UpdateExampleValue2() *Example2UpsertOne { return u.Update(func(s *Example2Upsert) { s.UpdateExampleValue2() }) } // Exec executes the query. func (u *Example2UpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { return errors.New("db: missing options for Example2Create.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *Example2UpsertOne) 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 *Example2UpsertOne) ID(ctx context.Context) (id string, err error) { if u.create.driver.Dialect() == dialect.MySQL { // In case of "ON CONFLICT", there is no way to get back non-numeric ID // fields from the database since MySQL does not support the RETURNING clause. return id, errors.New("db: Example2UpsertOne.ID is not supported by MySQL driver. Use Example2UpsertOne.Exec instead") } 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 *Example2UpsertOne) IDX(ctx context.Context) string { id, err := u.ID(ctx) if err != nil { panic(err) } return id } // Example2CreateBulk is the builder for creating many Example2 entities in bulk. type Example2CreateBulk struct { config err error builders []*Example2Create conflict []sql.ConflictOption } // Save creates the Example2 entities in the database. func (_c *Example2CreateBulk) Save(ctx context.Context) ([]*Example2, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*Example2, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*Example2Mutation) 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, _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 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 *Example2CreateBulk) SaveX(ctx context.Context) []*Example2 { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *Example2CreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *Example2CreateBulk) 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.Example2.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.Example2Upsert) { // SetCreatedAt(v+v). // }). // Exec(ctx) func (_c *Example2CreateBulk) OnConflict(opts ...sql.ConflictOption) *Example2UpsertBulk { _c.conflict = opts return &Example2UpsertBulk{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.Example2.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *Example2CreateBulk) OnConflictColumns(columns ...string) *Example2UpsertBulk { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &Example2UpsertBulk{ create: _c, } } // Example2UpsertBulk is the builder for "upsert"-ing // a bulk of Example2 nodes. type Example2UpsertBulk struct { create *Example2CreateBulk } // UpdateNewValues updates the mutable fields using the new values that // were set on create. Using this option is equivalent to using: // // client.Example2.Create(). // OnConflict( // sql.ResolveWithNewValues(), // sql.ResolveWith(func(u *sql.UpdateSet) { // u.SetIgnore(example2.FieldID) // }), // ). // Exec(ctx) func (u *Example2UpsertBulk) UpdateNewValues() *Example2UpsertBulk { 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(example2.FieldID) } if _, exists := b.mutation.CreatedAt(); exists { s.SetIgnore(example2.FieldCreatedAt) } } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.Example2.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *Example2UpsertBulk) Ignore() *Example2UpsertBulk { 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 *Example2UpsertBulk) DoNothing() *Example2UpsertBulk { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the Example2CreateBulk.OnConflict // documentation for more info. func (u *Example2UpsertBulk) Update(set func(*Example2Upsert)) *Example2UpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&Example2Upsert{UpdateSet: update}) })) return u } // SetUpdatedAt sets the "updated_at" field. func (u *Example2UpsertBulk) SetUpdatedAt(v time.Time) *Example2UpsertBulk { return u.Update(func(s *Example2Upsert) { s.SetUpdatedAt(v) }) } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *Example2UpsertBulk) UpdateUpdatedAt() *Example2UpsertBulk { return u.Update(func(s *Example2Upsert) { s.UpdateUpdatedAt() }) } // SetDeletedAt sets the "deleted_at" field. func (u *Example2UpsertBulk) SetDeletedAt(v time.Time) *Example2UpsertBulk { return u.Update(func(s *Example2Upsert) { s.SetDeletedAt(v) }) } // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. func (u *Example2UpsertBulk) UpdateDeletedAt() *Example2UpsertBulk { return u.Update(func(s *Example2Upsert) { s.UpdateDeletedAt() }) } // ClearDeletedAt clears the value of the "deleted_at" field. func (u *Example2UpsertBulk) ClearDeletedAt() *Example2UpsertBulk { return u.Update(func(s *Example2Upsert) { s.ClearDeletedAt() }) } // SetExampleValue2 sets the "example_value_2" field. func (u *Example2UpsertBulk) SetExampleValue2(v string) *Example2UpsertBulk { return u.Update(func(s *Example2Upsert) { s.SetExampleValue2(v) }) } // UpdateExampleValue2 sets the "example_value_2" field to the value that was provided on create. func (u *Example2UpsertBulk) UpdateExampleValue2() *Example2UpsertBulk { return u.Update(func(s *Example2Upsert) { s.UpdateExampleValue2() }) } // Exec executes the query. func (u *Example2UpsertBulk) 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("db: OnConflict was set for builder %d. Set it on the Example2CreateBulk instead", i) } } if len(u.create.conflict) == 0 { return errors.New("db: missing options for Example2CreateBulk.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *Example2UpsertBulk) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } }