// 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 versioned import ( "context" "errors" "fmt" "sync" "time" "entgo.io/ent" "entgo.io/ent/entc/integration/multischema/versioned/friendship" "entgo.io/ent/entc/integration/multischema/versioned/group" "entgo.io/ent/entc/integration/multischema/versioned/pet" "entgo.io/ent/entc/integration/multischema/versioned/user" ) const ( // Operation types. OpCreate = ent.OpCreate OpDelete = ent.OpDelete OpDeleteOne = ent.OpDeleteOne OpUpdate = ent.OpUpdate OpUpdateOne = ent.OpUpdateOne // Node types. TypeFriendship = "Friendship" TypeGroup = "Group" TypePet = "Pet" TypeUser = "User" ) // FriendshipMutation represents an operation that mutates the Friendship nodes in the graph. type FriendshipMutation struct { friendship.Mutation config id *int done bool oldValue func(context.Context) (*Friendship, error) } var _ ent.Mutation = (*FriendshipMutation)(nil) // friendshipOption allows management of the mutation configuration using functional options. type friendshipOption func(*FriendshipMutation) // newFriendshipMutation creates new mutation for the Friendship entity. func newFriendshipMutation(c config, op Op, opts ...friendshipOption) *FriendshipMutation { m := &FriendshipMutation{ Mutation: *friendship.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 *FriendshipMutation) ID() (id int, exists bool) { if m.id == nil { return } return *m.id, true } // withFriendshipID sets the ID field of the mutation. func withFriendshipID(id int) friendshipOption { return func(m *FriendshipMutation) { var ( err error once sync.Once value *Friendship ) m.oldValue = func(ctx context.Context) (*Friendship, error) { once.Do(func() { if m.done { err = errors.New("querying old values post mutation is not allowed") } else { value, err = m.Client().Friendship.Get(ctx, id) } }) return value, err } m.id = &id } } // withFriendship sets the old Friendship of the mutation. func withFriendship(node *Friendship) friendshipOption { return func(m *FriendshipMutation) { m.oldValue = func(context.Context) (*Friendship, 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 FriendshipMutation) 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 FriendshipMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { return nil, errors.New("versioned: 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 *FriendshipMutation) 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().Friendship.Query().Where(m.Predicates()...).IDs(ctx) default: return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op()) } } // OldWeight returns the old "weight" field's value of the Friendship entity. // If the Friendship 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 *FriendshipMutation) OldWeight(ctx context.Context) (v int, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldWeight is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldWeight requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldWeight: %w", err) } return oldValue.Weight, nil } // OldCreatedAt returns the old "created_at" field's value of the Friendship entity. // If the Friendship 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 *FriendshipMutation) 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 } // OldUserID returns the old "user_id" field's value of the Friendship entity. // If the Friendship 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 *FriendshipMutation) 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 } // OldFriendID returns the old "friend_id" field's value of the Friendship entity. // If the Friendship 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 *FriendshipMutation) OldFriendID(ctx context.Context) (v int, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldFriendID is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldFriendID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldFriendID: %w", err) } return oldValue.FriendID, 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 *FriendshipMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { case friendship.FieldWeight: return m.OldWeight(ctx) case friendship.FieldCreatedAt: return m.OldCreatedAt(ctx) case friendship.FieldUserID: return m.OldUserID(ctx) case friendship.FieldFriendID: return m.OldFriendID(ctx) } return nil, fmt.Errorf("unknown Friendship field %s", name) } // GroupMutation represents an operation that mutates the Group nodes in the graph. type GroupMutation struct { group.Mutation config id *int done bool oldValue func(context.Context) (*Group, error) } var _ ent.Mutation = (*GroupMutation)(nil) // groupOption allows management of the mutation configuration using functional options. type groupOption func(*GroupMutation) // newGroupMutation creates new mutation for the Group entity. func newGroupMutation(c config, op Op, opts ...groupOption) *GroupMutation { m := &GroupMutation{ Mutation: *group.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 *GroupMutation) ID() (id int, exists bool) { if m.id == nil { return } return *m.id, true } // withGroupID sets the ID field of the mutation. func withGroupID(id int) groupOption { return func(m *GroupMutation) { var ( err error once sync.Once value *Group ) m.oldValue = func(ctx context.Context) (*Group, error) { once.Do(func() { if m.done { err = errors.New("querying old values post mutation is not allowed") } else { value, err = m.Client().Group.Get(ctx, id) } }) return value, err } m.id = &id } } // withGroup sets the old Group of the mutation. func withGroup(node *Group) groupOption { return func(m *GroupMutation) { m.oldValue = func(context.Context) (*Group, 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 GroupMutation) 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 GroupMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { return nil, errors.New("versioned: 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 *GroupMutation) 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().Group.Query().Where(m.Predicates()...).IDs(ctx) default: return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op()) } } // OldName returns the old "name" field's value of the Group entity. // If the Group 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 *GroupMutation) 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 *GroupMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { case group.FieldName: return m.OldName(ctx) } return nil, fmt.Errorf("unknown Group 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("versioned: 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()) } } // 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 } // 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.FieldName: return m.OldName(ctx) case pet.FieldOwnerID: return m.OldOwnerID(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("versioned: 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()) } } // 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 } // 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.FieldName: return m.OldName(ctx) } return nil, fmt.Errorf("unknown User field %s", name) }