nidus-sync/db/models/arcgis.service_map.bob.go

677 lines
19 KiB
Go

// 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"
"github.com/aarondl/opt/omit"
)
// ArcgisServiceMap is an object representing the database table.
type ArcgisServiceMap struct {
AccountID string `db:"account_id" `
ArcgisID string `db:"arcgis_id,pk" `
Name string `db:"name" `
Title string `db:"title" `
URL string `db:"url" `
R arcgisServiceMapR `db:"-" `
}
// ArcgisServiceMapSlice is an alias for a slice of pointers to ArcgisServiceMap.
// This should almost always be used instead of []*ArcgisServiceMap.
type ArcgisServiceMapSlice []*ArcgisServiceMap
// ArcgisServiceMaps contains methods to work with the service_map table
var ArcgisServiceMaps = psql.NewTablex[*ArcgisServiceMap, ArcgisServiceMapSlice, *ArcgisServiceMapSetter]("arcgis", "service_map", buildArcgisServiceMapColumns("arcgis.service_map"))
// ArcgisServiceMapsQuery is a query on the service_map table
type ArcgisServiceMapsQuery = *psql.ViewQuery[*ArcgisServiceMap, ArcgisServiceMapSlice]
// arcgisServiceMapR is where relationships are stored.
type arcgisServiceMapR struct {
Account *ArcgisAccount // arcgis.service_map.service_map_account_id_fkey
}
func buildArcgisServiceMapColumns(alias string) arcgisServiceMapColumns {
return arcgisServiceMapColumns{
ColumnsExpr: expr.NewColumnsExpr(
"account_id", "arcgis_id", "name", "title", "url",
).WithParent("arcgis.service_map"),
tableAlias: alias,
AccountID: psql.Quote(alias, "account_id"),
ArcgisID: psql.Quote(alias, "arcgis_id"),
Name: psql.Quote(alias, "name"),
Title: psql.Quote(alias, "title"),
URL: psql.Quote(alias, "url"),
}
}
type arcgisServiceMapColumns struct {
expr.ColumnsExpr
tableAlias string
AccountID psql.Expression
ArcgisID psql.Expression
Name psql.Expression
Title psql.Expression
URL psql.Expression
}
func (c arcgisServiceMapColumns) Alias() string {
return c.tableAlias
}
func (arcgisServiceMapColumns) AliasedAs(alias string) arcgisServiceMapColumns {
return buildArcgisServiceMapColumns(alias)
}
// ArcgisServiceMapSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type ArcgisServiceMapSetter struct {
AccountID omit.Val[string] `db:"account_id" `
ArcgisID omit.Val[string] `db:"arcgis_id,pk" `
Name omit.Val[string] `db:"name" `
Title omit.Val[string] `db:"title" `
URL omit.Val[string] `db:"url" `
}
func (s ArcgisServiceMapSetter) SetColumns() []string {
vals := make([]string, 0, 5)
if s.AccountID.IsValue() {
vals = append(vals, "account_id")
}
if s.ArcgisID.IsValue() {
vals = append(vals, "arcgis_id")
}
if s.Name.IsValue() {
vals = append(vals, "name")
}
if s.Title.IsValue() {
vals = append(vals, "title")
}
if s.URL.IsValue() {
vals = append(vals, "url")
}
return vals
}
func (s ArcgisServiceMapSetter) Overwrite(t *ArcgisServiceMap) {
if s.AccountID.IsValue() {
t.AccountID = s.AccountID.MustGet()
}
if s.ArcgisID.IsValue() {
t.ArcgisID = s.ArcgisID.MustGet()
}
if s.Name.IsValue() {
t.Name = s.Name.MustGet()
}
if s.Title.IsValue() {
t.Title = s.Title.MustGet()
}
if s.URL.IsValue() {
t.URL = s.URL.MustGet()
}
}
func (s *ArcgisServiceMapSetter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return ArcgisServiceMaps.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, 5)
if s.AccountID.IsValue() {
vals[0] = psql.Arg(s.AccountID.MustGet())
} else {
vals[0] = psql.Raw("DEFAULT")
}
if s.ArcgisID.IsValue() {
vals[1] = psql.Arg(s.ArcgisID.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.Title.IsValue() {
vals[3] = psql.Arg(s.Title.MustGet())
} else {
vals[3] = psql.Raw("DEFAULT")
}
if s.URL.IsValue() {
vals[4] = psql.Arg(s.URL.MustGet())
} else {
vals[4] = psql.Raw("DEFAULT")
}
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
func (s ArcgisServiceMapSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
return um.Set(s.Expressions()...)
}
func (s ArcgisServiceMapSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 5)
if s.AccountID.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "account_id")...),
psql.Arg(s.AccountID),
}})
}
if s.ArcgisID.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "arcgis_id")...),
psql.Arg(s.ArcgisID),
}})
}
if s.Name.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "name")...),
psql.Arg(s.Name),
}})
}
if s.Title.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "title")...),
psql.Arg(s.Title),
}})
}
if s.URL.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "url")...),
psql.Arg(s.URL),
}})
}
return exprs
}
// FindArcgisServiceMap retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindArcgisServiceMap(ctx context.Context, exec bob.Executor, ArcgisIDPK string, cols ...string) (*ArcgisServiceMap, error) {
if len(cols) == 0 {
return ArcgisServiceMaps.Query(
sm.Where(ArcgisServiceMaps.Columns.ArcgisID.EQ(psql.Arg(ArcgisIDPK))),
).One(ctx, exec)
}
return ArcgisServiceMaps.Query(
sm.Where(ArcgisServiceMaps.Columns.ArcgisID.EQ(psql.Arg(ArcgisIDPK))),
sm.Columns(ArcgisServiceMaps.Columns.Only(cols...)),
).One(ctx, exec)
}
// ArcgisServiceMapExists checks the presence of a single record by primary key
func ArcgisServiceMapExists(ctx context.Context, exec bob.Executor, ArcgisIDPK string) (bool, error) {
return ArcgisServiceMaps.Query(
sm.Where(ArcgisServiceMaps.Columns.ArcgisID.EQ(psql.Arg(ArcgisIDPK))),
).Exists(ctx, exec)
}
// AfterQueryHook is called after ArcgisServiceMap is retrieved from the database
func (o *ArcgisServiceMap) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = ArcgisServiceMaps.AfterSelectHooks.RunHooks(ctx, exec, ArcgisServiceMapSlice{o})
case bob.QueryTypeInsert:
ctx, err = ArcgisServiceMaps.AfterInsertHooks.RunHooks(ctx, exec, ArcgisServiceMapSlice{o})
case bob.QueryTypeUpdate:
ctx, err = ArcgisServiceMaps.AfterUpdateHooks.RunHooks(ctx, exec, ArcgisServiceMapSlice{o})
case bob.QueryTypeDelete:
ctx, err = ArcgisServiceMaps.AfterDeleteHooks.RunHooks(ctx, exec, ArcgisServiceMapSlice{o})
}
return err
}
// primaryKeyVals returns the primary key values of the ArcgisServiceMap
func (o *ArcgisServiceMap) primaryKeyVals() bob.Expression {
return psql.Arg(o.ArcgisID)
}
func (o *ArcgisServiceMap) pkEQ() dialect.Expression {
return psql.Quote("arcgis.service_map", "arcgis_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 ArcgisServiceMap
func (o *ArcgisServiceMap) Update(ctx context.Context, exec bob.Executor, s *ArcgisServiceMapSetter) error {
v, err := ArcgisServiceMaps.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 ArcgisServiceMap record with an executor
func (o *ArcgisServiceMap) Delete(ctx context.Context, exec bob.Executor) error {
_, err := ArcgisServiceMaps.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
return err
}
// Reload refreshes the ArcgisServiceMap using the executor
func (o *ArcgisServiceMap) Reload(ctx context.Context, exec bob.Executor) error {
o2, err := ArcgisServiceMaps.Query(
sm.Where(ArcgisServiceMaps.Columns.ArcgisID.EQ(psql.Arg(o.ArcgisID))),
).One(ctx, exec)
if err != nil {
return err
}
o2.R = o.R
*o = *o2
return nil
}
// AfterQueryHook is called after ArcgisServiceMapSlice is retrieved from the database
func (o ArcgisServiceMapSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = ArcgisServiceMaps.AfterSelectHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeInsert:
ctx, err = ArcgisServiceMaps.AfterInsertHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeUpdate:
ctx, err = ArcgisServiceMaps.AfterUpdateHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeDelete:
ctx, err = ArcgisServiceMaps.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}
func (o ArcgisServiceMapSlice) pkIN() dialect.Expression {
if len(o) == 0 {
return psql.Raw("NULL")
}
return psql.Quote("arcgis.service_map", "arcgis_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 ArcgisServiceMapSlice) copyMatchingRows(from ...*ArcgisServiceMap) {
for i, old := range o {
for _, new := range from {
if new.ArcgisID != old.ArcgisID {
continue
}
new.R = old.R
o[i] = new
break
}
}
}
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o ArcgisServiceMapSlice) 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 ArcgisServiceMaps.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 *ArcgisServiceMap:
o.copyMatchingRows(retrieved)
case []*ArcgisServiceMap:
o.copyMatchingRows(retrieved...)
case ArcgisServiceMapSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a ArcgisServiceMap or a slice of ArcgisServiceMap
// then run the AfterUpdateHooks on the slice
_, err = ArcgisServiceMaps.AfterUpdateHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o ArcgisServiceMapSlice) 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 ArcgisServiceMaps.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 *ArcgisServiceMap:
o.copyMatchingRows(retrieved)
case []*ArcgisServiceMap:
o.copyMatchingRows(retrieved...)
case ArcgisServiceMapSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a ArcgisServiceMap or a slice of ArcgisServiceMap
// then run the AfterDeleteHooks on the slice
_, err = ArcgisServiceMaps.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
func (o ArcgisServiceMapSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals ArcgisServiceMapSetter) error {
if len(o) == 0 {
return nil
}
_, err := ArcgisServiceMaps.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
return err
}
func (o ArcgisServiceMapSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
_, err := ArcgisServiceMaps.Delete(o.DeleteMod()).Exec(ctx, exec)
return err
}
func (o ArcgisServiceMapSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
o2, err := ArcgisServiceMaps.Query(sm.Where(o.pkIN())).All(ctx, exec)
if err != nil {
return err
}
o.copyMatchingRows(o2...)
return nil
}
// Account starts a query for related objects on arcgis.account
func (o *ArcgisServiceMap) Account(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisAccountsQuery {
return ArcgisAccounts.Query(append(mods,
sm.Where(ArcgisAccounts.Columns.ID.EQ(psql.Arg(o.AccountID))),
)...)
}
func (os ArcgisServiceMapSlice) Account(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisAccountsQuery {
pkAccountID := make(pgtypes.Array[string], 0, len(os))
for _, o := range os {
if o == nil {
continue
}
pkAccountID = append(pkAccountID, o.AccountID)
}
PKArgExpr := psql.Select(sm.Columns(
psql.F("unnest", psql.Cast(psql.Arg(pkAccountID), "text[]")),
))
return ArcgisAccounts.Query(append(mods,
sm.Where(psql.Group(ArcgisAccounts.Columns.ID).OP("IN", PKArgExpr)),
)...)
}
func attachArcgisServiceMapAccount0(ctx context.Context, exec bob.Executor, count int, arcgisServiceMap0 *ArcgisServiceMap, arcgisAccount1 *ArcgisAccount) (*ArcgisServiceMap, error) {
setter := &ArcgisServiceMapSetter{
AccountID: omit.From(arcgisAccount1.ID),
}
err := arcgisServiceMap0.Update(ctx, exec, setter)
if err != nil {
return nil, fmt.Errorf("attachArcgisServiceMapAccount0: %w", err)
}
return arcgisServiceMap0, nil
}
func (arcgisServiceMap0 *ArcgisServiceMap) InsertAccount(ctx context.Context, exec bob.Executor, related *ArcgisAccountSetter) error {
var err error
arcgisAccount1, err := ArcgisAccounts.Insert(related).One(ctx, exec)
if err != nil {
return fmt.Errorf("inserting related objects: %w", err)
}
_, err = attachArcgisServiceMapAccount0(ctx, exec, 1, arcgisServiceMap0, arcgisAccount1)
if err != nil {
return err
}
arcgisServiceMap0.R.Account = arcgisAccount1
arcgisAccount1.R.ServiceMaps = append(arcgisAccount1.R.ServiceMaps, arcgisServiceMap0)
return nil
}
func (arcgisServiceMap0 *ArcgisServiceMap) AttachAccount(ctx context.Context, exec bob.Executor, arcgisAccount1 *ArcgisAccount) error {
var err error
_, err = attachArcgisServiceMapAccount0(ctx, exec, 1, arcgisServiceMap0, arcgisAccount1)
if err != nil {
return err
}
arcgisServiceMap0.R.Account = arcgisAccount1
arcgisAccount1.R.ServiceMaps = append(arcgisAccount1.R.ServiceMaps, arcgisServiceMap0)
return nil
}
type arcgisServiceMapWhere[Q psql.Filterable] struct {
AccountID psql.WhereMod[Q, string]
ArcgisID psql.WhereMod[Q, string]
Name psql.WhereMod[Q, string]
Title psql.WhereMod[Q, string]
URL psql.WhereMod[Q, string]
}
func (arcgisServiceMapWhere[Q]) AliasedAs(alias string) arcgisServiceMapWhere[Q] {
return buildArcgisServiceMapWhere[Q](buildArcgisServiceMapColumns(alias))
}
func buildArcgisServiceMapWhere[Q psql.Filterable](cols arcgisServiceMapColumns) arcgisServiceMapWhere[Q] {
return arcgisServiceMapWhere[Q]{
AccountID: psql.Where[Q, string](cols.AccountID),
ArcgisID: psql.Where[Q, string](cols.ArcgisID),
Name: psql.Where[Q, string](cols.Name),
Title: psql.Where[Q, string](cols.Title),
URL: psql.Where[Q, string](cols.URL),
}
}
func (o *ArcgisServiceMap) Preload(name string, retrieved any) error {
if o == nil {
return nil
}
switch name {
case "Account":
rel, ok := retrieved.(*ArcgisAccount)
if !ok {
return fmt.Errorf("arcgisServiceMap cannot load %T as %q", retrieved, name)
}
o.R.Account = rel
if rel != nil {
rel.R.ServiceMaps = ArcgisServiceMapSlice{o}
}
return nil
default:
return fmt.Errorf("arcgisServiceMap has no relationship %q", name)
}
}
type arcgisServiceMapPreloader struct {
Account func(...psql.PreloadOption) psql.Preloader
}
func buildArcgisServiceMapPreloader() arcgisServiceMapPreloader {
return arcgisServiceMapPreloader{
Account: func(opts ...psql.PreloadOption) psql.Preloader {
return psql.Preload[*ArcgisAccount, ArcgisAccountSlice](psql.PreloadRel{
Name: "Account",
Sides: []psql.PreloadSide{
{
From: ArcgisServiceMaps,
To: ArcgisAccounts,
FromColumns: []string{"account_id"},
ToColumns: []string{"id"},
},
},
}, ArcgisAccounts.Columns.Names(), opts...)
},
}
}
type arcgisServiceMapThenLoader[Q orm.Loadable] struct {
Account func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildArcgisServiceMapThenLoader[Q orm.Loadable]() arcgisServiceMapThenLoader[Q] {
type AccountLoadInterface interface {
LoadAccount(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return arcgisServiceMapThenLoader[Q]{
Account: thenLoadBuilder[Q](
"Account",
func(ctx context.Context, exec bob.Executor, retrieved AccountLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadAccount(ctx, exec, mods...)
},
),
}
}
// LoadAccount loads the arcgisServiceMap's Account into the .R struct
func (o *ArcgisServiceMap) LoadAccount(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.Account = nil
related, err := o.Account(mods...).One(ctx, exec)
if err != nil {
return err
}
related.R.ServiceMaps = ArcgisServiceMapSlice{o}
o.R.Account = related
return nil
}
// LoadAccount loads the arcgisServiceMap's Account into the .R struct
func (os ArcgisServiceMapSlice) LoadAccount(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
arcgisAccounts, err := os.Account(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range arcgisAccounts {
if !(o.AccountID == rel.ID) {
continue
}
rel.R.ServiceMaps = append(rel.R.ServiceMaps, o)
o.R.Account = rel
break
}
}
return nil
}
type arcgisServiceMapJoins[Q dialect.Joinable] struct {
typ string
Account modAs[Q, arcgisAccountColumns]
}
func (j arcgisServiceMapJoins[Q]) aliasedAs(alias string) arcgisServiceMapJoins[Q] {
return buildArcgisServiceMapJoins[Q](buildArcgisServiceMapColumns(alias), j.typ)
}
func buildArcgisServiceMapJoins[Q dialect.Joinable](cols arcgisServiceMapColumns, typ string) arcgisServiceMapJoins[Q] {
return arcgisServiceMapJoins[Q]{
typ: typ,
Account: modAs[Q, arcgisAccountColumns]{
c: ArcgisAccounts.Columns,
f: func(to arcgisAccountColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, ArcgisAccounts.Name().As(to.Alias())).On(
to.ID.EQ(cols.AccountID),
))
}
return mods
},
},
}
}