Files
polaris/ent/storage_create.go
2024-07-10 17:35:16 +08:00

302 lines
8.1 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"polaris/ent/storage"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// StorageCreate is the builder for creating a Storage entity.
type StorageCreate struct {
config
mutation *StorageMutation
hooks []Hook
}
// SetName sets the "name" field.
func (sc *StorageCreate) SetName(s string) *StorageCreate {
sc.mutation.SetName(s)
return sc
}
// SetImplementation sets the "implementation" field.
func (sc *StorageCreate) SetImplementation(s string) *StorageCreate {
sc.mutation.SetImplementation(s)
return sc
}
// SetPath sets the "path" field.
func (sc *StorageCreate) SetPath(s string) *StorageCreate {
sc.mutation.SetPath(s)
return sc
}
// SetUser sets the "user" field.
func (sc *StorageCreate) SetUser(s string) *StorageCreate {
sc.mutation.SetUser(s)
return sc
}
// SetNillableUser sets the "user" field if the given value is not nil.
func (sc *StorageCreate) SetNillableUser(s *string) *StorageCreate {
if s != nil {
sc.SetUser(*s)
}
return sc
}
// SetPassword sets the "password" field.
func (sc *StorageCreate) SetPassword(s string) *StorageCreate {
sc.mutation.SetPassword(s)
return sc
}
// SetNillablePassword sets the "password" field if the given value is not nil.
func (sc *StorageCreate) SetNillablePassword(s *string) *StorageCreate {
if s != nil {
sc.SetPassword(*s)
}
return sc
}
// SetDeleted sets the "deleted" field.
func (sc *StorageCreate) SetDeleted(b bool) *StorageCreate {
sc.mutation.SetDeleted(b)
return sc
}
// SetNillableDeleted sets the "deleted" field if the given value is not nil.
func (sc *StorageCreate) SetNillableDeleted(b *bool) *StorageCreate {
if b != nil {
sc.SetDeleted(*b)
}
return sc
}
// SetDefault sets the "default" field.
func (sc *StorageCreate) SetDefault(b bool) *StorageCreate {
sc.mutation.SetDefault(b)
return sc
}
// SetNillableDefault sets the "default" field if the given value is not nil.
func (sc *StorageCreate) SetNillableDefault(b *bool) *StorageCreate {
if b != nil {
sc.SetDefault(*b)
}
return sc
}
// Mutation returns the StorageMutation object of the builder.
func (sc *StorageCreate) Mutation() *StorageMutation {
return sc.mutation
}
// Save creates the Storage in the database.
func (sc *StorageCreate) Save(ctx context.Context) (*Storage, error) {
sc.defaults()
return withHooks(ctx, sc.sqlSave, sc.mutation, sc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (sc *StorageCreate) SaveX(ctx context.Context) *Storage {
v, err := sc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (sc *StorageCreate) Exec(ctx context.Context) error {
_, err := sc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (sc *StorageCreate) ExecX(ctx context.Context) {
if err := sc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (sc *StorageCreate) defaults() {
if _, ok := sc.mutation.Deleted(); !ok {
v := storage.DefaultDeleted
sc.mutation.SetDeleted(v)
}
if _, ok := sc.mutation.Default(); !ok {
v := storage.DefaultDefault
sc.mutation.SetDefault(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (sc *StorageCreate) check() error {
if _, ok := sc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Storage.name"`)}
}
if _, ok := sc.mutation.Implementation(); !ok {
return &ValidationError{Name: "implementation", err: errors.New(`ent: missing required field "Storage.implementation"`)}
}
if _, ok := sc.mutation.Path(); !ok {
return &ValidationError{Name: "path", err: errors.New(`ent: missing required field "Storage.path"`)}
}
if _, ok := sc.mutation.Deleted(); !ok {
return &ValidationError{Name: "deleted", err: errors.New(`ent: missing required field "Storage.deleted"`)}
}
if _, ok := sc.mutation.Default(); !ok {
return &ValidationError{Name: "default", err: errors.New(`ent: missing required field "Storage.default"`)}
}
return nil
}
func (sc *StorageCreate) sqlSave(ctx context.Context) (*Storage, error) {
if err := sc.check(); err != nil {
return nil, err
}
_node, _spec := sc.createSpec()
if err := sqlgraph.CreateNode(ctx, sc.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)
sc.mutation.id = &_node.ID
sc.mutation.done = true
return _node, nil
}
func (sc *StorageCreate) createSpec() (*Storage, *sqlgraph.CreateSpec) {
var (
_node = &Storage{config: sc.config}
_spec = sqlgraph.NewCreateSpec(storage.Table, sqlgraph.NewFieldSpec(storage.FieldID, field.TypeInt))
)
if value, ok := sc.mutation.Name(); ok {
_spec.SetField(storage.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := sc.mutation.Implementation(); ok {
_spec.SetField(storage.FieldImplementation, field.TypeString, value)
_node.Implementation = value
}
if value, ok := sc.mutation.Path(); ok {
_spec.SetField(storage.FieldPath, field.TypeString, value)
_node.Path = value
}
if value, ok := sc.mutation.User(); ok {
_spec.SetField(storage.FieldUser, field.TypeString, value)
_node.User = value
}
if value, ok := sc.mutation.Password(); ok {
_spec.SetField(storage.FieldPassword, field.TypeString, value)
_node.Password = value
}
if value, ok := sc.mutation.Deleted(); ok {
_spec.SetField(storage.FieldDeleted, field.TypeBool, value)
_node.Deleted = value
}
if value, ok := sc.mutation.Default(); ok {
_spec.SetField(storage.FieldDefault, field.TypeBool, value)
_node.Default = value
}
return _node, _spec
}
// StorageCreateBulk is the builder for creating many Storage entities in bulk.
type StorageCreateBulk struct {
config
err error
builders []*StorageCreate
}
// Save creates the Storage entities in the database.
func (scb *StorageCreateBulk) Save(ctx context.Context) ([]*Storage, error) {
if scb.err != nil {
return nil, scb.err
}
specs := make([]*sqlgraph.CreateSpec, len(scb.builders))
nodes := make([]*Storage, len(scb.builders))
mutators := make([]Mutator, len(scb.builders))
for i := range scb.builders {
func(i int, root context.Context) {
builder := scb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*StorageMutation)
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, scb.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, scb.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, scb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (scb *StorageCreateBulk) SaveX(ctx context.Context) []*Storage {
v, err := scb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (scb *StorageCreateBulk) Exec(ctx context.Context) error {
_, err := scb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (scb *StorageCreateBulk) ExecX(ctx context.Context) {
if err := scb.Exec(ctx); err != nil {
panic(err)
}
}