// Code generated by ObjectBox; DO NOT EDIT.
// Learn more about defining entities and generating this file - visit https://golang.objectbox.io/entity-annotations

package object

import (
	"errors"
	"github.com/google/flatbuffers/go"
	"github.com/objectbox/objectbox-go/objectbox"
	"github.com/objectbox/objectbox-go/objectbox/fbutils"
)

type taskRelEmbedded_EntityInfo struct {
	objectbox.Entity
	Uid uint64
}

var TaskRelEmbeddedBinding = taskRelEmbedded_EntityInfo{
	Entity: objectbox.Entity{
		Id: 4,
	},
	Uid: 7837839688282259259,
}

// TaskRelEmbedded_ contains type-based Property helpers to facilitate some common operations such as Queries.
var TaskRelEmbedded_ = struct {
	Id        *objectbox.PropertyUint64
	Group     *objectbox.RelationToOne
	GroupsNew *objectbox.RelationToMany
}{
	Id: &objectbox.PropertyUint64{
		BaseProperty: &objectbox.BaseProperty{
			Id:     1,
			Entity: &TaskRelEmbeddedBinding.Entity,
		},
	},
	Group: &objectbox.RelationToOne{
		Property: &objectbox.BaseProperty{
			Id:     2,
			Entity: &TaskRelEmbeddedBinding.Entity,
		},
		Target: &GroupBinding.Entity,
	},
	GroupsNew: &objectbox.RelationToMany{
		Id:     3,
		Source: &TaskRelEmbeddedBinding.Entity,
		Target: &GroupBinding.Entity,
	},
}

// GeneratorVersion is called by ObjectBox to verify the compatibility of the generator used to generate this code
func (taskRelEmbedded_EntityInfo) GeneratorVersion() int {
	return 6
}

// AddToModel is called by ObjectBox during model build
func (taskRelEmbedded_EntityInfo) AddToModel(model *objectbox.Model) {
	model.Entity("TaskRelEmbedded", 4, 7837839688282259259)
	model.Property("Id", 6, 1, 2518412263346885298)
	model.PropertyFlags(1)
	model.Property("Group", 11, 2, 5617773211005988520)
	model.PropertyFlags(520)
	model.PropertyRelation("Group", 4, 2339563716805116249)
	model.EntityLastPropertyId(2, 5617773211005988520)
	model.Relation(3, 6392442863481646880, GroupBinding.Id, GroupBinding.Uid)
}

// GetId is called by ObjectBox during Put operations to check for existing ID on an object
func (taskRelEmbedded_EntityInfo) GetId(object interface{}) (uint64, error) {
	return object.(*TaskRelEmbedded).Id, nil
}

// SetId is called by ObjectBox during Put to update an ID on an object that has just been inserted
func (taskRelEmbedded_EntityInfo) SetId(object interface{}, id uint64) error {
	object.(*TaskRelEmbedded).Id = id
	return nil
}

// PutRelated is called by ObjectBox to put related entities before the object itself is flattened and put
func (taskRelEmbedded_EntityInfo) PutRelated(ob *objectbox.ObjectBox, object interface{}, id uint64) error {
	if rel := object.(*TaskRelEmbedded).WithGroup.Group; rel != nil {
		if rId, err := GroupBinding.GetId(rel); err != nil {
			return err
		} else if rId == 0 {
			// NOTE Put/PutAsync() has a side-effect of setting the rel.ID
			if _, err := BoxForGroup(ob).Put(rel); err != nil {
				return err
			}
		}
	}
	if err := BoxForTaskRelEmbedded(ob).RelationReplace(TaskRelEmbedded_.GroupsNew, id, object, object.(*TaskRelEmbedded).WithGroup.GroupsNew); err != nil {
		return err
	}

	return nil
}

// Flatten is called by ObjectBox to transform an object to a FlatBuffer
func (taskRelEmbedded_EntityInfo) Flatten(object interface{}, fbb *flatbuffers.Builder, id uint64) error {
	obj := object.(*TaskRelEmbedded)

	var rIdGroup uint64
	if rel := obj.WithGroup.Group; rel != nil {
		if rId, err := GroupBinding.GetId(rel); err != nil {
			return err
		} else {
			rIdGroup = rId
		}
	}

	// build the FlatBuffers object
	fbb.StartObject(2)
	fbutils.SetUint64Slot(fbb, 0, id)
	if obj.WithGroup.Group != nil {
		fbutils.SetUint64Slot(fbb, 1, rIdGroup)
	}
	return nil
}

// Load is called by ObjectBox to load an object from a FlatBuffer
func (taskRelEmbedded_EntityInfo) Load(ob *objectbox.ObjectBox, bytes []byte) (interface{}, error) {
	if len(bytes) == 0 { // sanity check, should "never" happen
		return nil, errors.New("can't deserialize an object of type 'TaskRelEmbedded' - no data received")
	}

	var table = &flatbuffers.Table{
		Bytes: bytes,
		Pos:   flatbuffers.GetUOffsetT(bytes),
	}

	var propId = table.GetUint64Slot(4, 0)

	var relGroup *Group
	if rId := fbutils.GetUint64PtrSlot(table, 6); rId != nil && *rId > 0 {
		if rObject, err := BoxForGroup(ob).Get(*rId); err != nil {
			return nil, err
		} else {
			relGroup = rObject
		}
	}

	var relGroupsNew []*Group
	if rIds, err := BoxForTaskRelEmbedded(ob).RelationIds(TaskRelEmbedded_.GroupsNew, propId); err != nil {
		return nil, err
	} else if rSlice, err := BoxForGroup(ob).GetManyExisting(rIds...); err != nil {
		return nil, err
	} else {
		relGroupsNew = rSlice
	}

	return &TaskRelEmbedded{
		Id: propId,
		WithGroup: WithGroup{
			Group:     relGroup,
			GroupsNew: relGroupsNew,
		},
	}, nil
}

// MakeSlice is called by ObjectBox to construct a new slice to hold the read objects
func (taskRelEmbedded_EntityInfo) MakeSlice(capacity int) interface{} {
	return make([]*TaskRelEmbedded, 0, capacity)
}

// AppendToSlice is called by ObjectBox to fill the slice of the read objects
func (taskRelEmbedded_EntityInfo) AppendToSlice(slice interface{}, object interface{}) interface{} {
	if object == nil {
		return append(slice.([]*TaskRelEmbedded), nil)
	}
	return append(slice.([]*TaskRelEmbedded), object.(*TaskRelEmbedded))
}

// Box provides CRUD access to TaskRelEmbedded objects
type TaskRelEmbeddedBox struct {
	*objectbox.Box
}

// BoxForTaskRelEmbedded opens a box of TaskRelEmbedded objects
func BoxForTaskRelEmbedded(ob *objectbox.ObjectBox) *TaskRelEmbeddedBox {
	return &TaskRelEmbeddedBox{
		Box: ob.InternalBox(4),
	}
}

// Put synchronously inserts/updates a single object.
// In case the Id is not specified, it would be assigned automatically (auto-increment).
// When inserting, the TaskRelEmbedded.Id property on the passed object will be assigned the new ID as well.
func (box *TaskRelEmbeddedBox) Put(object *TaskRelEmbedded) (uint64, error) {
	return box.Box.Put(object)
}

// Insert synchronously inserts a single object. As opposed to Put, Insert will fail if given an ID that already exists.
// In case the Id is not specified, it would be assigned automatically (auto-increment).
// When inserting, the TaskRelEmbedded.Id property on the passed object will be assigned the new ID as well.
func (box *TaskRelEmbeddedBox) Insert(object *TaskRelEmbedded) (uint64, error) {
	return box.Box.Insert(object)
}

// Update synchronously updates a single object.
// As opposed to Put, Update will fail if an object with the same ID is not found in the database.
func (box *TaskRelEmbeddedBox) Update(object *TaskRelEmbedded) error {
	return box.Box.Update(object)
}

// PutAsync asynchronously inserts/updates a single object.
// Deprecated: use box.Async().Put() instead
func (box *TaskRelEmbeddedBox) PutAsync(object *TaskRelEmbedded) (uint64, error) {
	return box.Box.PutAsync(object)
}

// PutMany inserts multiple objects in single transaction.
// In case Ids are not set on the objects, they would be assigned automatically (auto-increment).
//
// Returns: IDs of the put objects (in the same order).
// When inserting, the TaskRelEmbedded.Id property on the objects in the slice will be assigned the new IDs as well.
//
// Note: In case an error occurs during the transaction, some of the objects may already have the TaskRelEmbedded.Id assigned
// even though the transaction has been rolled back and the objects are not stored under those IDs.
//
// Note: The slice may be empty or even nil; in both cases, an empty IDs slice and no error is returned.
func (box *TaskRelEmbeddedBox) PutMany(objects []*TaskRelEmbedded) ([]uint64, error) {
	return box.Box.PutMany(objects)
}

// Get reads a single object.
//
// Returns nil (and no error) in case the object with the given ID doesn't exist.
func (box *TaskRelEmbeddedBox) Get(id uint64) (*TaskRelEmbedded, error) {
	object, err := box.Box.Get(id)
	if err != nil {
		return nil, err
	} else if object == nil {
		return nil, nil
	}
	return object.(*TaskRelEmbedded), nil
}

// GetMany reads multiple objects at once.
// If any of the objects doesn't exist, its position in the return slice is nil
func (box *TaskRelEmbeddedBox) GetMany(ids ...uint64) ([]*TaskRelEmbedded, error) {
	objects, err := box.Box.GetMany(ids...)
	if err != nil {
		return nil, err
	}
	return objects.([]*TaskRelEmbedded), nil
}

// GetManyExisting reads multiple objects at once, skipping those that do not exist.
func (box *TaskRelEmbeddedBox) GetManyExisting(ids ...uint64) ([]*TaskRelEmbedded, error) {
	objects, err := box.Box.GetManyExisting(ids...)
	if err != nil {
		return nil, err
	}
	return objects.([]*TaskRelEmbedded), nil
}

// GetAll reads all stored objects
func (box *TaskRelEmbeddedBox) GetAll() ([]*TaskRelEmbedded, error) {
	objects, err := box.Box.GetAll()
	if err != nil {
		return nil, err
	}
	return objects.([]*TaskRelEmbedded), nil
}

// Remove deletes a single object
func (box *TaskRelEmbeddedBox) Remove(object *TaskRelEmbedded) error {
	return box.Box.Remove(object)
}

// RemoveMany deletes multiple objects at once.
// Returns the number of deleted object or error on failure.
// Note that this method will not fail if an object is not found (e.g. already removed).
// In case you need to strictly check whether all of the objects exist before removing them,
// you can execute multiple box.Contains() and box.Remove() inside a single write transaction.
func (box *TaskRelEmbeddedBox) RemoveMany(objects ...*TaskRelEmbedded) (uint64, error) {
	var ids = make([]uint64, len(objects))
	for k, object := range objects {
		ids[k] = object.Id
	}
	return box.Box.RemoveIds(ids...)
}

// Creates a query with the given conditions. Use the fields of the TaskRelEmbedded_ struct to create conditions.
// Keep the *TaskRelEmbeddedQuery if you intend to execute the query multiple times.
// Note: this function panics if you try to create illegal queries; e.g. use properties of an alien type.
// This is typically a programming error. Use QueryOrError instead if you want the explicit error check.
func (box *TaskRelEmbeddedBox) Query(conditions ...objectbox.Condition) *TaskRelEmbeddedQuery {
	return &TaskRelEmbeddedQuery{
		box.Box.Query(conditions...),
	}
}

// Creates a query with the given conditions. Use the fields of the TaskRelEmbedded_ struct to create conditions.
// Keep the *TaskRelEmbeddedQuery if you intend to execute the query multiple times.
func (box *TaskRelEmbeddedBox) QueryOrError(conditions ...objectbox.Condition) (*TaskRelEmbeddedQuery, error) {
	if query, err := box.Box.QueryOrError(conditions...); err != nil {
		return nil, err
	} else {
		return &TaskRelEmbeddedQuery{query}, nil
	}
}

// Async provides access to the default Async Box for asynchronous operations. See TaskRelEmbeddedAsyncBox for more information.
func (box *TaskRelEmbeddedBox) Async() *TaskRelEmbeddedAsyncBox {
	return &TaskRelEmbeddedAsyncBox{AsyncBox: box.Box.Async()}
}

// TaskRelEmbeddedAsyncBox provides asynchronous operations on TaskRelEmbedded objects.
//
// Asynchronous operations are executed on a separate internal thread for better performance.
//
// There are two main use cases:
//
// 1) "execute & forget:" you gain faster put/remove operations as you don't have to wait for the transaction to finish.
//
// 2) Many small transactions: if your write load is typically a lot of individual puts that happen in parallel,
// this will merge small transactions into bigger ones. This results in a significant gain in overall throughput.
//
// In situations with (extremely) high async load, an async method may be throttled (~1ms) or delayed up to 1 second.
// In the unlikely event that the object could still not be enqueued (full queue), an error will be returned.
//
// Note that async methods do not give you hard durability guarantees like the synchronous Box provides.
// There is a small time window in which the data may not have been committed durably yet.
type TaskRelEmbeddedAsyncBox struct {
	*objectbox.AsyncBox
}

// AsyncBoxForTaskRelEmbedded creates a new async box with the given operation timeout in case an async queue is full.
// The returned struct must be freed explicitly using the Close() method.
// It's usually preferable to use TaskRelEmbeddedBox::Async() which takes care of resource management and doesn't require closing.
func AsyncBoxForTaskRelEmbedded(ob *objectbox.ObjectBox, timeoutMs uint64) *TaskRelEmbeddedAsyncBox {
	var async, err = objectbox.NewAsyncBox(ob, 4, timeoutMs)
	if err != nil {
		panic("Could not create async box for entity ID 4: %s" + err.Error())
	}
	return &TaskRelEmbeddedAsyncBox{AsyncBox: async}
}

// Put inserts/updates a single object asynchronously.
// When inserting a new object, the Id property on the passed object will be assigned the new ID the entity would hold
// if the insert is ultimately successful. The newly assigned ID may not become valid if the insert fails.
func (asyncBox *TaskRelEmbeddedAsyncBox) Put(object *TaskRelEmbedded) (uint64, error) {
	return asyncBox.AsyncBox.Put(object)
}

// Insert a single object asynchronously.
// The Id property on the passed object will be assigned the new ID the entity would hold if the insert is ultimately
// successful. The newly assigned ID may not become valid if the insert fails.
// Fails silently if an object with the same ID already exists (this error is not returned).
func (asyncBox *TaskRelEmbeddedAsyncBox) Insert(object *TaskRelEmbedded) (id uint64, err error) {
	return asyncBox.AsyncBox.Insert(object)
}

// Update a single object asynchronously.
// The object must already exists or the update fails silently (without an error returned).
func (asyncBox *TaskRelEmbeddedAsyncBox) Update(object *TaskRelEmbedded) error {
	return asyncBox.AsyncBox.Update(object)
}

// Remove deletes a single object asynchronously.
func (asyncBox *TaskRelEmbeddedAsyncBox) Remove(object *TaskRelEmbedded) error {
	return asyncBox.AsyncBox.Remove(object)
}

// Query provides a way to search stored objects
//
// For example, you can find all TaskRelEmbedded which Id is either 42 or 47:
//
// box.Query(TaskRelEmbedded_.Id.In(42, 47)).Find()
type TaskRelEmbeddedQuery struct {
	*objectbox.Query
}

// Find returns all objects matching the query
func (query *TaskRelEmbeddedQuery) Find() ([]*TaskRelEmbedded, error) {
	objects, err := query.Query.Find()
	if err != nil {
		return nil, err
	}
	return objects.([]*TaskRelEmbedded), nil
}

// Offset defines the index of the first object to process (how many objects to skip)
func (query *TaskRelEmbeddedQuery) Offset(offset uint64) *TaskRelEmbeddedQuery {
	query.Query.Offset(offset)
	return query
}

// Limit sets the number of elements to process by the query
func (query *TaskRelEmbeddedQuery) Limit(limit uint64) *TaskRelEmbeddedQuery {
	query.Query.Limit(limit)
	return query
}
