// Code generated by BobGen psql v0.42.5. 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"
	"time"

	"github.com/Gleipnir-Technology/bob"
	"github.com/Gleipnir-Technology/bob/dialect/psql"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/um"
	"github.com/Gleipnir-Technology/bob/expr"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	"github.com/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	enums "source.gleipnir.technology/Gleipnir/nidus-sync/db/enums"
)

// FileuploadCSV is an object representing the database table.
type FileuploadCSV struct {
	Committed null.Val[time.Time]     `db:"committed" `
	FileID    int32                   `db:"file_id,pk" `
	Rowcount  int32                   `db:"rowcount" `
	Type      enums.FileuploadCsvtype `db:"type_" `

	R fileuploadCSVR `db:"-" `
}

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

// FileuploadCSVS contains methods to work with the csv table
var FileuploadCSVS = psql.NewTablex[*FileuploadCSV, FileuploadCSVSlice, *FileuploadCSVSetter]("fileupload", "csv", buildFileuploadCSVColumns("fileupload.csv"))

// FileuploadCSVSQuery is a query on the csv table
type FileuploadCSVSQuery = *psql.ViewQuery[*FileuploadCSV, FileuploadCSVSlice]

// fileuploadCSVR is where relationships are stored.
type fileuploadCSVR struct {
	File             *FileuploadFile         // fileupload.csv.csv_file_id_fkey
	CSVFileErrorCSVS FileuploadErrorCSVSlice // fileupload.error_csv.error_csv_csv_file_id_fkey
	CSVFilePools     FileuploadPoolSlice     // fileupload.pool.pool_csv_file_fkey
}

func buildFileuploadCSVColumns(alias string) fileuploadCSVColumns {
	return fileuploadCSVColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"committed", "file_id", "rowcount", "type_",
		).WithParent("fileupload.csv"),
		tableAlias: alias,
		Committed:  psql.Quote(alias, "committed"),
		FileID:     psql.Quote(alias, "file_id"),
		Rowcount:   psql.Quote(alias, "rowcount"),
		Type:       psql.Quote(alias, "type_"),
	}
}

type fileuploadCSVColumns struct {
	expr.ColumnsExpr
	tableAlias string
	Committed  psql.Expression
	FileID     psql.Expression
	Rowcount   psql.Expression
	Type       psql.Expression
}

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

func (fileuploadCSVColumns) AliasedAs(alias string) fileuploadCSVColumns {
	return buildFileuploadCSVColumns(alias)
}

// FileuploadCSVSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type FileuploadCSVSetter struct {
	Committed omitnull.Val[time.Time]           `db:"committed" `
	FileID    omit.Val[int32]                   `db:"file_id,pk" `
	Rowcount  omit.Val[int32]                   `db:"rowcount" `
	Type      omit.Val[enums.FileuploadCsvtype] `db:"type_" `
}

func (s FileuploadCSVSetter) SetColumns() []string {
	vals := make([]string, 0, 4)
	if !s.Committed.IsUnset() {
		vals = append(vals, "committed")
	}
	if s.FileID.IsValue() {
		vals = append(vals, "file_id")
	}
	if s.Rowcount.IsValue() {
		vals = append(vals, "rowcount")
	}
	if s.Type.IsValue() {
		vals = append(vals, "type_")
	}
	return vals
}

func (s FileuploadCSVSetter) Overwrite(t *FileuploadCSV) {
	if !s.Committed.IsUnset() {
		t.Committed = s.Committed.MustGetNull()
	}
	if s.FileID.IsValue() {
		t.FileID = s.FileID.MustGet()
	}
	if s.Rowcount.IsValue() {
		t.Rowcount = s.Rowcount.MustGet()
	}
	if s.Type.IsValue() {
		t.Type = s.Type.MustGet()
	}
}

func (s *FileuploadCSVSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return FileuploadCSVS.BeforeInsertHooks.RunHooks(ctx, exec, s)
	})

	q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		vals := make([]bob.Expression, 4)
		if !s.Committed.IsUnset() {
			vals[0] = psql.Arg(s.Committed.MustGetNull())
		} else {
			vals[0] = psql.Raw("DEFAULT")
		}

		if s.FileID.IsValue() {
			vals[1] = psql.Arg(s.FileID.MustGet())
		} else {
			vals[1] = psql.Raw("DEFAULT")
		}

		if s.Rowcount.IsValue() {
			vals[2] = psql.Arg(s.Rowcount.MustGet())
		} else {
			vals[2] = psql.Raw("DEFAULT")
		}

		if s.Type.IsValue() {
			vals[3] = psql.Arg(s.Type.MustGet())
		} else {
			vals[3] = psql.Raw("DEFAULT")
		}

		return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
	}))
}

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

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

	if !s.Committed.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "committed")...),
			psql.Arg(s.Committed),
		}})
	}

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

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

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

	return exprs
}

// FindFileuploadCSV retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindFileuploadCSV(ctx context.Context, exec bob.Executor, FileIDPK int32, cols ...string) (*FileuploadCSV, error) {
	if len(cols) == 0 {
		return FileuploadCSVS.Query(
			sm.Where(FileuploadCSVS.Columns.FileID.EQ(psql.Arg(FileIDPK))),
		).One(ctx, exec)
	}

	return FileuploadCSVS.Query(
		sm.Where(FileuploadCSVS.Columns.FileID.EQ(psql.Arg(FileIDPK))),
		sm.Columns(FileuploadCSVS.Columns.Only(cols...)),
	).One(ctx, exec)
}

// FileuploadCSVExists checks the presence of a single record by primary key
func FileuploadCSVExists(ctx context.Context, exec bob.Executor, FileIDPK int32) (bool, error) {
	return FileuploadCSVS.Query(
		sm.Where(FileuploadCSVS.Columns.FileID.EQ(psql.Arg(FileIDPK))),
	).Exists(ctx, exec)
}

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

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

	return err
}

// primaryKeyVals returns the primary key values of the FileuploadCSV
func (o *FileuploadCSV) primaryKeyVals() bob.Expression {
	return psql.Arg(o.FileID)
}

func (o *FileuploadCSV) pkEQ() dialect.Expression {
	return psql.Quote("fileupload.csv", "file_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 FileuploadCSV
func (o *FileuploadCSV) Update(ctx context.Context, exec bob.Executor, s *FileuploadCSVSetter) error {
	v, err := FileuploadCSVS.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
	if err != nil {
		return err
	}

	o.R = v.R
	*o = *v

	return nil
}

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

// Reload refreshes the FileuploadCSV using the executor
func (o *FileuploadCSV) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := FileuploadCSVS.Query(
		sm.Where(FileuploadCSVS.Columns.FileID.EQ(psql.Arg(o.FileID))),
	).One(ctx, exec)
	if err != nil {
		return err
	}
	o2.R = o.R
	*o = *o2

	return nil
}

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

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

	return err
}

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

	return psql.Quote("fileupload.csv", "file_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 FileuploadCSVSlice) copyMatchingRows(from ...*FileuploadCSV) {
	for i, old := range o {
		for _, new := range from {
			if new.FileID != old.FileID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

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

			return err
		}))

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

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

			return err
		}))

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

func (o FileuploadCSVSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals FileuploadCSVSetter) error {
	if len(o) == 0 {
		return nil
	}

	_, err := FileuploadCSVS.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
	return err
}

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// File starts a query for related objects on fileupload.file
func (o *FileuploadCSV) File(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadFilesQuery {
	return FileuploadFiles.Query(append(mods,
		sm.Where(FileuploadFiles.Columns.ID.EQ(psql.Arg(o.FileID))),
	)...)
}

func (os FileuploadCSVSlice) File(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadFilesQuery {
	pkFileID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkFileID = append(pkFileID, o.FileID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkFileID), "integer[]")),
	))

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

// CSVFileErrorCSVS starts a query for related objects on fileupload.error_csv
func (o *FileuploadCSV) CSVFileErrorCSVS(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadErrorCSVSQuery {
	return FileuploadErrorCSVS.Query(append(mods,
		sm.Where(FileuploadErrorCSVS.Columns.CSVFileID.EQ(psql.Arg(o.FileID))),
	)...)
}

func (os FileuploadCSVSlice) CSVFileErrorCSVS(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadErrorCSVSQuery {
	pkFileID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkFileID = append(pkFileID, o.FileID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkFileID), "integer[]")),
	))

	return FileuploadErrorCSVS.Query(append(mods,
		sm.Where(psql.Group(FileuploadErrorCSVS.Columns.CSVFileID).OP("IN", PKArgExpr)),
	)...)
}

// CSVFilePools starts a query for related objects on fileupload.pool
func (o *FileuploadCSV) CSVFilePools(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadPoolsQuery {
	return FileuploadPools.Query(append(mods,
		sm.Where(FileuploadPools.Columns.CSVFile.EQ(psql.Arg(o.FileID))),
	)...)
}

func (os FileuploadCSVSlice) CSVFilePools(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadPoolsQuery {
	pkFileID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkFileID = append(pkFileID, o.FileID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkFileID), "integer[]")),
	))

	return FileuploadPools.Query(append(mods,
		sm.Where(psql.Group(FileuploadPools.Columns.CSVFile).OP("IN", PKArgExpr)),
	)...)
}

func attachFileuploadCSVFile0(ctx context.Context, exec bob.Executor, count int, fileuploadCSV0 *FileuploadCSV, fileuploadFile1 *FileuploadFile) (*FileuploadCSV, error) {
	setter := &FileuploadCSVSetter{
		FileID: omit.From(fileuploadFile1.ID),
	}

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

	return fileuploadCSV0, nil
}

func (fileuploadCSV0 *FileuploadCSV) InsertFile(ctx context.Context, exec bob.Executor, related *FileuploadFileSetter) error {
	var err error

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

	_, err = attachFileuploadCSVFile0(ctx, exec, 1, fileuploadCSV0, fileuploadFile1)
	if err != nil {
		return err
	}

	fileuploadCSV0.R.File = fileuploadFile1

	fileuploadFile1.R.CSV = fileuploadCSV0

	return nil
}

func (fileuploadCSV0 *FileuploadCSV) AttachFile(ctx context.Context, exec bob.Executor, fileuploadFile1 *FileuploadFile) error {
	var err error

	_, err = attachFileuploadCSVFile0(ctx, exec, 1, fileuploadCSV0, fileuploadFile1)
	if err != nil {
		return err
	}

	fileuploadCSV0.R.File = fileuploadFile1

	fileuploadFile1.R.CSV = fileuploadCSV0

	return nil
}

func insertFileuploadCSVCSVFileErrorCSVS0(ctx context.Context, exec bob.Executor, fileuploadErrorCSVS1 []*FileuploadErrorCSVSetter, fileuploadCSV0 *FileuploadCSV) (FileuploadErrorCSVSlice, error) {
	for i := range fileuploadErrorCSVS1 {
		fileuploadErrorCSVS1[i].CSVFileID = omit.From(fileuploadCSV0.FileID)
	}

	ret, err := FileuploadErrorCSVS.Insert(bob.ToMods(fileuploadErrorCSVS1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertFileuploadCSVCSVFileErrorCSVS0: %w", err)
	}

	return ret, nil
}

func attachFileuploadCSVCSVFileErrorCSVS0(ctx context.Context, exec bob.Executor, count int, fileuploadErrorCSVS1 FileuploadErrorCSVSlice, fileuploadCSV0 *FileuploadCSV) (FileuploadErrorCSVSlice, error) {
	setter := &FileuploadErrorCSVSetter{
		CSVFileID: omit.From(fileuploadCSV0.FileID),
	}

	err := fileuploadErrorCSVS1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachFileuploadCSVCSVFileErrorCSVS0: %w", err)
	}

	return fileuploadErrorCSVS1, nil
}

func (fileuploadCSV0 *FileuploadCSV) InsertCSVFileErrorCSVS(ctx context.Context, exec bob.Executor, related ...*FileuploadErrorCSVSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	fileuploadErrorCSVS1, err := insertFileuploadCSVCSVFileErrorCSVS0(ctx, exec, related, fileuploadCSV0)
	if err != nil {
		return err
	}

	fileuploadCSV0.R.CSVFileErrorCSVS = append(fileuploadCSV0.R.CSVFileErrorCSVS, fileuploadErrorCSVS1...)

	for _, rel := range fileuploadErrorCSVS1 {
		rel.R.CSVFileCSV = fileuploadCSV0
	}
	return nil
}

func (fileuploadCSV0 *FileuploadCSV) AttachCSVFileErrorCSVS(ctx context.Context, exec bob.Executor, related ...*FileuploadErrorCSV) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	fileuploadErrorCSVS1 := FileuploadErrorCSVSlice(related)

	_, err = attachFileuploadCSVCSVFileErrorCSVS0(ctx, exec, len(related), fileuploadErrorCSVS1, fileuploadCSV0)
	if err != nil {
		return err
	}

	fileuploadCSV0.R.CSVFileErrorCSVS = append(fileuploadCSV0.R.CSVFileErrorCSVS, fileuploadErrorCSVS1...)

	for _, rel := range related {
		rel.R.CSVFileCSV = fileuploadCSV0
	}

	return nil
}

func insertFileuploadCSVCSVFilePools0(ctx context.Context, exec bob.Executor, fileuploadPools1 []*FileuploadPoolSetter, fileuploadCSV0 *FileuploadCSV) (FileuploadPoolSlice, error) {
	for i := range fileuploadPools1 {
		fileuploadPools1[i].CSVFile = omit.From(fileuploadCSV0.FileID)
	}

	ret, err := FileuploadPools.Insert(bob.ToMods(fileuploadPools1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertFileuploadCSVCSVFilePools0: %w", err)
	}

	return ret, nil
}

func attachFileuploadCSVCSVFilePools0(ctx context.Context, exec bob.Executor, count int, fileuploadPools1 FileuploadPoolSlice, fileuploadCSV0 *FileuploadCSV) (FileuploadPoolSlice, error) {
	setter := &FileuploadPoolSetter{
		CSVFile: omit.From(fileuploadCSV0.FileID),
	}

	err := fileuploadPools1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachFileuploadCSVCSVFilePools0: %w", err)
	}

	return fileuploadPools1, nil
}

func (fileuploadCSV0 *FileuploadCSV) InsertCSVFilePools(ctx context.Context, exec bob.Executor, related ...*FileuploadPoolSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	fileuploadPools1, err := insertFileuploadCSVCSVFilePools0(ctx, exec, related, fileuploadCSV0)
	if err != nil {
		return err
	}

	fileuploadCSV0.R.CSVFilePools = append(fileuploadCSV0.R.CSVFilePools, fileuploadPools1...)

	for _, rel := range fileuploadPools1 {
		rel.R.CSVFileCSV = fileuploadCSV0
	}
	return nil
}

func (fileuploadCSV0 *FileuploadCSV) AttachCSVFilePools(ctx context.Context, exec bob.Executor, related ...*FileuploadPool) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	fileuploadPools1 := FileuploadPoolSlice(related)

	_, err = attachFileuploadCSVCSVFilePools0(ctx, exec, len(related), fileuploadPools1, fileuploadCSV0)
	if err != nil {
		return err
	}

	fileuploadCSV0.R.CSVFilePools = append(fileuploadCSV0.R.CSVFilePools, fileuploadPools1...)

	for _, rel := range related {
		rel.R.CSVFileCSV = fileuploadCSV0
	}

	return nil
}

type fileuploadCSVWhere[Q psql.Filterable] struct {
	Committed psql.WhereNullMod[Q, time.Time]
	FileID    psql.WhereMod[Q, int32]
	Rowcount  psql.WhereMod[Q, int32]
	Type      psql.WhereMod[Q, enums.FileuploadCsvtype]
}

func (fileuploadCSVWhere[Q]) AliasedAs(alias string) fileuploadCSVWhere[Q] {
	return buildFileuploadCSVWhere[Q](buildFileuploadCSVColumns(alias))
}

func buildFileuploadCSVWhere[Q psql.Filterable](cols fileuploadCSVColumns) fileuploadCSVWhere[Q] {
	return fileuploadCSVWhere[Q]{
		Committed: psql.WhereNull[Q, time.Time](cols.Committed),
		FileID:    psql.Where[Q, int32](cols.FileID),
		Rowcount:  psql.Where[Q, int32](cols.Rowcount),
		Type:      psql.Where[Q, enums.FileuploadCsvtype](cols.Type),
	}
}

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

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

		o.R.File = rel

		if rel != nil {
			rel.R.CSV = o
		}
		return nil
	case "CSVFileErrorCSVS":
		rels, ok := retrieved.(FileuploadErrorCSVSlice)
		if !ok {
			return fmt.Errorf("fileuploadCSV cannot load %T as %q", retrieved, name)
		}

		o.R.CSVFileErrorCSVS = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CSVFileCSV = o
			}
		}
		return nil
	case "CSVFilePools":
		rels, ok := retrieved.(FileuploadPoolSlice)
		if !ok {
			return fmt.Errorf("fileuploadCSV cannot load %T as %q", retrieved, name)
		}

		o.R.CSVFilePools = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CSVFileCSV = o
			}
		}
		return nil
	default:
		return fmt.Errorf("fileuploadCSV has no relationship %q", name)
	}
}

type fileuploadCSVPreloader struct {
	File func(...psql.PreloadOption) psql.Preloader
}

func buildFileuploadCSVPreloader() fileuploadCSVPreloader {
	return fileuploadCSVPreloader{
		File: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*FileuploadFile, FileuploadFileSlice](psql.PreloadRel{
				Name: "File",
				Sides: []psql.PreloadSide{
					{
						From:        FileuploadCSVS,
						To:          FileuploadFiles,
						FromColumns: []string{"file_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, FileuploadFiles.Columns.Names(), opts...)
		},
	}
}

type fileuploadCSVThenLoader[Q orm.Loadable] struct {
	File             func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CSVFileErrorCSVS func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CSVFilePools     func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildFileuploadCSVThenLoader[Q orm.Loadable]() fileuploadCSVThenLoader[Q] {
	type FileLoadInterface interface {
		LoadFile(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CSVFileErrorCSVSLoadInterface interface {
		LoadCSVFileErrorCSVS(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CSVFilePoolsLoadInterface interface {
		LoadCSVFilePools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return fileuploadCSVThenLoader[Q]{
		File: thenLoadBuilder[Q](
			"File",
			func(ctx context.Context, exec bob.Executor, retrieved FileLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadFile(ctx, exec, mods...)
			},
		),
		CSVFileErrorCSVS: thenLoadBuilder[Q](
			"CSVFileErrorCSVS",
			func(ctx context.Context, exec bob.Executor, retrieved CSVFileErrorCSVSLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCSVFileErrorCSVS(ctx, exec, mods...)
			},
		),
		CSVFilePools: thenLoadBuilder[Q](
			"CSVFilePools",
			func(ctx context.Context, exec bob.Executor, retrieved CSVFilePoolsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCSVFilePools(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.CSV = o

	o.R.File = related
	return nil
}

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

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

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

		for _, rel := range fileuploadFiles {

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

			rel.R.CSV = o

			o.R.File = rel
			break
		}
	}

	return nil
}

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

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

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

	for _, rel := range related {
		rel.R.CSVFileCSV = o
	}

	o.R.CSVFileErrorCSVS = related
	return nil
}

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

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

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

		o.R.CSVFileErrorCSVS = nil
	}

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

		for _, rel := range fileuploadErrorCSVS {

			if !(o.FileID == rel.CSVFileID) {
				continue
			}

			rel.R.CSVFileCSV = o

			o.R.CSVFileErrorCSVS = append(o.R.CSVFileErrorCSVS, rel)
		}
	}

	return nil
}

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

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

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

	for _, rel := range related {
		rel.R.CSVFileCSV = o
	}

	o.R.CSVFilePools = related
	return nil
}

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

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

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

		o.R.CSVFilePools = nil
	}

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

		for _, rel := range fileuploadPools {

			if !(o.FileID == rel.CSVFile) {
				continue
			}

			rel.R.CSVFileCSV = o

			o.R.CSVFilePools = append(o.R.CSVFilePools, rel)
		}
	}

	return nil
}
