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

// FileuploadPool is an object representing the database table.
type FileuploadPool struct {
	AddressPostalCode      string                  `db:"address_postal_code" `
	AddressStreet          string                  `db:"address_street" `
	Committed              bool                    `db:"committed" `
	Created                time.Time               `db:"created" `
	CreatorID              int32                   `db:"creator_id" `
	CSVFile                int32                   `db:"csv_file" `
	Deleted                null.Val[time.Time]     `db:"deleted" `
	Geom                   null.Val[string]        `db:"geom" `
	H3cell                 null.Val[string]        `db:"h3cell" `
	ID                     int32                   `db:"id,pk" `
	IsInDistrict           bool                    `db:"is_in_district" `
	IsNew                  bool                    `db:"is_new" `
	Notes                  string                  `db:"notes" `
	PropertyOwnerName      string                  `db:"property_owner_name" `
	PropertyOwnerPhoneE164 null.Val[string]        `db:"property_owner_phone_e164" `
	ResidentOwned          null.Val[bool]          `db:"resident_owned" `
	ResidentPhoneE164      null.Val[string]        `db:"resident_phone_e164" `
	LineNumber             int32                   `db:"line_number" `
	Tags                   pgtypes.HStore          `db:"tags" `
	AddressNumber          string                  `db:"address_number" `
	AddressLocality        string                  `db:"address_locality" `
	AddressRegion          string                  `db:"address_region" `
	Condition              enums.Poolconditiontype `db:"condition" `
	AddressID              null.Val[int32]         `db:"address_id" `

	R fileuploadPoolR `db:"-" `
}

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

// FileuploadPools contains methods to work with the pool table
var FileuploadPools = psql.NewTablex[*FileuploadPool, FileuploadPoolSlice, *FileuploadPoolSetter]("fileupload", "pool", buildFileuploadPoolColumns("fileupload.pool"))

// FileuploadPoolsQuery is a query on the pool table
type FileuploadPoolsQuery = *psql.ViewQuery[*FileuploadPool, FileuploadPoolSlice]

// fileuploadPoolR is where relationships are stored.
type fileuploadPoolR struct {
	Address                     *Address       // fileupload.pool.pool_address_id_fkey
	CreatorUser                 *User          // fileupload.pool.pool_creator_id_fkey
	CSVFileCSV                  *FileuploadCSV // fileupload.pool.pool_csv_file_fkey
	PropertyOwnerPhoneE164Phone *CommsPhone    // fileupload.pool.pool_property_owner_phone_e164_fkey
	ResidentPhoneE164Phone      *CommsPhone    // fileupload.pool.pool_resident_phone_e164_fkey
}

func buildFileuploadPoolColumns(alias string) fileuploadPoolColumns {
	return fileuploadPoolColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"address_postal_code", "address_street", "committed", "created", "creator_id", "csv_file", "deleted", "geom", "h3cell", "id", "is_in_district", "is_new", "notes", "property_owner_name", "property_owner_phone_e164", "resident_owned", "resident_phone_e164", "line_number", "tags", "address_number", "address_locality", "address_region", "condition", "address_id",
		).WithParent("fileupload.pool"),
		tableAlias:             alias,
		AddressPostalCode:      psql.Quote(alias, "address_postal_code"),
		AddressStreet:          psql.Quote(alias, "address_street"),
		Committed:              psql.Quote(alias, "committed"),
		Created:                psql.Quote(alias, "created"),
		CreatorID:              psql.Quote(alias, "creator_id"),
		CSVFile:                psql.Quote(alias, "csv_file"),
		Deleted:                psql.Quote(alias, "deleted"),
		Geom:                   psql.Quote(alias, "geom"),
		H3cell:                 psql.Quote(alias, "h3cell"),
		ID:                     psql.Quote(alias, "id"),
		IsInDistrict:           psql.Quote(alias, "is_in_district"),
		IsNew:                  psql.Quote(alias, "is_new"),
		Notes:                  psql.Quote(alias, "notes"),
		PropertyOwnerName:      psql.Quote(alias, "property_owner_name"),
		PropertyOwnerPhoneE164: psql.Quote(alias, "property_owner_phone_e164"),
		ResidentOwned:          psql.Quote(alias, "resident_owned"),
		ResidentPhoneE164:      psql.Quote(alias, "resident_phone_e164"),
		LineNumber:             psql.Quote(alias, "line_number"),
		Tags:                   psql.Quote(alias, "tags"),
		AddressNumber:          psql.Quote(alias, "address_number"),
		AddressLocality:        psql.Quote(alias, "address_locality"),
		AddressRegion:          psql.Quote(alias, "address_region"),
		Condition:              psql.Quote(alias, "condition"),
		AddressID:              psql.Quote(alias, "address_id"),
	}
}

type fileuploadPoolColumns struct {
	expr.ColumnsExpr
	tableAlias             string
	AddressPostalCode      psql.Expression
	AddressStreet          psql.Expression
	Committed              psql.Expression
	Created                psql.Expression
	CreatorID              psql.Expression
	CSVFile                psql.Expression
	Deleted                psql.Expression
	Geom                   psql.Expression
	H3cell                 psql.Expression
	ID                     psql.Expression
	IsInDistrict           psql.Expression
	IsNew                  psql.Expression
	Notes                  psql.Expression
	PropertyOwnerName      psql.Expression
	PropertyOwnerPhoneE164 psql.Expression
	ResidentOwned          psql.Expression
	ResidentPhoneE164      psql.Expression
	LineNumber             psql.Expression
	Tags                   psql.Expression
	AddressNumber          psql.Expression
	AddressLocality        psql.Expression
	AddressRegion          psql.Expression
	Condition              psql.Expression
	AddressID              psql.Expression
}

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

func (fileuploadPoolColumns) AliasedAs(alias string) fileuploadPoolColumns {
	return buildFileuploadPoolColumns(alias)
}

// FileuploadPoolSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type FileuploadPoolSetter struct {
	AddressPostalCode      omit.Val[string]                  `db:"address_postal_code" `
	AddressStreet          omit.Val[string]                  `db:"address_street" `
	Committed              omit.Val[bool]                    `db:"committed" `
	Created                omit.Val[time.Time]               `db:"created" `
	CreatorID              omit.Val[int32]                   `db:"creator_id" `
	CSVFile                omit.Val[int32]                   `db:"csv_file" `
	Deleted                omitnull.Val[time.Time]           `db:"deleted" `
	Geom                   omitnull.Val[string]              `db:"geom" `
	H3cell                 omitnull.Val[string]              `db:"h3cell" `
	ID                     omit.Val[int32]                   `db:"id,pk" `
	IsInDistrict           omit.Val[bool]                    `db:"is_in_district" `
	IsNew                  omit.Val[bool]                    `db:"is_new" `
	Notes                  omit.Val[string]                  `db:"notes" `
	PropertyOwnerName      omit.Val[string]                  `db:"property_owner_name" `
	PropertyOwnerPhoneE164 omitnull.Val[string]              `db:"property_owner_phone_e164" `
	ResidentOwned          omitnull.Val[bool]                `db:"resident_owned" `
	ResidentPhoneE164      omitnull.Val[string]              `db:"resident_phone_e164" `
	LineNumber             omit.Val[int32]                   `db:"line_number" `
	Tags                   omit.Val[pgtypes.HStore]          `db:"tags" `
	AddressNumber          omit.Val[string]                  `db:"address_number" `
	AddressLocality        omit.Val[string]                  `db:"address_locality" `
	AddressRegion          omit.Val[string]                  `db:"address_region" `
	Condition              omit.Val[enums.Poolconditiontype] `db:"condition" `
	AddressID              omitnull.Val[int32]               `db:"address_id" `
}

func (s FileuploadPoolSetter) SetColumns() []string {
	vals := make([]string, 0, 24)
	if s.AddressPostalCode.IsValue() {
		vals = append(vals, "address_postal_code")
	}
	if s.AddressStreet.IsValue() {
		vals = append(vals, "address_street")
	}
	if s.Committed.IsValue() {
		vals = append(vals, "committed")
	}
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if s.CreatorID.IsValue() {
		vals = append(vals, "creator_id")
	}
	if s.CSVFile.IsValue() {
		vals = append(vals, "csv_file")
	}
	if !s.Deleted.IsUnset() {
		vals = append(vals, "deleted")
	}
	if !s.Geom.IsUnset() {
		vals = append(vals, "geom")
	}
	if !s.H3cell.IsUnset() {
		vals = append(vals, "h3cell")
	}
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.IsInDistrict.IsValue() {
		vals = append(vals, "is_in_district")
	}
	if s.IsNew.IsValue() {
		vals = append(vals, "is_new")
	}
	if s.Notes.IsValue() {
		vals = append(vals, "notes")
	}
	if s.PropertyOwnerName.IsValue() {
		vals = append(vals, "property_owner_name")
	}
	if !s.PropertyOwnerPhoneE164.IsUnset() {
		vals = append(vals, "property_owner_phone_e164")
	}
	if !s.ResidentOwned.IsUnset() {
		vals = append(vals, "resident_owned")
	}
	if !s.ResidentPhoneE164.IsUnset() {
		vals = append(vals, "resident_phone_e164")
	}
	if s.LineNumber.IsValue() {
		vals = append(vals, "line_number")
	}
	if s.Tags.IsValue() {
		vals = append(vals, "tags")
	}
	if s.AddressNumber.IsValue() {
		vals = append(vals, "address_number")
	}
	if s.AddressLocality.IsValue() {
		vals = append(vals, "address_locality")
	}
	if s.AddressRegion.IsValue() {
		vals = append(vals, "address_region")
	}
	if s.Condition.IsValue() {
		vals = append(vals, "condition")
	}
	if !s.AddressID.IsUnset() {
		vals = append(vals, "address_id")
	}
	return vals
}

func (s FileuploadPoolSetter) Overwrite(t *FileuploadPool) {
	if s.AddressPostalCode.IsValue() {
		t.AddressPostalCode = s.AddressPostalCode.MustGet()
	}
	if s.AddressStreet.IsValue() {
		t.AddressStreet = s.AddressStreet.MustGet()
	}
	if s.Committed.IsValue() {
		t.Committed = s.Committed.MustGet()
	}
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if s.CreatorID.IsValue() {
		t.CreatorID = s.CreatorID.MustGet()
	}
	if s.CSVFile.IsValue() {
		t.CSVFile = s.CSVFile.MustGet()
	}
	if !s.Deleted.IsUnset() {
		t.Deleted = s.Deleted.MustGetNull()
	}
	if !s.Geom.IsUnset() {
		t.Geom = s.Geom.MustGetNull()
	}
	if !s.H3cell.IsUnset() {
		t.H3cell = s.H3cell.MustGetNull()
	}
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.IsInDistrict.IsValue() {
		t.IsInDistrict = s.IsInDistrict.MustGet()
	}
	if s.IsNew.IsValue() {
		t.IsNew = s.IsNew.MustGet()
	}
	if s.Notes.IsValue() {
		t.Notes = s.Notes.MustGet()
	}
	if s.PropertyOwnerName.IsValue() {
		t.PropertyOwnerName = s.PropertyOwnerName.MustGet()
	}
	if !s.PropertyOwnerPhoneE164.IsUnset() {
		t.PropertyOwnerPhoneE164 = s.PropertyOwnerPhoneE164.MustGetNull()
	}
	if !s.ResidentOwned.IsUnset() {
		t.ResidentOwned = s.ResidentOwned.MustGetNull()
	}
	if !s.ResidentPhoneE164.IsUnset() {
		t.ResidentPhoneE164 = s.ResidentPhoneE164.MustGetNull()
	}
	if s.LineNumber.IsValue() {
		t.LineNumber = s.LineNumber.MustGet()
	}
	if s.Tags.IsValue() {
		t.Tags = s.Tags.MustGet()
	}
	if s.AddressNumber.IsValue() {
		t.AddressNumber = s.AddressNumber.MustGet()
	}
	if s.AddressLocality.IsValue() {
		t.AddressLocality = s.AddressLocality.MustGet()
	}
	if s.AddressRegion.IsValue() {
		t.AddressRegion = s.AddressRegion.MustGet()
	}
	if s.Condition.IsValue() {
		t.Condition = s.Condition.MustGet()
	}
	if !s.AddressID.IsUnset() {
		t.AddressID = s.AddressID.MustGetNull()
	}
}

func (s *FileuploadPoolSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return FileuploadPools.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, 24)
		if s.AddressPostalCode.IsValue() {
			vals[0] = psql.Arg(s.AddressPostalCode.MustGet())
		} else {
			vals[0] = psql.Raw("DEFAULT")
		}

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

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

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

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

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

		if !s.Deleted.IsUnset() {
			vals[6] = psql.Arg(s.Deleted.MustGetNull())
		} else {
			vals[6] = psql.Raw("DEFAULT")
		}

		if !s.Geom.IsUnset() {
			vals[7] = psql.Arg(s.Geom.MustGetNull())
		} else {
			vals[7] = psql.Raw("DEFAULT")
		}

		if !s.H3cell.IsUnset() {
			vals[8] = psql.Arg(s.H3cell.MustGetNull())
		} else {
			vals[8] = psql.Raw("DEFAULT")
		}

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

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

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

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

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

		if !s.PropertyOwnerPhoneE164.IsUnset() {
			vals[14] = psql.Arg(s.PropertyOwnerPhoneE164.MustGetNull())
		} else {
			vals[14] = psql.Raw("DEFAULT")
		}

		if !s.ResidentOwned.IsUnset() {
			vals[15] = psql.Arg(s.ResidentOwned.MustGetNull())
		} else {
			vals[15] = psql.Raw("DEFAULT")
		}

		if !s.ResidentPhoneE164.IsUnset() {
			vals[16] = psql.Arg(s.ResidentPhoneE164.MustGetNull())
		} else {
			vals[16] = psql.Raw("DEFAULT")
		}

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

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

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

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

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

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

		if !s.AddressID.IsUnset() {
			vals[23] = psql.Arg(s.AddressID.MustGetNull())
		} else {
			vals[23] = psql.Raw("DEFAULT")
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

func (o *FileuploadPool) pkEQ() dialect.Expression {
	return psql.Quote("fileupload.pool", "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 FileuploadPool
func (o *FileuploadPool) Update(ctx context.Context, exec bob.Executor, s *FileuploadPoolSetter) error {
	v, err := FileuploadPools.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 FileuploadPool record with an executor
func (o *FileuploadPool) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := FileuploadPools.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

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

	return nil
}

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// Address starts a query for related objects on address
func (o *FileuploadPool) Address(mods ...bob.Mod[*dialect.SelectQuery]) AddressesQuery {
	return Addresses.Query(append(mods,
		sm.Where(Addresses.Columns.ID.EQ(psql.Arg(o.AddressID))),
	)...)
}

func (os FileuploadPoolSlice) Address(mods ...bob.Mod[*dialect.SelectQuery]) AddressesQuery {
	pkAddressID := make(pgtypes.Array[null.Val[int32]], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkAddressID = append(pkAddressID, o.AddressID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkAddressID), "integer[]")),
	))

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

// CreatorUser starts a query for related objects on user_
func (o *FileuploadPool) CreatorUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
	return Users.Query(append(mods,
		sm.Where(Users.Columns.ID.EQ(psql.Arg(o.CreatorID))),
	)...)
}

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

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

// CSVFileCSV starts a query for related objects on fileupload.csv
func (o *FileuploadPool) CSVFileCSV(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadCSVSQuery {
	return FileuploadCSVS.Query(append(mods,
		sm.Where(FileuploadCSVS.Columns.FileID.EQ(psql.Arg(o.CSVFile))),
	)...)
}

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

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

// PropertyOwnerPhoneE164Phone starts a query for related objects on comms.phone
func (o *FileuploadPool) PropertyOwnerPhoneE164Phone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
	return CommsPhones.Query(append(mods,
		sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(o.PropertyOwnerPhoneE164))),
	)...)
}

func (os FileuploadPoolSlice) PropertyOwnerPhoneE164Phone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
	pkPropertyOwnerPhoneE164 := make(pgtypes.Array[null.Val[string]], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkPropertyOwnerPhoneE164 = append(pkPropertyOwnerPhoneE164, o.PropertyOwnerPhoneE164)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkPropertyOwnerPhoneE164), "text[]")),
	))

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

// ResidentPhoneE164Phone starts a query for related objects on comms.phone
func (o *FileuploadPool) ResidentPhoneE164Phone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
	return CommsPhones.Query(append(mods,
		sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(o.ResidentPhoneE164))),
	)...)
}

func (os FileuploadPoolSlice) ResidentPhoneE164Phone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
	pkResidentPhoneE164 := make(pgtypes.Array[null.Val[string]], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkResidentPhoneE164 = append(pkResidentPhoneE164, o.ResidentPhoneE164)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkResidentPhoneE164), "text[]")),
	))

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

func attachFileuploadPoolAddress0(ctx context.Context, exec bob.Executor, count int, fileuploadPool0 *FileuploadPool, address1 *Address) (*FileuploadPool, error) {
	setter := &FileuploadPoolSetter{
		AddressID: omitnull.From(address1.ID),
	}

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

	return fileuploadPool0, nil
}

func (fileuploadPool0 *FileuploadPool) InsertAddress(ctx context.Context, exec bob.Executor, related *AddressSetter) error {
	var err error

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

	_, err = attachFileuploadPoolAddress0(ctx, exec, 1, fileuploadPool0, address1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.Address = address1

	address1.R.Pools = append(address1.R.Pools, fileuploadPool0)

	return nil
}

func (fileuploadPool0 *FileuploadPool) AttachAddress(ctx context.Context, exec bob.Executor, address1 *Address) error {
	var err error

	_, err = attachFileuploadPoolAddress0(ctx, exec, 1, fileuploadPool0, address1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.Address = address1

	address1.R.Pools = append(address1.R.Pools, fileuploadPool0)

	return nil
}

func attachFileuploadPoolCreatorUser0(ctx context.Context, exec bob.Executor, count int, fileuploadPool0 *FileuploadPool, user1 *User) (*FileuploadPool, error) {
	setter := &FileuploadPoolSetter{
		CreatorID: omit.From(user1.ID),
	}

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

	return fileuploadPool0, nil
}

func (fileuploadPool0 *FileuploadPool) InsertCreatorUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
	var err error

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

	_, err = attachFileuploadPoolCreatorUser0(ctx, exec, 1, fileuploadPool0, user1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.CreatorUser = user1

	user1.R.FileuploadPool = append(user1.R.FileuploadPool, fileuploadPool0)

	return nil
}

func (fileuploadPool0 *FileuploadPool) AttachCreatorUser(ctx context.Context, exec bob.Executor, user1 *User) error {
	var err error

	_, err = attachFileuploadPoolCreatorUser0(ctx, exec, 1, fileuploadPool0, user1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.CreatorUser = user1

	user1.R.FileuploadPool = append(user1.R.FileuploadPool, fileuploadPool0)

	return nil
}

func attachFileuploadPoolCSVFileCSV0(ctx context.Context, exec bob.Executor, count int, fileuploadPool0 *FileuploadPool, fileuploadCSV1 *FileuploadCSV) (*FileuploadPool, error) {
	setter := &FileuploadPoolSetter{
		CSVFile: omit.From(fileuploadCSV1.FileID),
	}

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

	return fileuploadPool0, nil
}

func (fileuploadPool0 *FileuploadPool) InsertCSVFileCSV(ctx context.Context, exec bob.Executor, related *FileuploadCSVSetter) error {
	var err error

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

	_, err = attachFileuploadPoolCSVFileCSV0(ctx, exec, 1, fileuploadPool0, fileuploadCSV1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.CSVFileCSV = fileuploadCSV1

	fileuploadCSV1.R.CSVFilePools = append(fileuploadCSV1.R.CSVFilePools, fileuploadPool0)

	return nil
}

func (fileuploadPool0 *FileuploadPool) AttachCSVFileCSV(ctx context.Context, exec bob.Executor, fileuploadCSV1 *FileuploadCSV) error {
	var err error

	_, err = attachFileuploadPoolCSVFileCSV0(ctx, exec, 1, fileuploadPool0, fileuploadCSV1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.CSVFileCSV = fileuploadCSV1

	fileuploadCSV1.R.CSVFilePools = append(fileuploadCSV1.R.CSVFilePools, fileuploadPool0)

	return nil
}

func attachFileuploadPoolPropertyOwnerPhoneE164Phone0(ctx context.Context, exec bob.Executor, count int, fileuploadPool0 *FileuploadPool, commsPhone1 *CommsPhone) (*FileuploadPool, error) {
	setter := &FileuploadPoolSetter{
		PropertyOwnerPhoneE164: omitnull.From(commsPhone1.E164),
	}

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

	return fileuploadPool0, nil
}

func (fileuploadPool0 *FileuploadPool) InsertPropertyOwnerPhoneE164Phone(ctx context.Context, exec bob.Executor, related *CommsPhoneSetter) error {
	var err error

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

	_, err = attachFileuploadPoolPropertyOwnerPhoneE164Phone0(ctx, exec, 1, fileuploadPool0, commsPhone1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.PropertyOwnerPhoneE164Phone = commsPhone1

	commsPhone1.R.PropertyOwnerPhoneE164Pools = append(commsPhone1.R.PropertyOwnerPhoneE164Pools, fileuploadPool0)

	return nil
}

func (fileuploadPool0 *FileuploadPool) AttachPropertyOwnerPhoneE164Phone(ctx context.Context, exec bob.Executor, commsPhone1 *CommsPhone) error {
	var err error

	_, err = attachFileuploadPoolPropertyOwnerPhoneE164Phone0(ctx, exec, 1, fileuploadPool0, commsPhone1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.PropertyOwnerPhoneE164Phone = commsPhone1

	commsPhone1.R.PropertyOwnerPhoneE164Pools = append(commsPhone1.R.PropertyOwnerPhoneE164Pools, fileuploadPool0)

	return nil
}

func attachFileuploadPoolResidentPhoneE164Phone0(ctx context.Context, exec bob.Executor, count int, fileuploadPool0 *FileuploadPool, commsPhone1 *CommsPhone) (*FileuploadPool, error) {
	setter := &FileuploadPoolSetter{
		ResidentPhoneE164: omitnull.From(commsPhone1.E164),
	}

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

	return fileuploadPool0, nil
}

func (fileuploadPool0 *FileuploadPool) InsertResidentPhoneE164Phone(ctx context.Context, exec bob.Executor, related *CommsPhoneSetter) error {
	var err error

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

	_, err = attachFileuploadPoolResidentPhoneE164Phone0(ctx, exec, 1, fileuploadPool0, commsPhone1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.ResidentPhoneE164Phone = commsPhone1

	commsPhone1.R.ResidentPhoneE164Pools = append(commsPhone1.R.ResidentPhoneE164Pools, fileuploadPool0)

	return nil
}

func (fileuploadPool0 *FileuploadPool) AttachResidentPhoneE164Phone(ctx context.Context, exec bob.Executor, commsPhone1 *CommsPhone) error {
	var err error

	_, err = attachFileuploadPoolResidentPhoneE164Phone0(ctx, exec, 1, fileuploadPool0, commsPhone1)
	if err != nil {
		return err
	}

	fileuploadPool0.R.ResidentPhoneE164Phone = commsPhone1

	commsPhone1.R.ResidentPhoneE164Pools = append(commsPhone1.R.ResidentPhoneE164Pools, fileuploadPool0)

	return nil
}

type fileuploadPoolWhere[Q psql.Filterable] struct {
	AddressPostalCode      psql.WhereMod[Q, string]
	AddressStreet          psql.WhereMod[Q, string]
	Committed              psql.WhereMod[Q, bool]
	Created                psql.WhereMod[Q, time.Time]
	CreatorID              psql.WhereMod[Q, int32]
	CSVFile                psql.WhereMod[Q, int32]
	Deleted                psql.WhereNullMod[Q, time.Time]
	Geom                   psql.WhereNullMod[Q, string]
	H3cell                 psql.WhereNullMod[Q, string]
	ID                     psql.WhereMod[Q, int32]
	IsInDistrict           psql.WhereMod[Q, bool]
	IsNew                  psql.WhereMod[Q, bool]
	Notes                  psql.WhereMod[Q, string]
	PropertyOwnerName      psql.WhereMod[Q, string]
	PropertyOwnerPhoneE164 psql.WhereNullMod[Q, string]
	ResidentOwned          psql.WhereNullMod[Q, bool]
	ResidentPhoneE164      psql.WhereNullMod[Q, string]
	LineNumber             psql.WhereMod[Q, int32]
	Tags                   psql.WhereMod[Q, pgtypes.HStore]
	AddressNumber          psql.WhereMod[Q, string]
	AddressLocality        psql.WhereMod[Q, string]
	AddressRegion          psql.WhereMod[Q, string]
	Condition              psql.WhereMod[Q, enums.Poolconditiontype]
	AddressID              psql.WhereNullMod[Q, int32]
}

func (fileuploadPoolWhere[Q]) AliasedAs(alias string) fileuploadPoolWhere[Q] {
	return buildFileuploadPoolWhere[Q](buildFileuploadPoolColumns(alias))
}

func buildFileuploadPoolWhere[Q psql.Filterable](cols fileuploadPoolColumns) fileuploadPoolWhere[Q] {
	return fileuploadPoolWhere[Q]{
		AddressPostalCode:      psql.Where[Q, string](cols.AddressPostalCode),
		AddressStreet:          psql.Where[Q, string](cols.AddressStreet),
		Committed:              psql.Where[Q, bool](cols.Committed),
		Created:                psql.Where[Q, time.Time](cols.Created),
		CreatorID:              psql.Where[Q, int32](cols.CreatorID),
		CSVFile:                psql.Where[Q, int32](cols.CSVFile),
		Deleted:                psql.WhereNull[Q, time.Time](cols.Deleted),
		Geom:                   psql.WhereNull[Q, string](cols.Geom),
		H3cell:                 psql.WhereNull[Q, string](cols.H3cell),
		ID:                     psql.Where[Q, int32](cols.ID),
		IsInDistrict:           psql.Where[Q, bool](cols.IsInDistrict),
		IsNew:                  psql.Where[Q, bool](cols.IsNew),
		Notes:                  psql.Where[Q, string](cols.Notes),
		PropertyOwnerName:      psql.Where[Q, string](cols.PropertyOwnerName),
		PropertyOwnerPhoneE164: psql.WhereNull[Q, string](cols.PropertyOwnerPhoneE164),
		ResidentOwned:          psql.WhereNull[Q, bool](cols.ResidentOwned),
		ResidentPhoneE164:      psql.WhereNull[Q, string](cols.ResidentPhoneE164),
		LineNumber:             psql.Where[Q, int32](cols.LineNumber),
		Tags:                   psql.Where[Q, pgtypes.HStore](cols.Tags),
		AddressNumber:          psql.Where[Q, string](cols.AddressNumber),
		AddressLocality:        psql.Where[Q, string](cols.AddressLocality),
		AddressRegion:          psql.Where[Q, string](cols.AddressRegion),
		Condition:              psql.Where[Q, enums.Poolconditiontype](cols.Condition),
		AddressID:              psql.WhereNull[Q, int32](cols.AddressID),
	}
}

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

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

		o.R.Address = rel

		if rel != nil {
			rel.R.Pools = FileuploadPoolSlice{o}
		}
		return nil
	case "CreatorUser":
		rel, ok := retrieved.(*User)
		if !ok {
			return fmt.Errorf("fileuploadPool cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorUser = rel

		if rel != nil {
			rel.R.FileuploadPool = FileuploadPoolSlice{o}
		}
		return nil
	case "CSVFileCSV":
		rel, ok := retrieved.(*FileuploadCSV)
		if !ok {
			return fmt.Errorf("fileuploadPool cannot load %T as %q", retrieved, name)
		}

		o.R.CSVFileCSV = rel

		if rel != nil {
			rel.R.CSVFilePools = FileuploadPoolSlice{o}
		}
		return nil
	case "PropertyOwnerPhoneE164Phone":
		rel, ok := retrieved.(*CommsPhone)
		if !ok {
			return fmt.Errorf("fileuploadPool cannot load %T as %q", retrieved, name)
		}

		o.R.PropertyOwnerPhoneE164Phone = rel

		if rel != nil {
			rel.R.PropertyOwnerPhoneE164Pools = FileuploadPoolSlice{o}
		}
		return nil
	case "ResidentPhoneE164Phone":
		rel, ok := retrieved.(*CommsPhone)
		if !ok {
			return fmt.Errorf("fileuploadPool cannot load %T as %q", retrieved, name)
		}

		o.R.ResidentPhoneE164Phone = rel

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

type fileuploadPoolPreloader struct {
	Address                     func(...psql.PreloadOption) psql.Preloader
	CreatorUser                 func(...psql.PreloadOption) psql.Preloader
	CSVFileCSV                  func(...psql.PreloadOption) psql.Preloader
	PropertyOwnerPhoneE164Phone func(...psql.PreloadOption) psql.Preloader
	ResidentPhoneE164Phone      func(...psql.PreloadOption) psql.Preloader
}

func buildFileuploadPoolPreloader() fileuploadPoolPreloader {
	return fileuploadPoolPreloader{
		Address: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*Address, AddressSlice](psql.PreloadRel{
				Name: "Address",
				Sides: []psql.PreloadSide{
					{
						From:        FileuploadPools,
						To:          Addresses,
						FromColumns: []string{"address_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Addresses.Columns.Names(), opts...)
		},
		CreatorUser: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*User, UserSlice](psql.PreloadRel{
				Name: "CreatorUser",
				Sides: []psql.PreloadSide{
					{
						From:        FileuploadPools,
						To:          Users,
						FromColumns: []string{"creator_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Users.Columns.Names(), opts...)
		},
		CSVFileCSV: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*FileuploadCSV, FileuploadCSVSlice](psql.PreloadRel{
				Name: "CSVFileCSV",
				Sides: []psql.PreloadSide{
					{
						From:        FileuploadPools,
						To:          FileuploadCSVS,
						FromColumns: []string{"csv_file"},
						ToColumns:   []string{"file_id"},
					},
				},
			}, FileuploadCSVS.Columns.Names(), opts...)
		},
		PropertyOwnerPhoneE164Phone: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*CommsPhone, CommsPhoneSlice](psql.PreloadRel{
				Name: "PropertyOwnerPhoneE164Phone",
				Sides: []psql.PreloadSide{
					{
						From:        FileuploadPools,
						To:          CommsPhones,
						FromColumns: []string{"property_owner_phone_e164"},
						ToColumns:   []string{"e164"},
					},
				},
			}, CommsPhones.Columns.Names(), opts...)
		},
		ResidentPhoneE164Phone: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*CommsPhone, CommsPhoneSlice](psql.PreloadRel{
				Name: "ResidentPhoneE164Phone",
				Sides: []psql.PreloadSide{
					{
						From:        FileuploadPools,
						To:          CommsPhones,
						FromColumns: []string{"resident_phone_e164"},
						ToColumns:   []string{"e164"},
					},
				},
			}, CommsPhones.Columns.Names(), opts...)
		},
	}
}

type fileuploadPoolThenLoader[Q orm.Loadable] struct {
	Address                     func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorUser                 func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CSVFileCSV                  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	PropertyOwnerPhoneE164Phone func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	ResidentPhoneE164Phone      func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildFileuploadPoolThenLoader[Q orm.Loadable]() fileuploadPoolThenLoader[Q] {
	type AddressLoadInterface interface {
		LoadAddress(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorUserLoadInterface interface {
		LoadCreatorUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CSVFileCSVLoadInterface interface {
		LoadCSVFileCSV(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PropertyOwnerPhoneE164PhoneLoadInterface interface {
		LoadPropertyOwnerPhoneE164Phone(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ResidentPhoneE164PhoneLoadInterface interface {
		LoadResidentPhoneE164Phone(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return fileuploadPoolThenLoader[Q]{
		Address: thenLoadBuilder[Q](
			"Address",
			func(ctx context.Context, exec bob.Executor, retrieved AddressLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadAddress(ctx, exec, mods...)
			},
		),
		CreatorUser: thenLoadBuilder[Q](
			"CreatorUser",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorUser(ctx, exec, mods...)
			},
		),
		CSVFileCSV: thenLoadBuilder[Q](
			"CSVFileCSV",
			func(ctx context.Context, exec bob.Executor, retrieved CSVFileCSVLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCSVFileCSV(ctx, exec, mods...)
			},
		),
		PropertyOwnerPhoneE164Phone: thenLoadBuilder[Q](
			"PropertyOwnerPhoneE164Phone",
			func(ctx context.Context, exec bob.Executor, retrieved PropertyOwnerPhoneE164PhoneLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadPropertyOwnerPhoneE164Phone(ctx, exec, mods...)
			},
		),
		ResidentPhoneE164Phone: thenLoadBuilder[Q](
			"ResidentPhoneE164Phone",
			func(ctx context.Context, exec bob.Executor, retrieved ResidentPhoneE164PhoneLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadResidentPhoneE164Phone(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.Pools = FileuploadPoolSlice{o}

	o.R.Address = related
	return nil
}

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

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

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

		for _, rel := range addresses {
			if !o.AddressID.IsValue() {
				continue
			}

			if !(o.AddressID.IsValue() && o.AddressID.MustGet() == rel.ID) {
				continue
			}

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

			o.R.Address = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.FileuploadPool = FileuploadPoolSlice{o}

	o.R.CreatorUser = related
	return nil
}

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

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

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

		for _, rel := range users {

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

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

			o.R.CreatorUser = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.CSVFilePools = FileuploadPoolSlice{o}

	o.R.CSVFileCSV = related
	return nil
}

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

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

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

		for _, rel := range fileuploadCSVS {

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

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

			o.R.CSVFileCSV = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.PropertyOwnerPhoneE164Pools = FileuploadPoolSlice{o}

	o.R.PropertyOwnerPhoneE164Phone = related
	return nil
}

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

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

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

		for _, rel := range commsPhones {
			if !o.PropertyOwnerPhoneE164.IsValue() {
				continue
			}

			if !(o.PropertyOwnerPhoneE164.IsValue() && o.PropertyOwnerPhoneE164.MustGet() == rel.E164) {
				continue
			}

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

			o.R.PropertyOwnerPhoneE164Phone = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.ResidentPhoneE164Pools = FileuploadPoolSlice{o}

	o.R.ResidentPhoneE164Phone = related
	return nil
}

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

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

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

		for _, rel := range commsPhones {
			if !o.ResidentPhoneE164.IsValue() {
				continue
			}

			if !(o.ResidentPhoneE164.IsValue() && o.ResidentPhoneE164.MustGet() == rel.E164) {
				continue
			}

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

			o.R.ResidentPhoneE164Phone = rel
			break
		}
	}

	return nil
}
