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

// LocationsPackagedownloadtaskfile is an object representing the database table.
type LocationsPackagedownloadtaskfile struct {
	ID        int32  `db:"id,pk,autoincr" `
	UUID      string `db:"uuid" `
	Filename  string `db:"filename" `
	URL       string `db:"url" `
	Completed bool   `db:"completed" `
	Failed    bool   `db:"failed" `
	TaskID    string `db:"task_id" `

	R locationsPackagedownloadtaskfileR `db:"-" `
}

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

// LocationsPackagedownloadtaskfiles contains methods to work with the locations_packagedownloadtaskfile table
var LocationsPackagedownloadtaskfiles = mysql.NewTablex[*LocationsPackagedownloadtaskfile, LocationsPackagedownloadtaskfileSlice, *LocationsPackagedownloadtaskfileSetter]("locations_packagedownloadtaskfile", buildLocationsPackagedownloadtaskfileColumns("locations_packagedownloadtaskfile"), []string{"id"}, []string{"uuid"})

// LocationsPackagedownloadtaskfilesQuery is a query on the locations_packagedownloadtaskfile table
type LocationsPackagedownloadtaskfilesQuery = *mysql.ViewQuery[*LocationsPackagedownloadtaskfile, LocationsPackagedownloadtaskfileSlice]

// locationsPackagedownloadtaskfileR is where relationships are stored.
type locationsPackagedownloadtaskfileR struct {
	TaskLocationsPackagedownloadtask *LocationsPackagedownloadtask // locations_packagedow_task_id_a112c9ea_fk_locations
}

func buildLocationsPackagedownloadtaskfileColumns(alias string) locationsPackagedownloadtaskfileColumns {
	return locationsPackagedownloadtaskfileColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "uuid", "filename", "url", "completed", "failed", "task_id",
		).WithParent("locations_packagedownloadtaskfile"),
		tableAlias: alias,
		ID:         mysql.Quote(alias, "id"),
		UUID:       mysql.Quote(alias, "uuid"),
		Filename:   mysql.Quote(alias, "filename"),
		URL:        mysql.Quote(alias, "url"),
		Completed:  mysql.Quote(alias, "completed"),
		Failed:     mysql.Quote(alias, "failed"),
		TaskID:     mysql.Quote(alias, "task_id"),
	}
}

type locationsPackagedownloadtaskfileColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ID         mysql.Expression
	UUID       mysql.Expression
	Filename   mysql.Expression
	URL        mysql.Expression
	Completed  mysql.Expression
	Failed     mysql.Expression
	TaskID     mysql.Expression
}

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

func (locationsPackagedownloadtaskfileColumns) AliasedAs(alias string) locationsPackagedownloadtaskfileColumns {
	return buildLocationsPackagedownloadtaskfileColumns(alias)
}

// LocationsPackagedownloadtaskfileSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type LocationsPackagedownloadtaskfileSetter struct {
	ID        omit.Val[int32]  `db:"id,pk,autoincr" `
	UUID      omit.Val[string] `db:"uuid" `
	Filename  omit.Val[string] `db:"filename" `
	URL       omit.Val[string] `db:"url" `
	Completed omit.Val[bool]   `db:"completed" `
	Failed    omit.Val[bool]   `db:"failed" `
	TaskID    omit.Val[string] `db:"task_id" `
}

func (s LocationsPackagedownloadtaskfileSetter) SetColumns() []string {
	vals := make([]string, 0, 7)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.UUID.IsValue() {
		vals = append(vals, "uuid")
	}
	if s.Filename.IsValue() {
		vals = append(vals, "filename")
	}
	if s.URL.IsValue() {
		vals = append(vals, "url")
	}
	if s.Completed.IsValue() {
		vals = append(vals, "completed")
	}
	if s.Failed.IsValue() {
		vals = append(vals, "failed")
	}
	if s.TaskID.IsValue() {
		vals = append(vals, "task_id")
	}
	return vals
}

func (s LocationsPackagedownloadtaskfileSetter) Overwrite(t *LocationsPackagedownloadtaskfile) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.UUID.IsValue() {
		t.UUID = s.UUID.MustGet()
	}
	if s.Filename.IsValue() {
		t.Filename = s.Filename.MustGet()
	}
	if s.URL.IsValue() {
		t.URL = s.URL.MustGet()
	}
	if s.Completed.IsValue() {
		t.Completed = s.Completed.MustGet()
	}
	if s.Failed.IsValue() {
		t.Failed = s.Failed.MustGet()
	}
	if s.TaskID.IsValue() {
		t.TaskID = s.TaskID.MustGet()
	}
}

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

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

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

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

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

	s.Overwrite(o)

	return nil
}

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

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

func (o LocationsPackagedownloadtaskfileSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsPackagedownloadtaskfileSetter) error {
	_, err := LocationsPackagedownloadtaskfiles.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

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

	return err
}

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// TaskLocationsPackagedownloadtask starts a query for related objects on locations_packagedownloadtask
func (o *LocationsPackagedownloadtaskfile) TaskLocationsPackagedownloadtask(mods ...bob.Mod[*dialect.SelectQuery]) LocationsPackagedownloadtasksQuery {
	return LocationsPackagedownloadtasks.Query(append(mods,
		sm.Where(LocationsPackagedownloadtasks.Columns.UUID.EQ(mysql.Arg(o.TaskID))),
	)...)
}

func (os LocationsPackagedownloadtaskfileSlice) TaskLocationsPackagedownloadtask(mods ...bob.Mod[*dialect.SelectQuery]) LocationsPackagedownloadtasksQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.TaskID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

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

func attachLocationsPackagedownloadtaskfileTaskLocationsPackagedownloadtask0(ctx context.Context, exec bob.Executor, count int, locationsPackagedownloadtaskfile0 *LocationsPackagedownloadtaskfile, locationsPackagedownloadtask1 *LocationsPackagedownloadtask) (*LocationsPackagedownloadtaskfile, error) {
	setter := &LocationsPackagedownloadtaskfileSetter{
		TaskID: omit.From(locationsPackagedownloadtask1.UUID),
	}

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

	return locationsPackagedownloadtaskfile0, nil
}

func (locationsPackagedownloadtaskfile0 *LocationsPackagedownloadtaskfile) InsertTaskLocationsPackagedownloadtask(ctx context.Context, exec bob.Executor, related *LocationsPackagedownloadtaskSetter) error {
	var err error

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

	_, err = attachLocationsPackagedownloadtaskfileTaskLocationsPackagedownloadtask0(ctx, exec, 1, locationsPackagedownloadtaskfile0, locationsPackagedownloadtask1)
	if err != nil {
		return err
	}

	locationsPackagedownloadtaskfile0.R.TaskLocationsPackagedownloadtask = locationsPackagedownloadtask1

	locationsPackagedownloadtask1.R.UUIDLocationsPackagedownloadtaskfiles = append(locationsPackagedownloadtask1.R.UUIDLocationsPackagedownloadtaskfiles, locationsPackagedownloadtaskfile0)

	return nil
}

func (locationsPackagedownloadtaskfile0 *LocationsPackagedownloadtaskfile) AttachTaskLocationsPackagedownloadtask(ctx context.Context, exec bob.Executor, locationsPackagedownloadtask1 *LocationsPackagedownloadtask) error {
	var err error

	_, err = attachLocationsPackagedownloadtaskfileTaskLocationsPackagedownloadtask0(ctx, exec, 1, locationsPackagedownloadtaskfile0, locationsPackagedownloadtask1)
	if err != nil {
		return err
	}

	locationsPackagedownloadtaskfile0.R.TaskLocationsPackagedownloadtask = locationsPackagedownloadtask1

	locationsPackagedownloadtask1.R.UUIDLocationsPackagedownloadtaskfiles = append(locationsPackagedownloadtask1.R.UUIDLocationsPackagedownloadtaskfiles, locationsPackagedownloadtaskfile0)

	return nil
}

type locationsPackagedownloadtaskfileWhere[Q mysql.Filterable] struct {
	ID        mysql.WhereMod[Q, int32]
	UUID      mysql.WhereMod[Q, string]
	Filename  mysql.WhereMod[Q, string]
	URL       mysql.WhereMod[Q, string]
	Completed mysql.WhereMod[Q, bool]
	Failed    mysql.WhereMod[Q, bool]
	TaskID    mysql.WhereMod[Q, string]
}

func (locationsPackagedownloadtaskfileWhere[Q]) AliasedAs(alias string) locationsPackagedownloadtaskfileWhere[Q] {
	return buildLocationsPackagedownloadtaskfileWhere[Q](buildLocationsPackagedownloadtaskfileColumns(alias))
}

func buildLocationsPackagedownloadtaskfileWhere[Q mysql.Filterable](cols locationsPackagedownloadtaskfileColumns) locationsPackagedownloadtaskfileWhere[Q] {
	return locationsPackagedownloadtaskfileWhere[Q]{
		ID:        mysql.Where[Q, int32](cols.ID),
		UUID:      mysql.Where[Q, string](cols.UUID),
		Filename:  mysql.Where[Q, string](cols.Filename),
		URL:       mysql.Where[Q, string](cols.URL),
		Completed: mysql.Where[Q, bool](cols.Completed),
		Failed:    mysql.Where[Q, bool](cols.Failed),
		TaskID:    mysql.Where[Q, string](cols.TaskID),
	}
}

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

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

		o.R.TaskLocationsPackagedownloadtask = rel

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

type locationsPackagedownloadtaskfilePreloader struct {
	TaskLocationsPackagedownloadtask func(...mysql.PreloadOption) mysql.Preloader
}

func buildLocationsPackagedownloadtaskfilePreloader() locationsPackagedownloadtaskfilePreloader {
	return locationsPackagedownloadtaskfilePreloader{
		TaskLocationsPackagedownloadtask: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*LocationsPackagedownloadtask, LocationsPackagedownloadtaskSlice](mysql.PreloadRel{
				Name: "TaskLocationsPackagedownloadtask",
				Sides: []mysql.PreloadSide{
					{
						From:        LocationsPackagedownloadtaskfiles,
						To:          LocationsPackagedownloadtasks,
						FromColumns: []string{"task_id"},
						ToColumns:   []string{"uuid"},
					},
				},
			}, LocationsPackagedownloadtasks.Columns.Names(), opts...)
		},
	}
}

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

func buildLocationsPackagedownloadtaskfileThenLoader[Q orm.Loadable]() locationsPackagedownloadtaskfileThenLoader[Q] {
	type TaskLocationsPackagedownloadtaskLoadInterface interface {
		LoadTaskLocationsPackagedownloadtask(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return locationsPackagedownloadtaskfileThenLoader[Q]{
		TaskLocationsPackagedownloadtask: thenLoadBuilder[Q](
			"TaskLocationsPackagedownloadtask",
			func(ctx context.Context, exec bob.Executor, retrieved TaskLocationsPackagedownloadtaskLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadTaskLocationsPackagedownloadtask(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.UUIDLocationsPackagedownloadtaskfiles = LocationsPackagedownloadtaskfileSlice{o}

	o.R.TaskLocationsPackagedownloadtask = related
	return nil
}

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

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

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

		for _, rel := range locationsPackagedownloadtasks {

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

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

			o.R.TaskLocationsPackagedownloadtask = rel
			break
		}
	}

	return nil
}

type locationsPackagedownloadtaskfileJoins[Q dialect.Joinable] struct {
	typ                              string
	TaskLocationsPackagedownloadtask modAs[Q, locationsPackagedownloadtaskColumns]
}

func (j locationsPackagedownloadtaskfileJoins[Q]) aliasedAs(alias string) locationsPackagedownloadtaskfileJoins[Q] {
	return buildLocationsPackagedownloadtaskfileJoins[Q](buildLocationsPackagedownloadtaskfileColumns(alias), j.typ)
}

func buildLocationsPackagedownloadtaskfileJoins[Q dialect.Joinable](cols locationsPackagedownloadtaskfileColumns, typ string) locationsPackagedownloadtaskfileJoins[Q] {
	return locationsPackagedownloadtaskfileJoins[Q]{
		typ: typ,
		TaskLocationsPackagedownloadtask: modAs[Q, locationsPackagedownloadtaskColumns]{
			c: LocationsPackagedownloadtasks.Columns,
			f: func(to locationsPackagedownloadtaskColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

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

				return mods
			},
		},
	}
}
