// Code generated by BobGen mysql v0.42.0. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.

package models

import (
	"context"
	"fmt"
	"io"

	"github.com/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/stephenafamo/bob"
	"github.com/stephenafamo/bob/dialect/mysql"
	"github.com/stephenafamo/bob/dialect/mysql/dialect"
	"github.com/stephenafamo/bob/dialect/mysql/dm"
	"github.com/stephenafamo/bob/dialect/mysql/sm"
	"github.com/stephenafamo/bob/dialect/mysql/um"
	"github.com/stephenafamo/bob/expr"
	"github.com/stephenafamo/bob/mods"
	"github.com/stephenafamo/bob/orm"
)

// LocationsLocation is an object representing the database table.
type LocationsLocation struct {
	ID           int32            `db:"id,pk,autoincr" `
	UUID         string           `db:"uuid" `
	Purpose      string           `db:"purpose" `
	RelativePath string           `db:"relative_path" `
	Description  null.Val[string] `db:"description" `
	Quota        null.Val[int64]  `db:"quota" `
	Used         int64            `db:"used" `
	Enabled      bool             `db:"enabled" `
	SpaceID      string           `db:"space_id" `

	R locationsLocationR `db:"-" `
}

// LocationsLocationSlice is an alias for a slice of pointers to LocationsLocation.
// This should almost always be used instead of []*LocationsLocation.
type LocationsLocationSlice []*LocationsLocation

// LocationsLocations contains methods to work with the locations_location table
var LocationsLocations = mysql.NewTablex[*LocationsLocation, LocationsLocationSlice, *LocationsLocationSetter]("locations_location", buildLocationsLocationColumns("locations_location"), []string{"id"}, []string{"uuid"})

// LocationsLocationsQuery is a query on the locations_location table
type LocationsLocationsQuery = *mysql.ViewQuery[*LocationsLocation, LocationsLocationSlice]

// locationsLocationR is where relationships are stored.
type locationsLocationR struct {
	FromLocationLocationsLocationReplicators LocationsLocationReplicatorSlice // locations_location_r_from_location_id_3b9a55f5_fk_locations
	ToLocationLocationsLocationReplicators   LocationsLocationReplicatorSlice // locations_location_r_to_location_id_27be9af1_fk_locations
	SpaceLocationsSpace                      *LocationsSpace                  // locations_location_space_id_748845a7_fk_locations_space_uuid
	LocationLocationsLocationpipelines       LocationsLocationpipelineSlice   // locations_locationpi_location_id_1bff2f57_fk_locations
	CurrentLocationLocationsPackages         LocationsPackageSlice            // locations_package_current_location_id_c8f7daee_fk_locations
	PointerFileLocationLocationsPackages     LocationsPackageSlice            // locations_package_pointer_file_locatio_2adf5d3c_fk_locations
}

func buildLocationsLocationColumns(alias string) locationsLocationColumns {
	return locationsLocationColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "uuid", "purpose", "relative_path", "description", "quota", "used", "enabled", "space_id",
		).WithParent("locations_location"),
		tableAlias:   alias,
		ID:           mysql.Quote(alias, "id"),
		UUID:         mysql.Quote(alias, "uuid"),
		Purpose:      mysql.Quote(alias, "purpose"),
		RelativePath: mysql.Quote(alias, "relative_path"),
		Description:  mysql.Quote(alias, "description"),
		Quota:        mysql.Quote(alias, "quota"),
		Used:         mysql.Quote(alias, "used"),
		Enabled:      mysql.Quote(alias, "enabled"),
		SpaceID:      mysql.Quote(alias, "space_id"),
	}
}

type locationsLocationColumns struct {
	expr.ColumnsExpr
	tableAlias   string
	ID           mysql.Expression
	UUID         mysql.Expression
	Purpose      mysql.Expression
	RelativePath mysql.Expression
	Description  mysql.Expression
	Quota        mysql.Expression
	Used         mysql.Expression
	Enabled      mysql.Expression
	SpaceID      mysql.Expression
}

func (c locationsLocationColumns) Alias() string {
	return c.tableAlias
}

func (locationsLocationColumns) AliasedAs(alias string) locationsLocationColumns {
	return buildLocationsLocationColumns(alias)
}

// LocationsLocationSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type LocationsLocationSetter struct {
	ID           omit.Val[int32]      `db:"id,pk,autoincr" `
	UUID         omit.Val[string]     `db:"uuid" `
	Purpose      omit.Val[string]     `db:"purpose" `
	RelativePath omit.Val[string]     `db:"relative_path" `
	Description  omitnull.Val[string] `db:"description" `
	Quota        omitnull.Val[int64]  `db:"quota" `
	Used         omit.Val[int64]      `db:"used" `
	Enabled      omit.Val[bool]       `db:"enabled" `
	SpaceID      omit.Val[string]     `db:"space_id" `
}

func (s LocationsLocationSetter) SetColumns() []string {
	vals := make([]string, 0, 9)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.UUID.IsValue() {
		vals = append(vals, "uuid")
	}
	if s.Purpose.IsValue() {
		vals = append(vals, "purpose")
	}
	if s.RelativePath.IsValue() {
		vals = append(vals, "relative_path")
	}
	if !s.Description.IsUnset() {
		vals = append(vals, "description")
	}
	if !s.Quota.IsUnset() {
		vals = append(vals, "quota")
	}
	if s.Used.IsValue() {
		vals = append(vals, "used")
	}
	if s.Enabled.IsValue() {
		vals = append(vals, "enabled")
	}
	if s.SpaceID.IsValue() {
		vals = append(vals, "space_id")
	}
	return vals
}

func (s LocationsLocationSetter) Overwrite(t *LocationsLocation) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.UUID.IsValue() {
		t.UUID = s.UUID.MustGet()
	}
	if s.Purpose.IsValue() {
		t.Purpose = s.Purpose.MustGet()
	}
	if s.RelativePath.IsValue() {
		t.RelativePath = s.RelativePath.MustGet()
	}
	if !s.Description.IsUnset() {
		t.Description = s.Description.MustGetNull()
	}
	if !s.Quota.IsUnset() {
		t.Quota = s.Quota.MustGetNull()
	}
	if s.Used.IsValue() {
		t.Used = s.Used.MustGet()
	}
	if s.Enabled.IsValue() {
		t.Enabled = s.Enabled.MustGet()
	}
	if s.SpaceID.IsValue() {
		t.SpaceID = s.SpaceID.MustGet()
	}
}

func (s *LocationsLocationSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return LocationsLocations.BeforeInsertHooks.RunHooks(ctx, exec, s)
	})

	q.AppendValues(
		bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.ID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.ID.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.UUID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.UUID.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.Purpose.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Purpose.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.RelativePath.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.RelativePath.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(!s.Description.IsUnset()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Description.MustGetNull()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(!s.Quota.IsUnset()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Quota.MustGetNull()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.Used.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Used.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.Enabled.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Enabled.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.SpaceID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.SpaceID.MustGet()).WriteSQL(ctx, w, d, start)
		}))
}

func (s LocationsLocationSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions("locations_location")...)
}

func (s LocationsLocationSetter) Expressions(prefix ...string) []bob.Expression {
	exprs := make([]bob.Expression, 0, 9)

	if s.ID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "id")...),
			mysql.Arg(s.ID),
		}})
	}

	if s.UUID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "uuid")...),
			mysql.Arg(s.UUID),
		}})
	}

	if s.Purpose.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "purpose")...),
			mysql.Arg(s.Purpose),
		}})
	}

	if s.RelativePath.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "relative_path")...),
			mysql.Arg(s.RelativePath),
		}})
	}

	if !s.Description.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "description")...),
			mysql.Arg(s.Description),
		}})
	}

	if !s.Quota.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "quota")...),
			mysql.Arg(s.Quota),
		}})
	}

	if s.Used.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "used")...),
			mysql.Arg(s.Used),
		}})
	}

	if s.Enabled.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "enabled")...),
			mysql.Arg(s.Enabled),
		}})
	}

	if s.SpaceID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "space_id")...),
			mysql.Arg(s.SpaceID),
		}})
	}

	return exprs
}

// FindLocationsLocation retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindLocationsLocation(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*LocationsLocation, error) {
	if len(cols) == 0 {
		return LocationsLocations.Query(
			sm.Where(LocationsLocations.Columns.ID.EQ(mysql.Arg(IDPK))),
		).One(ctx, exec)
	}

	return LocationsLocations.Query(
		sm.Where(LocationsLocations.Columns.ID.EQ(mysql.Arg(IDPK))),
		sm.Columns(LocationsLocations.Columns.Only(cols...)),
	).One(ctx, exec)
}

// LocationsLocationExists checks the presence of a single record by primary key
func LocationsLocationExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
	return LocationsLocations.Query(
		sm.Where(LocationsLocations.Columns.ID.EQ(mysql.Arg(IDPK))),
	).Exists(ctx, exec)
}

// AfterQueryHook is called after LocationsLocation is retrieved from the database
func (o *LocationsLocation) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = LocationsLocations.AfterSelectHooks.RunHooks(ctx, exec, LocationsLocationSlice{o})
	case bob.QueryTypeInsert:
		ctx, err = LocationsLocations.AfterInsertHooks.RunHooks(ctx, exec, LocationsLocationSlice{o})
	case bob.QueryTypeUpdate:
		ctx, err = LocationsLocations.AfterUpdateHooks.RunHooks(ctx, exec, LocationsLocationSlice{o})
	case bob.QueryTypeDelete:
		ctx, err = LocationsLocations.AfterDeleteHooks.RunHooks(ctx, exec, LocationsLocationSlice{o})
	}

	return err
}

// primaryKeyVals returns the primary key values of the LocationsLocation
func (o *LocationsLocation) primaryKeyVals() bob.Expression {
	return mysql.Arg(o.ID)
}

func (o *LocationsLocation) pkEQ() dialect.Expression {
	return mysql.Quote("locations_location", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
	}))
}

// Update uses an executor to update the LocationsLocation
func (o *LocationsLocation) Update(ctx context.Context, exec bob.Executor, s *LocationsLocationSetter) error {
	_, err := LocationsLocations.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec)
	if err != nil {
		return err
	}

	s.Overwrite(o)

	return nil
}

// Delete deletes a single LocationsLocation record with an executor
func (o *LocationsLocation) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := LocationsLocations.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

// Reload refreshes the LocationsLocation using the executor
func (o *LocationsLocation) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := LocationsLocations.Query(
		sm.Where(LocationsLocations.Columns.ID.EQ(mysql.Arg(o.ID))),
	).One(ctx, exec)
	if err != nil {
		return err
	}
	o2.R = o.R
	*o = *o2

	return nil
}

// AfterQueryHook is called after LocationsLocationSlice is retrieved from the database
func (o LocationsLocationSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = LocationsLocations.AfterSelectHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeInsert:
		ctx, err = LocationsLocations.AfterInsertHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeUpdate:
		ctx, err = LocationsLocations.AfterUpdateHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeDelete:
		ctx, err = LocationsLocations.AfterDeleteHooks.RunHooks(ctx, exec, o)
	}

	return err
}

func (o LocationsLocationSlice) pkIN() dialect.Expression {
	if len(o) == 0 {
		return mysql.Raw("NULL")
	}

	return mysql.Quote("locations_location", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		pkPairs := make([]bob.Expression, len(o))
		for i, row := range o {
			pkPairs[i] = row.primaryKeyVals()
		}
		return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
	}))
}

// copyMatchingRows finds models in the given slice that have the same primary key
// then it first copies the existing relationships from the old model to the new model
// and then replaces the old model in the slice with the new model
func (o LocationsLocationSlice) copyMatchingRows(from ...*LocationsLocation) {
	for i, old := range o {
		for _, new := range from {
			if new.ID != old.ID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o LocationsLocationSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return LocationsLocations.BeforeUpdateHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *LocationsLocation:
				o.copyMatchingRows(retrieved)
			case []*LocationsLocation:
				o.copyMatchingRows(retrieved...)
			case LocationsLocationSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a LocationsLocation or a slice of LocationsLocation
				// then run the AfterUpdateHooks on the slice
				_, err = LocationsLocations.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o LocationsLocationSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
	return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return LocationsLocations.BeforeDeleteHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *LocationsLocation:
				o.copyMatchingRows(retrieved)
			case []*LocationsLocation:
				o.copyMatchingRows(retrieved...)
			case LocationsLocationSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a LocationsLocation or a slice of LocationsLocation
				// then run the AfterDeleteHooks on the slice
				_, err = LocationsLocations.AfterDeleteHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

func (o LocationsLocationSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsLocationSetter) error {
	_, err := LocationsLocations.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

	for i := range o {
		vals.Overwrite(o[i])
	}

	return err
}

func (o LocationsLocationSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	_, err := LocationsLocations.Delete(o.DeleteMod()).Exec(ctx, exec)
	return err
}

func (o LocationsLocationSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	o2, err := LocationsLocations.Query(sm.Where(o.pkIN())).All(ctx, exec)
	if err != nil {
		return err
	}

	o.copyMatchingRows(o2...)

	return nil
}

// FromLocationLocationsLocationReplicators starts a query for related objects on locations_location_replicators
func (o *LocationsLocation) FromLocationLocationsLocationReplicators(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationReplicatorsQuery {
	return LocationsLocationReplicators.Query(append(mods,
		sm.Where(LocationsLocationReplicators.Columns.FromLocationID.EQ(mysql.Arg(o.ID))),
	)...)
}

func (os LocationsLocationSlice) FromLocationLocationsLocationReplicators(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationReplicatorsQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.ID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return LocationsLocationReplicators.Query(append(mods,
		sm.Where(mysql.Group(LocationsLocationReplicators.Columns.FromLocationID).OP("IN", PKArgExpr)),
	)...)
}

// ToLocationLocationsLocationReplicators starts a query for related objects on locations_location_replicators
func (o *LocationsLocation) ToLocationLocationsLocationReplicators(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationReplicatorsQuery {
	return LocationsLocationReplicators.Query(append(mods,
		sm.Where(LocationsLocationReplicators.Columns.ToLocationID.EQ(mysql.Arg(o.ID))),
	)...)
}

func (os LocationsLocationSlice) ToLocationLocationsLocationReplicators(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationReplicatorsQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.ID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return LocationsLocationReplicators.Query(append(mods,
		sm.Where(mysql.Group(LocationsLocationReplicators.Columns.ToLocationID).OP("IN", PKArgExpr)),
	)...)
}

// SpaceLocationsSpace starts a query for related objects on locations_space
func (o *LocationsLocation) SpaceLocationsSpace(mods ...bob.Mod[*dialect.SelectQuery]) LocationsSpacesQuery {
	return LocationsSpaces.Query(append(mods,
		sm.Where(LocationsSpaces.Columns.UUID.EQ(mysql.Arg(o.SpaceID))),
	)...)
}

func (os LocationsLocationSlice) SpaceLocationsSpace(mods ...bob.Mod[*dialect.SelectQuery]) LocationsSpacesQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.SpaceID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return LocationsSpaces.Query(append(mods,
		sm.Where(mysql.Group(LocationsSpaces.Columns.UUID).OP("IN", PKArgExpr)),
	)...)
}

// LocationLocationsLocationpipelines starts a query for related objects on locations_locationpipeline
func (o *LocationsLocation) LocationLocationsLocationpipelines(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationpipelinesQuery {
	return LocationsLocationpipelines.Query(append(mods,
		sm.Where(LocationsLocationpipelines.Columns.LocationID.EQ(mysql.Arg(o.UUID))),
	)...)
}

func (os LocationsLocationSlice) LocationLocationsLocationpipelines(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationpipelinesQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.UUID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return LocationsLocationpipelines.Query(append(mods,
		sm.Where(mysql.Group(LocationsLocationpipelines.Columns.LocationID).OP("IN", PKArgExpr)),
	)...)
}

// CurrentLocationLocationsPackages starts a query for related objects on locations_package
func (o *LocationsLocation) CurrentLocationLocationsPackages(mods ...bob.Mod[*dialect.SelectQuery]) LocationsPackagesQuery {
	return LocationsPackages.Query(append(mods,
		sm.Where(LocationsPackages.Columns.CurrentLocationID.EQ(mysql.Arg(o.UUID))),
	)...)
}

func (os LocationsLocationSlice) CurrentLocationLocationsPackages(mods ...bob.Mod[*dialect.SelectQuery]) LocationsPackagesQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.UUID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return LocationsPackages.Query(append(mods,
		sm.Where(mysql.Group(LocationsPackages.Columns.CurrentLocationID).OP("IN", PKArgExpr)),
	)...)
}

// PointerFileLocationLocationsPackages starts a query for related objects on locations_package
func (o *LocationsLocation) PointerFileLocationLocationsPackages(mods ...bob.Mod[*dialect.SelectQuery]) LocationsPackagesQuery {
	return LocationsPackages.Query(append(mods,
		sm.Where(LocationsPackages.Columns.PointerFileLocationID.EQ(mysql.Arg(o.UUID))),
	)...)
}

func (os LocationsLocationSlice) PointerFileLocationLocationsPackages(mods ...bob.Mod[*dialect.SelectQuery]) LocationsPackagesQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.UUID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return LocationsPackages.Query(append(mods,
		sm.Where(mysql.Group(LocationsPackages.Columns.PointerFileLocationID).OP("IN", PKArgExpr)),
	)...)
}

func insertLocationsLocationFromLocationLocationsLocationReplicators0(ctx context.Context, exec bob.Executor, locationsLocationReplicators1 []*LocationsLocationReplicatorSetter, locationsLocation0 *LocationsLocation) (LocationsLocationReplicatorSlice, error) {
	for i := range locationsLocationReplicators1 {
		locationsLocationReplicators1[i].FromLocationID = omit.From(locationsLocation0.ID)
	}

	ret, err := LocationsLocationReplicators.Insert(bob.ToMods(locationsLocationReplicators1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertLocationsLocationFromLocationLocationsLocationReplicators0: %w", err)
	}

	return ret, nil
}

func attachLocationsLocationFromLocationLocationsLocationReplicators0(ctx context.Context, exec bob.Executor, count int, locationsLocationReplicators1 LocationsLocationReplicatorSlice, locationsLocation0 *LocationsLocation) (LocationsLocationReplicatorSlice, error) {
	setter := &LocationsLocationReplicatorSetter{
		FromLocationID: omit.From(locationsLocation0.ID),
	}

	err := locationsLocationReplicators1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachLocationsLocationFromLocationLocationsLocationReplicators0: %w", err)
	}

	return locationsLocationReplicators1, nil
}

func (locationsLocation0 *LocationsLocation) InsertFromLocationLocationsLocationReplicators(ctx context.Context, exec bob.Executor, related ...*LocationsLocationReplicatorSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	locationsLocationReplicators1, err := insertLocationsLocationFromLocationLocationsLocationReplicators0(ctx, exec, related, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.FromLocationLocationsLocationReplicators = append(locationsLocation0.R.FromLocationLocationsLocationReplicators, locationsLocationReplicators1...)

	for _, rel := range locationsLocationReplicators1 {
		rel.R.FromLocationLocationsLocation = locationsLocation0
	}
	return nil
}

func (locationsLocation0 *LocationsLocation) AttachFromLocationLocationsLocationReplicators(ctx context.Context, exec bob.Executor, related ...*LocationsLocationReplicator) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	locationsLocationReplicators1 := LocationsLocationReplicatorSlice(related)

	_, err = attachLocationsLocationFromLocationLocationsLocationReplicators0(ctx, exec, len(related), locationsLocationReplicators1, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.FromLocationLocationsLocationReplicators = append(locationsLocation0.R.FromLocationLocationsLocationReplicators, locationsLocationReplicators1...)

	for _, rel := range related {
		rel.R.FromLocationLocationsLocation = locationsLocation0
	}

	return nil
}

func insertLocationsLocationToLocationLocationsLocationReplicators0(ctx context.Context, exec bob.Executor, locationsLocationReplicators1 []*LocationsLocationReplicatorSetter, locationsLocation0 *LocationsLocation) (LocationsLocationReplicatorSlice, error) {
	for i := range locationsLocationReplicators1 {
		locationsLocationReplicators1[i].ToLocationID = omit.From(locationsLocation0.ID)
	}

	ret, err := LocationsLocationReplicators.Insert(bob.ToMods(locationsLocationReplicators1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertLocationsLocationToLocationLocationsLocationReplicators0: %w", err)
	}

	return ret, nil
}

func attachLocationsLocationToLocationLocationsLocationReplicators0(ctx context.Context, exec bob.Executor, count int, locationsLocationReplicators1 LocationsLocationReplicatorSlice, locationsLocation0 *LocationsLocation) (LocationsLocationReplicatorSlice, error) {
	setter := &LocationsLocationReplicatorSetter{
		ToLocationID: omit.From(locationsLocation0.ID),
	}

	err := locationsLocationReplicators1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachLocationsLocationToLocationLocationsLocationReplicators0: %w", err)
	}

	return locationsLocationReplicators1, nil
}

func (locationsLocation0 *LocationsLocation) InsertToLocationLocationsLocationReplicators(ctx context.Context, exec bob.Executor, related ...*LocationsLocationReplicatorSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	locationsLocationReplicators1, err := insertLocationsLocationToLocationLocationsLocationReplicators0(ctx, exec, related, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.ToLocationLocationsLocationReplicators = append(locationsLocation0.R.ToLocationLocationsLocationReplicators, locationsLocationReplicators1...)

	for _, rel := range locationsLocationReplicators1 {
		rel.R.ToLocationLocationsLocation = locationsLocation0
	}
	return nil
}

func (locationsLocation0 *LocationsLocation) AttachToLocationLocationsLocationReplicators(ctx context.Context, exec bob.Executor, related ...*LocationsLocationReplicator) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	locationsLocationReplicators1 := LocationsLocationReplicatorSlice(related)

	_, err = attachLocationsLocationToLocationLocationsLocationReplicators0(ctx, exec, len(related), locationsLocationReplicators1, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.ToLocationLocationsLocationReplicators = append(locationsLocation0.R.ToLocationLocationsLocationReplicators, locationsLocationReplicators1...)

	for _, rel := range related {
		rel.R.ToLocationLocationsLocation = locationsLocation0
	}

	return nil
}

func attachLocationsLocationSpaceLocationsSpace0(ctx context.Context, exec bob.Executor, count int, locationsLocation0 *LocationsLocation, locationsSpace1 *LocationsSpace) (*LocationsLocation, error) {
	setter := &LocationsLocationSetter{
		SpaceID: omit.From(locationsSpace1.UUID),
	}

	err := locationsLocation0.Update(ctx, exec, setter)
	if err != nil {
		return nil, fmt.Errorf("attachLocationsLocationSpaceLocationsSpace0: %w", err)
	}

	return locationsLocation0, nil
}

func (locationsLocation0 *LocationsLocation) InsertSpaceLocationsSpace(ctx context.Context, exec bob.Executor, related *LocationsSpaceSetter) error {
	var err error

	locationsSpace1, err := LocationsSpaces.Insert(related).One(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}

	_, err = attachLocationsLocationSpaceLocationsSpace0(ctx, exec, 1, locationsLocation0, locationsSpace1)
	if err != nil {
		return err
	}

	locationsLocation0.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsLocations = append(locationsSpace1.R.SpaceLocationsLocations, locationsLocation0)

	return nil
}

func (locationsLocation0 *LocationsLocation) AttachSpaceLocationsSpace(ctx context.Context, exec bob.Executor, locationsSpace1 *LocationsSpace) error {
	var err error

	_, err = attachLocationsLocationSpaceLocationsSpace0(ctx, exec, 1, locationsLocation0, locationsSpace1)
	if err != nil {
		return err
	}

	locationsLocation0.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsLocations = append(locationsSpace1.R.SpaceLocationsLocations, locationsLocation0)

	return nil
}

func insertLocationsLocationLocationLocationsLocationpipelines0(ctx context.Context, exec bob.Executor, locationsLocationpipelines1 []*LocationsLocationpipelineSetter, locationsLocation0 *LocationsLocation) (LocationsLocationpipelineSlice, error) {
	for i := range locationsLocationpipelines1 {
		locationsLocationpipelines1[i].LocationID = omit.From(locationsLocation0.UUID)
	}

	ret, err := LocationsLocationpipelines.Insert(bob.ToMods(locationsLocationpipelines1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertLocationsLocationLocationLocationsLocationpipelines0: %w", err)
	}

	return ret, nil
}

func attachLocationsLocationLocationLocationsLocationpipelines0(ctx context.Context, exec bob.Executor, count int, locationsLocationpipelines1 LocationsLocationpipelineSlice, locationsLocation0 *LocationsLocation) (LocationsLocationpipelineSlice, error) {
	setter := &LocationsLocationpipelineSetter{
		LocationID: omit.From(locationsLocation0.UUID),
	}

	err := locationsLocationpipelines1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachLocationsLocationLocationLocationsLocationpipelines0: %w", err)
	}

	return locationsLocationpipelines1, nil
}

func (locationsLocation0 *LocationsLocation) InsertLocationLocationsLocationpipelines(ctx context.Context, exec bob.Executor, related ...*LocationsLocationpipelineSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	locationsLocationpipelines1, err := insertLocationsLocationLocationLocationsLocationpipelines0(ctx, exec, related, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.LocationLocationsLocationpipelines = append(locationsLocation0.R.LocationLocationsLocationpipelines, locationsLocationpipelines1...)

	for _, rel := range locationsLocationpipelines1 {
		rel.R.LocationLocationsLocation = locationsLocation0
	}
	return nil
}

func (locationsLocation0 *LocationsLocation) AttachLocationLocationsLocationpipelines(ctx context.Context, exec bob.Executor, related ...*LocationsLocationpipeline) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	locationsLocationpipelines1 := LocationsLocationpipelineSlice(related)

	_, err = attachLocationsLocationLocationLocationsLocationpipelines0(ctx, exec, len(related), locationsLocationpipelines1, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.LocationLocationsLocationpipelines = append(locationsLocation0.R.LocationLocationsLocationpipelines, locationsLocationpipelines1...)

	for _, rel := range related {
		rel.R.LocationLocationsLocation = locationsLocation0
	}

	return nil
}

func insertLocationsLocationCurrentLocationLocationsPackages0(ctx context.Context, exec bob.Executor, locationsPackages1 []*LocationsPackageSetter, locationsLocation0 *LocationsLocation) (LocationsPackageSlice, error) {
	for i := range locationsPackages1 {
		locationsPackages1[i].CurrentLocationID = omit.From(locationsLocation0.UUID)
	}

	ret, err := LocationsPackages.Insert(bob.ToMods(locationsPackages1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertLocationsLocationCurrentLocationLocationsPackages0: %w", err)
	}

	return ret, nil
}

func attachLocationsLocationCurrentLocationLocationsPackages0(ctx context.Context, exec bob.Executor, count int, locationsPackages1 LocationsPackageSlice, locationsLocation0 *LocationsLocation) (LocationsPackageSlice, error) {
	setter := &LocationsPackageSetter{
		CurrentLocationID: omit.From(locationsLocation0.UUID),
	}

	err := locationsPackages1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachLocationsLocationCurrentLocationLocationsPackages0: %w", err)
	}

	return locationsPackages1, nil
}

func (locationsLocation0 *LocationsLocation) InsertCurrentLocationLocationsPackages(ctx context.Context, exec bob.Executor, related ...*LocationsPackageSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	locationsPackages1, err := insertLocationsLocationCurrentLocationLocationsPackages0(ctx, exec, related, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.CurrentLocationLocationsPackages = append(locationsLocation0.R.CurrentLocationLocationsPackages, locationsPackages1...)

	for _, rel := range locationsPackages1 {
		rel.R.CurrentLocationLocationsLocation = locationsLocation0
	}
	return nil
}

func (locationsLocation0 *LocationsLocation) AttachCurrentLocationLocationsPackages(ctx context.Context, exec bob.Executor, related ...*LocationsPackage) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	locationsPackages1 := LocationsPackageSlice(related)

	_, err = attachLocationsLocationCurrentLocationLocationsPackages0(ctx, exec, len(related), locationsPackages1, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.CurrentLocationLocationsPackages = append(locationsLocation0.R.CurrentLocationLocationsPackages, locationsPackages1...)

	for _, rel := range related {
		rel.R.CurrentLocationLocationsLocation = locationsLocation0
	}

	return nil
}

func insertLocationsLocationPointerFileLocationLocationsPackages0(ctx context.Context, exec bob.Executor, locationsPackages1 []*LocationsPackageSetter, locationsLocation0 *LocationsLocation) (LocationsPackageSlice, error) {
	for i := range locationsPackages1 {
		locationsPackages1[i].PointerFileLocationID = omitnull.From(locationsLocation0.UUID)
	}

	ret, err := LocationsPackages.Insert(bob.ToMods(locationsPackages1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertLocationsLocationPointerFileLocationLocationsPackages0: %w", err)
	}

	return ret, nil
}

func attachLocationsLocationPointerFileLocationLocationsPackages0(ctx context.Context, exec bob.Executor, count int, locationsPackages1 LocationsPackageSlice, locationsLocation0 *LocationsLocation) (LocationsPackageSlice, error) {
	setter := &LocationsPackageSetter{
		PointerFileLocationID: omitnull.From(locationsLocation0.UUID),
	}

	err := locationsPackages1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachLocationsLocationPointerFileLocationLocationsPackages0: %w", err)
	}

	return locationsPackages1, nil
}

func (locationsLocation0 *LocationsLocation) InsertPointerFileLocationLocationsPackages(ctx context.Context, exec bob.Executor, related ...*LocationsPackageSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	locationsPackages1, err := insertLocationsLocationPointerFileLocationLocationsPackages0(ctx, exec, related, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.PointerFileLocationLocationsPackages = append(locationsLocation0.R.PointerFileLocationLocationsPackages, locationsPackages1...)

	for _, rel := range locationsPackages1 {
		rel.R.PointerFileLocationLocationsLocation = locationsLocation0
	}
	return nil
}

func (locationsLocation0 *LocationsLocation) AttachPointerFileLocationLocationsPackages(ctx context.Context, exec bob.Executor, related ...*LocationsPackage) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	locationsPackages1 := LocationsPackageSlice(related)

	_, err = attachLocationsLocationPointerFileLocationLocationsPackages0(ctx, exec, len(related), locationsPackages1, locationsLocation0)
	if err != nil {
		return err
	}

	locationsLocation0.R.PointerFileLocationLocationsPackages = append(locationsLocation0.R.PointerFileLocationLocationsPackages, locationsPackages1...)

	for _, rel := range related {
		rel.R.PointerFileLocationLocationsLocation = locationsLocation0
	}

	return nil
}

type locationsLocationWhere[Q mysql.Filterable] struct {
	ID           mysql.WhereMod[Q, int32]
	UUID         mysql.WhereMod[Q, string]
	Purpose      mysql.WhereMod[Q, string]
	RelativePath mysql.WhereMod[Q, string]
	Description  mysql.WhereNullMod[Q, string]
	Quota        mysql.WhereNullMod[Q, int64]
	Used         mysql.WhereMod[Q, int64]
	Enabled      mysql.WhereMod[Q, bool]
	SpaceID      mysql.WhereMod[Q, string]
}

func (locationsLocationWhere[Q]) AliasedAs(alias string) locationsLocationWhere[Q] {
	return buildLocationsLocationWhere[Q](buildLocationsLocationColumns(alias))
}

func buildLocationsLocationWhere[Q mysql.Filterable](cols locationsLocationColumns) locationsLocationWhere[Q] {
	return locationsLocationWhere[Q]{
		ID:           mysql.Where[Q, int32](cols.ID),
		UUID:         mysql.Where[Q, string](cols.UUID),
		Purpose:      mysql.Where[Q, string](cols.Purpose),
		RelativePath: mysql.Where[Q, string](cols.RelativePath),
		Description:  mysql.WhereNull[Q, string](cols.Description),
		Quota:        mysql.WhereNull[Q, int64](cols.Quota),
		Used:         mysql.Where[Q, int64](cols.Used),
		Enabled:      mysql.Where[Q, bool](cols.Enabled),
		SpaceID:      mysql.Where[Q, string](cols.SpaceID),
	}
}

func (o *LocationsLocation) Preload(name string, retrieved any) error {
	if o == nil {
		return nil
	}

	switch name {
	case "FromLocationLocationsLocationReplicators":
		rels, ok := retrieved.(LocationsLocationReplicatorSlice)
		if !ok {
			return fmt.Errorf("locationsLocation cannot load %T as %q", retrieved, name)
		}

		o.R.FromLocationLocationsLocationReplicators = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.FromLocationLocationsLocation = o
			}
		}
		return nil
	case "ToLocationLocationsLocationReplicators":
		rels, ok := retrieved.(LocationsLocationReplicatorSlice)
		if !ok {
			return fmt.Errorf("locationsLocation cannot load %T as %q", retrieved, name)
		}

		o.R.ToLocationLocationsLocationReplicators = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.ToLocationLocationsLocation = o
			}
		}
		return nil
	case "SpaceLocationsSpace":
		rel, ok := retrieved.(*LocationsSpace)
		if !ok {
			return fmt.Errorf("locationsLocation cannot load %T as %q", retrieved, name)
		}

		o.R.SpaceLocationsSpace = rel

		if rel != nil {
			rel.R.SpaceLocationsLocations = LocationsLocationSlice{o}
		}
		return nil
	case "LocationLocationsLocationpipelines":
		rels, ok := retrieved.(LocationsLocationpipelineSlice)
		if !ok {
			return fmt.Errorf("locationsLocation cannot load %T as %q", retrieved, name)
		}

		o.R.LocationLocationsLocationpipelines = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.LocationLocationsLocation = o
			}
		}
		return nil
	case "CurrentLocationLocationsPackages":
		rels, ok := retrieved.(LocationsPackageSlice)
		if !ok {
			return fmt.Errorf("locationsLocation cannot load %T as %q", retrieved, name)
		}

		o.R.CurrentLocationLocationsPackages = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CurrentLocationLocationsLocation = o
			}
		}
		return nil
	case "PointerFileLocationLocationsPackages":
		rels, ok := retrieved.(LocationsPackageSlice)
		if !ok {
			return fmt.Errorf("locationsLocation cannot load %T as %q", retrieved, name)
		}

		o.R.PointerFileLocationLocationsPackages = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.PointerFileLocationLocationsLocation = o
			}
		}
		return nil
	default:
		return fmt.Errorf("locationsLocation has no relationship %q", name)
	}
}

type locationsLocationPreloader struct {
	SpaceLocationsSpace func(...mysql.PreloadOption) mysql.Preloader
}

func buildLocationsLocationPreloader() locationsLocationPreloader {
	return locationsLocationPreloader{
		SpaceLocationsSpace: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*LocationsSpace, LocationsSpaceSlice](mysql.PreloadRel{
				Name: "SpaceLocationsSpace",
				Sides: []mysql.PreloadSide{
					{
						From:        LocationsLocations,
						To:          LocationsSpaces,
						FromColumns: []string{"space_id"},
						ToColumns:   []string{"uuid"},
					},
				},
			}, LocationsSpaces.Columns.Names(), opts...)
		},
	}
}

type locationsLocationThenLoader[Q orm.Loadable] struct {
	FromLocationLocationsLocationReplicators func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	ToLocationLocationsLocationReplicators   func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	SpaceLocationsSpace                      func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	LocationLocationsLocationpipelines       func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CurrentLocationLocationsPackages         func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	PointerFileLocationLocationsPackages     func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildLocationsLocationThenLoader[Q orm.Loadable]() locationsLocationThenLoader[Q] {
	type FromLocationLocationsLocationReplicatorsLoadInterface interface {
		LoadFromLocationLocationsLocationReplicators(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ToLocationLocationsLocationReplicatorsLoadInterface interface {
		LoadToLocationLocationsLocationReplicators(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type SpaceLocationsSpaceLoadInterface interface {
		LoadSpaceLocationsSpace(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type LocationLocationsLocationpipelinesLoadInterface interface {
		LoadLocationLocationsLocationpipelines(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CurrentLocationLocationsPackagesLoadInterface interface {
		LoadCurrentLocationLocationsPackages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PointerFileLocationLocationsPackagesLoadInterface interface {
		LoadPointerFileLocationLocationsPackages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return locationsLocationThenLoader[Q]{
		FromLocationLocationsLocationReplicators: thenLoadBuilder[Q](
			"FromLocationLocationsLocationReplicators",
			func(ctx context.Context, exec bob.Executor, retrieved FromLocationLocationsLocationReplicatorsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadFromLocationLocationsLocationReplicators(ctx, exec, mods...)
			},
		),
		ToLocationLocationsLocationReplicators: thenLoadBuilder[Q](
			"ToLocationLocationsLocationReplicators",
			func(ctx context.Context, exec bob.Executor, retrieved ToLocationLocationsLocationReplicatorsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadToLocationLocationsLocationReplicators(ctx, exec, mods...)
			},
		),
		SpaceLocationsSpace: thenLoadBuilder[Q](
			"SpaceLocationsSpace",
			func(ctx context.Context, exec bob.Executor, retrieved SpaceLocationsSpaceLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadSpaceLocationsSpace(ctx, exec, mods...)
			},
		),
		LocationLocationsLocationpipelines: thenLoadBuilder[Q](
			"LocationLocationsLocationpipelines",
			func(ctx context.Context, exec bob.Executor, retrieved LocationLocationsLocationpipelinesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadLocationLocationsLocationpipelines(ctx, exec, mods...)
			},
		),
		CurrentLocationLocationsPackages: thenLoadBuilder[Q](
			"CurrentLocationLocationsPackages",
			func(ctx context.Context, exec bob.Executor, retrieved CurrentLocationLocationsPackagesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCurrentLocationLocationsPackages(ctx, exec, mods...)
			},
		),
		PointerFileLocationLocationsPackages: thenLoadBuilder[Q](
			"PointerFileLocationLocationsPackages",
			func(ctx context.Context, exec bob.Executor, retrieved PointerFileLocationLocationsPackagesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadPointerFileLocationLocationsPackages(ctx, exec, mods...)
			},
		),
	}
}

// LoadFromLocationLocationsLocationReplicators loads the locationsLocation's FromLocationLocationsLocationReplicators into the .R struct
func (o *LocationsLocation) LoadFromLocationLocationsLocationReplicators(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.FromLocationLocationsLocationReplicators = nil

	related, err := o.FromLocationLocationsLocationReplicators(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.FromLocationLocationsLocation = o
	}

	o.R.FromLocationLocationsLocationReplicators = related
	return nil
}

// LoadFromLocationLocationsLocationReplicators loads the locationsLocation's FromLocationLocationsLocationReplicators into the .R struct
func (os LocationsLocationSlice) LoadFromLocationLocationsLocationReplicators(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	locationsLocationReplicators, err := os.FromLocationLocationsLocationReplicators(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.FromLocationLocationsLocationReplicators = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range locationsLocationReplicators {

			if !(o.ID == rel.FromLocationID) {
				continue
			}

			rel.R.FromLocationLocationsLocation = o

			o.R.FromLocationLocationsLocationReplicators = append(o.R.FromLocationLocationsLocationReplicators, rel)
		}
	}

	return nil
}

// LoadToLocationLocationsLocationReplicators loads the locationsLocation's ToLocationLocationsLocationReplicators into the .R struct
func (o *LocationsLocation) LoadToLocationLocationsLocationReplicators(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.ToLocationLocationsLocationReplicators = nil

	related, err := o.ToLocationLocationsLocationReplicators(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.ToLocationLocationsLocation = o
	}

	o.R.ToLocationLocationsLocationReplicators = related
	return nil
}

// LoadToLocationLocationsLocationReplicators loads the locationsLocation's ToLocationLocationsLocationReplicators into the .R struct
func (os LocationsLocationSlice) LoadToLocationLocationsLocationReplicators(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	locationsLocationReplicators, err := os.ToLocationLocationsLocationReplicators(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.ToLocationLocationsLocationReplicators = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range locationsLocationReplicators {

			if !(o.ID == rel.ToLocationID) {
				continue
			}

			rel.R.ToLocationLocationsLocation = o

			o.R.ToLocationLocationsLocationReplicators = append(o.R.ToLocationLocationsLocationReplicators, rel)
		}
	}

	return nil
}

// LoadSpaceLocationsSpace loads the locationsLocation's SpaceLocationsSpace into the .R struct
func (o *LocationsLocation) LoadSpaceLocationsSpace(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.SpaceLocationsSpace = nil

	related, err := o.SpaceLocationsSpace(mods...).One(ctx, exec)
	if err != nil {
		return err
	}

	related.R.SpaceLocationsLocations = LocationsLocationSlice{o}

	o.R.SpaceLocationsSpace = related
	return nil
}

// LoadSpaceLocationsSpace loads the locationsLocation's SpaceLocationsSpace into the .R struct
func (os LocationsLocationSlice) LoadSpaceLocationsSpace(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	locationsSpaces, err := os.SpaceLocationsSpace(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range locationsSpaces {

			if !(o.SpaceID == rel.UUID) {
				continue
			}

			rel.R.SpaceLocationsLocations = append(rel.R.SpaceLocationsLocations, o)

			o.R.SpaceLocationsSpace = rel
			break
		}
	}

	return nil
}

// LoadLocationLocationsLocationpipelines loads the locationsLocation's LocationLocationsLocationpipelines into the .R struct
func (o *LocationsLocation) LoadLocationLocationsLocationpipelines(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.LocationLocationsLocationpipelines = nil

	related, err := o.LocationLocationsLocationpipelines(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.LocationLocationsLocation = o
	}

	o.R.LocationLocationsLocationpipelines = related
	return nil
}

// LoadLocationLocationsLocationpipelines loads the locationsLocation's LocationLocationsLocationpipelines into the .R struct
func (os LocationsLocationSlice) LoadLocationLocationsLocationpipelines(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	locationsLocationpipelines, err := os.LocationLocationsLocationpipelines(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.LocationLocationsLocationpipelines = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range locationsLocationpipelines {

			if !(o.UUID == rel.LocationID) {
				continue
			}

			rel.R.LocationLocationsLocation = o

			o.R.LocationLocationsLocationpipelines = append(o.R.LocationLocationsLocationpipelines, rel)
		}
	}

	return nil
}

// LoadCurrentLocationLocationsPackages loads the locationsLocation's CurrentLocationLocationsPackages into the .R struct
func (o *LocationsLocation) LoadCurrentLocationLocationsPackages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.CurrentLocationLocationsPackages = nil

	related, err := o.CurrentLocationLocationsPackages(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CurrentLocationLocationsLocation = o
	}

	o.R.CurrentLocationLocationsPackages = related
	return nil
}

// LoadCurrentLocationLocationsPackages loads the locationsLocation's CurrentLocationLocationsPackages into the .R struct
func (os LocationsLocationSlice) LoadCurrentLocationLocationsPackages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	locationsPackages, err := os.CurrentLocationLocationsPackages(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.CurrentLocationLocationsPackages = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range locationsPackages {

			if !(o.UUID == rel.CurrentLocationID) {
				continue
			}

			rel.R.CurrentLocationLocationsLocation = o

			o.R.CurrentLocationLocationsPackages = append(o.R.CurrentLocationLocationsPackages, rel)
		}
	}

	return nil
}

// LoadPointerFileLocationLocationsPackages loads the locationsLocation's PointerFileLocationLocationsPackages into the .R struct
func (o *LocationsLocation) LoadPointerFileLocationLocationsPackages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.PointerFileLocationLocationsPackages = nil

	related, err := o.PointerFileLocationLocationsPackages(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.PointerFileLocationLocationsLocation = o
	}

	o.R.PointerFileLocationLocationsPackages = related
	return nil
}

// LoadPointerFileLocationLocationsPackages loads the locationsLocation's PointerFileLocationLocationsPackages into the .R struct
func (os LocationsLocationSlice) LoadPointerFileLocationLocationsPackages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	locationsPackages, err := os.PointerFileLocationLocationsPackages(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.PointerFileLocationLocationsPackages = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range locationsPackages {

			if !rel.PointerFileLocationID.IsValue() {
				continue
			}
			if !(rel.PointerFileLocationID.IsValue() && o.UUID == rel.PointerFileLocationID.MustGet()) {
				continue
			}

			rel.R.PointerFileLocationLocationsLocation = o

			o.R.PointerFileLocationLocationsPackages = append(o.R.PointerFileLocationLocationsPackages, rel)
		}
	}

	return nil
}

type locationsLocationJoins[Q dialect.Joinable] struct {
	typ                                      string
	FromLocationLocationsLocationReplicators modAs[Q, locationsLocationReplicatorColumns]
	ToLocationLocationsLocationReplicators   modAs[Q, locationsLocationReplicatorColumns]
	SpaceLocationsSpace                      modAs[Q, locationsSpaceColumns]
	LocationLocationsLocationpipelines       modAs[Q, locationsLocationpipelineColumns]
	CurrentLocationLocationsPackages         modAs[Q, locationsPackageColumns]
	PointerFileLocationLocationsPackages     modAs[Q, locationsPackageColumns]
}

func (j locationsLocationJoins[Q]) aliasedAs(alias string) locationsLocationJoins[Q] {
	return buildLocationsLocationJoins[Q](buildLocationsLocationColumns(alias), j.typ)
}

func buildLocationsLocationJoins[Q dialect.Joinable](cols locationsLocationColumns, typ string) locationsLocationJoins[Q] {
	return locationsLocationJoins[Q]{
		typ: typ,
		FromLocationLocationsLocationReplicators: modAs[Q, locationsLocationReplicatorColumns]{
			c: LocationsLocationReplicators.Columns,
			f: func(to locationsLocationReplicatorColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, LocationsLocationReplicators.Name().As(to.Alias())).On(
						to.FromLocationID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		ToLocationLocationsLocationReplicators: modAs[Q, locationsLocationReplicatorColumns]{
			c: LocationsLocationReplicators.Columns,
			f: func(to locationsLocationReplicatorColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, LocationsLocationReplicators.Name().As(to.Alias())).On(
						to.ToLocationID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		SpaceLocationsSpace: modAs[Q, locationsSpaceColumns]{
			c: LocationsSpaces.Columns,
			f: func(to locationsSpaceColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, LocationsSpaces.Name().As(to.Alias())).On(
						to.UUID.EQ(cols.SpaceID),
					))
				}

				return mods
			},
		},
		LocationLocationsLocationpipelines: modAs[Q, locationsLocationpipelineColumns]{
			c: LocationsLocationpipelines.Columns,
			f: func(to locationsLocationpipelineColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, LocationsLocationpipelines.Name().As(to.Alias())).On(
						to.LocationID.EQ(cols.UUID),
					))
				}

				return mods
			},
		},
		CurrentLocationLocationsPackages: modAs[Q, locationsPackageColumns]{
			c: LocationsPackages.Columns,
			f: func(to locationsPackageColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, LocationsPackages.Name().As(to.Alias())).On(
						to.CurrentLocationID.EQ(cols.UUID),
					))
				}

				return mods
			},
		},
		PointerFileLocationLocationsPackages: modAs[Q, locationsPackageColumns]{
			c: LocationsPackages.Columns,
			f: func(to locationsPackageColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, LocationsPackages.Name().As(to.Alias())).On(
						to.PointerFileLocationID.EQ(cols.UUID),
					))
				}

				return mods
			},
		},
	}
}
