532 lines
15 KiB
Go
532 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"
|
|
"time"
|
|
|
|
"entgo.io/ent/dialect/sql"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/examples/migration/ent/card"
|
|
"entgo.io/ent/examples/migration/ent/payment"
|
|
"entgo.io/ent/examples/migration/ent/predicate"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// PaymentUpdate is the builder for updating Payment entities.
|
|
type PaymentUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *PaymentMutation
|
|
}
|
|
|
|
// Where appends a list predicates to the PaymentUpdate builder.
|
|
func (_u *PaymentUpdate) Where(ps ...predicate.Payment) *PaymentUpdate {
|
|
_u.mutation.Where(ps...)
|
|
return _u
|
|
}
|
|
|
|
// SetCardID sets the "card_id" field.
|
|
func (_u *PaymentUpdate) SetCardID(v int) *PaymentUpdate {
|
|
_u.mutation.SetCardID(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableCardID sets the "card_id" field if the given value is not nil.
|
|
func (_u *PaymentUpdate) SetNillableCardID(v *int) *PaymentUpdate {
|
|
if v != nil {
|
|
_u.SetCardID(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetAmount sets the "amount" field.
|
|
func (_u *PaymentUpdate) SetAmount(v float64) *PaymentUpdate {
|
|
_u.mutation.ResetAmount()
|
|
_u.mutation.SetAmount(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableAmount sets the "amount" field if the given value is not nil.
|
|
func (_u *PaymentUpdate) SetNillableAmount(v *float64) *PaymentUpdate {
|
|
if v != nil {
|
|
_u.SetAmount(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// AddAmount adds value to the "amount" field.
|
|
func (_u *PaymentUpdate) AddAmount(v float64) *PaymentUpdate {
|
|
_u.mutation.AddAmount(v)
|
|
return _u
|
|
}
|
|
|
|
// SetCurrency sets the "currency" field.
|
|
func (_u *PaymentUpdate) SetCurrency(v payment.Currency) *PaymentUpdate {
|
|
_u.mutation.SetCurrency(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableCurrency sets the "currency" field if the given value is not nil.
|
|
func (_u *PaymentUpdate) SetNillableCurrency(v *payment.Currency) *PaymentUpdate {
|
|
if v != nil {
|
|
_u.SetCurrency(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetTime sets the "time" field.
|
|
func (_u *PaymentUpdate) SetTime(v time.Time) *PaymentUpdate {
|
|
_u.mutation.SetTime(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableTime sets the "time" field if the given value is not nil.
|
|
func (_u *PaymentUpdate) SetNillableTime(v *time.Time) *PaymentUpdate {
|
|
if v != nil {
|
|
_u.SetTime(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetDescription sets the "description" field.
|
|
func (_u *PaymentUpdate) SetDescription(v string) *PaymentUpdate {
|
|
_u.mutation.SetDescription(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
|
func (_u *PaymentUpdate) SetNillableDescription(v *string) *PaymentUpdate {
|
|
if v != nil {
|
|
_u.SetDescription(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetStatus sets the "status" field.
|
|
func (_u *PaymentUpdate) SetStatus(v payment.Status) *PaymentUpdate {
|
|
_u.mutation.SetStatus(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableStatus sets the "status" field if the given value is not nil.
|
|
func (_u *PaymentUpdate) SetNillableStatus(v *payment.Status) *PaymentUpdate {
|
|
if v != nil {
|
|
_u.SetStatus(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetCard sets the "card" edge to the Card entity.
|
|
func (_u *PaymentUpdate) SetCard(v *Card) *PaymentUpdate {
|
|
return _u.SetCardID(v.ID)
|
|
}
|
|
|
|
// Mutation returns the PaymentMutation object of the builder.
|
|
func (_u *PaymentUpdate) Mutation() *PaymentMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// ClearCard clears the "card" edge to the Card entity.
|
|
func (_u *PaymentUpdate) ClearCard() *PaymentUpdate {
|
|
_u.mutation.ClearCard()
|
|
return _u
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (_u *PaymentUpdate) 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 *PaymentUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_u *PaymentUpdate) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *PaymentUpdate) 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 *PaymentUpdate) check() error {
|
|
if v, ok := _u.mutation.Amount(); ok {
|
|
if err := payment.AmountValidator(v); err != nil {
|
|
return &ValidationError{Name: "amount", err: fmt.Errorf(`ent: validator failed for field "Payment.amount": %w`, err)}
|
|
}
|
|
}
|
|
if v, ok := _u.mutation.Currency(); ok {
|
|
if err := payment.CurrencyValidator(v); err != nil {
|
|
return &ValidationError{Name: "currency", err: fmt.Errorf(`ent: validator failed for field "Payment.currency": %w`, err)}
|
|
}
|
|
}
|
|
if v, ok := _u.mutation.Status(); ok {
|
|
if err := payment.StatusValidator(v); err != nil {
|
|
return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Payment.status": %w`, err)}
|
|
}
|
|
}
|
|
if _u.mutation.CardCleared() && len(_u.mutation.CardIDs()) > 0 {
|
|
return errors.New(`ent: clearing a required unique edge "Payment.card"`)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_u *PaymentUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
|
if err := _u.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(payment.Table, payment.Columns, sqlgraph.NewFieldSpec(payment.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.Amount(); ok {
|
|
_spec.SetField(payment.FieldAmount, field.TypeFloat64, value)
|
|
}
|
|
if value, ok := _u.mutation.AddedAmount(); ok {
|
|
_spec.AddField(payment.FieldAmount, field.TypeFloat64, value)
|
|
}
|
|
if value, ok := _u.mutation.Currency(); ok {
|
|
_spec.SetField(payment.FieldCurrency, field.TypeEnum, value)
|
|
}
|
|
if value, ok := _u.mutation.Time(); ok {
|
|
_spec.SetField(payment.FieldTime, field.TypeTime, value)
|
|
}
|
|
if value, ok := _u.mutation.Description(); ok {
|
|
_spec.SetField(payment.FieldDescription, field.TypeString, value)
|
|
}
|
|
if value, ok := _u.mutation.Status(); ok {
|
|
_spec.SetField(payment.FieldStatus, field.TypeEnum, value)
|
|
}
|
|
if _u.mutation.CardCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: payment.CardTable,
|
|
Columns: []string{payment.CardColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(card.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.CardIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: payment.CardTable,
|
|
Columns: []string{payment.CardColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(card.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{payment.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return 0, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
// PaymentUpdateOne is the builder for updating a single Payment entity.
|
|
type PaymentUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *PaymentMutation
|
|
}
|
|
|
|
// SetCardID sets the "card_id" field.
|
|
func (_u *PaymentUpdateOne) SetCardID(v int) *PaymentUpdateOne {
|
|
_u.mutation.SetCardID(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableCardID sets the "card_id" field if the given value is not nil.
|
|
func (_u *PaymentUpdateOne) SetNillableCardID(v *int) *PaymentUpdateOne {
|
|
if v != nil {
|
|
_u.SetCardID(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetAmount sets the "amount" field.
|
|
func (_u *PaymentUpdateOne) SetAmount(v float64) *PaymentUpdateOne {
|
|
_u.mutation.ResetAmount()
|
|
_u.mutation.SetAmount(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableAmount sets the "amount" field if the given value is not nil.
|
|
func (_u *PaymentUpdateOne) SetNillableAmount(v *float64) *PaymentUpdateOne {
|
|
if v != nil {
|
|
_u.SetAmount(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// AddAmount adds value to the "amount" field.
|
|
func (_u *PaymentUpdateOne) AddAmount(v float64) *PaymentUpdateOne {
|
|
_u.mutation.AddAmount(v)
|
|
return _u
|
|
}
|
|
|
|
// SetCurrency sets the "currency" field.
|
|
func (_u *PaymentUpdateOne) SetCurrency(v payment.Currency) *PaymentUpdateOne {
|
|
_u.mutation.SetCurrency(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableCurrency sets the "currency" field if the given value is not nil.
|
|
func (_u *PaymentUpdateOne) SetNillableCurrency(v *payment.Currency) *PaymentUpdateOne {
|
|
if v != nil {
|
|
_u.SetCurrency(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetTime sets the "time" field.
|
|
func (_u *PaymentUpdateOne) SetTime(v time.Time) *PaymentUpdateOne {
|
|
_u.mutation.SetTime(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableTime sets the "time" field if the given value is not nil.
|
|
func (_u *PaymentUpdateOne) SetNillableTime(v *time.Time) *PaymentUpdateOne {
|
|
if v != nil {
|
|
_u.SetTime(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetDescription sets the "description" field.
|
|
func (_u *PaymentUpdateOne) SetDescription(v string) *PaymentUpdateOne {
|
|
_u.mutation.SetDescription(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
|
func (_u *PaymentUpdateOne) SetNillableDescription(v *string) *PaymentUpdateOne {
|
|
if v != nil {
|
|
_u.SetDescription(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetStatus sets the "status" field.
|
|
func (_u *PaymentUpdateOne) SetStatus(v payment.Status) *PaymentUpdateOne {
|
|
_u.mutation.SetStatus(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableStatus sets the "status" field if the given value is not nil.
|
|
func (_u *PaymentUpdateOne) SetNillableStatus(v *payment.Status) *PaymentUpdateOne {
|
|
if v != nil {
|
|
_u.SetStatus(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetCard sets the "card" edge to the Card entity.
|
|
func (_u *PaymentUpdateOne) SetCard(v *Card) *PaymentUpdateOne {
|
|
return _u.SetCardID(v.ID)
|
|
}
|
|
|
|
// Mutation returns the PaymentMutation object of the builder.
|
|
func (_u *PaymentUpdateOne) Mutation() *PaymentMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// ClearCard clears the "card" edge to the Card entity.
|
|
func (_u *PaymentUpdateOne) ClearCard() *PaymentUpdateOne {
|
|
_u.mutation.ClearCard()
|
|
return _u
|
|
}
|
|
|
|
// Where appends a list predicates to the PaymentUpdate builder.
|
|
func (_u *PaymentUpdateOne) Where(ps ...predicate.Payment) *PaymentUpdateOne {
|
|
_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 *PaymentUpdateOne) Select(field string, fields ...string) *PaymentUpdateOne {
|
|
_u.fields = append([]string{field}, fields...)
|
|
return _u
|
|
}
|
|
|
|
// Save executes the query and returns the updated Payment entity.
|
|
func (_u *PaymentUpdateOne) Save(ctx context.Context) (*Payment, error) {
|
|
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_u *PaymentUpdateOne) SaveX(ctx context.Context) *Payment {
|
|
node, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (_u *PaymentUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *PaymentUpdateOne) 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 *PaymentUpdateOne) check() error {
|
|
if v, ok := _u.mutation.Amount(); ok {
|
|
if err := payment.AmountValidator(v); err != nil {
|
|
return &ValidationError{Name: "amount", err: fmt.Errorf(`ent: validator failed for field "Payment.amount": %w`, err)}
|
|
}
|
|
}
|
|
if v, ok := _u.mutation.Currency(); ok {
|
|
if err := payment.CurrencyValidator(v); err != nil {
|
|
return &ValidationError{Name: "currency", err: fmt.Errorf(`ent: validator failed for field "Payment.currency": %w`, err)}
|
|
}
|
|
}
|
|
if v, ok := _u.mutation.Status(); ok {
|
|
if err := payment.StatusValidator(v); err != nil {
|
|
return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Payment.status": %w`, err)}
|
|
}
|
|
}
|
|
if _u.mutation.CardCleared() && len(_u.mutation.CardIDs()) > 0 {
|
|
return errors.New(`ent: clearing a required unique edge "Payment.card"`)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_u *PaymentUpdateOne) sqlSave(ctx context.Context) (_node *Payment, err error) {
|
|
if err := _u.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(payment.Table, payment.Columns, sqlgraph.NewFieldSpec(payment.FieldID, field.TypeInt))
|
|
id, ok := _u.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Payment.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, payment.FieldID)
|
|
for _, f := range fields {
|
|
if !payment.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
if f != payment.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.Amount(); ok {
|
|
_spec.SetField(payment.FieldAmount, field.TypeFloat64, value)
|
|
}
|
|
if value, ok := _u.mutation.AddedAmount(); ok {
|
|
_spec.AddField(payment.FieldAmount, field.TypeFloat64, value)
|
|
}
|
|
if value, ok := _u.mutation.Currency(); ok {
|
|
_spec.SetField(payment.FieldCurrency, field.TypeEnum, value)
|
|
}
|
|
if value, ok := _u.mutation.Time(); ok {
|
|
_spec.SetField(payment.FieldTime, field.TypeTime, value)
|
|
}
|
|
if value, ok := _u.mutation.Description(); ok {
|
|
_spec.SetField(payment.FieldDescription, field.TypeString, value)
|
|
}
|
|
if value, ok := _u.mutation.Status(); ok {
|
|
_spec.SetField(payment.FieldStatus, field.TypeEnum, value)
|
|
}
|
|
if _u.mutation.CardCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: payment.CardTable,
|
|
Columns: []string{payment.CardColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(card.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.CardIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: payment.CardTable,
|
|
Columns: []string{payment.CardColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(card.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
_node = &Payment{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{payment.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|