rag/ent/documentblock_create.go

325 lines
9.5 KiB
Go
Raw Normal View History

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"leafdev.top/leaf/rag/ent/document"
"leafdev.top/leaf/rag/ent/documentblock"
)
// DocumentBlockCreate is the builder for creating a DocumentBlock entity.
type DocumentBlockCreate struct {
config
mutation *DocumentBlockMutation
hooks []Hook
}
// SetName sets the "name" field.
func (dbc *DocumentBlockCreate) SetName(s string) *DocumentBlockCreate {
dbc.mutation.SetName(s)
return dbc
}
// SetContent sets the "content" field.
func (dbc *DocumentBlockCreate) SetContent(s string) *DocumentBlockCreate {
dbc.mutation.SetContent(s)
return dbc
}
// SetOrder sets the "order" field.
func (dbc *DocumentBlockCreate) SetOrder(i int) *DocumentBlockCreate {
dbc.mutation.SetOrder(i)
return dbc
}
// SetDocumentID sets the "document_id" field.
func (dbc *DocumentBlockCreate) SetDocumentID(i int) *DocumentBlockCreate {
dbc.mutation.SetDocumentID(i)
return dbc
}
// SetUserID sets the "user_id" field.
func (dbc *DocumentBlockCreate) SetUserID(s string) *DocumentBlockCreate {
dbc.mutation.SetUserID(s)
return dbc
}
// SetCreatedAt sets the "created_at" field.
func (dbc *DocumentBlockCreate) SetCreatedAt(t time.Time) *DocumentBlockCreate {
dbc.mutation.SetCreatedAt(t)
return dbc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (dbc *DocumentBlockCreate) SetNillableCreatedAt(t *time.Time) *DocumentBlockCreate {
if t != nil {
dbc.SetCreatedAt(*t)
}
return dbc
}
// SetUpdatedAt sets the "updated_at" field.
func (dbc *DocumentBlockCreate) SetUpdatedAt(t time.Time) *DocumentBlockCreate {
dbc.mutation.SetUpdatedAt(t)
return dbc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (dbc *DocumentBlockCreate) SetNillableUpdatedAt(t *time.Time) *DocumentBlockCreate {
if t != nil {
dbc.SetUpdatedAt(*t)
}
return dbc
}
// AddDocumentIDs adds the "document" edge to the Document entity by IDs.
func (dbc *DocumentBlockCreate) AddDocumentIDs(ids ...int) *DocumentBlockCreate {
dbc.mutation.AddDocumentIDs(ids...)
return dbc
}
// AddDocument adds the "document" edges to the Document entity.
func (dbc *DocumentBlockCreate) AddDocument(d ...*Document) *DocumentBlockCreate {
ids := make([]int, len(d))
for i := range d {
ids[i] = d[i].ID
}
return dbc.AddDocumentIDs(ids...)
}
// Mutation returns the DocumentBlockMutation object of the builder.
func (dbc *DocumentBlockCreate) Mutation() *DocumentBlockMutation {
return dbc.mutation
}
// Save creates the DocumentBlock in the database.
func (dbc *DocumentBlockCreate) Save(ctx context.Context) (*DocumentBlock, error) {
dbc.defaults()
return withHooks(ctx, dbc.sqlSave, dbc.mutation, dbc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (dbc *DocumentBlockCreate) SaveX(ctx context.Context) *DocumentBlock {
v, err := dbc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (dbc *DocumentBlockCreate) Exec(ctx context.Context) error {
_, err := dbc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (dbc *DocumentBlockCreate) ExecX(ctx context.Context) {
if err := dbc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (dbc *DocumentBlockCreate) defaults() {
if _, ok := dbc.mutation.CreatedAt(); !ok {
v := documentblock.DefaultCreatedAt()
dbc.mutation.SetCreatedAt(v)
}
if _, ok := dbc.mutation.UpdatedAt(); !ok {
v := documentblock.DefaultUpdatedAt()
dbc.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (dbc *DocumentBlockCreate) check() error {
if _, ok := dbc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "DocumentBlock.name"`)}
}
if _, ok := dbc.mutation.Content(); !ok {
return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "DocumentBlock.content"`)}
}
if _, ok := dbc.mutation.Order(); !ok {
return &ValidationError{Name: "order", err: errors.New(`ent: missing required field "DocumentBlock.order"`)}
}
if _, ok := dbc.mutation.DocumentID(); !ok {
return &ValidationError{Name: "document_id", err: errors.New(`ent: missing required field "DocumentBlock.document_id"`)}
}
if _, ok := dbc.mutation.UserID(); !ok {
return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "DocumentBlock.user_id"`)}
}
if _, ok := dbc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "DocumentBlock.created_at"`)}
}
if _, ok := dbc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "DocumentBlock.updated_at"`)}
}
return nil
}
func (dbc *DocumentBlockCreate) sqlSave(ctx context.Context) (*DocumentBlock, error) {
if err := dbc.check(); err != nil {
return nil, err
}
_node, _spec := dbc.createSpec()
if err := sqlgraph.CreateNode(ctx, dbc.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)
dbc.mutation.id = &_node.ID
dbc.mutation.done = true
return _node, nil
}
func (dbc *DocumentBlockCreate) createSpec() (*DocumentBlock, *sqlgraph.CreateSpec) {
var (
_node = &DocumentBlock{config: dbc.config}
_spec = sqlgraph.NewCreateSpec(documentblock.Table, sqlgraph.NewFieldSpec(documentblock.FieldID, field.TypeInt))
)
if value, ok := dbc.mutation.Name(); ok {
_spec.SetField(documentblock.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := dbc.mutation.Content(); ok {
_spec.SetField(documentblock.FieldContent, field.TypeString, value)
_node.Content = value
}
if value, ok := dbc.mutation.Order(); ok {
_spec.SetField(documentblock.FieldOrder, field.TypeInt, value)
_node.Order = value
}
if value, ok := dbc.mutation.DocumentID(); ok {
_spec.SetField(documentblock.FieldDocumentID, field.TypeInt, value)
_node.DocumentID = value
}
if value, ok := dbc.mutation.UserID(); ok {
_spec.SetField(documentblock.FieldUserID, field.TypeString, value)
_node.UserID = value
}
if value, ok := dbc.mutation.CreatedAt(); ok {
_spec.SetField(documentblock.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := dbc.mutation.UpdatedAt(); ok {
_spec.SetField(documentblock.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if nodes := dbc.mutation.DocumentIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: documentblock.DocumentTable,
Columns: []string{documentblock.DocumentColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(document.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// DocumentBlockCreateBulk is the builder for creating many DocumentBlock entities in bulk.
type DocumentBlockCreateBulk struct {
config
err error
builders []*DocumentBlockCreate
}
// Save creates the DocumentBlock entities in the database.
func (dbcb *DocumentBlockCreateBulk) Save(ctx context.Context) ([]*DocumentBlock, error) {
if dbcb.err != nil {
return nil, dbcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(dbcb.builders))
nodes := make([]*DocumentBlock, len(dbcb.builders))
mutators := make([]Mutator, len(dbcb.builders))
for i := range dbcb.builders {
func(i int, root context.Context) {
builder := dbcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*DocumentBlockMutation)
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, dbcb.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, dbcb.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, dbcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (dbcb *DocumentBlockCreateBulk) SaveX(ctx context.Context) []*DocumentBlock {
v, err := dbcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (dbcb *DocumentBlockCreateBulk) Exec(ctx context.Context) error {
_, err := dbcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (dbcb *DocumentBlockCreateBulk) ExecX(ctx context.Context) {
if err := dbcb.Exec(ctx); err != nil {
panic(err)
}
}