Files
ent/entc/integration/edgeschema/ent/attachedfile_update.go

473 lines
14 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"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/entc/integration/edgeschema/ent/attachedfile"
"entgo.io/ent/entc/integration/edgeschema/ent/file"
"entgo.io/ent/entc/integration/edgeschema/ent/predicate"
"entgo.io/ent/entc/integration/edgeschema/ent/process"
"entgo.io/ent/schema/field"
)
// AttachedFileUpdate is the builder for updating AttachedFile entities.
type AttachedFileUpdate struct {
config
hooks []Hook
mutation *AttachedFileMutation
}
// Where appends a list predicates to the AttachedFileUpdate builder.
func (_u *AttachedFileUpdate) Where(ps ...predicate.AttachedFile) *AttachedFileUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetAttachTime sets the "attach_time" field.
func (_u *AttachedFileUpdate) SetAttachTime(v time.Time) *AttachedFileUpdate {
_u.mutation.SetAttachTime(v)
return _u
}
// SetNillableAttachTime sets the "attach_time" field if the given value is not nil.
func (_u *AttachedFileUpdate) SetNillableAttachTime(v *time.Time) *AttachedFileUpdate {
if v != nil {
_u.SetAttachTime(*v)
}
return _u
}
// SetFID sets the "f_id" field.
func (_u *AttachedFileUpdate) SetFID(v int) *AttachedFileUpdate {
_u.mutation.SetFID(v)
return _u
}
// SetNillableFID sets the "f_id" field if the given value is not nil.
func (_u *AttachedFileUpdate) SetNillableFID(v *int) *AttachedFileUpdate {
if v != nil {
_u.SetFID(*v)
}
return _u
}
// SetProcID sets the "proc_id" field.
func (_u *AttachedFileUpdate) SetProcID(v int) *AttachedFileUpdate {
_u.mutation.SetProcID(v)
return _u
}
// SetNillableProcID sets the "proc_id" field if the given value is not nil.
func (_u *AttachedFileUpdate) SetNillableProcID(v *int) *AttachedFileUpdate {
if v != nil {
_u.SetProcID(*v)
}
return _u
}
// SetFiID sets the "fi" edge to the File entity by ID.
func (_u *AttachedFileUpdate) SetFiID(id int) *AttachedFileUpdate {
_u.mutation.SetFiID(id)
return _u
}
// SetFi sets the "fi" edge to the File entity.
func (_u *AttachedFileUpdate) SetFi(v *File) *AttachedFileUpdate {
return _u.SetFiID(v.ID)
}
// SetProc sets the "proc" edge to the Process entity.
func (_u *AttachedFileUpdate) SetProc(v *Process) *AttachedFileUpdate {
return _u.SetProcID(v.ID)
}
// Mutation returns the AttachedFileMutation object of the builder.
func (_u *AttachedFileUpdate) Mutation() *AttachedFileMutation {
return _u.mutation
}
// ClearFi clears the "fi" edge to the File entity.
func (_u *AttachedFileUpdate) ClearFi() *AttachedFileUpdate {
_u.mutation.ClearFi()
return _u
}
// ClearProc clears the "proc" edge to the Process entity.
func (_u *AttachedFileUpdate) ClearProc() *AttachedFileUpdate {
_u.mutation.ClearProc()
return _u
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *AttachedFileUpdate) 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 *AttachedFileUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *AttachedFileUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *AttachedFileUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *AttachedFileUpdate) check() error {
if _u.mutation.FiCleared() && len(_u.mutation.FiIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "AttachedFile.fi"`)
}
if _u.mutation.ProcCleared() && len(_u.mutation.ProcIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "AttachedFile.proc"`)
}
return nil
}
func (_u *AttachedFileUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(attachedfile.Table, attachedfile.Columns, sqlgraph.NewFieldSpec(attachedfile.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.AttachTime(); ok {
_spec.SetField(attachedfile.FieldAttachTime, field.TypeTime, value)
}
if _u.mutation.FiCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: attachedfile.FiTable,
Columns: []string{attachedfile.FiColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.FiIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: attachedfile.FiTable,
Columns: []string{attachedfile.FiColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.ProcCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: attachedfile.ProcTable,
Columns: []string{attachedfile.ProcColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(process.FieldID, field.TypeInt),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.ProcIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: attachedfile.ProcTable,
Columns: []string{attachedfile.ProcColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(process.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{attachedfile.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// AttachedFileUpdateOne is the builder for updating a single AttachedFile entity.
type AttachedFileUpdateOne struct {
config
fields []string
hooks []Hook
mutation *AttachedFileMutation
}
// SetAttachTime sets the "attach_time" field.
func (_u *AttachedFileUpdateOne) SetAttachTime(v time.Time) *AttachedFileUpdateOne {
_u.mutation.SetAttachTime(v)
return _u
}
// SetNillableAttachTime sets the "attach_time" field if the given value is not nil.
func (_u *AttachedFileUpdateOne) SetNillableAttachTime(v *time.Time) *AttachedFileUpdateOne {
if v != nil {
_u.SetAttachTime(*v)
}
return _u
}
// SetFID sets the "f_id" field.
func (_u *AttachedFileUpdateOne) SetFID(v int) *AttachedFileUpdateOne {
_u.mutation.SetFID(v)
return _u
}
// SetNillableFID sets the "f_id" field if the given value is not nil.
func (_u *AttachedFileUpdateOne) SetNillableFID(v *int) *AttachedFileUpdateOne {
if v != nil {
_u.SetFID(*v)
}
return _u
}
// SetProcID sets the "proc_id" field.
func (_u *AttachedFileUpdateOne) SetProcID(v int) *AttachedFileUpdateOne {
_u.mutation.SetProcID(v)
return _u
}
// SetNillableProcID sets the "proc_id" field if the given value is not nil.
func (_u *AttachedFileUpdateOne) SetNillableProcID(v *int) *AttachedFileUpdateOne {
if v != nil {
_u.SetProcID(*v)
}
return _u
}
// SetFiID sets the "fi" edge to the File entity by ID.
func (_u *AttachedFileUpdateOne) SetFiID(id int) *AttachedFileUpdateOne {
_u.mutation.SetFiID(id)
return _u
}
// SetFi sets the "fi" edge to the File entity.
func (_u *AttachedFileUpdateOne) SetFi(v *File) *AttachedFileUpdateOne {
return _u.SetFiID(v.ID)
}
// SetProc sets the "proc" edge to the Process entity.
func (_u *AttachedFileUpdateOne) SetProc(v *Process) *AttachedFileUpdateOne {
return _u.SetProcID(v.ID)
}
// Mutation returns the AttachedFileMutation object of the builder.
func (_u *AttachedFileUpdateOne) Mutation() *AttachedFileMutation {
return _u.mutation
}
// ClearFi clears the "fi" edge to the File entity.
func (_u *AttachedFileUpdateOne) ClearFi() *AttachedFileUpdateOne {
_u.mutation.ClearFi()
return _u
}
// ClearProc clears the "proc" edge to the Process entity.
func (_u *AttachedFileUpdateOne) ClearProc() *AttachedFileUpdateOne {
_u.mutation.ClearProc()
return _u
}
// Where appends a list predicates to the AttachedFileUpdate builder.
func (_u *AttachedFileUpdateOne) Where(ps ...predicate.AttachedFile) *AttachedFileUpdateOne {
_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 *AttachedFileUpdateOne) Select(field string, fields ...string) *AttachedFileUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated AttachedFile entity.
func (_u *AttachedFileUpdateOne) Save(ctx context.Context) (*AttachedFile, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *AttachedFileUpdateOne) SaveX(ctx context.Context) *AttachedFile {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *AttachedFileUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *AttachedFileUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *AttachedFileUpdateOne) check() error {
if _u.mutation.FiCleared() && len(_u.mutation.FiIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "AttachedFile.fi"`)
}
if _u.mutation.ProcCleared() && len(_u.mutation.ProcIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "AttachedFile.proc"`)
}
return nil
}
func (_u *AttachedFileUpdateOne) sqlSave(ctx context.Context) (_node *AttachedFile, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(attachedfile.Table, attachedfile.Columns, sqlgraph.NewFieldSpec(attachedfile.FieldID, field.TypeInt))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AttachedFile.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, attachedfile.FieldID)
for _, f := range fields {
if !attachedfile.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != attachedfile.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.AttachTime(); ok {
_spec.SetField(attachedfile.FieldAttachTime, field.TypeTime, value)
}
if _u.mutation.FiCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: attachedfile.FiTable,
Columns: []string{attachedfile.FiColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.FiIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: attachedfile.FiTable,
Columns: []string{attachedfile.FiColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.ProcCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: attachedfile.ProcTable,
Columns: []string{attachedfile.ProcColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(process.FieldID, field.TypeInt),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.ProcIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: attachedfile.ProcTable,
Columns: []string{attachedfile.ProcColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(process.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &AttachedFile{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{attachedfile.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}