mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
340 lines
8.8 KiB
Go
340 lines
8.8 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 entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"entgo.io/ent/dialect/sql"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/entc/integration/ent/predicate"
|
|
"entgo.io/ent/entc/integration/ent/schema"
|
|
"entgo.io/ent/entc/integration/ent/task"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// TaskUpdate is the builder for updating Task entities.
|
|
type TaskUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *TaskMutation
|
|
}
|
|
|
|
// Where adds a new predicate for the TaskUpdate builder.
|
|
func (tu *TaskUpdate) Where(ps ...predicate.Task) *TaskUpdate {
|
|
tu.mutation.predicates = append(tu.mutation.predicates, ps...)
|
|
return tu
|
|
}
|
|
|
|
// SetPriority sets the "priority" field.
|
|
func (tu *TaskUpdate) SetPriority(s schema.Priority) *TaskUpdate {
|
|
tu.mutation.ResetPriority()
|
|
tu.mutation.SetPriority(s)
|
|
return tu
|
|
}
|
|
|
|
// SetNillablePriority sets the "priority" field if the given value is not nil.
|
|
func (tu *TaskUpdate) SetNillablePriority(s *schema.Priority) *TaskUpdate {
|
|
if s != nil {
|
|
tu.SetPriority(*s)
|
|
}
|
|
return tu
|
|
}
|
|
|
|
// AddPriority adds s to the "priority" field.
|
|
func (tu *TaskUpdate) AddPriority(s schema.Priority) *TaskUpdate {
|
|
tu.mutation.AddPriority(s)
|
|
return tu
|
|
}
|
|
|
|
// Mutation returns the TaskMutation object of the builder.
|
|
func (tu *TaskUpdate) Mutation() *TaskMutation {
|
|
return tu.mutation
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (tu *TaskUpdate) Save(ctx context.Context) (int, error) {
|
|
var (
|
|
err error
|
|
affected int
|
|
)
|
|
if len(tu.hooks) == 0 {
|
|
if err = tu.check(); err != nil {
|
|
return 0, err
|
|
}
|
|
affected, err = tu.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*TaskMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err = tu.check(); err != nil {
|
|
return 0, err
|
|
}
|
|
tu.mutation = mutation
|
|
affected, err = tu.sqlSave(ctx)
|
|
mutation.done = true
|
|
return affected, err
|
|
})
|
|
for i := len(tu.hooks) - 1; i >= 0; i-- {
|
|
mut = tu.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, tu.mutation); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return affected, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (tu *TaskUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := tu.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (tu *TaskUpdate) Exec(ctx context.Context) error {
|
|
_, err := tu.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (tu *TaskUpdate) ExecX(ctx context.Context) {
|
|
if err := tu.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (tu *TaskUpdate) check() error {
|
|
if v, ok := tu.mutation.Priority(); ok {
|
|
if err := task.PriorityValidator(int(v)); err != nil {
|
|
return &ValidationError{Name: "priority", err: fmt.Errorf("ent: validator failed for field \"priority\": %w", err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|
_spec := &sqlgraph.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: task.Table,
|
|
Columns: task.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: task.FieldID,
|
|
},
|
|
},
|
|
}
|
|
if ps := tu.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := tu.mutation.Priority(); ok {
|
|
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: task.FieldPriority,
|
|
})
|
|
}
|
|
if value, ok := tu.mutation.AddedPriority(); ok {
|
|
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: task.FieldPriority,
|
|
})
|
|
}
|
|
if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{task.Label}
|
|
} else if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
return 0, err
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// TaskUpdateOne is the builder for updating a single Task entity.
|
|
type TaskUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *TaskMutation
|
|
}
|
|
|
|
// SetPriority sets the "priority" field.
|
|
func (tuo *TaskUpdateOne) SetPriority(s schema.Priority) *TaskUpdateOne {
|
|
tuo.mutation.ResetPriority()
|
|
tuo.mutation.SetPriority(s)
|
|
return tuo
|
|
}
|
|
|
|
// SetNillablePriority sets the "priority" field if the given value is not nil.
|
|
func (tuo *TaskUpdateOne) SetNillablePriority(s *schema.Priority) *TaskUpdateOne {
|
|
if s != nil {
|
|
tuo.SetPriority(*s)
|
|
}
|
|
return tuo
|
|
}
|
|
|
|
// AddPriority adds s to the "priority" field.
|
|
func (tuo *TaskUpdateOne) AddPriority(s schema.Priority) *TaskUpdateOne {
|
|
tuo.mutation.AddPriority(s)
|
|
return tuo
|
|
}
|
|
|
|
// Mutation returns the TaskMutation object of the builder.
|
|
func (tuo *TaskUpdateOne) Mutation() *TaskMutation {
|
|
return tuo.mutation
|
|
}
|
|
|
|
// Select allows selecting one or more fields (columns) of the returned entity.
|
|
// The default is selecting all fields defined in the entity schema.
|
|
func (tuo *TaskUpdateOne) Select(field string, fields ...string) *TaskUpdateOne {
|
|
tuo.fields = append([]string{field}, fields...)
|
|
return tuo
|
|
}
|
|
|
|
// Save executes the query and returns the updated Task entity.
|
|
func (tuo *TaskUpdateOne) Save(ctx context.Context) (*Task, error) {
|
|
var (
|
|
err error
|
|
node *Task
|
|
)
|
|
if len(tuo.hooks) == 0 {
|
|
if err = tuo.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
node, err = tuo.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*TaskMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err = tuo.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
tuo.mutation = mutation
|
|
node, err = tuo.sqlSave(ctx)
|
|
mutation.done = true
|
|
return node, err
|
|
})
|
|
for i := len(tuo.hooks) - 1; i >= 0; i-- {
|
|
mut = tuo.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, tuo.mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (tuo *TaskUpdateOne) SaveX(ctx context.Context) *Task {
|
|
node, err := tuo.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (tuo *TaskUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := tuo.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (tuo *TaskUpdateOne) ExecX(ctx context.Context) {
|
|
if err := tuo.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (tuo *TaskUpdateOne) check() error {
|
|
if v, ok := tuo.mutation.Priority(); ok {
|
|
if err := task.PriorityValidator(int(v)); err != nil {
|
|
return &ValidationError{Name: "priority", err: fmt.Errorf("ent: validator failed for field \"priority\": %w", err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) {
|
|
_spec := &sqlgraph.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: task.Table,
|
|
Columns: task.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: task.FieldID,
|
|
},
|
|
},
|
|
}
|
|
id, ok := tuo.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing Task.ID for update")}
|
|
}
|
|
_spec.Node.ID.Value = id
|
|
if fields := tuo.fields; len(fields) > 0 {
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
_spec.Node.Columns = append(_spec.Node.Columns, task.FieldID)
|
|
for _, f := range fields {
|
|
if !task.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
if f != task.FieldID {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, f)
|
|
}
|
|
}
|
|
}
|
|
if ps := tuo.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := tuo.mutation.Priority(); ok {
|
|
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: task.FieldPriority,
|
|
})
|
|
}
|
|
if value, ok := tuo.mutation.AddedPriority(); ok {
|
|
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: task.FieldPriority,
|
|
})
|
|
}
|
|
_node = &Task{config: tuo.config}
|
|
_spec.Assign = _node.assignValues
|
|
_spec.ScanValues = _node.scanValues
|
|
if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{task.Label}
|
|
} else if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
return nil, err
|
|
}
|
|
return _node, nil
|
|
}
|