1403 lines
43 KiB
Go
1403 lines
43 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"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/car"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/card"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/info"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/metadata"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/node"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/pet"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/post"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/rental"
|
|
"entgo.io/ent/entc/integration/edgefield/ent/user"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const (
|
|
// Operation types.
|
|
OpCreate = ent.OpCreate
|
|
OpDelete = ent.OpDelete
|
|
OpDeleteOne = ent.OpDeleteOne
|
|
OpUpdate = ent.OpUpdate
|
|
OpUpdateOne = ent.OpUpdateOne
|
|
|
|
// Node types.
|
|
TypeCar = "Car"
|
|
TypeCard = "Card"
|
|
TypeInfo = "Info"
|
|
TypeMetadata = "Metadata"
|
|
TypeNode = "Node"
|
|
TypePet = "Pet"
|
|
TypePost = "Post"
|
|
TypeRental = "Rental"
|
|
TypeUser = "User"
|
|
)
|
|
|
|
// CarMutation represents an operation that mutates the Car nodes in the graph.
|
|
type CarMutation struct {
|
|
car.Mutation
|
|
config
|
|
id *uuid.UUID
|
|
done bool
|
|
oldValue func(context.Context) (*Car, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*CarMutation)(nil)
|
|
|
|
// carOption allows management of the mutation configuration using functional options.
|
|
type carOption func(*CarMutation)
|
|
|
|
// newCarMutation creates new mutation for the Car entity.
|
|
func newCarMutation(c config, op Op, opts ...carOption) *CarMutation {
|
|
m := &CarMutation{
|
|
Mutation: *car.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// SetID sets the value of the id field. Note that this
|
|
// operation is only accepted on creation of Car entities.
|
|
func (m *CarMutation) SetID(id uuid.UUID) {
|
|
m.id = &id
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *CarMutation) ID() (id uuid.UUID, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withCarID sets the ID field of the mutation.
|
|
func withCarID(id uuid.UUID) carOption {
|
|
return func(m *CarMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *Car
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*Car, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().Car.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withCar sets the old Car of the mutation.
|
|
func withCar(node *Car) carOption {
|
|
return func(m *CarMutation) {
|
|
m.oldValue = func(context.Context) (*Car, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m CarMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m CarMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *CarMutation) IDs(ctx context.Context) ([]uuid.UUID, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []uuid.UUID{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().Car.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldNumber returns the old "number" field's value of the Car entity.
|
|
// If the Car object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *CarMutation) OldNumber(ctx context.Context) (v string, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldNumber is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldNumber requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldNumber: %w", err)
|
|
}
|
|
return oldValue.Number, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *CarMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case car.FieldNumber:
|
|
return m.OldNumber(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown Car field %s", name)
|
|
}
|
|
|
|
// CardMutation represents an operation that mutates the Card nodes in the graph.
|
|
type CardMutation struct {
|
|
card.Mutation
|
|
config
|
|
id *int
|
|
done bool
|
|
oldValue func(context.Context) (*Card, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*CardMutation)(nil)
|
|
|
|
// cardOption allows management of the mutation configuration using functional options.
|
|
type cardOption func(*CardMutation)
|
|
|
|
// newCardMutation creates new mutation for the Card entity.
|
|
func newCardMutation(c config, op Op, opts ...cardOption) *CardMutation {
|
|
m := &CardMutation{
|
|
Mutation: *card.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *CardMutation) ID() (id int, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withCardID sets the ID field of the mutation.
|
|
func withCardID(id int) cardOption {
|
|
return func(m *CardMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *Card
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*Card, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().Card.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withCard sets the old Card of the mutation.
|
|
func withCard(node *Card) cardOption {
|
|
return func(m *CardMutation) {
|
|
m.oldValue = func(context.Context) (*Card, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m CardMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m CardMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *CardMutation) IDs(ctx context.Context) ([]int, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []int{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().Card.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldNumber returns the old "number" field's value of the Card entity.
|
|
// If the Card object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *CardMutation) OldNumber(ctx context.Context) (v string, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldNumber is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldNumber requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldNumber: %w", err)
|
|
}
|
|
return oldValue.Number, nil
|
|
}
|
|
|
|
// OldOwnerID returns the old "owner_id" field's value of the Card entity.
|
|
// If the Card object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *CardMutation) OldOwnerID(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldOwnerID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldOwnerID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldOwnerID: %w", err)
|
|
}
|
|
return oldValue.OwnerID, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *CardMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case card.FieldNumber:
|
|
return m.OldNumber(ctx)
|
|
case card.FieldOwnerID:
|
|
return m.OldOwnerID(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown Card field %s", name)
|
|
}
|
|
|
|
// InfoMutation represents an operation that mutates the Info nodes in the graph.
|
|
type InfoMutation struct {
|
|
info.Mutation
|
|
config
|
|
id *int
|
|
done bool
|
|
oldValue func(context.Context) (*Info, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*InfoMutation)(nil)
|
|
|
|
// infoOption allows management of the mutation configuration using functional options.
|
|
type infoOption func(*InfoMutation)
|
|
|
|
// newInfoMutation creates new mutation for the Info entity.
|
|
func newInfoMutation(c config, op Op, opts ...infoOption) *InfoMutation {
|
|
m := &InfoMutation{
|
|
Mutation: *info.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// SetID sets the value of the id field. Note that this
|
|
// operation is only accepted on creation of Info entities.
|
|
func (m *InfoMutation) SetID(id int) {
|
|
m.id = &id
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *InfoMutation) ID() (id int, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withInfoID sets the ID field of the mutation.
|
|
func withInfoID(id int) infoOption {
|
|
return func(m *InfoMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *Info
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*Info, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().Info.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withInfo sets the old Info of the mutation.
|
|
func withInfo(node *Info) infoOption {
|
|
return func(m *InfoMutation) {
|
|
m.oldValue = func(context.Context) (*Info, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m InfoMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m InfoMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *InfoMutation) IDs(ctx context.Context) ([]int, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []int{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().Info.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldContent returns the old "content" field's value of the Info entity.
|
|
// If the Info object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *InfoMutation) OldContent(ctx context.Context) (v json.RawMessage, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldContent is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldContent requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldContent: %w", err)
|
|
}
|
|
return oldValue.Content, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *InfoMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case info.FieldContent:
|
|
return m.OldContent(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown Info field %s", name)
|
|
}
|
|
|
|
// MetadataMutation represents an operation that mutates the Metadata nodes in the graph.
|
|
type MetadataMutation struct {
|
|
metadata.Mutation
|
|
config
|
|
id *int
|
|
done bool
|
|
oldValue func(context.Context) (*Metadata, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*MetadataMutation)(nil)
|
|
|
|
// metadataOption allows management of the mutation configuration using functional options.
|
|
type metadataOption func(*MetadataMutation)
|
|
|
|
// newMetadataMutation creates new mutation for the Metadata entity.
|
|
func newMetadataMutation(c config, op Op, opts ...metadataOption) *MetadataMutation {
|
|
m := &MetadataMutation{
|
|
Mutation: *metadata.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// SetID sets the value of the id field. Note that this
|
|
// operation is only accepted on creation of Metadata entities.
|
|
func (m *MetadataMutation) SetID(id int) {
|
|
m.id = &id
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *MetadataMutation) ID() (id int, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withMetadataID sets the ID field of the mutation.
|
|
func withMetadataID(id int) metadataOption {
|
|
return func(m *MetadataMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *Metadata
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*Metadata, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().Metadata.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withMetadata sets the old Metadata of the mutation.
|
|
func withMetadata(node *Metadata) metadataOption {
|
|
return func(m *MetadataMutation) {
|
|
m.oldValue = func(context.Context) (*Metadata, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m MetadataMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m MetadataMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *MetadataMutation) IDs(ctx context.Context) ([]int, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []int{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().Metadata.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldAge returns the old "age" field's value of the Metadata entity.
|
|
// If the Metadata object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *MetadataMutation) OldAge(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldAge is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldAge requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldAge: %w", err)
|
|
}
|
|
return oldValue.Age, nil
|
|
}
|
|
|
|
// OldParentID returns the old "parent_id" field's value of the Metadata entity.
|
|
// If the Metadata object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *MetadataMutation) OldParentID(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldParentID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldParentID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldParentID: %w", err)
|
|
}
|
|
return oldValue.ParentID, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *MetadataMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case metadata.FieldAge:
|
|
return m.OldAge(ctx)
|
|
case metadata.FieldParentID:
|
|
return m.OldParentID(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown Metadata field %s", name)
|
|
}
|
|
|
|
// NodeMutation represents an operation that mutates the Node nodes in the graph.
|
|
type NodeMutation struct {
|
|
node.Mutation
|
|
config
|
|
id *int
|
|
done bool
|
|
oldValue func(context.Context) (*Node, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*NodeMutation)(nil)
|
|
|
|
// nodeOption allows management of the mutation configuration using functional options.
|
|
type nodeOption func(*NodeMutation)
|
|
|
|
// newNodeMutation creates new mutation for the Node entity.
|
|
func newNodeMutation(c config, op Op, opts ...nodeOption) *NodeMutation {
|
|
m := &NodeMutation{
|
|
Mutation: *node.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *NodeMutation) ID() (id int, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withNodeID sets the ID field of the mutation.
|
|
func withNodeID(id int) nodeOption {
|
|
return func(m *NodeMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *Node
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*Node, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().Node.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withNode sets the old Node of the mutation.
|
|
func withNode(node *Node) nodeOption {
|
|
return func(m *NodeMutation) {
|
|
m.oldValue = func(context.Context) (*Node, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m NodeMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m NodeMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *NodeMutation) IDs(ctx context.Context) ([]int, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []int{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().Node.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldValue returns the old "value" field's value of the Node entity.
|
|
// If the Node object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *NodeMutation) OldValue(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldValue is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldValue requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldValue: %w", err)
|
|
}
|
|
return oldValue.Value, nil
|
|
}
|
|
|
|
// OldPrevID returns the old "prev_id" field's value of the Node entity.
|
|
// If the Node object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *NodeMutation) OldPrevID(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldPrevID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldPrevID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldPrevID: %w", err)
|
|
}
|
|
return oldValue.PrevID, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *NodeMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case node.FieldValue:
|
|
return m.OldValue(ctx)
|
|
case node.FieldPrevID:
|
|
return m.OldPrevID(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown Node field %s", name)
|
|
}
|
|
|
|
// PetMutation represents an operation that mutates the Pet nodes in the graph.
|
|
type PetMutation struct {
|
|
pet.Mutation
|
|
config
|
|
id *int
|
|
done bool
|
|
oldValue func(context.Context) (*Pet, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*PetMutation)(nil)
|
|
|
|
// petOption allows management of the mutation configuration using functional options.
|
|
type petOption func(*PetMutation)
|
|
|
|
// newPetMutation creates new mutation for the Pet entity.
|
|
func newPetMutation(c config, op Op, opts ...petOption) *PetMutation {
|
|
m := &PetMutation{
|
|
Mutation: *pet.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *PetMutation) ID() (id int, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withPetID sets the ID field of the mutation.
|
|
func withPetID(id int) petOption {
|
|
return func(m *PetMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *Pet
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*Pet, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().Pet.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withPet sets the old Pet of the mutation.
|
|
func withPet(node *Pet) petOption {
|
|
return func(m *PetMutation) {
|
|
m.oldValue = func(context.Context) (*Pet, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m PetMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m PetMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *PetMutation) IDs(ctx context.Context) ([]int, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []int{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().Pet.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldOwnerID returns the old "owner_id" field's value of the Pet entity.
|
|
// If the Pet object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *PetMutation) OldOwnerID(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldOwnerID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldOwnerID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldOwnerID: %w", err)
|
|
}
|
|
return oldValue.OwnerID, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *PetMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case pet.FieldOwnerID:
|
|
return m.OldOwnerID(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown Pet field %s", name)
|
|
}
|
|
|
|
// PostMutation represents an operation that mutates the Post nodes in the graph.
|
|
type PostMutation struct {
|
|
post.Mutation
|
|
config
|
|
id *int
|
|
done bool
|
|
oldValue func(context.Context) (*Post, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*PostMutation)(nil)
|
|
|
|
// postOption allows management of the mutation configuration using functional options.
|
|
type postOption func(*PostMutation)
|
|
|
|
// newPostMutation creates new mutation for the Post entity.
|
|
func newPostMutation(c config, op Op, opts ...postOption) *PostMutation {
|
|
m := &PostMutation{
|
|
Mutation: *post.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *PostMutation) ID() (id int, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withPostID sets the ID field of the mutation.
|
|
func withPostID(id int) postOption {
|
|
return func(m *PostMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *Post
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*Post, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().Post.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withPost sets the old Post of the mutation.
|
|
func withPost(node *Post) postOption {
|
|
return func(m *PostMutation) {
|
|
m.oldValue = func(context.Context) (*Post, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m PostMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m PostMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *PostMutation) IDs(ctx context.Context) ([]int, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []int{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().Post.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldText returns the old "text" field's value of the Post entity.
|
|
// If the Post object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *PostMutation) OldText(ctx context.Context) (v string, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldText is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldText requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldText: %w", err)
|
|
}
|
|
return oldValue.Text, nil
|
|
}
|
|
|
|
// OldAuthorID returns the old "author_id" field's value of the Post entity.
|
|
// If the Post object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *PostMutation) OldAuthorID(ctx context.Context) (v *int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldAuthorID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldAuthorID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldAuthorID: %w", err)
|
|
}
|
|
return oldValue.AuthorID, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *PostMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case post.FieldText:
|
|
return m.OldText(ctx)
|
|
case post.FieldAuthorID:
|
|
return m.OldAuthorID(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown Post field %s", name)
|
|
}
|
|
|
|
// RentalMutation represents an operation that mutates the Rental nodes in the graph.
|
|
type RentalMutation struct {
|
|
rental.Mutation
|
|
config
|
|
id *int
|
|
done bool
|
|
oldValue func(context.Context) (*Rental, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*RentalMutation)(nil)
|
|
|
|
// rentalOption allows management of the mutation configuration using functional options.
|
|
type rentalOption func(*RentalMutation)
|
|
|
|
// newRentalMutation creates new mutation for the Rental entity.
|
|
func newRentalMutation(c config, op Op, opts ...rentalOption) *RentalMutation {
|
|
m := &RentalMutation{
|
|
Mutation: *rental.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *RentalMutation) ID() (id int, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withRentalID sets the ID field of the mutation.
|
|
func withRentalID(id int) rentalOption {
|
|
return func(m *RentalMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *Rental
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*Rental, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().Rental.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withRental sets the old Rental of the mutation.
|
|
func withRental(node *Rental) rentalOption {
|
|
return func(m *RentalMutation) {
|
|
m.oldValue = func(context.Context) (*Rental, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m RentalMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m RentalMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *RentalMutation) IDs(ctx context.Context) ([]int, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []int{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().Rental.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldDate returns the old "date" field's value of the Rental entity.
|
|
// If the Rental object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *RentalMutation) OldDate(ctx context.Context) (v time.Time, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldDate is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldDate requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldDate: %w", err)
|
|
}
|
|
return oldValue.Date, nil
|
|
}
|
|
|
|
// OldUserID returns the old "user_id" field's value of the Rental entity.
|
|
// If the Rental object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *RentalMutation) OldUserID(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldUserID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldUserID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldUserID: %w", err)
|
|
}
|
|
return oldValue.UserID, nil
|
|
}
|
|
|
|
// OldCarID returns the old "car_id" field's value of the Rental entity.
|
|
// If the Rental object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *RentalMutation) OldCarID(ctx context.Context) (v uuid.UUID, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldCarID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldCarID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldCarID: %w", err)
|
|
}
|
|
return oldValue.CarID, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *RentalMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case rental.FieldDate:
|
|
return m.OldDate(ctx)
|
|
case rental.FieldUserID:
|
|
return m.OldUserID(ctx)
|
|
case rental.FieldCarID:
|
|
return m.OldCarID(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown Rental field %s", name)
|
|
}
|
|
|
|
// UserMutation represents an operation that mutates the User nodes in the graph.
|
|
type UserMutation struct {
|
|
user.Mutation
|
|
config
|
|
id *int
|
|
done bool
|
|
oldValue func(context.Context) (*User, error)
|
|
}
|
|
|
|
var _ ent.Mutation = (*UserMutation)(nil)
|
|
|
|
// userOption allows management of the mutation configuration using functional options.
|
|
type userOption func(*UserMutation)
|
|
|
|
// newUserMutation creates new mutation for the User entity.
|
|
func newUserMutation(c config, op Op, opts ...userOption) *UserMutation {
|
|
m := &UserMutation{
|
|
Mutation: *user.NewMutation(op),
|
|
config: c,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(m)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// SetID sets the value of the id field. Note that this
|
|
// operation is only accepted on creation of User entities.
|
|
func (m *UserMutation) SetID(id int) {
|
|
m.id = &id
|
|
}
|
|
|
|
// ID returns the ID value in the mutation. Note that the ID is only available
|
|
// if it was provided to the builder or after it was returned from the database.
|
|
func (m *UserMutation) ID() (id int, exists bool) {
|
|
if m.id == nil {
|
|
return
|
|
}
|
|
return *m.id, true
|
|
}
|
|
|
|
// withUserID sets the ID field of the mutation.
|
|
func withUserID(id int) userOption {
|
|
return func(m *UserMutation) {
|
|
var (
|
|
err error
|
|
once sync.Once
|
|
value *User
|
|
)
|
|
m.oldValue = func(ctx context.Context) (*User, error) {
|
|
once.Do(func() {
|
|
if m.done {
|
|
err = errors.New("querying old values post mutation is not allowed")
|
|
} else {
|
|
value, err = m.Client().User.Get(ctx, id)
|
|
}
|
|
})
|
|
return value, err
|
|
}
|
|
m.id = &id
|
|
}
|
|
}
|
|
|
|
// withUser sets the old User of the mutation.
|
|
func withUser(node *User) userOption {
|
|
return func(m *UserMutation) {
|
|
m.oldValue = func(context.Context) (*User, error) {
|
|
return node, nil
|
|
}
|
|
m.id = &node.ID
|
|
}
|
|
}
|
|
|
|
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
|
// executed in a transaction (ent.Tx), a transactional client is returned.
|
|
func (m UserMutation) Client() *Client {
|
|
client := &Client{config: m.config}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
|
// it returns an error otherwise.
|
|
func (m UserMutation) Tx() (*Tx, error) {
|
|
if _, ok := m.driver.(*txDriver); !ok {
|
|
return nil, errors.New("ent: mutation is not running in a transaction")
|
|
}
|
|
tx := &Tx{config: m.config}
|
|
tx.init()
|
|
return tx, nil
|
|
}
|
|
|
|
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
|
// That means, if the mutation is applied within a transaction with an isolation level such
|
|
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
|
// or updated by the mutation.
|
|
func (m *UserMutation) IDs(ctx context.Context) ([]int, error) {
|
|
switch {
|
|
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
|
id, exists := m.ID()
|
|
if exists {
|
|
return []int{id}, nil
|
|
}
|
|
fallthrough
|
|
case m.Op().Is(OpUpdate | OpDelete):
|
|
return m.Client().User.Query().Where(m.Predicates()...).IDs(ctx)
|
|
default:
|
|
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
|
}
|
|
}
|
|
|
|
// OldParentID returns the old "parent_id" field's value of the User entity.
|
|
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *UserMutation) OldParentID(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldParentID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldParentID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldParentID: %w", err)
|
|
}
|
|
return oldValue.ParentID, nil
|
|
}
|
|
|
|
// OldSpouseID returns the old "spouse_id" field's value of the User entity.
|
|
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
|
func (m *UserMutation) OldSpouseID(ctx context.Context) (v int, err error) {
|
|
if !m.Op().Is(OpUpdateOne) {
|
|
return v, errors.New("OldSpouseID is only allowed on UpdateOne operations")
|
|
}
|
|
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
|
return v, errors.New("OldSpouseID requires an ID field in the mutation")
|
|
}
|
|
oldValue, err := m.oldValue(ctx)
|
|
if err != nil {
|
|
return v, fmt.Errorf("querying old value for OldSpouseID: %w", err)
|
|
}
|
|
return oldValue.SpouseID, nil
|
|
}
|
|
|
|
// OldField returns the old value of the field from the database. An error is
|
|
// returned if the mutation operation is not UpdateOne, or the query to the
|
|
// database failed.
|
|
func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
|
switch name {
|
|
case user.FieldParentID:
|
|
return m.OldParentID(ctx)
|
|
case user.FieldSpouseID:
|
|
return m.OldSpouseID(ctx)
|
|
}
|
|
return nil, fmt.Errorf("unknown User field %s", name)
|
|
}
|