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

// LocationsLocationpipeline is an object representing the database table.
type LocationsLocationpipeline struct {
	ID         int32  `db:"id,pk,autoincr" `
	LocationID string `db:"location_id" `
	PipelineID string `db:"pipeline_id" `

	R locationsLocationpipelineR `db:"-" `
}

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

// LocationsLocationpipelines contains methods to work with the locations_locationpipeline table
var LocationsLocationpipelines = mysql.NewTablex[*LocationsLocationpipeline, LocationsLocationpipelineSlice, *LocationsLocationpipelineSetter]("locations_locationpipeline", buildLocationsLocationpipelineColumns("locations_locationpipeline"), []string{"id"})

// LocationsLocationpipelinesQuery is a query on the locations_locationpipeline table
type LocationsLocationpipelinesQuery = *mysql.ViewQuery[*LocationsLocationpipeline, LocationsLocationpipelineSlice]

// locationsLocationpipelineR is where relationships are stored.
type locationsLocationpipelineR struct {
	LocationLocationsLocation *LocationsLocation // locations_locationpi_location_id_1bff2f57_fk_locations
	PipelineLocationsPipeline *LocationsPipeline // locations_locationpi_pipeline_id_2ad721b4_fk_locations
}

func buildLocationsLocationpipelineColumns(alias string) locationsLocationpipelineColumns {
	return locationsLocationpipelineColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "location_id", "pipeline_id",
		).WithParent("locations_locationpipeline"),
		tableAlias: alias,
		ID:         mysql.Quote(alias, "id"),
		LocationID: mysql.Quote(alias, "location_id"),
		PipelineID: mysql.Quote(alias, "pipeline_id"),
	}
}

type locationsLocationpipelineColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ID         mysql.Expression
	LocationID mysql.Expression
	PipelineID mysql.Expression
}

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

func (locationsLocationpipelineColumns) AliasedAs(alias string) locationsLocationpipelineColumns {
	return buildLocationsLocationpipelineColumns(alias)
}

// LocationsLocationpipelineSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type LocationsLocationpipelineSetter struct {
	ID         omit.Val[int32]  `db:"id,pk,autoincr" `
	LocationID omit.Val[string] `db:"location_id" `
	PipelineID omit.Val[string] `db:"pipeline_id" `
}

func (s LocationsLocationpipelineSetter) SetColumns() []string {
	vals := make([]string, 0, 3)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.LocationID.IsValue() {
		vals = append(vals, "location_id")
	}
	if s.PipelineID.IsValue() {
		vals = append(vals, "pipeline_id")
	}
	return vals
}

func (s LocationsLocationpipelineSetter) Overwrite(t *LocationsLocationpipeline) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.LocationID.IsValue() {
		t.LocationID = s.LocationID.MustGet()
	}
	if s.PipelineID.IsValue() {
		t.PipelineID = s.PipelineID.MustGet()
	}
}

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

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

	s.Overwrite(o)

	return nil
}

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

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

func (o LocationsLocationpipelineSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsLocationpipelineSetter) error {
	_, err := LocationsLocationpipelines.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

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

	return err
}

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

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

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

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

// PipelineLocationsPipeline starts a query for related objects on locations_pipeline
func (o *LocationsLocationpipeline) PipelineLocationsPipeline(mods ...bob.Mod[*dialect.SelectQuery]) LocationsPipelinesQuery {
	return LocationsPipelines.Query(append(mods,
		sm.Where(LocationsPipelines.Columns.UUID.EQ(mysql.Arg(o.PipelineID))),
	)...)
}

func (os LocationsLocationpipelineSlice) PipelineLocationsPipeline(mods ...bob.Mod[*dialect.SelectQuery]) LocationsPipelinesQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.PipelineID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

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

func attachLocationsLocationpipelineLocationLocationsLocation0(ctx context.Context, exec bob.Executor, count int, locationsLocationpipeline0 *LocationsLocationpipeline, locationsLocation1 *LocationsLocation) (*LocationsLocationpipeline, error) {
	setter := &LocationsLocationpipelineSetter{
		LocationID: omit.From(locationsLocation1.UUID),
	}

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

	return locationsLocationpipeline0, nil
}

func (locationsLocationpipeline0 *LocationsLocationpipeline) InsertLocationLocationsLocation(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 = attachLocationsLocationpipelineLocationLocationsLocation0(ctx, exec, 1, locationsLocationpipeline0, locationsLocation1)
	if err != nil {
		return err
	}

	locationsLocationpipeline0.R.LocationLocationsLocation = locationsLocation1

	locationsLocation1.R.LocationLocationsLocationpipelines = append(locationsLocation1.R.LocationLocationsLocationpipelines, locationsLocationpipeline0)

	return nil
}

func (locationsLocationpipeline0 *LocationsLocationpipeline) AttachLocationLocationsLocation(ctx context.Context, exec bob.Executor, locationsLocation1 *LocationsLocation) error {
	var err error

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

	locationsLocationpipeline0.R.LocationLocationsLocation = locationsLocation1

	locationsLocation1.R.LocationLocationsLocationpipelines = append(locationsLocation1.R.LocationLocationsLocationpipelines, locationsLocationpipeline0)

	return nil
}

func attachLocationsLocationpipelinePipelineLocationsPipeline0(ctx context.Context, exec bob.Executor, count int, locationsLocationpipeline0 *LocationsLocationpipeline, locationsPipeline1 *LocationsPipeline) (*LocationsLocationpipeline, error) {
	setter := &LocationsLocationpipelineSetter{
		PipelineID: omit.From(locationsPipeline1.UUID),
	}

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

	return locationsLocationpipeline0, nil
}

func (locationsLocationpipeline0 *LocationsLocationpipeline) InsertPipelineLocationsPipeline(ctx context.Context, exec bob.Executor, related *LocationsPipelineSetter) error {
	var err error

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

	_, err = attachLocationsLocationpipelinePipelineLocationsPipeline0(ctx, exec, 1, locationsLocationpipeline0, locationsPipeline1)
	if err != nil {
		return err
	}

	locationsLocationpipeline0.R.PipelineLocationsPipeline = locationsPipeline1

	locationsPipeline1.R.PipelineLocationsLocationpipelines = append(locationsPipeline1.R.PipelineLocationsLocationpipelines, locationsLocationpipeline0)

	return nil
}

func (locationsLocationpipeline0 *LocationsLocationpipeline) AttachPipelineLocationsPipeline(ctx context.Context, exec bob.Executor, locationsPipeline1 *LocationsPipeline) error {
	var err error

	_, err = attachLocationsLocationpipelinePipelineLocationsPipeline0(ctx, exec, 1, locationsLocationpipeline0, locationsPipeline1)
	if err != nil {
		return err
	}

	locationsLocationpipeline0.R.PipelineLocationsPipeline = locationsPipeline1

	locationsPipeline1.R.PipelineLocationsLocationpipelines = append(locationsPipeline1.R.PipelineLocationsLocationpipelines, locationsLocationpipeline0)

	return nil
}

type locationsLocationpipelineWhere[Q mysql.Filterable] struct {
	ID         mysql.WhereMod[Q, int32]
	LocationID mysql.WhereMod[Q, string]
	PipelineID mysql.WhereMod[Q, string]
}

func (locationsLocationpipelineWhere[Q]) AliasedAs(alias string) locationsLocationpipelineWhere[Q] {
	return buildLocationsLocationpipelineWhere[Q](buildLocationsLocationpipelineColumns(alias))
}

func buildLocationsLocationpipelineWhere[Q mysql.Filterable](cols locationsLocationpipelineColumns) locationsLocationpipelineWhere[Q] {
	return locationsLocationpipelineWhere[Q]{
		ID:         mysql.Where[Q, int32](cols.ID),
		LocationID: mysql.Where[Q, string](cols.LocationID),
		PipelineID: mysql.Where[Q, string](cols.PipelineID),
	}
}

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

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

		o.R.LocationLocationsLocation = rel

		if rel != nil {
			rel.R.LocationLocationsLocationpipelines = LocationsLocationpipelineSlice{o}
		}
		return nil
	case "PipelineLocationsPipeline":
		rel, ok := retrieved.(*LocationsPipeline)
		if !ok {
			return fmt.Errorf("locationsLocationpipeline cannot load %T as %q", retrieved, name)
		}

		o.R.PipelineLocationsPipeline = rel

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

type locationsLocationpipelinePreloader struct {
	LocationLocationsLocation func(...mysql.PreloadOption) mysql.Preloader
	PipelineLocationsPipeline func(...mysql.PreloadOption) mysql.Preloader
}

func buildLocationsLocationpipelinePreloader() locationsLocationpipelinePreloader {
	return locationsLocationpipelinePreloader{
		LocationLocationsLocation: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*LocationsLocation, LocationsLocationSlice](mysql.PreloadRel{
				Name: "LocationLocationsLocation",
				Sides: []mysql.PreloadSide{
					{
						From:        LocationsLocationpipelines,
						To:          LocationsLocations,
						FromColumns: []string{"location_id"},
						ToColumns:   []string{"uuid"},
					},
				},
			}, LocationsLocations.Columns.Names(), opts...)
		},
		PipelineLocationsPipeline: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*LocationsPipeline, LocationsPipelineSlice](mysql.PreloadRel{
				Name: "PipelineLocationsPipeline",
				Sides: []mysql.PreloadSide{
					{
						From:        LocationsLocationpipelines,
						To:          LocationsPipelines,
						FromColumns: []string{"pipeline_id"},
						ToColumns:   []string{"uuid"},
					},
				},
			}, LocationsPipelines.Columns.Names(), opts...)
		},
	}
}

type locationsLocationpipelineThenLoader[Q orm.Loadable] struct {
	LocationLocationsLocation func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	PipelineLocationsPipeline func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildLocationsLocationpipelineThenLoader[Q orm.Loadable]() locationsLocationpipelineThenLoader[Q] {
	type LocationLocationsLocationLoadInterface interface {
		LoadLocationLocationsLocation(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PipelineLocationsPipelineLoadInterface interface {
		LoadPipelineLocationsPipeline(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return locationsLocationpipelineThenLoader[Q]{
		LocationLocationsLocation: thenLoadBuilder[Q](
			"LocationLocationsLocation",
			func(ctx context.Context, exec bob.Executor, retrieved LocationLocationsLocationLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadLocationLocationsLocation(ctx, exec, mods...)
			},
		),
		PipelineLocationsPipeline: thenLoadBuilder[Q](
			"PipelineLocationsPipeline",
			func(ctx context.Context, exec bob.Executor, retrieved PipelineLocationsPipelineLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadPipelineLocationsPipeline(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.LocationLocationsLocationpipelines = LocationsLocationpipelineSlice{o}

	o.R.LocationLocationsLocation = related
	return nil
}

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

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

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

		for _, rel := range locationsLocations {

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

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

			o.R.LocationLocationsLocation = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.PipelineLocationsLocationpipelines = LocationsLocationpipelineSlice{o}

	o.R.PipelineLocationsPipeline = related
	return nil
}

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

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

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

		for _, rel := range locationsPipelines {

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

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

			o.R.PipelineLocationsPipeline = rel
			break
		}
	}

	return nil
}

type locationsLocationpipelineJoins[Q dialect.Joinable] struct {
	typ                       string
	LocationLocationsLocation modAs[Q, locationsLocationColumns]
	PipelineLocationsPipeline modAs[Q, locationsPipelineColumns]
}

func (j locationsLocationpipelineJoins[Q]) aliasedAs(alias string) locationsLocationpipelineJoins[Q] {
	return buildLocationsLocationpipelineJoins[Q](buildLocationsLocationpipelineColumns(alias), j.typ)
}

func buildLocationsLocationpipelineJoins[Q dialect.Joinable](cols locationsLocationpipelineColumns, typ string) locationsLocationpipelineJoins[Q] {
	return locationsLocationpipelineJoins[Q]{
		typ: typ,
		LocationLocationsLocation: 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.UUID.EQ(cols.LocationID),
					))
				}

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

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

				return mods
			},
		},
	}
}
