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

// LocationsPipelinelocalf is an object representing the database table.
type LocationsPipelinelocalf struct {
	ID                int32  `db:"id,pk,autoincr" `
	RemoteUser        string `db:"remote_user" `
	RemoteName        string `db:"remote_name" `
	SpaceID           string `db:"space_id" `
	AssumeRsyncDaemon bool   `db:"assume_rsync_daemon" `
	RsyncPassword     string `db:"rsync_password" `

	R locationsPipelinelocalfR `db:"-" `
}

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

// LocationsPipelinelocalfs contains methods to work with the locations_pipelinelocalfs table
var LocationsPipelinelocalfs = mysql.NewTablex[*LocationsPipelinelocalf, LocationsPipelinelocalfSlice, *LocationsPipelinelocalfSetter]("locations_pipelinelocalfs", buildLocationsPipelinelocalfColumns("locations_pipelinelocalfs"), []string{"id"}, []string{"space_id"})

// LocationsPipelinelocalfsQuery is a query on the locations_pipelinelocalfs table
type LocationsPipelinelocalfsQuery = *mysql.ViewQuery[*LocationsPipelinelocalf, LocationsPipelinelocalfSlice]

// locationsPipelinelocalfR is where relationships are stored.
type locationsPipelinelocalfR struct {
	SpaceLocationsSpace *LocationsSpace // locations_pipelinelo_space_id_6f6ba9f6_fk_locations
}

func buildLocationsPipelinelocalfColumns(alias string) locationsPipelinelocalfColumns {
	return locationsPipelinelocalfColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "remote_user", "remote_name", "space_id", "assume_rsync_daemon", "rsync_password",
		).WithParent("locations_pipelinelocalfs"),
		tableAlias:        alias,
		ID:                mysql.Quote(alias, "id"),
		RemoteUser:        mysql.Quote(alias, "remote_user"),
		RemoteName:        mysql.Quote(alias, "remote_name"),
		SpaceID:           mysql.Quote(alias, "space_id"),
		AssumeRsyncDaemon: mysql.Quote(alias, "assume_rsync_daemon"),
		RsyncPassword:     mysql.Quote(alias, "rsync_password"),
	}
}

type locationsPipelinelocalfColumns struct {
	expr.ColumnsExpr
	tableAlias        string
	ID                mysql.Expression
	RemoteUser        mysql.Expression
	RemoteName        mysql.Expression
	SpaceID           mysql.Expression
	AssumeRsyncDaemon mysql.Expression
	RsyncPassword     mysql.Expression
}

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

func (locationsPipelinelocalfColumns) AliasedAs(alias string) locationsPipelinelocalfColumns {
	return buildLocationsPipelinelocalfColumns(alias)
}

// LocationsPipelinelocalfSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type LocationsPipelinelocalfSetter struct {
	ID                omit.Val[int32]  `db:"id,pk,autoincr" `
	RemoteUser        omit.Val[string] `db:"remote_user" `
	RemoteName        omit.Val[string] `db:"remote_name" `
	SpaceID           omit.Val[string] `db:"space_id" `
	AssumeRsyncDaemon omit.Val[bool]   `db:"assume_rsync_daemon" `
	RsyncPassword     omit.Val[string] `db:"rsync_password" `
}

func (s LocationsPipelinelocalfSetter) SetColumns() []string {
	vals := make([]string, 0, 6)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.RemoteUser.IsValue() {
		vals = append(vals, "remote_user")
	}
	if s.RemoteName.IsValue() {
		vals = append(vals, "remote_name")
	}
	if s.SpaceID.IsValue() {
		vals = append(vals, "space_id")
	}
	if s.AssumeRsyncDaemon.IsValue() {
		vals = append(vals, "assume_rsync_daemon")
	}
	if s.RsyncPassword.IsValue() {
		vals = append(vals, "rsync_password")
	}
	return vals
}

func (s LocationsPipelinelocalfSetter) Overwrite(t *LocationsPipelinelocalf) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.RemoteUser.IsValue() {
		t.RemoteUser = s.RemoteUser.MustGet()
	}
	if s.RemoteName.IsValue() {
		t.RemoteName = s.RemoteName.MustGet()
	}
	if s.SpaceID.IsValue() {
		t.SpaceID = s.SpaceID.MustGet()
	}
	if s.AssumeRsyncDaemon.IsValue() {
		t.AssumeRsyncDaemon = s.AssumeRsyncDaemon.MustGet()
	}
	if s.RsyncPassword.IsValue() {
		t.RsyncPassword = s.RsyncPassword.MustGet()
	}
}

func (s *LocationsPipelinelocalfSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return LocationsPipelinelocalfs.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.RemoteUser.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.RemoteUser.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.SpaceID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.SpaceID.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.AssumeRsyncDaemon.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.AssumeRsyncDaemon.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.RsyncPassword.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.RsyncPassword.MustGet()).WriteSQL(ctx, w, d, start)
		}))
}

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

func (s LocationsPipelinelocalfSetter) 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.RemoteUser.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "remote_user")...),
			mysql.Arg(s.RemoteUser),
		}})
	}

	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.SpaceID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "space_id")...),
			mysql.Arg(s.SpaceID),
		}})
	}

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

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

	s.Overwrite(o)

	return nil
}

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

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

func (o LocationsPipelinelocalfSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsPipelinelocalfSetter) error {
	_, err := LocationsPipelinelocalfs.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

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

	return err
}

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

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

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

	o2, err := LocationsPipelinelocalfs.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 *LocationsPipelinelocalf) SpaceLocationsSpace(mods ...bob.Mod[*dialect.SelectQuery]) LocationsSpacesQuery {
	return LocationsSpaces.Query(append(mods,
		sm.Where(LocationsSpaces.Columns.UUID.EQ(mysql.Arg(o.SpaceID))),
	)...)
}

func (os LocationsPipelinelocalfSlice) 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 attachLocationsPipelinelocalfSpaceLocationsSpace0(ctx context.Context, exec bob.Executor, count int, locationsPipelinelocalf0 *LocationsPipelinelocalf, locationsSpace1 *LocationsSpace) (*LocationsPipelinelocalf, error) {
	setter := &LocationsPipelinelocalfSetter{
		SpaceID: omit.From(locationsSpace1.UUID),
	}

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

	return locationsPipelinelocalf0, nil
}

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

	locationsPipelinelocalf0.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsPipelinelocalf = locationsPipelinelocalf0

	return nil
}

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

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

	locationsPipelinelocalf0.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsPipelinelocalf = locationsPipelinelocalf0

	return nil
}

type locationsPipelinelocalfWhere[Q mysql.Filterable] struct {
	ID                mysql.WhereMod[Q, int32]
	RemoteUser        mysql.WhereMod[Q, string]
	RemoteName        mysql.WhereMod[Q, string]
	SpaceID           mysql.WhereMod[Q, string]
	AssumeRsyncDaemon mysql.WhereMod[Q, bool]
	RsyncPassword     mysql.WhereMod[Q, string]
}

func (locationsPipelinelocalfWhere[Q]) AliasedAs(alias string) locationsPipelinelocalfWhere[Q] {
	return buildLocationsPipelinelocalfWhere[Q](buildLocationsPipelinelocalfColumns(alias))
}

func buildLocationsPipelinelocalfWhere[Q mysql.Filterable](cols locationsPipelinelocalfColumns) locationsPipelinelocalfWhere[Q] {
	return locationsPipelinelocalfWhere[Q]{
		ID:                mysql.Where[Q, int32](cols.ID),
		RemoteUser:        mysql.Where[Q, string](cols.RemoteUser),
		RemoteName:        mysql.Where[Q, string](cols.RemoteName),
		SpaceID:           mysql.Where[Q, string](cols.SpaceID),
		AssumeRsyncDaemon: mysql.Where[Q, bool](cols.AssumeRsyncDaemon),
		RsyncPassword:     mysql.Where[Q, string](cols.RsyncPassword),
	}
}

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

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

		o.R.SpaceLocationsSpace = rel

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

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

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

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

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

	return locationsPipelinelocalfThenLoader[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 locationsPipelinelocalf's SpaceLocationsSpace into the .R struct
func (o *LocationsPipelinelocalf) 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.SpaceLocationsPipelinelocalf = o

	o.R.SpaceLocationsSpace = related
	return nil
}

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

			o.R.SpaceLocationsSpace = rel
			break
		}
	}

	return nil
}

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

func (j locationsPipelinelocalfJoins[Q]) aliasedAs(alias string) locationsPipelinelocalfJoins[Q] {
	return buildLocationsPipelinelocalfJoins[Q](buildLocationsPipelinelocalfColumns(alias), j.typ)
}

func buildLocationsPipelinelocalfJoins[Q dialect.Joinable](cols locationsPipelinelocalfColumns, typ string) locationsPipelinelocalfJoins[Q] {
	return locationsPipelinelocalfJoins[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
			},
		},
	}
}
