// 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/omit"
	"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"
)

// LocationsNF is an object representing the database table.
type LocationsNF struct {
	ID              int32  `db:"id,pk,autoincr" `
	RemoteName      string `db:"remote_name" `
	RemotePath      string `db:"remote_path" `
	Version         string `db:"version" `
	ManuallyMounted bool   `db:"manually_mounted" `
	SpaceID         string `db:"space_id" `

	R locationsNFR `db:"-" `
}

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

// LocationsNFS contains methods to work with the locations_nfs table
var LocationsNFS = mysql.NewTablex[*LocationsNF, LocationsNFSlice, *LocationsNFSetter]("locations_nfs", buildLocationsNFColumns("locations_nfs"), []string{"id"}, []string{"space_id"})

// LocationsNFSQuery is a query on the locations_nfs table
type LocationsNFSQuery = *mysql.ViewQuery[*LocationsNF, LocationsNFSlice]

// locationsNFR is where relationships are stored.
type locationsNFR struct {
	SpaceLocationsSpace *LocationsSpace // locations_nfs_space_id_34913c3b_fk_locations_space_uuid
}

func buildLocationsNFColumns(alias string) locationsNFColumns {
	return locationsNFColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "remote_name", "remote_path", "version", "manually_mounted", "space_id",
		).WithParent("locations_nfs"),
		tableAlias:      alias,
		ID:              mysql.Quote(alias, "id"),
		RemoteName:      mysql.Quote(alias, "remote_name"),
		RemotePath:      mysql.Quote(alias, "remote_path"),
		Version:         mysql.Quote(alias, "version"),
		ManuallyMounted: mysql.Quote(alias, "manually_mounted"),
		SpaceID:         mysql.Quote(alias, "space_id"),
	}
}

type locationsNFColumns struct {
	expr.ColumnsExpr
	tableAlias      string
	ID              mysql.Expression
	RemoteName      mysql.Expression
	RemotePath      mysql.Expression
	Version         mysql.Expression
	ManuallyMounted mysql.Expression
	SpaceID         mysql.Expression
}

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

func (locationsNFColumns) AliasedAs(alias string) locationsNFColumns {
	return buildLocationsNFColumns(alias)
}

// LocationsNFSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type LocationsNFSetter struct {
	ID              omit.Val[int32]  `db:"id,pk,autoincr" `
	RemoteName      omit.Val[string] `db:"remote_name" `
	RemotePath      omit.Val[string] `db:"remote_path" `
	Version         omit.Val[string] `db:"version" `
	ManuallyMounted omit.Val[bool]   `db:"manually_mounted" `
	SpaceID         omit.Val[string] `db:"space_id" `
}

func (s LocationsNFSetter) SetColumns() []string {
	vals := make([]string, 0, 6)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.RemoteName.IsValue() {
		vals = append(vals, "remote_name")
	}
	if s.RemotePath.IsValue() {
		vals = append(vals, "remote_path")
	}
	if s.Version.IsValue() {
		vals = append(vals, "version")
	}
	if s.ManuallyMounted.IsValue() {
		vals = append(vals, "manually_mounted")
	}
	if s.SpaceID.IsValue() {
		vals = append(vals, "space_id")
	}
	return vals
}

func (s LocationsNFSetter) Overwrite(t *LocationsNF) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.RemoteName.IsValue() {
		t.RemoteName = s.RemoteName.MustGet()
	}
	if s.RemotePath.IsValue() {
		t.RemotePath = s.RemotePath.MustGet()
	}
	if s.Version.IsValue() {
		t.Version = s.Version.MustGet()
	}
	if s.ManuallyMounted.IsValue() {
		t.ManuallyMounted = s.ManuallyMounted.MustGet()
	}
	if s.SpaceID.IsValue() {
		t.SpaceID = s.SpaceID.MustGet()
	}
}

func (s *LocationsNFSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return LocationsNFS.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.RemoteName.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.RemoteName.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.RemotePath.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.RemotePath.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.Version.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Version.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.ManuallyMounted.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.ManuallyMounted.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 LocationsNFSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions("locations_nfs")...)
}

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

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

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

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

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

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

	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
}

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

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

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

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

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

	return err
}

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

func (o *LocationsNF) pkEQ() dialect.Expression {
	return mysql.Quote("locations_nfs", "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 LocationsNF
func (o *LocationsNF) Update(ctx context.Context, exec bob.Executor, s *LocationsNFSetter) error {
	_, err := LocationsNFS.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec)
	if err != nil {
		return err
	}

	s.Overwrite(o)

	return nil
}

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

// Reload refreshes the LocationsNF using the executor
func (o *LocationsNF) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := LocationsNFS.Query(
		sm.Where(LocationsNFS.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 LocationsNFSlice is retrieved from the database
func (o LocationsNFSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

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

	return err
}

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

	return mysql.Quote("locations_nfs", "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 LocationsNFSlice) copyMatchingRows(from ...*LocationsNF) {
	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 LocationsNFSlice) 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 LocationsNFS.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 *LocationsNF:
				o.copyMatchingRows(retrieved)
			case []*LocationsNF:
				o.copyMatchingRows(retrieved...)
			case LocationsNFSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a LocationsNF or a slice of LocationsNF
				// then run the AfterUpdateHooks on the slice
				_, err = LocationsNFS.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o LocationsNFSlice) 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 LocationsNFS.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 *LocationsNF:
				o.copyMatchingRows(retrieved)
			case []*LocationsNF:
				o.copyMatchingRows(retrieved...)
			case LocationsNFSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a LocationsNF or a slice of LocationsNF
				// then run the AfterDeleteHooks on the slice
				_, err = LocationsNFS.AfterDeleteHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

func (o LocationsNFSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsNFSetter) error {
	_, err := LocationsNFS.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

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

	return err
}

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

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

func (os LocationsNFSlice) 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)),
	)...)
}

func attachLocationsNFSpaceLocationsSpace0(ctx context.Context, exec bob.Executor, count int, locationsNF0 *LocationsNF, locationsSpace1 *LocationsSpace) (*LocationsNF, error) {
	setter := &LocationsNFSetter{
		SpaceID: omit.From(locationsSpace1.UUID),
	}

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

	return locationsNF0, nil
}

func (locationsNF0 *LocationsNF) 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 = attachLocationsNFSpaceLocationsSpace0(ctx, exec, 1, locationsNF0, locationsSpace1)
	if err != nil {
		return err
	}

	locationsNF0.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsNF = locationsNF0

	return nil
}

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

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

	locationsNF0.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsNF = locationsNF0

	return nil
}

type locationsNFWhere[Q mysql.Filterable] struct {
	ID              mysql.WhereMod[Q, int32]
	RemoteName      mysql.WhereMod[Q, string]
	RemotePath      mysql.WhereMod[Q, string]
	Version         mysql.WhereMod[Q, string]
	ManuallyMounted mysql.WhereMod[Q, bool]
	SpaceID         mysql.WhereMod[Q, string]
}

func (locationsNFWhere[Q]) AliasedAs(alias string) locationsNFWhere[Q] {
	return buildLocationsNFWhere[Q](buildLocationsNFColumns(alias))
}

func buildLocationsNFWhere[Q mysql.Filterable](cols locationsNFColumns) locationsNFWhere[Q] {
	return locationsNFWhere[Q]{
		ID:              mysql.Where[Q, int32](cols.ID),
		RemoteName:      mysql.Where[Q, string](cols.RemoteName),
		RemotePath:      mysql.Where[Q, string](cols.RemotePath),
		Version:         mysql.Where[Q, string](cols.Version),
		ManuallyMounted: mysql.Where[Q, bool](cols.ManuallyMounted),
		SpaceID:         mysql.Where[Q, string](cols.SpaceID),
	}
}

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

	switch name {
	case "SpaceLocationsSpace":
		rel, ok := retrieved.(*LocationsSpace)
		if !ok {
			return fmt.Errorf("locationsNF cannot load %T as %q", retrieved, name)
		}

		o.R.SpaceLocationsSpace = rel

		if rel != nil {
			rel.R.SpaceLocationsNF = o
		}
		return nil
	default:
		return fmt.Errorf("locationsNF has no relationship %q", name)
	}
}

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

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

type locationsNFThenLoader[Q orm.Loadable] struct {
	SpaceLocationsSpace func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildLocationsNFThenLoader[Q orm.Loadable]() locationsNFThenLoader[Q] {
	type SpaceLocationsSpaceLoadInterface interface {
		LoadSpaceLocationsSpace(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return locationsNFThenLoader[Q]{
		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...)
			},
		),
	}
}

// LoadSpaceLocationsSpace loads the locationsNF's SpaceLocationsSpace into the .R struct
func (o *LocationsNF) 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.SpaceLocationsNF = o

	o.R.SpaceLocationsSpace = related
	return nil
}

// LoadSpaceLocationsSpace loads the locationsNF's SpaceLocationsSpace into the .R struct
func (os LocationsNFSlice) 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.SpaceLocationsNF = o

			o.R.SpaceLocationsSpace = rel
			break
		}
	}

	return nil
}

type locationsNFJoins[Q dialect.Joinable] struct {
	typ                 string
	SpaceLocationsSpace modAs[Q, locationsSpaceColumns]
}

func (j locationsNFJoins[Q]) aliasedAs(alias string) locationsNFJoins[Q] {
	return buildLocationsNFJoins[Q](buildLocationsNFColumns(alias), j.typ)
}

func buildLocationsNFJoins[Q dialect.Joinable](cols locationsNFColumns, typ string) locationsNFJoins[Q] {
	return locationsNFJoins[Q]{
		typ: typ,
		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
			},
		},
	}
}
