// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "leafdev.top/leaf/rag/ent/library" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // LibraryCreate is the builder for creating a Library entity. type LibraryCreate struct { config mutation *LibraryMutation hooks []Hook } // SetName sets the "name" field. func (lc *LibraryCreate) SetName(s string) *LibraryCreate { lc.mutation.SetName(s) return lc } // SetUserID sets the "user_id" field. func (lc *LibraryCreate) SetUserID(u uint64) *LibraryCreate { lc.mutation.SetUserID(u) return lc } // SetCreatedAt sets the "created_at" field. func (lc *LibraryCreate) SetCreatedAt(t time.Time) *LibraryCreate { lc.mutation.SetCreatedAt(t) return lc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (lc *LibraryCreate) SetNillableCreatedAt(t *time.Time) *LibraryCreate { if t != nil { lc.SetCreatedAt(*t) } return lc } // SetUpdatedAt sets the "updated_at" field. func (lc *LibraryCreate) SetUpdatedAt(t time.Time) *LibraryCreate { lc.mutation.SetUpdatedAt(t) return lc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (lc *LibraryCreate) SetNillableUpdatedAt(t *time.Time) *LibraryCreate { if t != nil { lc.SetUpdatedAt(*t) } return lc } // Mutation returns the LibraryMutation object of the builder. func (lc *LibraryCreate) Mutation() *LibraryMutation { return lc.mutation } // Save creates the Library in the database. func (lc *LibraryCreate) Save(ctx context.Context) (*Library, error) { lc.defaults() return withHooks(ctx, lc.sqlSave, lc.mutation, lc.hooks) } // SaveX calls Save and panics if Save returns an error. func (lc *LibraryCreate) SaveX(ctx context.Context) *Library { v, err := lc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (lc *LibraryCreate) Exec(ctx context.Context) error { _, err := lc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (lc *LibraryCreate) ExecX(ctx context.Context) { if err := lc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (lc *LibraryCreate) defaults() { if _, ok := lc.mutation.CreatedAt(); !ok { v := library.DefaultCreatedAt() lc.mutation.SetCreatedAt(v) } if _, ok := lc.mutation.UpdatedAt(); !ok { v := library.DefaultUpdatedAt() lc.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (lc *LibraryCreate) check() error { if _, ok := lc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Library.name"`)} } if _, ok := lc.mutation.UserID(); !ok { return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "Library.user_id"`)} } if _, ok := lc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Library.created_at"`)} } if _, ok := lc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Library.updated_at"`)} } return nil } func (lc *LibraryCreate) sqlSave(ctx context.Context) (*Library, error) { if err := lc.check(); err != nil { return nil, err } _node, _spec := lc.createSpec() if err := sqlgraph.CreateNode(ctx, lc.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) lc.mutation.id = &_node.ID lc.mutation.done = true return _node, nil } func (lc *LibraryCreate) createSpec() (*Library, *sqlgraph.CreateSpec) { var ( _node = &Library{config: lc.config} _spec = sqlgraph.NewCreateSpec(library.Table, sqlgraph.NewFieldSpec(library.FieldID, field.TypeInt)) ) if value, ok := lc.mutation.Name(); ok { _spec.SetField(library.FieldName, field.TypeString, value) _node.Name = value } if value, ok := lc.mutation.UserID(); ok { _spec.SetField(library.FieldUserID, field.TypeUint64, value) _node.UserID = value } if value, ok := lc.mutation.CreatedAt(); ok { _spec.SetField(library.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := lc.mutation.UpdatedAt(); ok { _spec.SetField(library.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } return _node, _spec } // LibraryCreateBulk is the builder for creating many Library entities in bulk. type LibraryCreateBulk struct { config err error builders []*LibraryCreate } // Save creates the Library entities in the database. func (lcb *LibraryCreateBulk) Save(ctx context.Context) ([]*Library, error) { if lcb.err != nil { return nil, lcb.err } specs := make([]*sqlgraph.CreateSpec, len(lcb.builders)) nodes := make([]*Library, len(lcb.builders)) mutators := make([]Mutator, len(lcb.builders)) for i := range lcb.builders { func(i int, root context.Context) { builder := lcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*LibraryMutation) 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, lcb.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, lcb.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, lcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (lcb *LibraryCreateBulk) SaveX(ctx context.Context) []*Library { v, err := lcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (lcb *LibraryCreateBulk) Exec(ctx context.Context) error { _, err := lcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (lcb *LibraryCreateBulk) ExecX(ctx context.Context) { if err := lcb.Exec(ctx); err != nil { panic(err) } }