// 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" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/customid/ent/account" "entgo.io/ent/entc/integration/customid/ent/predicate" "entgo.io/ent/entc/integration/customid/ent/token" "entgo.io/ent/entc/integration/customid/sid" "entgo.io/ent/schema/field" ) // TokenUpdate is the builder for updating Token entities. type TokenUpdate struct { config hooks []Hook mutation *TokenMutation } // Where appends a list predicates to the TokenUpdate builder. func (_u *TokenUpdate) Where(ps ...predicate.Token) *TokenUpdate { _u.mutation.Where(ps...) return _u } // SetBody sets the "body" field. func (_u *TokenUpdate) SetBody(v string) *TokenUpdate { _u.mutation.SetBody(v) return _u } // SetNillableBody sets the "body" field if the given value is not nil. func (_u *TokenUpdate) SetNillableBody(v *string) *TokenUpdate { if v != nil { _u.SetBody(*v) } return _u } // SetAccountID sets the "account" edge to the Account entity by ID. func (_u *TokenUpdate) SetAccountID(id sid.ID) *TokenUpdate { _u.mutation.SetAccountID(id) return _u } // SetAccount sets the "account" edge to the Account entity. func (_u *TokenUpdate) SetAccount(v *Account) *TokenUpdate { return _u.SetAccountID(v.ID) } // Mutation returns the TokenMutation object of the builder. func (_u *TokenUpdate) Mutation() *TokenMutation { return _u.mutation } // ClearAccount clears the "account" edge to the Account entity. func (_u *TokenUpdate) ClearAccount() *TokenUpdate { _u.mutation.ClearAccount() return _u } // Save executes the query and returns the number of nodes affected by the update operation. func (_u *TokenUpdate) 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 *TokenUpdate) SaveX(ctx context.Context) int { affected, err := _u.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (_u *TokenUpdate) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *TokenUpdate) 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 *TokenUpdate) check() error { if v, ok := _u.mutation.Body(); ok { if err := token.BodyValidator(v); err != nil { return &ValidationError{Name: "body", err: fmt.Errorf(`ent: validator failed for field "Token.body": %w`, err)} } } if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Token.account"`) } return nil } func (_u *TokenUpdate) sqlSave(ctx context.Context) (_node int, err error) { if err := _u.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeOther)) 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.Body(); ok { _spec.SetField(token.FieldBody, field.TypeString, value) } if _u.mutation.AccountCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: token.AccountTable, Columns: []string{token.AccountColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeOther), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: token.AccountTable, Columns: []string{token.AccountColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeOther), }, } 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{token.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return 0, err } _u.mutation.done = true return _node, nil } // TokenUpdateOne is the builder for updating a single Token entity. type TokenUpdateOne struct { config fields []string hooks []Hook mutation *TokenMutation } // SetBody sets the "body" field. func (_u *TokenUpdateOne) SetBody(v string) *TokenUpdateOne { _u.mutation.SetBody(v) return _u } // SetNillableBody sets the "body" field if the given value is not nil. func (_u *TokenUpdateOne) SetNillableBody(v *string) *TokenUpdateOne { if v != nil { _u.SetBody(*v) } return _u } // SetAccountID sets the "account" edge to the Account entity by ID. func (_u *TokenUpdateOne) SetAccountID(id sid.ID) *TokenUpdateOne { _u.mutation.SetAccountID(id) return _u } // SetAccount sets the "account" edge to the Account entity. func (_u *TokenUpdateOne) SetAccount(v *Account) *TokenUpdateOne { return _u.SetAccountID(v.ID) } // Mutation returns the TokenMutation object of the builder. func (_u *TokenUpdateOne) Mutation() *TokenMutation { return _u.mutation } // ClearAccount clears the "account" edge to the Account entity. func (_u *TokenUpdateOne) ClearAccount() *TokenUpdateOne { _u.mutation.ClearAccount() return _u } // Where appends a list predicates to the TokenUpdate builder. func (_u *TokenUpdateOne) Where(ps ...predicate.Token) *TokenUpdateOne { _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 *TokenUpdateOne) Select(field string, fields ...string) *TokenUpdateOne { _u.fields = append([]string{field}, fields...) return _u } // Save executes the query and returns the updated Token entity. func (_u *TokenUpdateOne) Save(ctx context.Context) (*Token, error) { return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) } // SaveX is like Save, but panics if an error occurs. func (_u *TokenUpdateOne) SaveX(ctx context.Context) *Token { node, err := _u.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (_u *TokenUpdateOne) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *TokenUpdateOne) 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 *TokenUpdateOne) check() error { if v, ok := _u.mutation.Body(); ok { if err := token.BodyValidator(v); err != nil { return &ValidationError{Name: "body", err: fmt.Errorf(`ent: validator failed for field "Token.body": %w`, err)} } } if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Token.account"`) } return nil } func (_u *TokenUpdateOne) sqlSave(ctx context.Context) (_node *Token, err error) { if err := _u.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeOther)) id, ok := _u.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Token.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, token.FieldID) for _, f := range fields { if !token.ValidColumn(f) { return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } if f != token.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.Body(); ok { _spec.SetField(token.FieldBody, field.TypeString, value) } if _u.mutation.AccountCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: token.AccountTable, Columns: []string{token.AccountColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeOther), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: token.AccountTable, Columns: []string{token.AccountColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeOther), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } _node = &Token{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{token.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } _u.mutation.done = true return _node, nil }