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

// LocationsDspacerest is an object representing the database table.
type LocationsDspacerest struct {
	ID               int32  `db:"id,pk,autoincr" `
	DSRestURL        string `db:"ds_rest_url" `
	DSUser           string `db:"ds_user" `
	DSPassword       string `db:"ds_password" `
	DSDipCollection  string `db:"ds_dip_collection" `
	DSAipCollection  string `db:"ds_aip_collection" `
	AsURL            string `db:"as_url" `
	AsUser           string `db:"as_user" `
	AsPassword       string `db:"as_password" `
	AsRepository     string `db:"as_repository" `
	AsArchivalObject string `db:"as_archival_object" `
	VerifySSL        bool   `db:"verify_ssl" `
	UploadToTSM      bool   `db:"upload_to_tsm" `
	SpaceID          string `db:"space_id" `

	R locationsDspacerestR `db:"-" `
}

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

// LocationsDspacerests contains methods to work with the locations_dspacerest table
var LocationsDspacerests = mysql.NewTablex[*LocationsDspacerest, LocationsDspacerestSlice, *LocationsDspacerestSetter]("locations_dspacerest", buildLocationsDspacerestColumns("locations_dspacerest"), []string{"id"}, []string{"space_id"})

// LocationsDspacerestsQuery is a query on the locations_dspacerest table
type LocationsDspacerestsQuery = *mysql.ViewQuery[*LocationsDspacerest, LocationsDspacerestSlice]

// locationsDspacerestR is where relationships are stored.
type locationsDspacerestR struct {
	SpaceLocationsSpace *LocationsSpace // locations_dspacerest_space_id_6ae6a82b_fk_locations_space_uuid
}

func buildLocationsDspacerestColumns(alias string) locationsDspacerestColumns {
	return locationsDspacerestColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "ds_rest_url", "ds_user", "ds_password", "ds_dip_collection", "ds_aip_collection", "as_url", "as_user", "as_password", "as_repository", "as_archival_object", "verify_ssl", "upload_to_tsm", "space_id",
		).WithParent("locations_dspacerest"),
		tableAlias:       alias,
		ID:               mysql.Quote(alias, "id"),
		DSRestURL:        mysql.Quote(alias, "ds_rest_url"),
		DSUser:           mysql.Quote(alias, "ds_user"),
		DSPassword:       mysql.Quote(alias, "ds_password"),
		DSDipCollection:  mysql.Quote(alias, "ds_dip_collection"),
		DSAipCollection:  mysql.Quote(alias, "ds_aip_collection"),
		AsURL:            mysql.Quote(alias, "as_url"),
		AsUser:           mysql.Quote(alias, "as_user"),
		AsPassword:       mysql.Quote(alias, "as_password"),
		AsRepository:     mysql.Quote(alias, "as_repository"),
		AsArchivalObject: mysql.Quote(alias, "as_archival_object"),
		VerifySSL:        mysql.Quote(alias, "verify_ssl"),
		UploadToTSM:      mysql.Quote(alias, "upload_to_tsm"),
		SpaceID:          mysql.Quote(alias, "space_id"),
	}
}

type locationsDspacerestColumns struct {
	expr.ColumnsExpr
	tableAlias       string
	ID               mysql.Expression
	DSRestURL        mysql.Expression
	DSUser           mysql.Expression
	DSPassword       mysql.Expression
	DSDipCollection  mysql.Expression
	DSAipCollection  mysql.Expression
	AsURL            mysql.Expression
	AsUser           mysql.Expression
	AsPassword       mysql.Expression
	AsRepository     mysql.Expression
	AsArchivalObject mysql.Expression
	VerifySSL        mysql.Expression
	UploadToTSM      mysql.Expression
	SpaceID          mysql.Expression
}

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

func (locationsDspacerestColumns) AliasedAs(alias string) locationsDspacerestColumns {
	return buildLocationsDspacerestColumns(alias)
}

// LocationsDspacerestSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type LocationsDspacerestSetter struct {
	ID               omit.Val[int32]  `db:"id,pk,autoincr" `
	DSRestURL        omit.Val[string] `db:"ds_rest_url" `
	DSUser           omit.Val[string] `db:"ds_user" `
	DSPassword       omit.Val[string] `db:"ds_password" `
	DSDipCollection  omit.Val[string] `db:"ds_dip_collection" `
	DSAipCollection  omit.Val[string] `db:"ds_aip_collection" `
	AsURL            omit.Val[string] `db:"as_url" `
	AsUser           omit.Val[string] `db:"as_user" `
	AsPassword       omit.Val[string] `db:"as_password" `
	AsRepository     omit.Val[string] `db:"as_repository" `
	AsArchivalObject omit.Val[string] `db:"as_archival_object" `
	VerifySSL        omit.Val[bool]   `db:"verify_ssl" `
	UploadToTSM      omit.Val[bool]   `db:"upload_to_tsm" `
	SpaceID          omit.Val[string] `db:"space_id" `
}

func (s LocationsDspacerestSetter) SetColumns() []string {
	vals := make([]string, 0, 14)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.DSRestURL.IsValue() {
		vals = append(vals, "ds_rest_url")
	}
	if s.DSUser.IsValue() {
		vals = append(vals, "ds_user")
	}
	if s.DSPassword.IsValue() {
		vals = append(vals, "ds_password")
	}
	if s.DSDipCollection.IsValue() {
		vals = append(vals, "ds_dip_collection")
	}
	if s.DSAipCollection.IsValue() {
		vals = append(vals, "ds_aip_collection")
	}
	if s.AsURL.IsValue() {
		vals = append(vals, "as_url")
	}
	if s.AsUser.IsValue() {
		vals = append(vals, "as_user")
	}
	if s.AsPassword.IsValue() {
		vals = append(vals, "as_password")
	}
	if s.AsRepository.IsValue() {
		vals = append(vals, "as_repository")
	}
	if s.AsArchivalObject.IsValue() {
		vals = append(vals, "as_archival_object")
	}
	if s.VerifySSL.IsValue() {
		vals = append(vals, "verify_ssl")
	}
	if s.UploadToTSM.IsValue() {
		vals = append(vals, "upload_to_tsm")
	}
	if s.SpaceID.IsValue() {
		vals = append(vals, "space_id")
	}
	return vals
}

func (s LocationsDspacerestSetter) Overwrite(t *LocationsDspacerest) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.DSRestURL.IsValue() {
		t.DSRestURL = s.DSRestURL.MustGet()
	}
	if s.DSUser.IsValue() {
		t.DSUser = s.DSUser.MustGet()
	}
	if s.DSPassword.IsValue() {
		t.DSPassword = s.DSPassword.MustGet()
	}
	if s.DSDipCollection.IsValue() {
		t.DSDipCollection = s.DSDipCollection.MustGet()
	}
	if s.DSAipCollection.IsValue() {
		t.DSAipCollection = s.DSAipCollection.MustGet()
	}
	if s.AsURL.IsValue() {
		t.AsURL = s.AsURL.MustGet()
	}
	if s.AsUser.IsValue() {
		t.AsUser = s.AsUser.MustGet()
	}
	if s.AsPassword.IsValue() {
		t.AsPassword = s.AsPassword.MustGet()
	}
	if s.AsRepository.IsValue() {
		t.AsRepository = s.AsRepository.MustGet()
	}
	if s.AsArchivalObject.IsValue() {
		t.AsArchivalObject = s.AsArchivalObject.MustGet()
	}
	if s.VerifySSL.IsValue() {
		t.VerifySSL = s.VerifySSL.MustGet()
	}
	if s.UploadToTSM.IsValue() {
		t.UploadToTSM = s.UploadToTSM.MustGet()
	}
	if s.SpaceID.IsValue() {
		t.SpaceID = s.SpaceID.MustGet()
	}
}

func (s *LocationsDspacerestSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return LocationsDspacerests.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.DSRestURL.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.DSRestURL.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.DSUser.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.DSUser.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.DSPassword.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.DSPassword.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.DSDipCollection.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.DSDipCollection.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.DSAipCollection.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.DSAipCollection.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.AsURL.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.AsURL.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.AsUser.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.AsUser.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.AsPassword.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.AsPassword.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.AsRepository.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.AsRepository.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.AsArchivalObject.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.AsArchivalObject.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.VerifySSL.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.VerifySSL.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.UploadToTSM.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.UploadToTSM.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 LocationsDspacerestSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions("locations_dspacerest")...)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	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
}

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

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

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

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

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

	return err
}

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

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

	s.Overwrite(o)

	return nil
}

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

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

func (o LocationsDspacerestSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsDspacerestSetter) error {
	_, err := LocationsDspacerests.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

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

	return err
}

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

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

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

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

func (os LocationsDspacerestSlice) 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 attachLocationsDspacerestSpaceLocationsSpace0(ctx context.Context, exec bob.Executor, count int, locationsDspacerest0 *LocationsDspacerest, locationsSpace1 *LocationsSpace) (*LocationsDspacerest, error) {
	setter := &LocationsDspacerestSetter{
		SpaceID: omit.From(locationsSpace1.UUID),
	}

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

	return locationsDspacerest0, nil
}

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

	locationsDspacerest0.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsDspacerest = locationsDspacerest0

	return nil
}

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

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

	locationsDspacerest0.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsDspacerest = locationsDspacerest0

	return nil
}

type locationsDspacerestWhere[Q mysql.Filterable] struct {
	ID               mysql.WhereMod[Q, int32]
	DSRestURL        mysql.WhereMod[Q, string]
	DSUser           mysql.WhereMod[Q, string]
	DSPassword       mysql.WhereMod[Q, string]
	DSDipCollection  mysql.WhereMod[Q, string]
	DSAipCollection  mysql.WhereMod[Q, string]
	AsURL            mysql.WhereMod[Q, string]
	AsUser           mysql.WhereMod[Q, string]
	AsPassword       mysql.WhereMod[Q, string]
	AsRepository     mysql.WhereMod[Q, string]
	AsArchivalObject mysql.WhereMod[Q, string]
	VerifySSL        mysql.WhereMod[Q, bool]
	UploadToTSM      mysql.WhereMod[Q, bool]
	SpaceID          mysql.WhereMod[Q, string]
}

func (locationsDspacerestWhere[Q]) AliasedAs(alias string) locationsDspacerestWhere[Q] {
	return buildLocationsDspacerestWhere[Q](buildLocationsDspacerestColumns(alias))
}

func buildLocationsDspacerestWhere[Q mysql.Filterable](cols locationsDspacerestColumns) locationsDspacerestWhere[Q] {
	return locationsDspacerestWhere[Q]{
		ID:               mysql.Where[Q, int32](cols.ID),
		DSRestURL:        mysql.Where[Q, string](cols.DSRestURL),
		DSUser:           mysql.Where[Q, string](cols.DSUser),
		DSPassword:       mysql.Where[Q, string](cols.DSPassword),
		DSDipCollection:  mysql.Where[Q, string](cols.DSDipCollection),
		DSAipCollection:  mysql.Where[Q, string](cols.DSAipCollection),
		AsURL:            mysql.Where[Q, string](cols.AsURL),
		AsUser:           mysql.Where[Q, string](cols.AsUser),
		AsPassword:       mysql.Where[Q, string](cols.AsPassword),
		AsRepository:     mysql.Where[Q, string](cols.AsRepository),
		AsArchivalObject: mysql.Where[Q, string](cols.AsArchivalObject),
		VerifySSL:        mysql.Where[Q, bool](cols.VerifySSL),
		UploadToTSM:      mysql.Where[Q, bool](cols.UploadToTSM),
		SpaceID:          mysql.Where[Q, string](cols.SpaceID),
	}
}

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

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

		o.R.SpaceLocationsSpace = rel

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

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

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

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

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

	return locationsDspacerestThenLoader[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 locationsDspacerest's SpaceLocationsSpace into the .R struct
func (o *LocationsDspacerest) 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.SpaceLocationsDspacerest = o

	o.R.SpaceLocationsSpace = related
	return nil
}

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

			o.R.SpaceLocationsSpace = rel
			break
		}
	}

	return nil
}

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

func (j locationsDspacerestJoins[Q]) aliasedAs(alias string) locationsDspacerestJoins[Q] {
	return buildLocationsDspacerestJoins[Q](buildLocationsDspacerestColumns(alias), j.typ)
}

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