932 lines
26 KiB
Go
932 lines
26 KiB
Go
// 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"
|
|
"io"
|
|
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/dialect/sql"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/entc/integration/ent/document"
|
|
"entgo.io/ent/entc/integration/ent/schema"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// DocumentCreate is the builder for creating a Document entity.
|
|
type DocumentCreate struct {
|
|
config
|
|
mutation *DocumentMutation
|
|
hooks []Hook
|
|
conflict []sql.ConflictOption
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (_c *DocumentCreate) SetName(v string) *DocumentCreate {
|
|
_c.mutation.SetName(v)
|
|
return _c
|
|
}
|
|
|
|
// SetAttachment sets the "attachment" field.
|
|
func (_c *DocumentCreate) SetAttachment(v []byte) *DocumentCreate {
|
|
_c.mutation.SetAttachment(v)
|
|
return _c
|
|
}
|
|
|
|
// SetMetadata sets the "metadata" field.
|
|
func (_c *DocumentCreate) SetMetadata(v []byte) *DocumentCreate {
|
|
_c.mutation.SetMetadata(v)
|
|
return _c
|
|
}
|
|
|
|
// SetNillableMetadata sets the "metadata" field if the given value is not nil.
|
|
func (_c *DocumentCreate) SetNillableMetadata(v *[]byte) *DocumentCreate {
|
|
if v != nil {
|
|
_c.SetMetadata(*v)
|
|
}
|
|
return _c
|
|
}
|
|
|
|
// SetPayload sets the "payload" field.
|
|
func (_c *DocumentCreate) SetPayload(v *schema.DocPayload) *DocumentCreate {
|
|
_c.mutation.SetPayload(v)
|
|
return _c
|
|
}
|
|
|
|
// SetDescription sets the "description" field.
|
|
func (_c *DocumentCreate) SetDescription(v string) *DocumentCreate {
|
|
_c.mutation.SetDescription(v)
|
|
return _c
|
|
}
|
|
|
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
|
func (_c *DocumentCreate) SetNillableDescription(v *string) *DocumentCreate {
|
|
if v != nil {
|
|
_c.SetDescription(*v)
|
|
}
|
|
return _c
|
|
}
|
|
|
|
// SetContent sets the "content" field.
|
|
func (_c *DocumentCreate) SetContent(v io.Reader) *DocumentCreate {
|
|
_c.mutation.SetContent(v)
|
|
return _c
|
|
}
|
|
|
|
// SetThumbnail sets the "thumbnail" field.
|
|
func (_c *DocumentCreate) SetThumbnail(v io.Reader) *DocumentCreate {
|
|
_c.mutation.SetThumbnail(v)
|
|
return _c
|
|
}
|
|
|
|
// SetArchive sets the "archive" field.
|
|
func (_c *DocumentCreate) SetArchive(v io.Reader) *DocumentCreate {
|
|
_c.mutation.SetArchive(v)
|
|
return _c
|
|
}
|
|
|
|
// Mutation returns the DocumentMutation object of the builder.
|
|
func (_c *DocumentCreate) Mutation() *DocumentMutation {
|
|
return _c.mutation
|
|
}
|
|
|
|
// Save creates the Document in the database.
|
|
func (_c *DocumentCreate) Save(ctx context.Context) (*Document, error) {
|
|
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (_c *DocumentCreate) SaveX(ctx context.Context) *Document {
|
|
v, err := _c.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_c *DocumentCreate) Exec(ctx context.Context) error {
|
|
_, err := _c.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_c *DocumentCreate) 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 *DocumentCreate) check() error {
|
|
if _, ok := _c.mutation.Name(); !ok {
|
|
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Document.name"`)}
|
|
}
|
|
if _, ok := _c.mutation.Content(); !ok {
|
|
return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "Document.content"`)}
|
|
}
|
|
if _, ok := _c.mutation.Thumbnail(); !ok {
|
|
return &ValidationError{Name: "thumbnail", err: errors.New(`ent: missing required field "Document.thumbnail"`)}
|
|
}
|
|
if _, ok := _c.mutation.Attachment(); !ok {
|
|
return &ValidationError{Name: "attachment", err: errors.New(`ent: missing required field "Document.attachment"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_c *DocumentCreate) sqlSave(ctx context.Context) (*Document, error) {
|
|
if err := _c.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec, err := _c.createSpec()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_blobs := ent.NewBlobs(_c.mutation.blobOpeners.Document)
|
|
if r, ok := _c.mutation.Content(); ok {
|
|
_blobData, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: reading content: %w", err)
|
|
}
|
|
_blobs.Set(document.FieldContent, _blobData,
|
|
document.NewContentKey,
|
|
func(k string) {
|
|
_node.content_key = &k
|
|
_spec.SetField("content_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if r, ok := _c.mutation.Thumbnail(); ok {
|
|
_blobData, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: reading thumbnail: %w", err)
|
|
}
|
|
_blobs.Set(document.FieldThumbnail, _blobData,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_node.thumbnail_key = &k
|
|
_spec.SetField("thumbnail_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if value, ok := _c.mutation.Attachment(); ok {
|
|
_blobs.Set(document.FieldAttachment, value,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_node.attachment_key = &k
|
|
_spec.SetField("attachment_key", field.TypeString, k)
|
|
},
|
|
)
|
|
_node.Attachment = value
|
|
}
|
|
if value, ok := _c.mutation.Metadata(); ok {
|
|
_blobs.Set(document.FieldMetadata, value,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_node.metadata_key = &k
|
|
_spec.SetField("metadata_key", field.TypeString, k)
|
|
},
|
|
)
|
|
_node.Metadata = value
|
|
}
|
|
if value, ok := _c.mutation.Payload(); ok {
|
|
_blobDV, err := document.ValueScanner.Payload.Value(value)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: encoding payload: %w", err)
|
|
}
|
|
var _blobData []byte
|
|
switch v := _blobDV.(type) {
|
|
case []byte:
|
|
_blobData = v
|
|
case string:
|
|
_blobData = []byte(v)
|
|
default:
|
|
return nil, fmt.Errorf("ent: encoding payload: expected []byte or string, got %T", _blobDV)
|
|
}
|
|
_blobs.Set(document.FieldPayload, _blobData,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_node.payload_key = &k
|
|
_spec.SetField("payload_key", field.TypeString, k)
|
|
},
|
|
)
|
|
_node.Payload = value
|
|
}
|
|
if value, ok := _c.mutation.Description(); ok {
|
|
_blobs.Set(document.FieldDescription, []byte(value),
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_node.description_key = &k
|
|
_spec.SetField("description_key", field.TypeString, k)
|
|
},
|
|
)
|
|
_node.Description = value
|
|
}
|
|
if r, ok := _c.mutation.Archive(); ok {
|
|
_blobData, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: reading archive: %w", err)
|
|
}
|
|
_blobs.Set(document.FieldArchive, _blobData,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_node.archive_key = &k
|
|
_spec.SetField("archive_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
_blobCleanup, err := _blobs.Create(ctx, &sqlgraph.BlobSpec{
|
|
Driver: _c.driver,
|
|
Table: document.Table,
|
|
Columns: map[string]string{
|
|
document.FieldContent: "content_key",
|
|
document.FieldThumbnail: "thumbnail_key",
|
|
document.FieldAttachment: "attachment_key",
|
|
},
|
|
CheckRefs: map[string]bool{
|
|
document.FieldContent: true,
|
|
document.FieldThumbnail: true,
|
|
document.FieldAttachment: true,
|
|
},
|
|
})
|
|
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, errors.Join(err, _blobCleanup(ctx))
|
|
}
|
|
if txd, ok := _c.driver.(*txDriver); ok {
|
|
txd.mu.Lock()
|
|
txd.onRollback = append(txd.onRollback, func(next Rollbacker) Rollbacker {
|
|
return RollbackFunc(func(ctx context.Context, tx *Tx) error {
|
|
err := next.Rollback(ctx, tx)
|
|
return errors.Join(err, _blobCleanup(ctx))
|
|
})
|
|
})
|
|
txd.mu.Unlock()
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
_c.mutation.id = &_node.ID
|
|
_c.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (_c *DocumentCreate) createSpec() (*Document, *sqlgraph.CreateSpec, error) {
|
|
var (
|
|
_node = &Document{config: _c.config}
|
|
_spec = sqlgraph.NewCreateSpec(document.Table, sqlgraph.NewFieldSpec(document.FieldID, field.TypeInt))
|
|
)
|
|
_spec.OnConflict = _c.conflict
|
|
if value, ok := _c.mutation.Name(); ok {
|
|
_spec.SetField(document.FieldName, field.TypeString, value)
|
|
_node.Name = value
|
|
}
|
|
if value, ok := _c.mutation.Attachment(); ok {
|
|
_spec.SetField(document.FieldAttachment, field.TypeBytes, value)
|
|
_node.Attachment = value
|
|
}
|
|
if value, ok := _c.mutation.Payload(); ok {
|
|
vv, err := document.ValueScanner.Payload.Value(value)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
_spec.SetField(document.FieldPayload, field.TypeBytes, vv)
|
|
_node.Payload = value
|
|
}
|
|
return _node, _spec, nil
|
|
}
|
|
|
|
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
|
|
// of the `INSERT` statement. For example:
|
|
//
|
|
// client.Document.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.DocumentUpsert) {
|
|
// SetName(v+v).
|
|
// }).
|
|
// Exec(ctx)
|
|
func (_c *DocumentCreate) OnConflict(opts ...sql.ConflictOption) *DocumentUpsertOne {
|
|
_c.conflict = opts
|
|
return &DocumentUpsertOne{
|
|
create: _c,
|
|
}
|
|
}
|
|
|
|
// OnConflictColumns calls `OnConflict` and configures the columns
|
|
// as conflict target. Using this option is equivalent to using:
|
|
//
|
|
// client.Document.Create().
|
|
// OnConflict(sql.ConflictColumns(columns...)).
|
|
// Exec(ctx)
|
|
func (_c *DocumentCreate) OnConflictColumns(columns ...string) *DocumentUpsertOne {
|
|
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
|
|
return &DocumentUpsertOne{
|
|
create: _c,
|
|
}
|
|
}
|
|
|
|
type (
|
|
// DocumentUpsertOne is the builder for "upsert"-ing
|
|
// one Document node.
|
|
DocumentUpsertOne struct {
|
|
create *DocumentCreate
|
|
}
|
|
|
|
// DocumentUpsert is the "OnConflict" setter.
|
|
DocumentUpsert struct {
|
|
*sql.UpdateSet
|
|
}
|
|
)
|
|
|
|
// SetName sets the "name" field.
|
|
func (u *DocumentUpsert) SetName(v string) *DocumentUpsert {
|
|
u.Set(document.FieldName, v)
|
|
return u
|
|
}
|
|
|
|
// UpdateName sets the "name" field to the value that was provided on create.
|
|
func (u *DocumentUpsert) UpdateName() *DocumentUpsert {
|
|
u.SetExcluded(document.FieldName)
|
|
return u
|
|
}
|
|
|
|
// UpdateAttachment sets the "attachment" field to the value that was provided on create.
|
|
func (u *DocumentUpsert) UpdateAttachment() *DocumentUpsert {
|
|
u.SetExcluded(document.FieldAttachment)
|
|
u.SetExcluded("attachment_key")
|
|
return u
|
|
}
|
|
|
|
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
|
|
func (u *DocumentUpsert) UpdateMetadata() *DocumentUpsert {
|
|
u.SetExcluded("metadata_key")
|
|
return u
|
|
}
|
|
|
|
// ClearMetadata clears the value of the "metadata" field.
|
|
func (u *DocumentUpsert) ClearMetadata() *DocumentUpsert {
|
|
u.SetNull("metadata_key")
|
|
return u
|
|
}
|
|
|
|
// UpdatePayload sets the "payload" field to the value that was provided on create.
|
|
func (u *DocumentUpsert) UpdatePayload() *DocumentUpsert {
|
|
u.SetExcluded(document.FieldPayload)
|
|
u.SetExcluded("payload_key")
|
|
return u
|
|
}
|
|
|
|
// ClearPayload clears the value of the "payload" field.
|
|
func (u *DocumentUpsert) ClearPayload() *DocumentUpsert {
|
|
u.SetNull(document.FieldPayload)
|
|
u.SetNull("payload_key")
|
|
return u
|
|
}
|
|
|
|
// UpdateDescription sets the "description" field to the value that was provided on create.
|
|
func (u *DocumentUpsert) UpdateDescription() *DocumentUpsert {
|
|
u.SetExcluded("description_key")
|
|
return u
|
|
}
|
|
|
|
// ClearDescription clears the value of the "description" field.
|
|
func (u *DocumentUpsert) ClearDescription() *DocumentUpsert {
|
|
u.SetNull("description_key")
|
|
return u
|
|
}
|
|
|
|
// UpdateNewValues updates the mutable fields using the new values that were set on create.
|
|
// Using this option is equivalent to using:
|
|
//
|
|
// client.Document.Create().
|
|
// OnConflict(
|
|
// sql.ResolveWithNewValues(),
|
|
// ).
|
|
// Exec(ctx)
|
|
func (u *DocumentUpsertOne) UpdateNewValues() *DocumentUpsertOne {
|
|
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
|
|
return u
|
|
}
|
|
|
|
// Ignore sets each column to itself in case of conflict.
|
|
// Using this option is equivalent to using:
|
|
//
|
|
// client.Document.Create().
|
|
// OnConflict(sql.ResolveWithIgnore()).
|
|
// Exec(ctx)
|
|
func (u *DocumentUpsertOne) Ignore() *DocumentUpsertOne {
|
|
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 *DocumentUpsertOne) DoNothing() *DocumentUpsertOne {
|
|
u.create.conflict = append(u.create.conflict, sql.DoNothing())
|
|
return u
|
|
}
|
|
|
|
// Update allows overriding fields `UPDATE` values. See the DocumentCreate.OnConflict
|
|
// documentation for more info.
|
|
func (u *DocumentUpsertOne) Update(set func(*DocumentUpsert)) *DocumentUpsertOne {
|
|
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
|
|
set(&DocumentUpsert{UpdateSet: update})
|
|
}))
|
|
return u
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (u *DocumentUpsertOne) SetName(v string) *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.SetName(v)
|
|
})
|
|
}
|
|
|
|
// UpdateName sets the "name" field to the value that was provided on create.
|
|
func (u *DocumentUpsertOne) UpdateName() *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdateName()
|
|
})
|
|
}
|
|
|
|
// UpdateAttachment sets the "attachment" field to the value that was provided on create.
|
|
func (u *DocumentUpsertOne) UpdateAttachment() *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdateAttachment()
|
|
})
|
|
}
|
|
|
|
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
|
|
func (u *DocumentUpsertOne) UpdateMetadata() *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdateMetadata()
|
|
})
|
|
}
|
|
|
|
// ClearMetadata clears the value of the "metadata" field.
|
|
func (u *DocumentUpsertOne) ClearMetadata() *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.ClearMetadata()
|
|
})
|
|
}
|
|
|
|
// UpdatePayload sets the "payload" field to the value that was provided on create.
|
|
func (u *DocumentUpsertOne) UpdatePayload() *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdatePayload()
|
|
})
|
|
}
|
|
|
|
// ClearPayload clears the value of the "payload" field.
|
|
func (u *DocumentUpsertOne) ClearPayload() *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.ClearPayload()
|
|
})
|
|
}
|
|
|
|
// UpdateDescription sets the "description" field to the value that was provided on create.
|
|
func (u *DocumentUpsertOne) UpdateDescription() *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdateDescription()
|
|
})
|
|
}
|
|
|
|
// ClearDescription clears the value of the "description" field.
|
|
func (u *DocumentUpsertOne) ClearDescription() *DocumentUpsertOne {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.ClearDescription()
|
|
})
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (u *DocumentUpsertOne) Exec(ctx context.Context) error {
|
|
if len(u.create.conflict) == 0 {
|
|
return errors.New("ent: missing options for DocumentCreate.OnConflict")
|
|
}
|
|
return u.create.Exec(ctx)
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (u *DocumentUpsertOne) 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 *DocumentUpsertOne) ID(ctx context.Context) (id int, 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 *DocumentUpsertOne) IDX(ctx context.Context) int {
|
|
id, err := u.ID(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// DocumentCreateBulk is the builder for creating many Document entities in bulk.
|
|
type DocumentCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*DocumentCreate
|
|
conflict []sql.ConflictOption
|
|
}
|
|
|
|
// Save creates the Document entities in the database.
|
|
func (_c *DocumentCreateBulk) Save(ctx context.Context) ([]*Document, error) {
|
|
if _c.err != nil {
|
|
return nil, _c.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
|
|
nodes := make([]*Document, len(_c.builders))
|
|
mutators := make([]Mutator, len(_c.builders))
|
|
_blobs := ent.NewBlobs(_c.blobOpeners.Document)
|
|
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.(*DocumentMutation)
|
|
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 r, ok := mutation.Content(); ok {
|
|
_blobData, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: reading content: %w", err)
|
|
}
|
|
_blobs.Set(document.FieldContent, _blobData,
|
|
document.NewContentKey,
|
|
func(k string) {
|
|
nodes[i].content_key = &k
|
|
specs[i].SetField("content_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if r, ok := mutation.Thumbnail(); ok {
|
|
_blobData, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: reading thumbnail: %w", err)
|
|
}
|
|
_blobs.Set(document.FieldThumbnail, _blobData,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
nodes[i].thumbnail_key = &k
|
|
specs[i].SetField("thumbnail_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if value, ok := mutation.Attachment(); ok {
|
|
_blobs.Set(document.FieldAttachment, value,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
nodes[i].attachment_key = &k
|
|
specs[i].SetField("attachment_key", field.TypeString, k)
|
|
},
|
|
)
|
|
nodes[i].Attachment = value
|
|
}
|
|
if value, ok := mutation.Metadata(); ok {
|
|
_blobs.Set(document.FieldMetadata, value,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
nodes[i].metadata_key = &k
|
|
specs[i].SetField("metadata_key", field.TypeString, k)
|
|
},
|
|
)
|
|
nodes[i].Metadata = value
|
|
}
|
|
if value, ok := mutation.Payload(); ok {
|
|
_blobDV, err := document.ValueScanner.Payload.Value(value)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: encoding payload: %w", err)
|
|
}
|
|
var _blobData []byte
|
|
switch v := _blobDV.(type) {
|
|
case []byte:
|
|
_blobData = v
|
|
case string:
|
|
_blobData = []byte(v)
|
|
default:
|
|
return nil, fmt.Errorf("ent: encoding payload: expected []byte or string, got %T", _blobDV)
|
|
}
|
|
_blobs.Set(document.FieldPayload, _blobData,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
nodes[i].payload_key = &k
|
|
specs[i].SetField("payload_key", field.TypeString, k)
|
|
},
|
|
)
|
|
nodes[i].Payload = value
|
|
}
|
|
if value, ok := mutation.Description(); ok {
|
|
_blobs.Set(document.FieldDescription, []byte(value),
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
nodes[i].description_key = &k
|
|
specs[i].SetField("description_key", field.TypeString, k)
|
|
},
|
|
)
|
|
nodes[i].Description = value
|
|
}
|
|
if r, ok := mutation.Archive(); ok {
|
|
_blobData, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: reading archive: %w", err)
|
|
}
|
|
_blobs.Set(document.FieldArchive, _blobData,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
nodes[i].archive_key = &k
|
|
specs[i].SetField("archive_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if i < len(mutators)-1 {
|
|
_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
|
|
} else {
|
|
// Write blobs before creating SQL rows so insert failures can clean up written objects.
|
|
_blobCleanup, err := _blobs.Create(ctx, &sqlgraph.BlobSpec{
|
|
Driver: _c.driver,
|
|
Table: document.Table,
|
|
Columns: map[string]string{
|
|
document.FieldContent: "content_key",
|
|
document.FieldThumbnail: "thumbnail_key",
|
|
document.FieldAttachment: "attachment_key",
|
|
},
|
|
CheckRefs: map[string]bool{
|
|
document.FieldContent: true,
|
|
document.FieldThumbnail: true,
|
|
document.FieldAttachment: true,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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}
|
|
}
|
|
return nil, errors.Join(err, _blobCleanup(ctx))
|
|
}
|
|
if txd, ok := _c.driver.(*txDriver); ok {
|
|
txd.mu.Lock()
|
|
txd.onRollback = append(txd.onRollback, func(next Rollbacker) Rollbacker {
|
|
return RollbackFunc(func(ctx context.Context, tx *Tx) error {
|
|
err := next.Rollback(ctx, tx)
|
|
return errors.Join(err, _blobCleanup(ctx))
|
|
})
|
|
})
|
|
txd.mu.Unlock()
|
|
}
|
|
}
|
|
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, _c.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_c *DocumentCreateBulk) SaveX(ctx context.Context) []*Document {
|
|
v, err := _c.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_c *DocumentCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := _c.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_c *DocumentCreateBulk) 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.Document.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.DocumentUpsert) {
|
|
// SetName(v+v).
|
|
// }).
|
|
// Exec(ctx)
|
|
func (_c *DocumentCreateBulk) OnConflict(opts ...sql.ConflictOption) *DocumentUpsertBulk {
|
|
_c.conflict = opts
|
|
return &DocumentUpsertBulk{
|
|
create: _c,
|
|
}
|
|
}
|
|
|
|
// OnConflictColumns calls `OnConflict` and configures the columns
|
|
// as conflict target. Using this option is equivalent to using:
|
|
//
|
|
// client.Document.Create().
|
|
// OnConflict(sql.ConflictColumns(columns...)).
|
|
// Exec(ctx)
|
|
func (_c *DocumentCreateBulk) OnConflictColumns(columns ...string) *DocumentUpsertBulk {
|
|
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
|
|
return &DocumentUpsertBulk{
|
|
create: _c,
|
|
}
|
|
}
|
|
|
|
// DocumentUpsertBulk is the builder for "upsert"-ing
|
|
// a bulk of Document nodes.
|
|
type DocumentUpsertBulk struct {
|
|
create *DocumentCreateBulk
|
|
}
|
|
|
|
// UpdateNewValues updates the mutable fields using the new values that
|
|
// were set on create. Using this option is equivalent to using:
|
|
//
|
|
// client.Document.Create().
|
|
// OnConflict(
|
|
// sql.ResolveWithNewValues(),
|
|
// ).
|
|
// Exec(ctx)
|
|
func (u *DocumentUpsertBulk) UpdateNewValues() *DocumentUpsertBulk {
|
|
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
|
|
return u
|
|
}
|
|
|
|
// Ignore sets each column to itself in case of conflict.
|
|
// Using this option is equivalent to using:
|
|
//
|
|
// client.Document.Create().
|
|
// OnConflict(sql.ResolveWithIgnore()).
|
|
// Exec(ctx)
|
|
func (u *DocumentUpsertBulk) Ignore() *DocumentUpsertBulk {
|
|
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 *DocumentUpsertBulk) DoNothing() *DocumentUpsertBulk {
|
|
u.create.conflict = append(u.create.conflict, sql.DoNothing())
|
|
return u
|
|
}
|
|
|
|
// Update allows overriding fields `UPDATE` values. See the DocumentCreateBulk.OnConflict
|
|
// documentation for more info.
|
|
func (u *DocumentUpsertBulk) Update(set func(*DocumentUpsert)) *DocumentUpsertBulk {
|
|
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
|
|
set(&DocumentUpsert{UpdateSet: update})
|
|
}))
|
|
return u
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (u *DocumentUpsertBulk) SetName(v string) *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.SetName(v)
|
|
})
|
|
}
|
|
|
|
// UpdateName sets the "name" field to the value that was provided on create.
|
|
func (u *DocumentUpsertBulk) UpdateName() *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdateName()
|
|
})
|
|
}
|
|
|
|
// UpdateAttachment sets the "attachment" field to the value that was provided on create.
|
|
func (u *DocumentUpsertBulk) UpdateAttachment() *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdateAttachment()
|
|
})
|
|
}
|
|
|
|
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
|
|
func (u *DocumentUpsertBulk) UpdateMetadata() *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdateMetadata()
|
|
})
|
|
}
|
|
|
|
// ClearMetadata clears the value of the "metadata" field.
|
|
func (u *DocumentUpsertBulk) ClearMetadata() *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.ClearMetadata()
|
|
})
|
|
}
|
|
|
|
// UpdatePayload sets the "payload" field to the value that was provided on create.
|
|
func (u *DocumentUpsertBulk) UpdatePayload() *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdatePayload()
|
|
})
|
|
}
|
|
|
|
// ClearPayload clears the value of the "payload" field.
|
|
func (u *DocumentUpsertBulk) ClearPayload() *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.ClearPayload()
|
|
})
|
|
}
|
|
|
|
// UpdateDescription sets the "description" field to the value that was provided on create.
|
|
func (u *DocumentUpsertBulk) UpdateDescription() *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.UpdateDescription()
|
|
})
|
|
}
|
|
|
|
// ClearDescription clears the value of the "description" field.
|
|
func (u *DocumentUpsertBulk) ClearDescription() *DocumentUpsertBulk {
|
|
return u.Update(func(s *DocumentUpsert) {
|
|
s.ClearDescription()
|
|
})
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (u *DocumentUpsertBulk) 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 DocumentCreateBulk instead", i)
|
|
}
|
|
}
|
|
if len(u.create.conflict) == 0 {
|
|
return errors.New("ent: missing options for DocumentCreateBulk.OnConflict")
|
|
}
|
|
return u.create.Exec(ctx)
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (u *DocumentUpsertBulk) ExecX(ctx context.Context) {
|
|
if err := u.create.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|