529 lines
15 KiB
Go
529 lines
15 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/predicate"
|
|
"entgo.io/ent/entc/integration/ent/schema"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// DocumentUpdate is the builder for updating Document entities.
|
|
type DocumentUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *DocumentMutation
|
|
modifiers []func(*sql.UpdateBuilder)
|
|
}
|
|
|
|
// Where appends a list predicates to the DocumentUpdate builder.
|
|
func (_u *DocumentUpdate) Where(ps ...predicate.Document) *DocumentUpdate {
|
|
_u.mutation.Where(ps...)
|
|
return _u
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (_u *DocumentUpdate) SetName(v string) *DocumentUpdate {
|
|
_u.mutation.SetName(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableName sets the "name" field if the given value is not nil.
|
|
func (_u *DocumentUpdate) SetNillableName(v *string) *DocumentUpdate {
|
|
if v != nil {
|
|
_u.SetName(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// Mutation returns the DocumentMutation object of the builder.
|
|
func (_u *DocumentUpdate) Mutation() *DocumentMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (_u *DocumentUpdate) Save(ctx context.Context) (int, error) {
|
|
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_u *DocumentUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_u *DocumentUpdate) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *DocumentUpdate) ExecX(ctx context.Context) {
|
|
if err := _u.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
|
|
func (_u *DocumentUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *DocumentUpdate {
|
|
_u.modifiers = append(_u.modifiers, modifiers...)
|
|
return _u
|
|
}
|
|
|
|
func (_u *DocumentUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
|
_spec := sqlgraph.NewUpdateSpec(document.Table, document.Columns, sqlgraph.NewFieldSpec(document.FieldID, field.TypeInt))
|
|
if ps := _u.mutation.Predicates(); len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := _u.mutation.Name(); ok {
|
|
_spec.SetField(document.FieldName, field.TypeString, value)
|
|
}
|
|
if value, ok := _u.mutation.Attachment(); ok {
|
|
_spec.SetField(document.FieldAttachment, field.TypeBytes, value)
|
|
}
|
|
if value, ok := _u.mutation.Payload(); ok {
|
|
vv, err := document.ValueScanner.Payload.Value(value)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
_spec.SetField(document.FieldPayload, field.TypeBytes, vv)
|
|
}
|
|
if _u.mutation.PayloadCleared() {
|
|
_spec.ClearField(document.FieldPayload, field.TypeBytes)
|
|
}
|
|
_spec.AddModifiers(_u.modifiers...)
|
|
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{document.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return 0, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
// DocumentUpdateOne is the builder for updating a single Document entity.
|
|
type DocumentUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *DocumentMutation
|
|
modifiers []func(*sql.UpdateBuilder)
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (_u *DocumentUpdateOne) SetName(v string) *DocumentUpdateOne {
|
|
_u.mutation.SetName(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableName sets the "name" field if the given value is not nil.
|
|
func (_u *DocumentUpdateOne) SetNillableName(v *string) *DocumentUpdateOne {
|
|
if v != nil {
|
|
_u.SetName(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetAttachment sets the "attachment" field.
|
|
func (_u *DocumentUpdateOne) SetAttachment(v []byte) *DocumentUpdateOne {
|
|
_u.mutation.SetAttachment(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableAttachment sets the "attachment" field if the given value is not nil.
|
|
func (_u *DocumentUpdateOne) SetNillableAttachment(v *[]byte) *DocumentUpdateOne {
|
|
if v != nil {
|
|
_u.SetAttachment(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetMetadata sets the "metadata" field.
|
|
func (_u *DocumentUpdateOne) SetMetadata(v []byte) *DocumentUpdateOne {
|
|
_u.mutation.SetMetadata(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableMetadata sets the "metadata" field if the given value is not nil.
|
|
func (_u *DocumentUpdateOne) SetNillableMetadata(v *[]byte) *DocumentUpdateOne {
|
|
if v != nil {
|
|
_u.SetMetadata(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// ClearMetadata clears the value of the "metadata" field.
|
|
func (_u *DocumentUpdateOne) ClearMetadata() *DocumentUpdateOne {
|
|
_u.mutation.ClearMetadata()
|
|
return _u
|
|
}
|
|
|
|
// SetPayload sets the "payload" field.
|
|
func (_u *DocumentUpdateOne) SetPayload(v *schema.DocPayload) *DocumentUpdateOne {
|
|
_u.mutation.SetPayload(v)
|
|
return _u
|
|
}
|
|
|
|
// ClearPayload clears the value of the "payload" field.
|
|
func (_u *DocumentUpdateOne) ClearPayload() *DocumentUpdateOne {
|
|
_u.mutation.ClearPayload()
|
|
return _u
|
|
}
|
|
|
|
// SetDescription sets the "description" field.
|
|
func (_u *DocumentUpdateOne) SetDescription(v string) *DocumentUpdateOne {
|
|
_u.mutation.SetDescription(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
|
func (_u *DocumentUpdateOne) SetNillableDescription(v *string) *DocumentUpdateOne {
|
|
if v != nil {
|
|
_u.SetDescription(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// ClearDescription clears the value of the "description" field.
|
|
func (_u *DocumentUpdateOne) ClearDescription() *DocumentUpdateOne {
|
|
_u.mutation.ClearDescription()
|
|
return _u
|
|
}
|
|
|
|
// SetContent sets the "content" field.
|
|
func (_u *DocumentUpdateOne) SetContent(v io.Reader) *DocumentUpdateOne {
|
|
_u.mutation.SetContent(v)
|
|
return _u
|
|
}
|
|
|
|
// SetThumbnail sets the "thumbnail" field.
|
|
func (_u *DocumentUpdateOne) SetThumbnail(v io.Reader) *DocumentUpdateOne {
|
|
_u.mutation.SetThumbnail(v)
|
|
return _u
|
|
}
|
|
|
|
// SetArchive sets the "archive" field.
|
|
func (_u *DocumentUpdateOne) SetArchive(v io.Reader) *DocumentUpdateOne {
|
|
_u.mutation.SetArchive(v)
|
|
return _u
|
|
}
|
|
|
|
// ClearArchive clears the value of the "archive" field.
|
|
func (_u *DocumentUpdateOne) ClearArchive() *DocumentUpdateOne {
|
|
_u.mutation.ClearArchive()
|
|
return _u
|
|
}
|
|
|
|
// Mutation returns the DocumentMutation object of the builder.
|
|
func (_u *DocumentUpdateOne) Mutation() *DocumentMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// Where appends a list predicates to the DocumentUpdate builder.
|
|
func (_u *DocumentUpdateOne) Where(ps ...predicate.Document) *DocumentUpdateOne {
|
|
_u.mutation.Where(ps...)
|
|
return _u
|
|
}
|
|
|
|
// Select allows selecting one or more fields (columns) of the returned entity.
|
|
// The default is selecting all fields defined in the entity schema.
|
|
func (_u *DocumentUpdateOne) Select(field string, fields ...string) *DocumentUpdateOne {
|
|
_u.fields = append([]string{field}, fields...)
|
|
return _u
|
|
}
|
|
|
|
// Save executes the query and returns the updated Document entity.
|
|
func (_u *DocumentUpdateOne) Save(ctx context.Context) (*Document, error) {
|
|
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_u *DocumentUpdateOne) SaveX(ctx context.Context) *Document {
|
|
node, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (_u *DocumentUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *DocumentUpdateOne) ExecX(ctx context.Context) {
|
|
if err := _u.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
|
|
func (_u *DocumentUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *DocumentUpdateOne {
|
|
_u.modifiers = append(_u.modifiers, modifiers...)
|
|
return _u
|
|
}
|
|
|
|
func (_u *DocumentUpdateOne) sqlSave(ctx context.Context) (_node *Document, err error) {
|
|
_spec := sqlgraph.NewUpdateSpec(document.Table, document.Columns, sqlgraph.NewFieldSpec(document.FieldID, field.TypeInt))
|
|
id, ok := _u.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Document.id" for update`)}
|
|
}
|
|
_spec.Node.ID.Value = id
|
|
if fields := _u.fields; len(fields) > 0 {
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
_spec.Node.Columns = append(_spec.Node.Columns, document.FieldID)
|
|
for _, f := range fields {
|
|
if !document.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
if f != document.FieldID {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, f)
|
|
}
|
|
}
|
|
}
|
|
if ps := _u.mutation.Predicates(); len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := _u.mutation.Name(); ok {
|
|
_spec.SetField(document.FieldName, field.TypeString, value)
|
|
}
|
|
if value, ok := _u.mutation.Attachment(); ok {
|
|
_spec.SetField(document.FieldAttachment, field.TypeBytes, value)
|
|
}
|
|
if value, ok := _u.mutation.Payload(); ok {
|
|
vv, err := document.ValueScanner.Payload.Value(value)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_spec.SetField(document.FieldPayload, field.TypeBytes, vv)
|
|
}
|
|
if _u.mutation.PayloadCleared() {
|
|
_spec.ClearField(document.FieldPayload, field.TypeBytes)
|
|
}
|
|
_spec.AddModifiers(_u.modifiers...)
|
|
_blobs := ent.NewBlobs(_u.mutation.blobOpeners.Document)
|
|
if r, ok := _u.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) {
|
|
_spec.SetField("content_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if r, ok := _u.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) {
|
|
_spec.SetField("thumbnail_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if value, ok := _u.mutation.Attachment(); ok {
|
|
_blobs.Set(document.FieldAttachment, value,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_spec.SetField("attachment_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if value, ok := _u.mutation.Metadata(); ok {
|
|
_blobs.Set(document.FieldMetadata, value,
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_spec.SetField("metadata_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if _u.mutation.MetadataCleared() {
|
|
_blobs.SetCleared(document.FieldMetadata, func() {
|
|
_spec.ClearField("metadata_key", field.TypeString)
|
|
})
|
|
}
|
|
if value, ok := _u.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) {
|
|
_spec.SetField("payload_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if _u.mutation.PayloadCleared() {
|
|
_blobs.SetCleared(document.FieldPayload, func() {
|
|
_spec.ClearField("payload_key", field.TypeString)
|
|
})
|
|
}
|
|
if value, ok := _u.mutation.Description(); ok {
|
|
_blobs.Set(document.FieldDescription, []byte(value),
|
|
defaultBlobKey,
|
|
func(k string) {
|
|
_spec.SetField("description_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if _u.mutation.DescriptionCleared() {
|
|
_blobs.SetCleared(document.FieldDescription, func() {
|
|
_spec.ClearField("description_key", field.TypeString)
|
|
})
|
|
}
|
|
if r, ok := _u.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) {
|
|
_spec.SetField("archive_key", field.TypeString, k)
|
|
},
|
|
)
|
|
}
|
|
if _u.mutation.ArchiveCleared() {
|
|
_blobs.SetCleared(document.FieldArchive, func() {
|
|
_spec.ClearField("archive_key", field.TypeString)
|
|
})
|
|
}
|
|
_blobResult, err := _blobs.Update(ctx, &sqlgraph.BlobSpec{
|
|
Driver: _u.driver,
|
|
Table: document.Table,
|
|
Columns: map[string]string{
|
|
document.FieldContent: "content_key",
|
|
document.FieldThumbnail: "thumbnail_key",
|
|
document.FieldAttachment: "attachment_key",
|
|
document.FieldMetadata: "metadata_key",
|
|
document.FieldPayload: "payload_key",
|
|
document.FieldDescription: "description_key",
|
|
document.FieldArchive: "archive_key",
|
|
},
|
|
CheckRefs: map[string]bool{
|
|
document.FieldContent: true,
|
|
document.FieldThumbnail: true,
|
|
document.FieldAttachment: true,
|
|
},
|
|
Predicate: func(s *sql.Selector) {
|
|
s.Where(sql.EQ(document.FieldID, _spec.Node.ID.Value))
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_node = &Document{config: _u.config}
|
|
_spec.Assign = _node.assignValues
|
|
_spec.ScanValues = _node.scanValues
|
|
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{document.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, errors.Join(err, _blobResult.Rollback(ctx))
|
|
}
|
|
_u.mutation.done = true
|
|
// Resolve which replaced blobs are unreferenced now that the row moved to its new
|
|
// keys, while the transaction is still open, and remove them once it commits.
|
|
_blobCleanup, err := _blobResult.Cleanup(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if txd, ok := _u.driver.(*txDriver); ok {
|
|
txd.mu.Lock()
|
|
txd.onCommit = append(txd.onCommit, func(next Committer) Committer {
|
|
return CommitFunc(func(ctx context.Context, tx *Tx) error {
|
|
if err := next.Commit(ctx, tx); err != nil {
|
|
return err
|
|
}
|
|
return _blobCleanup(ctx)
|
|
})
|
|
})
|
|
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, _blobResult.Rollback(ctx))
|
|
})
|
|
})
|
|
txd.mu.Unlock()
|
|
} else if err := _blobCleanup(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
_blobReader := ent.NewBlobStore(_u.mutation.blobOpeners.Document)
|
|
if value, ok := _u.mutation.Attachment(); ok {
|
|
_node.Attachment = value
|
|
}
|
|
if value, ok := _u.mutation.Metadata(); ok {
|
|
_node.Metadata = value
|
|
} else if _node.metadata_key != nil && *_node.metadata_key != "" {
|
|
data, err := _blobReader.Read(ctx, document.FieldMetadata, *_node.metadata_key)
|
|
if err != nil {
|
|
return nil, errors.Join(fmt.Errorf("loading metadata after update: %w", err), _blobReader.Close())
|
|
}
|
|
_node.Metadata = data
|
|
}
|
|
if value, ok := _u.mutation.Payload(); ok {
|
|
_node.Payload = value
|
|
}
|
|
if value, ok := _u.mutation.Description(); ok {
|
|
_node.Description = value
|
|
} else if _node.description_key != nil && *_node.description_key != "" {
|
|
data, err := _blobReader.Read(ctx, document.FieldDescription, *_node.description_key)
|
|
if err != nil {
|
|
return nil, errors.Join(fmt.Errorf("loading description after update: %w", err), _blobReader.Close())
|
|
}
|
|
_node.Description = string(data)
|
|
}
|
|
if err := _blobReader.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
return _node, nil
|
|
}
|