404 lines
12 KiB
Go
404 lines
12 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"
|
|
|
|
"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"
|
|
)
|
|
|
|
// AccountUpdate is the builder for updating Account entities.
|
|
type AccountUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *AccountMutation
|
|
}
|
|
|
|
// Where appends a list predicates to the AccountUpdate builder.
|
|
func (_u *AccountUpdate) Where(ps ...predicate.Account) *AccountUpdate {
|
|
_u.mutation.Where(ps...)
|
|
return _u
|
|
}
|
|
|
|
// SetEmail sets the "email" field.
|
|
func (_u *AccountUpdate) SetEmail(v string) *AccountUpdate {
|
|
_u.mutation.SetEmail(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableEmail sets the "email" field if the given value is not nil.
|
|
func (_u *AccountUpdate) SetNillableEmail(v *string) *AccountUpdate {
|
|
if v != nil {
|
|
_u.SetEmail(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// AddTokenIDs adds the "token" edge to the Token entity by IDs.
|
|
func (_u *AccountUpdate) AddTokenIDs(ids ...sid.ID) *AccountUpdate {
|
|
_u.mutation.AddTokenIDs(ids...)
|
|
return _u
|
|
}
|
|
|
|
// AddToken adds the "token" edges to the Token entity.
|
|
func (_u *AccountUpdate) AddToken(v ...*Token) *AccountUpdate {
|
|
ids := make([]sid.ID, len(v))
|
|
for i := range v {
|
|
ids[i] = v[i].ID
|
|
}
|
|
return _u.AddTokenIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the AccountMutation object of the builder.
|
|
func (_u *AccountUpdate) Mutation() *AccountMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// ClearToken clears all "token" edges to the Token entity.
|
|
func (_u *AccountUpdate) ClearToken() *AccountUpdate {
|
|
_u.mutation.ClearToken()
|
|
return _u
|
|
}
|
|
|
|
// RemoveTokenIDs removes the "token" edge to Token entities by IDs.
|
|
func (_u *AccountUpdate) RemoveTokenIDs(ids ...sid.ID) *AccountUpdate {
|
|
_u.mutation.RemoveTokenIDs(ids...)
|
|
return _u
|
|
}
|
|
|
|
// RemoveToken removes "token" edges to Token entities.
|
|
func (_u *AccountUpdate) RemoveToken(v ...*Token) *AccountUpdate {
|
|
ids := make([]sid.ID, len(v))
|
|
for i := range v {
|
|
ids[i] = v[i].ID
|
|
}
|
|
return _u.RemoveTokenIDs(ids...)
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (_u *AccountUpdate) 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 *AccountUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_u *AccountUpdate) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *AccountUpdate) 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 *AccountUpdate) check() error {
|
|
if v, ok := _u.mutation.Email(); ok {
|
|
if err := account.EmailValidator(v); err != nil {
|
|
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "Account.email": %w`, err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_u *AccountUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
|
if err := _u.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(account.Table, account.Columns, sqlgraph.NewFieldSpec(account.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.Email(); ok {
|
|
_spec.SetField(account.FieldEmail, field.TypeString, value)
|
|
}
|
|
if _u.mutation.TokenCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: account.TokenTable,
|
|
Columns: []string{account.TokenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(token.FieldID, field.TypeOther),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.RemovedTokenIDs(); len(nodes) > 0 && !_u.mutation.TokenCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: account.TokenTable,
|
|
Columns: []string{account.TokenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(token.FieldID, field.TypeOther),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.TokenIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: account.TokenTable,
|
|
Columns: []string{account.TokenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(token.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{account.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return 0, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
// AccountUpdateOne is the builder for updating a single Account entity.
|
|
type AccountUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *AccountMutation
|
|
}
|
|
|
|
// SetEmail sets the "email" field.
|
|
func (_u *AccountUpdateOne) SetEmail(v string) *AccountUpdateOne {
|
|
_u.mutation.SetEmail(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableEmail sets the "email" field if the given value is not nil.
|
|
func (_u *AccountUpdateOne) SetNillableEmail(v *string) *AccountUpdateOne {
|
|
if v != nil {
|
|
_u.SetEmail(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// AddTokenIDs adds the "token" edge to the Token entity by IDs.
|
|
func (_u *AccountUpdateOne) AddTokenIDs(ids ...sid.ID) *AccountUpdateOne {
|
|
_u.mutation.AddTokenIDs(ids...)
|
|
return _u
|
|
}
|
|
|
|
// AddToken adds the "token" edges to the Token entity.
|
|
func (_u *AccountUpdateOne) AddToken(v ...*Token) *AccountUpdateOne {
|
|
ids := make([]sid.ID, len(v))
|
|
for i := range v {
|
|
ids[i] = v[i].ID
|
|
}
|
|
return _u.AddTokenIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the AccountMutation object of the builder.
|
|
func (_u *AccountUpdateOne) Mutation() *AccountMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// ClearToken clears all "token" edges to the Token entity.
|
|
func (_u *AccountUpdateOne) ClearToken() *AccountUpdateOne {
|
|
_u.mutation.ClearToken()
|
|
return _u
|
|
}
|
|
|
|
// RemoveTokenIDs removes the "token" edge to Token entities by IDs.
|
|
func (_u *AccountUpdateOne) RemoveTokenIDs(ids ...sid.ID) *AccountUpdateOne {
|
|
_u.mutation.RemoveTokenIDs(ids...)
|
|
return _u
|
|
}
|
|
|
|
// RemoveToken removes "token" edges to Token entities.
|
|
func (_u *AccountUpdateOne) RemoveToken(v ...*Token) *AccountUpdateOne {
|
|
ids := make([]sid.ID, len(v))
|
|
for i := range v {
|
|
ids[i] = v[i].ID
|
|
}
|
|
return _u.RemoveTokenIDs(ids...)
|
|
}
|
|
|
|
// Where appends a list predicates to the AccountUpdate builder.
|
|
func (_u *AccountUpdateOne) Where(ps ...predicate.Account) *AccountUpdateOne {
|
|
_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 *AccountUpdateOne) Select(field string, fields ...string) *AccountUpdateOne {
|
|
_u.fields = append([]string{field}, fields...)
|
|
return _u
|
|
}
|
|
|
|
// Save executes the query and returns the updated Account entity.
|
|
func (_u *AccountUpdateOne) Save(ctx context.Context) (*Account, error) {
|
|
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_u *AccountUpdateOne) SaveX(ctx context.Context) *Account {
|
|
node, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (_u *AccountUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *AccountUpdateOne) 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 *AccountUpdateOne) check() error {
|
|
if v, ok := _u.mutation.Email(); ok {
|
|
if err := account.EmailValidator(v); err != nil {
|
|
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "Account.email": %w`, err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_u *AccountUpdateOne) sqlSave(ctx context.Context) (_node *Account, err error) {
|
|
if err := _u.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(account.Table, account.Columns, sqlgraph.NewFieldSpec(account.FieldID, field.TypeOther))
|
|
id, ok := _u.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Account.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, account.FieldID)
|
|
for _, f := range fields {
|
|
if !account.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
if f != account.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.Email(); ok {
|
|
_spec.SetField(account.FieldEmail, field.TypeString, value)
|
|
}
|
|
if _u.mutation.TokenCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: account.TokenTable,
|
|
Columns: []string{account.TokenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(token.FieldID, field.TypeOther),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.RemovedTokenIDs(); len(nodes) > 0 && !_u.mutation.TokenCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: account.TokenTable,
|
|
Columns: []string{account.TokenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(token.FieldID, field.TypeOther),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.TokenIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: account.TokenTable,
|
|
Columns: []string{account.TokenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(token.FieldID, field.TypeOther),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
_node = &Account{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{account.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|