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

// LocationsLocationReplicator is an object representing the database table.
type LocationsLocationReplicator struct {
	ID             int32 `db:"id,pk,autoincr" `
	FromLocationID int32 `db:"from_location_id" `
	ToLocationID   int32 `db:"to_location_id" `

	R locationsLocationReplicatorR `db:"-" `
}

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

// LocationsLocationReplicators contains methods to work with the locations_location_replicators table
var LocationsLocationReplicators = mysql.NewTablex[*LocationsLocationReplicator, LocationsLocationReplicatorSlice, *LocationsLocationReplicatorSetter]("locations_location_replicators", buildLocationsLocationReplicatorColumns("locations_location_replicators"), []string{"id"}, []string{"from_location_id", "to_location_id"})

// LocationsLocationReplicatorsQuery is a query on the locations_location_replicators table
type LocationsLocationReplicatorsQuery = *mysql.ViewQuery[*LocationsLocationReplicator, LocationsLocationReplicatorSlice]

// locationsLocationReplicatorR is where relationships are stored.
type locationsLocationReplicatorR struct {
	FromLocationLocationsLocation *LocationsLocation // locations_location_r_from_location_id_3b9a55f5_fk_locations
	ToLocationLocationsLocation   *LocationsLocation // locations_location_r_to_location_id_27be9af1_fk_locations
}

func buildLocationsLocationReplicatorColumns(alias string) locationsLocationReplicatorColumns {
	return locationsLocationReplicatorColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "from_location_id", "to_location_id",
		).WithParent("locations_location_replicators"),
		tableAlias:     alias,
		ID:             mysql.Quote(alias, "id"),
		FromLocationID: mysql.Quote(alias, "from_location_id"),
		ToLocationID:   mysql.Quote(alias, "to_location_id"),
	}
}

type locationsLocationReplicatorColumns struct {
	expr.ColumnsExpr
	tableAlias     string
	ID             mysql.Expression
	FromLocationID mysql.Expression
	ToLocationID   mysql.Expression
}

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

func (locationsLocationReplicatorColumns) AliasedAs(alias string) locationsLocationReplicatorColumns {
	return buildLocationsLocationReplicatorColumns(alias)
}

// LocationsLocationReplicatorSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type LocationsLocationReplicatorSetter struct {
	ID             omit.Val[int32] `db:"id,pk,autoincr" `
	FromLocationID omit.Val[int32] `db:"from_location_id" `
	ToLocationID   omit.Val[int32] `db:"to_location_id" `
}

func (s LocationsLocationReplicatorSetter) SetColumns() []string {
	vals := make([]string, 0, 3)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.FromLocationID.IsValue() {
		vals = append(vals, "from_location_id")
	}
	if s.ToLocationID.IsValue() {
		vals = append(vals, "to_location_id")
	}
	return vals
}

func (s LocationsLocationReplicatorSetter) Overwrite(t *LocationsLocationReplicator) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.FromLocationID.IsValue() {
		t.FromLocationID = s.FromLocationID.MustGet()
	}
	if s.ToLocationID.IsValue() {
		t.ToLocationID = s.ToLocationID.MustGet()
	}
}

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

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

	s.Overwrite(o)

	return nil
}

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

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

func (o LocationsLocationReplicatorSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsLocationReplicatorSetter) error {
	_, err := LocationsLocationReplicators.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

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

	return err
}

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// FromLocationLocationsLocation starts a query for related objects on locations_location
func (o *LocationsLocationReplicator) FromLocationLocationsLocation(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationsQuery {
	return LocationsLocations.Query(append(mods,
		sm.Where(LocationsLocations.Columns.ID.EQ(mysql.Arg(o.FromLocationID))),
	)...)
}

func (os LocationsLocationReplicatorSlice) FromLocationLocationsLocation(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationsQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.FromLocationID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

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

// ToLocationLocationsLocation starts a query for related objects on locations_location
func (o *LocationsLocationReplicator) ToLocationLocationsLocation(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationsQuery {
	return LocationsLocations.Query(append(mods,
		sm.Where(LocationsLocations.Columns.ID.EQ(mysql.Arg(o.ToLocationID))),
	)...)
}

func (os LocationsLocationReplicatorSlice) ToLocationLocationsLocation(mods ...bob.Mod[*dialect.SelectQuery]) LocationsLocationsQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.ToLocationID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

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

func attachLocationsLocationReplicatorFromLocationLocationsLocation0(ctx context.Context, exec bob.Executor, count int, locationsLocationReplicator0 *LocationsLocationReplicator, locationsLocation1 *LocationsLocation) (*LocationsLocationReplicator, error) {
	setter := &LocationsLocationReplicatorSetter{
		FromLocationID: omit.From(locationsLocation1.ID),
	}

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

	return locationsLocationReplicator0, nil
}

func (locationsLocationReplicator0 *LocationsLocationReplicator) InsertFromLocationLocationsLocation(ctx context.Context, exec bob.Executor, related *LocationsLocationSetter) error {
	var err error

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

	_, err = attachLocationsLocationReplicatorFromLocationLocationsLocation0(ctx, exec, 1, locationsLocationReplicator0, locationsLocation1)
	if err != nil {
		return err
	}

	locationsLocationReplicator0.R.FromLocationLocationsLocation = locationsLocation1

	locationsLocation1.R.FromLocationLocationsLocationReplicators = append(locationsLocation1.R.FromLocationLocationsLocationReplicators, locationsLocationReplicator0)

	return nil
}

func (locationsLocationReplicator0 *LocationsLocationReplicator) AttachFromLocationLocationsLocation(ctx context.Context, exec bob.Executor, locationsLocation1 *LocationsLocation) error {
	var err error

	_, err = attachLocationsLocationReplicatorFromLocationLocationsLocation0(ctx, exec, 1, locationsLocationReplicator0, locationsLocation1)
	if err != nil {
		return err
	}

	locationsLocationReplicator0.R.FromLocationLocationsLocation = locationsLocation1

	locationsLocation1.R.FromLocationLocationsLocationReplicators = append(locationsLocation1.R.FromLocationLocationsLocationReplicators, locationsLocationReplicator0)

	return nil
}

func attachLocationsLocationReplicatorToLocationLocationsLocation0(ctx context.Context, exec bob.Executor, count int, locationsLocationReplicator0 *LocationsLocationReplicator, locationsLocation1 *LocationsLocation) (*LocationsLocationReplicator, error) {
	setter := &LocationsLocationReplicatorSetter{
		ToLocationID: omit.From(locationsLocation1.ID),
	}

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

	return locationsLocationReplicator0, nil
}

func (locationsLocationReplicator0 *LocationsLocationReplicator) InsertToLocationLocationsLocation(ctx context.Context, exec bob.Executor, related *LocationsLocationSetter) error {
	var err error

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

	_, err = attachLocationsLocationReplicatorToLocationLocationsLocation0(ctx, exec, 1, locationsLocationReplicator0, locationsLocation1)
	if err != nil {
		return err
	}

	locationsLocationReplicator0.R.ToLocationLocationsLocation = locationsLocation1

	locationsLocation1.R.ToLocationLocationsLocationReplicators = append(locationsLocation1.R.ToLocationLocationsLocationReplicators, locationsLocationReplicator0)

	return nil
}

func (locationsLocationReplicator0 *LocationsLocationReplicator) AttachToLocationLocationsLocation(ctx context.Context, exec bob.Executor, locationsLocation1 *LocationsLocation) error {
	var err error

	_, err = attachLocationsLocationReplicatorToLocationLocationsLocation0(ctx, exec, 1, locationsLocationReplicator0, locationsLocation1)
	if err != nil {
		return err
	}

	locationsLocationReplicator0.R.ToLocationLocationsLocation = locationsLocation1

	locationsLocation1.R.ToLocationLocationsLocationReplicators = append(locationsLocation1.R.ToLocationLocationsLocationReplicators, locationsLocationReplicator0)

	return nil
}

type locationsLocationReplicatorWhere[Q mysql.Filterable] struct {
	ID             mysql.WhereMod[Q, int32]
	FromLocationID mysql.WhereMod[Q, int32]
	ToLocationID   mysql.WhereMod[Q, int32]
}

func (locationsLocationReplicatorWhere[Q]) AliasedAs(alias string) locationsLocationReplicatorWhere[Q] {
	return buildLocationsLocationReplicatorWhere[Q](buildLocationsLocationReplicatorColumns(alias))
}

func buildLocationsLocationReplicatorWhere[Q mysql.Filterable](cols locationsLocationReplicatorColumns) locationsLocationReplicatorWhere[Q] {
	return locationsLocationReplicatorWhere[Q]{
		ID:             mysql.Where[Q, int32](cols.ID),
		FromLocationID: mysql.Where[Q, int32](cols.FromLocationID),
		ToLocationID:   mysql.Where[Q, int32](cols.ToLocationID),
	}
}

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

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

		o.R.FromLocationLocationsLocation = rel

		if rel != nil {
			rel.R.FromLocationLocationsLocationReplicators = LocationsLocationReplicatorSlice{o}
		}
		return nil
	case "ToLocationLocationsLocation":
		rel, ok := retrieved.(*LocationsLocation)
		if !ok {
			return fmt.Errorf("locationsLocationReplicator cannot load %T as %q", retrieved, name)
		}

		o.R.ToLocationLocationsLocation = rel

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

type locationsLocationReplicatorPreloader struct {
	FromLocationLocationsLocation func(...mysql.PreloadOption) mysql.Preloader
	ToLocationLocationsLocation   func(...mysql.PreloadOption) mysql.Preloader
}

func buildLocationsLocationReplicatorPreloader() locationsLocationReplicatorPreloader {
	return locationsLocationReplicatorPreloader{
		FromLocationLocationsLocation: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*LocationsLocation, LocationsLocationSlice](mysql.PreloadRel{
				Name: "FromLocationLocationsLocation",
				Sides: []mysql.PreloadSide{
					{
						From:        LocationsLocationReplicators,
						To:          LocationsLocations,
						FromColumns: []string{"from_location_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, LocationsLocations.Columns.Names(), opts...)
		},
		ToLocationLocationsLocation: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*LocationsLocation, LocationsLocationSlice](mysql.PreloadRel{
				Name: "ToLocationLocationsLocation",
				Sides: []mysql.PreloadSide{
					{
						From:        LocationsLocationReplicators,
						To:          LocationsLocations,
						FromColumns: []string{"to_location_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, LocationsLocations.Columns.Names(), opts...)
		},
	}
}

type locationsLocationReplicatorThenLoader[Q orm.Loadable] struct {
	FromLocationLocationsLocation func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	ToLocationLocationsLocation   func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildLocationsLocationReplicatorThenLoader[Q orm.Loadable]() locationsLocationReplicatorThenLoader[Q] {
	type FromLocationLocationsLocationLoadInterface interface {
		LoadFromLocationLocationsLocation(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ToLocationLocationsLocationLoadInterface interface {
		LoadToLocationLocationsLocation(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return locationsLocationReplicatorThenLoader[Q]{
		FromLocationLocationsLocation: thenLoadBuilder[Q](
			"FromLocationLocationsLocation",
			func(ctx context.Context, exec bob.Executor, retrieved FromLocationLocationsLocationLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadFromLocationLocationsLocation(ctx, exec, mods...)
			},
		),
		ToLocationLocationsLocation: thenLoadBuilder[Q](
			"ToLocationLocationsLocation",
			func(ctx context.Context, exec bob.Executor, retrieved ToLocationLocationsLocationLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadToLocationLocationsLocation(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.FromLocationLocationsLocationReplicators = LocationsLocationReplicatorSlice{o}

	o.R.FromLocationLocationsLocation = related
	return nil
}

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

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

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

		for _, rel := range locationsLocations {

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

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

			o.R.FromLocationLocationsLocation = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.ToLocationLocationsLocationReplicators = LocationsLocationReplicatorSlice{o}

	o.R.ToLocationLocationsLocation = related
	return nil
}

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

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

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

		for _, rel := range locationsLocations {

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

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

			o.R.ToLocationLocationsLocation = rel
			break
		}
	}

	return nil
}

type locationsLocationReplicatorJoins[Q dialect.Joinable] struct {
	typ                           string
	FromLocationLocationsLocation modAs[Q, locationsLocationColumns]
	ToLocationLocationsLocation   modAs[Q, locationsLocationColumns]
}

func (j locationsLocationReplicatorJoins[Q]) aliasedAs(alias string) locationsLocationReplicatorJoins[Q] {
	return buildLocationsLocationReplicatorJoins[Q](buildLocationsLocationReplicatorColumns(alias), j.typ)
}

func buildLocationsLocationReplicatorJoins[Q dialect.Joinable](cols locationsLocationReplicatorColumns, typ string) locationsLocationReplicatorJoins[Q] {
	return locationsLocationReplicatorJoins[Q]{
		typ: typ,
		FromLocationLocationsLocation: modAs[Q, locationsLocationColumns]{
			c: LocationsLocations.Columns,
			f: func(to locationsLocationColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

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

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

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

				return mods
			},
		},
	}
}
