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

	"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/mods"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
	"github.com/aarondl/opt/omit"
)

// ArcgisLayerField is an object representing the database table.
type ArcgisLayerField struct {
	LayerFeatureServiceItemID string                `db:"layer_feature_service_item_id,pk" `
	LayerIndex                int32                 `db:"layer_index,pk" `
	Name                      string                `db:"name,pk" `
	Type                      enums.ArcgisFieldtype `db:"type_" `

	R arcgisLayerFieldR `db:"-" `

	C arcgisLayerFieldC `db:"-" `
}

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

// ArcgisLayerFields contains methods to work with the layer_field table
var ArcgisLayerFields = psql.NewTablex[*ArcgisLayerField, ArcgisLayerFieldSlice, *ArcgisLayerFieldSetter]("arcgis", "layer_field", buildArcgisLayerFieldColumns("arcgis.layer_field"))

// ArcgisLayerFieldsQuery is a query on the layer_field table
type ArcgisLayerFieldsQuery = *psql.ViewQuery[*ArcgisLayerField, ArcgisLayerFieldSlice]

// arcgisLayerFieldR is where relationships are stored.
type arcgisLayerFieldR struct {
	AddressMappings ArcgisAddressMappingSlice // arcgis.address_mapping.address_mapping_layer_feature_service_item_id_layer_index__fkey
	Layer           *ArcgisLayer              // arcgis.layer_field.layer_field_layer_feature_service_item_id_layer_index_fkey
	ParcelMappings  ArcgisParcelMappingSlice  // arcgis.parcel_mapping.parcel_mapping_layer_feature_service_item_id_layer_index_l_fkey
}

func buildArcgisLayerFieldColumns(alias string) arcgisLayerFieldColumns {
	return arcgisLayerFieldColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"layer_feature_service_item_id", "layer_index", "name", "type_",
		).WithParent("arcgis.layer_field"),
		tableAlias:                alias,
		LayerFeatureServiceItemID: psql.Quote(alias, "layer_feature_service_item_id"),
		LayerIndex:                psql.Quote(alias, "layer_index"),
		Name:                      psql.Quote(alias, "name"),
		Type:                      psql.Quote(alias, "type_"),
	}
}

type arcgisLayerFieldColumns struct {
	expr.ColumnsExpr
	tableAlias                string
	LayerFeatureServiceItemID psql.Expression
	LayerIndex                psql.Expression
	Name                      psql.Expression
	Type                      psql.Expression
}

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

func (arcgisLayerFieldColumns) AliasedAs(alias string) arcgisLayerFieldColumns {
	return buildArcgisLayerFieldColumns(alias)
}

// ArcgisLayerFieldSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type ArcgisLayerFieldSetter struct {
	LayerFeatureServiceItemID omit.Val[string]                `db:"layer_feature_service_item_id,pk" `
	LayerIndex                omit.Val[int32]                 `db:"layer_index,pk" `
	Name                      omit.Val[string]                `db:"name,pk" `
	Type                      omit.Val[enums.ArcgisFieldtype] `db:"type_" `
}

func (s ArcgisLayerFieldSetter) SetColumns() []string {
	vals := make([]string, 0, 4)
	if s.LayerFeatureServiceItemID.IsValue() {
		vals = append(vals, "layer_feature_service_item_id")
	}
	if s.LayerIndex.IsValue() {
		vals = append(vals, "layer_index")
	}
	if s.Name.IsValue() {
		vals = append(vals, "name")
	}
	if s.Type.IsValue() {
		vals = append(vals, "type_")
	}
	return vals
}

func (s ArcgisLayerFieldSetter) Overwrite(t *ArcgisLayerField) {
	if s.LayerFeatureServiceItemID.IsValue() {
		t.LayerFeatureServiceItemID = s.LayerFeatureServiceItemID.MustGet()
	}
	if s.LayerIndex.IsValue() {
		t.LayerIndex = s.LayerIndex.MustGet()
	}
	if s.Name.IsValue() {
		t.Name = s.Name.MustGet()
	}
	if s.Type.IsValue() {
		t.Type = s.Type.MustGet()
	}
}

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

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

		if s.Name.IsValue() {
			vals[2] = psql.Arg(s.Name.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 ArcgisLayerFieldSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions()...)
}

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

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

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

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

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

	return exprs
}

// FindArcgisLayerField retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindArcgisLayerField(ctx context.Context, exec bob.Executor, LayerFeatureServiceItemIDPK string, LayerIndexPK int32, NamePK string, cols ...string) (*ArcgisLayerField, error) {
	if len(cols) == 0 {
		return ArcgisLayerFields.Query(
			sm.Where(ArcgisLayerFields.Columns.LayerFeatureServiceItemID.EQ(psql.Arg(LayerFeatureServiceItemIDPK))),
			sm.Where(ArcgisLayerFields.Columns.LayerIndex.EQ(psql.Arg(LayerIndexPK))),
			sm.Where(ArcgisLayerFields.Columns.Name.EQ(psql.Arg(NamePK))),
		).One(ctx, exec)
	}

	return ArcgisLayerFields.Query(
		sm.Where(ArcgisLayerFields.Columns.LayerFeatureServiceItemID.EQ(psql.Arg(LayerFeatureServiceItemIDPK))),
		sm.Where(ArcgisLayerFields.Columns.LayerIndex.EQ(psql.Arg(LayerIndexPK))),
		sm.Where(ArcgisLayerFields.Columns.Name.EQ(psql.Arg(NamePK))),
		sm.Columns(ArcgisLayerFields.Columns.Only(cols...)),
	).One(ctx, exec)
}

// ArcgisLayerFieldExists checks the presence of a single record by primary key
func ArcgisLayerFieldExists(ctx context.Context, exec bob.Executor, LayerFeatureServiceItemIDPK string, LayerIndexPK int32, NamePK string) (bool, error) {
	return ArcgisLayerFields.Query(
		sm.Where(ArcgisLayerFields.Columns.LayerFeatureServiceItemID.EQ(psql.Arg(LayerFeatureServiceItemIDPK))),
		sm.Where(ArcgisLayerFields.Columns.LayerIndex.EQ(psql.Arg(LayerIndexPK))),
		sm.Where(ArcgisLayerFields.Columns.Name.EQ(psql.Arg(NamePK))),
	).Exists(ctx, exec)
}

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

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

	return err
}

// primaryKeyVals returns the primary key values of the ArcgisLayerField
func (o *ArcgisLayerField) primaryKeyVals() bob.Expression {
	return psql.ArgGroup(
		o.LayerFeatureServiceItemID,
		o.LayerIndex,
		o.Name,
	)
}

func (o *ArcgisLayerField) pkEQ() dialect.Expression {
	return psql.Group(psql.Quote("arcgis.layer_field", "layer_feature_service_item_id"), psql.Quote("arcgis.layer_field", "layer_index"), psql.Quote("arcgis.layer_field", "name")).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 ArcgisLayerField
func (o *ArcgisLayerField) Update(ctx context.Context, exec bob.Executor, s *ArcgisLayerFieldSetter) error {
	v, err := ArcgisLayerFields.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 ArcgisLayerField record with an executor
func (o *ArcgisLayerField) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := ArcgisLayerFields.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

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

	return nil
}

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

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

	return err
}

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

	return psql.Group(psql.Quote("arcgis.layer_field", "layer_feature_service_item_id"), psql.Quote("arcgis.layer_field", "layer_index"), psql.Quote("arcgis.layer_field", "name")).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 ArcgisLayerFieldSlice) copyMatchingRows(from ...*ArcgisLayerField) {
	for i, old := range o {
		for _, new := range from {
			if new.LayerFeatureServiceItemID != old.LayerFeatureServiceItemID {
				continue
			}
			if new.LayerIndex != old.LayerIndex {
				continue
			}
			if new.Name != old.Name {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// AddressMappings starts a query for related objects on arcgis.address_mapping
func (o *ArcgisLayerField) AddressMappings(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisAddressMappingsQuery {
	return ArcgisAddressMappings.Query(append(mods,
		sm.Where(ArcgisAddressMappings.Columns.LayerFeatureServiceItemID.EQ(psql.Arg(o.LayerFeatureServiceItemID))), sm.Where(ArcgisAddressMappings.Columns.LayerIndex.EQ(psql.Arg(o.LayerIndex))), sm.Where(ArcgisAddressMappings.Columns.LayerFieldName.EQ(psql.Arg(o.Name))),
	)...)
}

func (os ArcgisLayerFieldSlice) AddressMappings(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisAddressMappingsQuery {
	pkLayerFeatureServiceItemID := make(pgtypes.Array[string], 0, len(os))

	pkLayerIndex := make(pgtypes.Array[int32], 0, len(os))

	pkName := make(pgtypes.Array[string], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkLayerFeatureServiceItemID = append(pkLayerFeatureServiceItemID, o.LayerFeatureServiceItemID)
		pkLayerIndex = append(pkLayerIndex, o.LayerIndex)
		pkName = append(pkName, o.Name)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkLayerFeatureServiceItemID), "text[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkLayerIndex), "integer[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkName), "text[]")),
	))

	return ArcgisAddressMappings.Query(append(mods,
		sm.Where(psql.Group(ArcgisAddressMappings.Columns.LayerFeatureServiceItemID, ArcgisAddressMappings.Columns.LayerIndex, ArcgisAddressMappings.Columns.LayerFieldName).OP("IN", PKArgExpr)),
	)...)
}

// Layer starts a query for related objects on arcgis.layer
func (o *ArcgisLayerField) Layer(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisLayersQuery {
	return ArcgisLayers.Query(append(mods,
		sm.Where(ArcgisLayers.Columns.FeatureServiceItemID.EQ(psql.Arg(o.LayerFeatureServiceItemID))), sm.Where(ArcgisLayers.Columns.Index.EQ(psql.Arg(o.LayerIndex))),
	)...)
}

func (os ArcgisLayerFieldSlice) Layer(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisLayersQuery {
	pkLayerFeatureServiceItemID := make(pgtypes.Array[string], 0, len(os))

	pkLayerIndex := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkLayerFeatureServiceItemID = append(pkLayerFeatureServiceItemID, o.LayerFeatureServiceItemID)
		pkLayerIndex = append(pkLayerIndex, o.LayerIndex)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkLayerFeatureServiceItemID), "text[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkLayerIndex), "integer[]")),
	))

	return ArcgisLayers.Query(append(mods,
		sm.Where(psql.Group(ArcgisLayers.Columns.FeatureServiceItemID, ArcgisLayers.Columns.Index).OP("IN", PKArgExpr)),
	)...)
}

// ParcelMappings starts a query for related objects on arcgis.parcel_mapping
func (o *ArcgisLayerField) ParcelMappings(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisParcelMappingsQuery {
	return ArcgisParcelMappings.Query(append(mods,
		sm.Where(ArcgisParcelMappings.Columns.LayerFeatureServiceItemID.EQ(psql.Arg(o.LayerFeatureServiceItemID))), sm.Where(ArcgisParcelMappings.Columns.LayerIndex.EQ(psql.Arg(o.LayerIndex))), sm.Where(ArcgisParcelMappings.Columns.LayerFieldName.EQ(psql.Arg(o.Name))),
	)...)
}

func (os ArcgisLayerFieldSlice) ParcelMappings(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisParcelMappingsQuery {
	pkLayerFeatureServiceItemID := make(pgtypes.Array[string], 0, len(os))

	pkLayerIndex := make(pgtypes.Array[int32], 0, len(os))

	pkName := make(pgtypes.Array[string], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkLayerFeatureServiceItemID = append(pkLayerFeatureServiceItemID, o.LayerFeatureServiceItemID)
		pkLayerIndex = append(pkLayerIndex, o.LayerIndex)
		pkName = append(pkName, o.Name)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkLayerFeatureServiceItemID), "text[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkLayerIndex), "integer[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkName), "text[]")),
	))

	return ArcgisParcelMappings.Query(append(mods,
		sm.Where(psql.Group(ArcgisParcelMappings.Columns.LayerFeatureServiceItemID, ArcgisParcelMappings.Columns.LayerIndex, ArcgisParcelMappings.Columns.LayerFieldName).OP("IN", PKArgExpr)),
	)...)
}

func insertArcgisLayerFieldAddressMappings0(ctx context.Context, exec bob.Executor, arcgisAddressMappings1 []*ArcgisAddressMappingSetter, arcgisLayerField0 *ArcgisLayerField) (ArcgisAddressMappingSlice, error) {
	for i := range arcgisAddressMappings1 {
		arcgisAddressMappings1[i].LayerFeatureServiceItemID = omit.From(arcgisLayerField0.LayerFeatureServiceItemID)
		arcgisAddressMappings1[i].LayerIndex = omit.From(arcgisLayerField0.LayerIndex)
		arcgisAddressMappings1[i].LayerFieldName = omit.From(arcgisLayerField0.Name)
	}

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

	return ret, nil
}

func attachArcgisLayerFieldAddressMappings0(ctx context.Context, exec bob.Executor, count int, arcgisAddressMappings1 ArcgisAddressMappingSlice, arcgisLayerField0 *ArcgisLayerField) (ArcgisAddressMappingSlice, error) {
	setter := &ArcgisAddressMappingSetter{
		LayerFeatureServiceItemID: omit.From(arcgisLayerField0.LayerFeatureServiceItemID),
		LayerIndex:                omit.From(arcgisLayerField0.LayerIndex),
		LayerFieldName:            omit.From(arcgisLayerField0.Name),
	}

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

	return arcgisAddressMappings1, nil
}

func (arcgisLayerField0 *ArcgisLayerField) InsertAddressMappings(ctx context.Context, exec bob.Executor, related ...*ArcgisAddressMappingSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	arcgisAddressMappings1, err := insertArcgisLayerFieldAddressMappings0(ctx, exec, related, arcgisLayerField0)
	if err != nil {
		return err
	}

	arcgisLayerField0.R.AddressMappings = append(arcgisLayerField0.R.AddressMappings, arcgisAddressMappings1...)

	for _, rel := range arcgisAddressMappings1 {
		rel.R.LayerField = arcgisLayerField0
	}
	return nil
}

func (arcgisLayerField0 *ArcgisLayerField) AttachAddressMappings(ctx context.Context, exec bob.Executor, related ...*ArcgisAddressMapping) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	arcgisAddressMappings1 := ArcgisAddressMappingSlice(related)

	_, err = attachArcgisLayerFieldAddressMappings0(ctx, exec, len(related), arcgisAddressMappings1, arcgisLayerField0)
	if err != nil {
		return err
	}

	arcgisLayerField0.R.AddressMappings = append(arcgisLayerField0.R.AddressMappings, arcgisAddressMappings1...)

	for _, rel := range related {
		rel.R.LayerField = arcgisLayerField0
	}

	return nil
}

func attachArcgisLayerFieldLayer0(ctx context.Context, exec bob.Executor, count int, arcgisLayerField0 *ArcgisLayerField, arcgisLayer1 *ArcgisLayer) (*ArcgisLayerField, error) {
	setter := &ArcgisLayerFieldSetter{
		LayerFeatureServiceItemID: omit.From(arcgisLayer1.FeatureServiceItemID),
		LayerIndex:                omit.From(arcgisLayer1.Index),
	}

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

	return arcgisLayerField0, nil
}

func (arcgisLayerField0 *ArcgisLayerField) InsertLayer(ctx context.Context, exec bob.Executor, related *ArcgisLayerSetter) error {
	var err error

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

	_, err = attachArcgisLayerFieldLayer0(ctx, exec, 1, arcgisLayerField0, arcgisLayer1)
	if err != nil {
		return err
	}

	arcgisLayerField0.R.Layer = arcgisLayer1

	arcgisLayer1.R.LayerFields = append(arcgisLayer1.R.LayerFields, arcgisLayerField0)

	return nil
}

func (arcgisLayerField0 *ArcgisLayerField) AttachLayer(ctx context.Context, exec bob.Executor, arcgisLayer1 *ArcgisLayer) error {
	var err error

	_, err = attachArcgisLayerFieldLayer0(ctx, exec, 1, arcgisLayerField0, arcgisLayer1)
	if err != nil {
		return err
	}

	arcgisLayerField0.R.Layer = arcgisLayer1

	arcgisLayer1.R.LayerFields = append(arcgisLayer1.R.LayerFields, arcgisLayerField0)

	return nil
}

func insertArcgisLayerFieldParcelMappings0(ctx context.Context, exec bob.Executor, arcgisParcelMappings1 []*ArcgisParcelMappingSetter, arcgisLayerField0 *ArcgisLayerField) (ArcgisParcelMappingSlice, error) {
	for i := range arcgisParcelMappings1 {
		arcgisParcelMappings1[i].LayerFeatureServiceItemID = omit.From(arcgisLayerField0.LayerFeatureServiceItemID)
		arcgisParcelMappings1[i].LayerIndex = omit.From(arcgisLayerField0.LayerIndex)
		arcgisParcelMappings1[i].LayerFieldName = omit.From(arcgisLayerField0.Name)
	}

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

	return ret, nil
}

func attachArcgisLayerFieldParcelMappings0(ctx context.Context, exec bob.Executor, count int, arcgisParcelMappings1 ArcgisParcelMappingSlice, arcgisLayerField0 *ArcgisLayerField) (ArcgisParcelMappingSlice, error) {
	setter := &ArcgisParcelMappingSetter{
		LayerFeatureServiceItemID: omit.From(arcgisLayerField0.LayerFeatureServiceItemID),
		LayerIndex:                omit.From(arcgisLayerField0.LayerIndex),
		LayerFieldName:            omit.From(arcgisLayerField0.Name),
	}

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

	return arcgisParcelMappings1, nil
}

func (arcgisLayerField0 *ArcgisLayerField) InsertParcelMappings(ctx context.Context, exec bob.Executor, related ...*ArcgisParcelMappingSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	arcgisParcelMappings1, err := insertArcgisLayerFieldParcelMappings0(ctx, exec, related, arcgisLayerField0)
	if err != nil {
		return err
	}

	arcgisLayerField0.R.ParcelMappings = append(arcgisLayerField0.R.ParcelMappings, arcgisParcelMappings1...)

	for _, rel := range arcgisParcelMappings1 {
		rel.R.LayerField = arcgisLayerField0
	}
	return nil
}

func (arcgisLayerField0 *ArcgisLayerField) AttachParcelMappings(ctx context.Context, exec bob.Executor, related ...*ArcgisParcelMapping) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	arcgisParcelMappings1 := ArcgisParcelMappingSlice(related)

	_, err = attachArcgisLayerFieldParcelMappings0(ctx, exec, len(related), arcgisParcelMappings1, arcgisLayerField0)
	if err != nil {
		return err
	}

	arcgisLayerField0.R.ParcelMappings = append(arcgisLayerField0.R.ParcelMappings, arcgisParcelMappings1...)

	for _, rel := range related {
		rel.R.LayerField = arcgisLayerField0
	}

	return nil
}

type arcgisLayerFieldWhere[Q psql.Filterable] struct {
	LayerFeatureServiceItemID psql.WhereMod[Q, string]
	LayerIndex                psql.WhereMod[Q, int32]
	Name                      psql.WhereMod[Q, string]
	Type                      psql.WhereMod[Q, enums.ArcgisFieldtype]
}

func (arcgisLayerFieldWhere[Q]) AliasedAs(alias string) arcgisLayerFieldWhere[Q] {
	return buildArcgisLayerFieldWhere[Q](buildArcgisLayerFieldColumns(alias))
}

func buildArcgisLayerFieldWhere[Q psql.Filterable](cols arcgisLayerFieldColumns) arcgisLayerFieldWhere[Q] {
	return arcgisLayerFieldWhere[Q]{
		LayerFeatureServiceItemID: psql.Where[Q, string](cols.LayerFeatureServiceItemID),
		LayerIndex:                psql.Where[Q, int32](cols.LayerIndex),
		Name:                      psql.Where[Q, string](cols.Name),
		Type:                      psql.Where[Q, enums.ArcgisFieldtype](cols.Type),
	}
}

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

	switch name {
	case "AddressMappings":
		rels, ok := retrieved.(ArcgisAddressMappingSlice)
		if !ok {
			return fmt.Errorf("arcgisLayerField cannot load %T as %q", retrieved, name)
		}

		o.R.AddressMappings = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.LayerField = o
			}
		}
		return nil
	case "Layer":
		rel, ok := retrieved.(*ArcgisLayer)
		if !ok {
			return fmt.Errorf("arcgisLayerField cannot load %T as %q", retrieved, name)
		}

		o.R.Layer = rel

		if rel != nil {
			rel.R.LayerFields = ArcgisLayerFieldSlice{o}
		}
		return nil
	case "ParcelMappings":
		rels, ok := retrieved.(ArcgisParcelMappingSlice)
		if !ok {
			return fmt.Errorf("arcgisLayerField cannot load %T as %q", retrieved, name)
		}

		o.R.ParcelMappings = rels

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

type arcgisLayerFieldPreloader struct {
	Layer func(...psql.PreloadOption) psql.Preloader
}

func buildArcgisLayerFieldPreloader() arcgisLayerFieldPreloader {
	return arcgisLayerFieldPreloader{
		Layer: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*ArcgisLayer, ArcgisLayerSlice](psql.PreloadRel{
				Name: "Layer",
				Sides: []psql.PreloadSide{
					{
						From:        ArcgisLayerFields,
						To:          ArcgisLayers,
						FromColumns: []string{"layer_feature_service_item_id", "layer_index"},
						ToColumns:   []string{"feature_service_item_id", "index_"},
					},
				},
			}, ArcgisLayers.Columns.Names(), opts...)
		},
	}
}

type arcgisLayerFieldThenLoader[Q orm.Loadable] struct {
	AddressMappings func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Layer           func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	ParcelMappings  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildArcgisLayerFieldThenLoader[Q orm.Loadable]() arcgisLayerFieldThenLoader[Q] {
	type AddressMappingsLoadInterface interface {
		LoadAddressMappings(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type LayerLoadInterface interface {
		LoadLayer(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ParcelMappingsLoadInterface interface {
		LoadParcelMappings(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return arcgisLayerFieldThenLoader[Q]{
		AddressMappings: thenLoadBuilder[Q](
			"AddressMappings",
			func(ctx context.Context, exec bob.Executor, retrieved AddressMappingsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadAddressMappings(ctx, exec, mods...)
			},
		),
		Layer: thenLoadBuilder[Q](
			"Layer",
			func(ctx context.Context, exec bob.Executor, retrieved LayerLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadLayer(ctx, exec, mods...)
			},
		),
		ParcelMappings: thenLoadBuilder[Q](
			"ParcelMappings",
			func(ctx context.Context, exec bob.Executor, retrieved ParcelMappingsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadParcelMappings(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

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

	o.R.AddressMappings = related
	return nil
}

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

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

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

		o.R.AddressMappings = nil
	}

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

		for _, rel := range arcgisAddressMappings {

			if !(o.LayerFeatureServiceItemID == rel.LayerFeatureServiceItemID) {
				continue
			}

			if !(o.LayerIndex == rel.LayerIndex) {
				continue
			}

			if !(o.Name == rel.LayerFieldName) {
				continue
			}

			rel.R.LayerField = o

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

	return nil
}

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

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

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

	related.R.LayerFields = ArcgisLayerFieldSlice{o}

	o.R.Layer = related
	return nil
}

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

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

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

		for _, rel := range arcgisLayers {

			if !(o.LayerFeatureServiceItemID == rel.FeatureServiceItemID) {
				continue
			}

			if !(o.LayerIndex == rel.Index) {
				continue
			}

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

			o.R.Layer = rel
			break
		}
	}

	return nil
}

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

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

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

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

	o.R.ParcelMappings = related
	return nil
}

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

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

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

		o.R.ParcelMappings = nil
	}

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

		for _, rel := range arcgisParcelMappings {

			if !(o.LayerFeatureServiceItemID == rel.LayerFeatureServiceItemID) {
				continue
			}

			if !(o.LayerIndex == rel.LayerIndex) {
				continue
			}

			if !(o.Name == rel.LayerFieldName) {
				continue
			}

			rel.R.LayerField = o

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

	return nil
}

// arcgisLayerFieldC is where relationship counts are stored.
type arcgisLayerFieldC struct {
	AddressMappings *int64
	ParcelMappings  *int64
}

// PreloadCount sets a count in the C struct by name
func (o *ArcgisLayerField) PreloadCount(name string, count int64) error {
	if o == nil {
		return nil
	}

	switch name {
	case "AddressMappings":
		o.C.AddressMappings = &count
	case "ParcelMappings":
		o.C.ParcelMappings = &count
	}
	return nil
}

type arcgisLayerFieldCountPreloader struct {
	AddressMappings func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	ParcelMappings  func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
}

func buildArcgisLayerFieldCountPreloader() arcgisLayerFieldCountPreloader {
	return arcgisLayerFieldCountPreloader{
		AddressMappings: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*ArcgisLayerField]("AddressMappings", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = ArcgisLayerFields.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(ArcgisAddressMappings.Name()),
					sm.Where(psql.Quote(ArcgisAddressMappings.Alias(), "layer_feature_service_item_id").EQ(psql.Quote(parent, "layer_feature_service_item_id"))),
					sm.Where(psql.Quote(ArcgisAddressMappings.Alias(), "layer_index").EQ(psql.Quote(parent, "layer_index"))),
					sm.Where(psql.Quote(ArcgisAddressMappings.Alias(), "layer_field_name").EQ(psql.Quote(parent, "name"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		ParcelMappings: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*ArcgisLayerField]("ParcelMappings", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = ArcgisLayerFields.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(ArcgisParcelMappings.Name()),
					sm.Where(psql.Quote(ArcgisParcelMappings.Alias(), "layer_feature_service_item_id").EQ(psql.Quote(parent, "layer_feature_service_item_id"))),
					sm.Where(psql.Quote(ArcgisParcelMappings.Alias(), "layer_index").EQ(psql.Quote(parent, "layer_index"))),
					sm.Where(psql.Quote(ArcgisParcelMappings.Alias(), "layer_field_name").EQ(psql.Quote(parent, "name"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
	}
}

type arcgisLayerFieldCountThenLoader[Q orm.Loadable] struct {
	AddressMappings func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	ParcelMappings  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildArcgisLayerFieldCountThenLoader[Q orm.Loadable]() arcgisLayerFieldCountThenLoader[Q] {
	type AddressMappingsCountInterface interface {
		LoadCountAddressMappings(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ParcelMappingsCountInterface interface {
		LoadCountParcelMappings(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return arcgisLayerFieldCountThenLoader[Q]{
		AddressMappings: countThenLoadBuilder[Q](
			"AddressMappings",
			func(ctx context.Context, exec bob.Executor, retrieved AddressMappingsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountAddressMappings(ctx, exec, mods...)
			},
		),
		ParcelMappings: countThenLoadBuilder[Q](
			"ParcelMappings",
			func(ctx context.Context, exec bob.Executor, retrieved ParcelMappingsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountParcelMappings(ctx, exec, mods...)
			},
		),
	}
}

// LoadCountAddressMappings loads the count of AddressMappings into the C struct
func (o *ArcgisLayerField) LoadCountAddressMappings(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.AddressMappings(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.AddressMappings = &count
	return nil
}

// LoadCountAddressMappings loads the count of AddressMappings for a slice
func (os ArcgisLayerFieldSlice) LoadCountAddressMappings(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountAddressMappings(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountParcelMappings loads the count of ParcelMappings into the C struct
func (o *ArcgisLayerField) LoadCountParcelMappings(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.ParcelMappings(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.ParcelMappings = &count
	return nil
}

// LoadCountParcelMappings loads the count of ParcelMappings for a slice
func (os ArcgisLayerFieldSlice) LoadCountParcelMappings(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountParcelMappings(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

type arcgisLayerFieldJoins[Q dialect.Joinable] struct {
	typ             string
	AddressMappings modAs[Q, arcgisAddressMappingColumns]
	Layer           modAs[Q, arcgisLayerColumns]
	ParcelMappings  modAs[Q, arcgisParcelMappingColumns]
}

func (j arcgisLayerFieldJoins[Q]) aliasedAs(alias string) arcgisLayerFieldJoins[Q] {
	return buildArcgisLayerFieldJoins[Q](buildArcgisLayerFieldColumns(alias), j.typ)
}

func buildArcgisLayerFieldJoins[Q dialect.Joinable](cols arcgisLayerFieldColumns, typ string) arcgisLayerFieldJoins[Q] {
	return arcgisLayerFieldJoins[Q]{
		typ: typ,
		AddressMappings: modAs[Q, arcgisAddressMappingColumns]{
			c: ArcgisAddressMappings.Columns,
			f: func(to arcgisAddressMappingColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, ArcgisAddressMappings.Name().As(to.Alias())).On(
						to.LayerFeatureServiceItemID.EQ(cols.LayerFeatureServiceItemID), to.LayerIndex.EQ(cols.LayerIndex), to.LayerFieldName.EQ(cols.Name),
					))
				}

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

				{
					mods = append(mods, dialect.Join[Q](typ, ArcgisLayers.Name().As(to.Alias())).On(
						to.FeatureServiceItemID.EQ(cols.LayerFeatureServiceItemID), to.Index.EQ(cols.LayerIndex),
					))
				}

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

				{
					mods = append(mods, dialect.Join[Q](typ, ArcgisParcelMappings.Name().As(to.Alias())).On(
						to.LayerFeatureServiceItemID.EQ(cols.LayerFeatureServiceItemID), to.LayerIndex.EQ(cols.LayerIndex), to.LayerFieldName.EQ(cols.Name),
					))
				}

				return mods
			},
		},
	}
}
