// 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" "sync" "time" "entgo.io/ent" "entgo.io/ent/entc/integration/hooks/ent/card" "entgo.io/ent/entc/integration/hooks/ent/pet" "entgo.io/ent/entc/integration/hooks/ent/user" ) const ( // Operation types. OpCreate = ent.OpCreate OpDelete = ent.OpDelete OpDeleteOne = ent.OpDeleteOne OpUpdate = ent.OpUpdate OpUpdateOne = ent.OpUpdateOne // Node types. TypeCard = "Card" TypePet = "Pet" TypeUser = "User" ) // 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 } // OldName returns the old "name" 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) OldName(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldName is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldName: %w", err) } return oldValue.Name, nil } // OldCreatedAt returns the old "created_at" 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) OldCreatedAt(ctx context.Context) (v time.Time, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldCreatedAt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err) } return oldValue.CreatedAt, nil } // OldInHook returns the old "in_hook" 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) OldInHook(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldInHook is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldInHook requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldInHook: %w", err) } return oldValue.InHook, nil } // OldExpiredAt returns the old "expired_at" 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) OldExpiredAt(ctx context.Context) (v time.Time, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldExpiredAt is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldExpiredAt requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldExpiredAt: %w", err) } return oldValue.ExpiredAt, 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.FieldName: return m.OldName(ctx) case card.FieldCreatedAt: return m.OldCreatedAt(ctx) case card.FieldInHook: return m.OldInHook(ctx) case card.FieldExpiredAt: return m.OldExpiredAt(ctx) } return nil, fmt.Errorf("unknown Card 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()) } } // OldDeleteTime returns the old "delete_time" 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) OldDeleteTime(ctx context.Context) (v time.Time, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldDeleteTime is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldDeleteTime requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldDeleteTime: %w", err) } return oldValue.DeleteTime, nil } // OldName returns the old "name" 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) OldName(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldName is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldName: %w", err) } return oldValue.Name, 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.FieldDeleteTime: return m.OldDeleteTime(ctx) case pet.FieldName: return m.OldName(ctx) } return nil, fmt.Errorf("unknown Pet 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 } // 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()) } } // OldVersion returns the old "version" 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) OldVersion(ctx context.Context) (v int, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldVersion is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldVersion requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldVersion: %w", err) } return oldValue.Version, nil } // OldName returns the old "name" 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) OldName(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldName is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldName: %w", err) } return oldValue.Name, nil } // OldWorth returns the old "worth" 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) OldWorth(ctx context.Context) (v uint, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldWorth is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldWorth requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldWorth: %w", err) } return oldValue.Worth, nil } // OldPassword returns the old "password" 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) OldPassword(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldPassword is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldPassword requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldPassword: %w", err) } return oldValue.Password, nil } // OldActive returns the old "active" 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) OldActive(ctx context.Context) (v bool, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldActive is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldActive requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldActive: %w", err) } return oldValue.Active, 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.FieldVersion: return m.OldVersion(ctx) case user.FieldName: return m.OldName(ctx) case user.FieldWorth: return m.OldWorth(ctx) case user.FieldPassword: return m.OldPassword(ctx) case user.FieldActive: return m.OldActive(ctx) } return nil, fmt.Errorf("unknown User field %s", name) }