// Code generated by BobGen psql v0.42.1. DO NOT EDIT. // This file is meant to be re-generated in place and/or deleted at any time. package models import ( "context" "fmt" "io" "github.com/aarondl/opt/null" "github.com/aarondl/opt/omit" "github.com/aarondl/opt/omitnull" "github.com/stephenafamo/bob" "github.com/stephenafamo/bob/dialect/psql" "github.com/stephenafamo/bob/dialect/psql/dialect" "github.com/stephenafamo/bob/dialect/psql/dm" "github.com/stephenafamo/bob/dialect/psql/sm" "github.com/stephenafamo/bob/dialect/psql/um" "github.com/stephenafamo/bob/expr" "github.com/stephenafamo/bob/mods" "github.com/stephenafamo/bob/orm" "github.com/stephenafamo/bob/types/pgtypes" ) // CommsPhone is an object representing the database table. type CommsPhone struct { E164 string `db:"e164,pk" ` IsSubscribed null.Val[bool] `db:"is_subscribed" ` R commsPhoneR `db:"-" ` C commsPhoneC `db:"-" ` } // CommsPhoneSlice is an alias for a slice of pointers to CommsPhone. // This should almost always be used instead of []*CommsPhone. type CommsPhoneSlice []*CommsPhone // CommsPhones contains methods to work with the phone table var CommsPhones = psql.NewTablex[*CommsPhone, CommsPhoneSlice, *CommsPhoneSetter]("comms", "phone", buildCommsPhoneColumns("comms.phone")) // CommsPhonesQuery is a query on the phone table type CommsPhonesQuery = *psql.ViewQuery[*CommsPhone, CommsPhoneSlice] // commsPhoneR is where relationships are stored. type commsPhoneR struct { DestinationTextJobs CommsTextJobSlice // comms.text_job.text_job_destination_fkey DestinationTextLogs CommsTextLogSlice // comms.text_log.text_log_destination_fkey SourceTextLogs CommsTextLogSlice // comms.text_log.text_log_source_fkey } func buildCommsPhoneColumns(alias string) commsPhoneColumns { return commsPhoneColumns{ ColumnsExpr: expr.NewColumnsExpr( "e164", "is_subscribed", ).WithParent("comms.phone"), tableAlias: alias, E164: psql.Quote(alias, "e164"), IsSubscribed: psql.Quote(alias, "is_subscribed"), } } type commsPhoneColumns struct { expr.ColumnsExpr tableAlias string E164 psql.Expression IsSubscribed psql.Expression } func (c commsPhoneColumns) Alias() string { return c.tableAlias } func (commsPhoneColumns) AliasedAs(alias string) commsPhoneColumns { return buildCommsPhoneColumns(alias) } // CommsPhoneSetter is used for insert/upsert/update operations // All values are optional, and do not have to be set // Generated columns are not included type CommsPhoneSetter struct { E164 omit.Val[string] `db:"e164,pk" ` IsSubscribed omitnull.Val[bool] `db:"is_subscribed" ` } func (s CommsPhoneSetter) SetColumns() []string { vals := make([]string, 0, 2) if s.E164.IsValue() { vals = append(vals, "e164") } if !s.IsSubscribed.IsUnset() { vals = append(vals, "is_subscribed") } return vals } func (s CommsPhoneSetter) Overwrite(t *CommsPhone) { if s.E164.IsValue() { t.E164 = s.E164.MustGet() } if !s.IsSubscribed.IsUnset() { t.IsSubscribed = s.IsSubscribed.MustGetNull() } } func (s *CommsPhoneSetter) Apply(q *dialect.InsertQuery) { q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { return CommsPhones.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, 2) if s.E164.IsValue() { vals[0] = psql.Arg(s.E164.MustGet()) } else { vals[0] = psql.Raw("DEFAULT") } if !s.IsSubscribed.IsUnset() { vals[1] = psql.Arg(s.IsSubscribed.MustGetNull()) } else { vals[1] = psql.Raw("DEFAULT") } return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "") })) } func (s CommsPhoneSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] { return um.Set(s.Expressions()...) } func (s CommsPhoneSetter) Expressions(prefix ...string) []bob.Expression { exprs := make([]bob.Expression, 0, 2) if s.E164.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ psql.Quote(append(prefix, "e164")...), psql.Arg(s.E164), }}) } if !s.IsSubscribed.IsUnset() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ psql.Quote(append(prefix, "is_subscribed")...), psql.Arg(s.IsSubscribed), }}) } return exprs } // FindCommsPhone retrieves a single record by primary key // If cols is empty Find will return all columns. func FindCommsPhone(ctx context.Context, exec bob.Executor, E164PK string, cols ...string) (*CommsPhone, error) { if len(cols) == 0 { return CommsPhones.Query( sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(E164PK))), ).One(ctx, exec) } return CommsPhones.Query( sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(E164PK))), sm.Columns(CommsPhones.Columns.Only(cols...)), ).One(ctx, exec) } // CommsPhoneExists checks the presence of a single record by primary key func CommsPhoneExists(ctx context.Context, exec bob.Executor, E164PK string) (bool, error) { return CommsPhones.Query( sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(E164PK))), ).Exists(ctx, exec) } // AfterQueryHook is called after CommsPhone is retrieved from the database func (o *CommsPhone) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error switch queryType { case bob.QueryTypeSelect: ctx, err = CommsPhones.AfterSelectHooks.RunHooks(ctx, exec, CommsPhoneSlice{o}) case bob.QueryTypeInsert: ctx, err = CommsPhones.AfterInsertHooks.RunHooks(ctx, exec, CommsPhoneSlice{o}) case bob.QueryTypeUpdate: ctx, err = CommsPhones.AfterUpdateHooks.RunHooks(ctx, exec, CommsPhoneSlice{o}) case bob.QueryTypeDelete: ctx, err = CommsPhones.AfterDeleteHooks.RunHooks(ctx, exec, CommsPhoneSlice{o}) } return err } // primaryKeyVals returns the primary key values of the CommsPhone func (o *CommsPhone) primaryKeyVals() bob.Expression { return psql.Arg(o.E164) } func (o *CommsPhone) pkEQ() dialect.Expression { return psql.Quote("comms.phone", "e164").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 CommsPhone func (o *CommsPhone) Update(ctx context.Context, exec bob.Executor, s *CommsPhoneSetter) error { v, err := CommsPhones.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 CommsPhone record with an executor func (o *CommsPhone) Delete(ctx context.Context, exec bob.Executor) error { _, err := CommsPhones.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec) return err } // Reload refreshes the CommsPhone using the executor func (o *CommsPhone) Reload(ctx context.Context, exec bob.Executor) error { o2, err := CommsPhones.Query( sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(o.E164))), ).One(ctx, exec) if err != nil { return err } o2.R = o.R *o = *o2 return nil } // AfterQueryHook is called after CommsPhoneSlice is retrieved from the database func (o CommsPhoneSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error switch queryType { case bob.QueryTypeSelect: ctx, err = CommsPhones.AfterSelectHooks.RunHooks(ctx, exec, o) case bob.QueryTypeInsert: ctx, err = CommsPhones.AfterInsertHooks.RunHooks(ctx, exec, o) case bob.QueryTypeUpdate: ctx, err = CommsPhones.AfterUpdateHooks.RunHooks(ctx, exec, o) case bob.QueryTypeDelete: ctx, err = CommsPhones.AfterDeleteHooks.RunHooks(ctx, exec, o) } return err } func (o CommsPhoneSlice) pkIN() dialect.Expression { if len(o) == 0 { return psql.Raw("NULL") } return psql.Quote("comms.phone", "e164").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 CommsPhoneSlice) copyMatchingRows(from ...*CommsPhone) { for i, old := range o { for _, new := range from { if new.E164 != old.E164 { continue } new.R = old.R o[i] = new break } } } // UpdateMod modifies an update query with "WHERE primary_key IN (o...)" func (o CommsPhoneSlice) 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 CommsPhones.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 *CommsPhone: o.copyMatchingRows(retrieved) case []*CommsPhone: o.copyMatchingRows(retrieved...) case CommsPhoneSlice: o.copyMatchingRows(retrieved...) default: // If the retrieved value is not a CommsPhone or a slice of CommsPhone // then run the AfterUpdateHooks on the slice _, err = CommsPhones.AfterUpdateHooks.RunHooks(ctx, exec, o) } return err })) q.AppendWhere(o.pkIN()) }) } // DeleteMod modifies an delete query with "WHERE primary_key IN (o...)" func (o CommsPhoneSlice) 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 CommsPhones.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 *CommsPhone: o.copyMatchingRows(retrieved) case []*CommsPhone: o.copyMatchingRows(retrieved...) case CommsPhoneSlice: o.copyMatchingRows(retrieved...) default: // If the retrieved value is not a CommsPhone or a slice of CommsPhone // then run the AfterDeleteHooks on the slice _, err = CommsPhones.AfterDeleteHooks.RunHooks(ctx, exec, o) } return err })) q.AppendWhere(o.pkIN()) }) } func (o CommsPhoneSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals CommsPhoneSetter) error { if len(o) == 0 { return nil } _, err := CommsPhones.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec) return err } func (o CommsPhoneSlice) DeleteAll(ctx context.Context, exec bob.Executor) error { if len(o) == 0 { return nil } _, err := CommsPhones.Delete(o.DeleteMod()).Exec(ctx, exec) return err } func (o CommsPhoneSlice) ReloadAll(ctx context.Context, exec bob.Executor) error { if len(o) == 0 { return nil } o2, err := CommsPhones.Query(sm.Where(o.pkIN())).All(ctx, exec) if err != nil { return err } o.copyMatchingRows(o2...) return nil } // DestinationTextJobs starts a query for related objects on comms.text_job func (o *CommsPhone) DestinationTextJobs(mods ...bob.Mod[*dialect.SelectQuery]) CommsTextJobsQuery { return CommsTextJobs.Query(append(mods, sm.Where(CommsTextJobs.Columns.Destination.EQ(psql.Arg(o.E164))), )...) } func (os CommsPhoneSlice) DestinationTextJobs(mods ...bob.Mod[*dialect.SelectQuery]) CommsTextJobsQuery { pkE164 := make(pgtypes.Array[string], 0, len(os)) for _, o := range os { if o == nil { continue } pkE164 = append(pkE164, o.E164) } PKArgExpr := psql.Select(sm.Columns( psql.F("unnest", psql.Cast(psql.Arg(pkE164), "text[]")), )) return CommsTextJobs.Query(append(mods, sm.Where(psql.Group(CommsTextJobs.Columns.Destination).OP("IN", PKArgExpr)), )...) } // DestinationTextLogs starts a query for related objects on comms.text_log func (o *CommsPhone) DestinationTextLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsTextLogsQuery { return CommsTextLogs.Query(append(mods, sm.Where(CommsTextLogs.Columns.Destination.EQ(psql.Arg(o.E164))), )...) } func (os CommsPhoneSlice) DestinationTextLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsTextLogsQuery { pkE164 := make(pgtypes.Array[string], 0, len(os)) for _, o := range os { if o == nil { continue } pkE164 = append(pkE164, o.E164) } PKArgExpr := psql.Select(sm.Columns( psql.F("unnest", psql.Cast(psql.Arg(pkE164), "text[]")), )) return CommsTextLogs.Query(append(mods, sm.Where(psql.Group(CommsTextLogs.Columns.Destination).OP("IN", PKArgExpr)), )...) } // SourceTextLogs starts a query for related objects on comms.text_log func (o *CommsPhone) SourceTextLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsTextLogsQuery { return CommsTextLogs.Query(append(mods, sm.Where(CommsTextLogs.Columns.Source.EQ(psql.Arg(o.E164))), )...) } func (os CommsPhoneSlice) SourceTextLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsTextLogsQuery { pkE164 := make(pgtypes.Array[string], 0, len(os)) for _, o := range os { if o == nil { continue } pkE164 = append(pkE164, o.E164) } PKArgExpr := psql.Select(sm.Columns( psql.F("unnest", psql.Cast(psql.Arg(pkE164), "text[]")), )) return CommsTextLogs.Query(append(mods, sm.Where(psql.Group(CommsTextLogs.Columns.Source).OP("IN", PKArgExpr)), )...) } func insertCommsPhoneDestinationTextJobs0(ctx context.Context, exec bob.Executor, commsTextJobs1 []*CommsTextJobSetter, commsPhone0 *CommsPhone) (CommsTextJobSlice, error) { for i := range commsTextJobs1 { commsTextJobs1[i].Destination = omit.From(commsPhone0.E164) } ret, err := CommsTextJobs.Insert(bob.ToMods(commsTextJobs1...)).All(ctx, exec) if err != nil { return ret, fmt.Errorf("insertCommsPhoneDestinationTextJobs0: %w", err) } return ret, nil } func attachCommsPhoneDestinationTextJobs0(ctx context.Context, exec bob.Executor, count int, commsTextJobs1 CommsTextJobSlice, commsPhone0 *CommsPhone) (CommsTextJobSlice, error) { setter := &CommsTextJobSetter{ Destination: omit.From(commsPhone0.E164), } err := commsTextJobs1.UpdateAll(ctx, exec, *setter) if err != nil { return nil, fmt.Errorf("attachCommsPhoneDestinationTextJobs0: %w", err) } return commsTextJobs1, nil } func (commsPhone0 *CommsPhone) InsertDestinationTextJobs(ctx context.Context, exec bob.Executor, related ...*CommsTextJobSetter) error { if len(related) == 0 { return nil } var err error commsTextJobs1, err := insertCommsPhoneDestinationTextJobs0(ctx, exec, related, commsPhone0) if err != nil { return err } commsPhone0.R.DestinationTextJobs = append(commsPhone0.R.DestinationTextJobs, commsTextJobs1...) for _, rel := range commsTextJobs1 { rel.R.DestinationPhone = commsPhone0 } return nil } func (commsPhone0 *CommsPhone) AttachDestinationTextJobs(ctx context.Context, exec bob.Executor, related ...*CommsTextJob) error { if len(related) == 0 { return nil } var err error commsTextJobs1 := CommsTextJobSlice(related) _, err = attachCommsPhoneDestinationTextJobs0(ctx, exec, len(related), commsTextJobs1, commsPhone0) if err != nil { return err } commsPhone0.R.DestinationTextJobs = append(commsPhone0.R.DestinationTextJobs, commsTextJobs1...) for _, rel := range related { rel.R.DestinationPhone = commsPhone0 } return nil } func insertCommsPhoneDestinationTextLogs0(ctx context.Context, exec bob.Executor, commsTextLogs1 []*CommsTextLogSetter, commsPhone0 *CommsPhone) (CommsTextLogSlice, error) { for i := range commsTextLogs1 { commsTextLogs1[i].Destination = omit.From(commsPhone0.E164) } ret, err := CommsTextLogs.Insert(bob.ToMods(commsTextLogs1...)).All(ctx, exec) if err != nil { return ret, fmt.Errorf("insertCommsPhoneDestinationTextLogs0: %w", err) } return ret, nil } func attachCommsPhoneDestinationTextLogs0(ctx context.Context, exec bob.Executor, count int, commsTextLogs1 CommsTextLogSlice, commsPhone0 *CommsPhone) (CommsTextLogSlice, error) { setter := &CommsTextLogSetter{ Destination: omit.From(commsPhone0.E164), } err := commsTextLogs1.UpdateAll(ctx, exec, *setter) if err != nil { return nil, fmt.Errorf("attachCommsPhoneDestinationTextLogs0: %w", err) } return commsTextLogs1, nil } func (commsPhone0 *CommsPhone) InsertDestinationTextLogs(ctx context.Context, exec bob.Executor, related ...*CommsTextLogSetter) error { if len(related) == 0 { return nil } var err error commsTextLogs1, err := insertCommsPhoneDestinationTextLogs0(ctx, exec, related, commsPhone0) if err != nil { return err } commsPhone0.R.DestinationTextLogs = append(commsPhone0.R.DestinationTextLogs, commsTextLogs1...) for _, rel := range commsTextLogs1 { rel.R.DestinationPhone = commsPhone0 } return nil } func (commsPhone0 *CommsPhone) AttachDestinationTextLogs(ctx context.Context, exec bob.Executor, related ...*CommsTextLog) error { if len(related) == 0 { return nil } var err error commsTextLogs1 := CommsTextLogSlice(related) _, err = attachCommsPhoneDestinationTextLogs0(ctx, exec, len(related), commsTextLogs1, commsPhone0) if err != nil { return err } commsPhone0.R.DestinationTextLogs = append(commsPhone0.R.DestinationTextLogs, commsTextLogs1...) for _, rel := range related { rel.R.DestinationPhone = commsPhone0 } return nil } func insertCommsPhoneSourceTextLogs0(ctx context.Context, exec bob.Executor, commsTextLogs1 []*CommsTextLogSetter, commsPhone0 *CommsPhone) (CommsTextLogSlice, error) { for i := range commsTextLogs1 { commsTextLogs1[i].Source = omit.From(commsPhone0.E164) } ret, err := CommsTextLogs.Insert(bob.ToMods(commsTextLogs1...)).All(ctx, exec) if err != nil { return ret, fmt.Errorf("insertCommsPhoneSourceTextLogs0: %w", err) } return ret, nil } func attachCommsPhoneSourceTextLogs0(ctx context.Context, exec bob.Executor, count int, commsTextLogs1 CommsTextLogSlice, commsPhone0 *CommsPhone) (CommsTextLogSlice, error) { setter := &CommsTextLogSetter{ Source: omit.From(commsPhone0.E164), } err := commsTextLogs1.UpdateAll(ctx, exec, *setter) if err != nil { return nil, fmt.Errorf("attachCommsPhoneSourceTextLogs0: %w", err) } return commsTextLogs1, nil } func (commsPhone0 *CommsPhone) InsertSourceTextLogs(ctx context.Context, exec bob.Executor, related ...*CommsTextLogSetter) error { if len(related) == 0 { return nil } var err error commsTextLogs1, err := insertCommsPhoneSourceTextLogs0(ctx, exec, related, commsPhone0) if err != nil { return err } commsPhone0.R.SourceTextLogs = append(commsPhone0.R.SourceTextLogs, commsTextLogs1...) for _, rel := range commsTextLogs1 { rel.R.SourcePhone = commsPhone0 } return nil } func (commsPhone0 *CommsPhone) AttachSourceTextLogs(ctx context.Context, exec bob.Executor, related ...*CommsTextLog) error { if len(related) == 0 { return nil } var err error commsTextLogs1 := CommsTextLogSlice(related) _, err = attachCommsPhoneSourceTextLogs0(ctx, exec, len(related), commsTextLogs1, commsPhone0) if err != nil { return err } commsPhone0.R.SourceTextLogs = append(commsPhone0.R.SourceTextLogs, commsTextLogs1...) for _, rel := range related { rel.R.SourcePhone = commsPhone0 } return nil } type commsPhoneWhere[Q psql.Filterable] struct { E164 psql.WhereMod[Q, string] IsSubscribed psql.WhereNullMod[Q, bool] } func (commsPhoneWhere[Q]) AliasedAs(alias string) commsPhoneWhere[Q] { return buildCommsPhoneWhere[Q](buildCommsPhoneColumns(alias)) } func buildCommsPhoneWhere[Q psql.Filterable](cols commsPhoneColumns) commsPhoneWhere[Q] { return commsPhoneWhere[Q]{ E164: psql.Where[Q, string](cols.E164), IsSubscribed: psql.WhereNull[Q, bool](cols.IsSubscribed), } } func (o *CommsPhone) Preload(name string, retrieved any) error { if o == nil { return nil } switch name { case "DestinationTextJobs": rels, ok := retrieved.(CommsTextJobSlice) if !ok { return fmt.Errorf("commsPhone cannot load %T as %q", retrieved, name) } o.R.DestinationTextJobs = rels for _, rel := range rels { if rel != nil { rel.R.DestinationPhone = o } } return nil case "DestinationTextLogs": rels, ok := retrieved.(CommsTextLogSlice) if !ok { return fmt.Errorf("commsPhone cannot load %T as %q", retrieved, name) } o.R.DestinationTextLogs = rels for _, rel := range rels { if rel != nil { rel.R.DestinationPhone = o } } return nil case "SourceTextLogs": rels, ok := retrieved.(CommsTextLogSlice) if !ok { return fmt.Errorf("commsPhone cannot load %T as %q", retrieved, name) } o.R.SourceTextLogs = rels for _, rel := range rels { if rel != nil { rel.R.SourcePhone = o } } return nil default: return fmt.Errorf("commsPhone has no relationship %q", name) } } type commsPhonePreloader struct{} func buildCommsPhonePreloader() commsPhonePreloader { return commsPhonePreloader{} } type commsPhoneThenLoader[Q orm.Loadable] struct { DestinationTextJobs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] DestinationTextLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] SourceTextLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildCommsPhoneThenLoader[Q orm.Loadable]() commsPhoneThenLoader[Q] { type DestinationTextJobsLoadInterface interface { LoadDestinationTextJobs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } type DestinationTextLogsLoadInterface interface { LoadDestinationTextLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } type SourceTextLogsLoadInterface interface { LoadSourceTextLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } return commsPhoneThenLoader[Q]{ DestinationTextJobs: thenLoadBuilder[Q]( "DestinationTextJobs", func(ctx context.Context, exec bob.Executor, retrieved DestinationTextJobsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadDestinationTextJobs(ctx, exec, mods...) }, ), DestinationTextLogs: thenLoadBuilder[Q]( "DestinationTextLogs", func(ctx context.Context, exec bob.Executor, retrieved DestinationTextLogsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadDestinationTextLogs(ctx, exec, mods...) }, ), SourceTextLogs: thenLoadBuilder[Q]( "SourceTextLogs", func(ctx context.Context, exec bob.Executor, retrieved SourceTextLogsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadSourceTextLogs(ctx, exec, mods...) }, ), } } // LoadDestinationTextJobs loads the commsPhone's DestinationTextJobs into the .R struct func (o *CommsPhone) LoadDestinationTextJobs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } // Reset the relationship o.R.DestinationTextJobs = nil related, err := o.DestinationTextJobs(mods...).All(ctx, exec) if err != nil { return err } for _, rel := range related { rel.R.DestinationPhone = o } o.R.DestinationTextJobs = related return nil } // LoadDestinationTextJobs loads the commsPhone's DestinationTextJobs into the .R struct func (os CommsPhoneSlice) LoadDestinationTextJobs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if len(os) == 0 { return nil } commsTextJobs, err := os.DestinationTextJobs(mods...).All(ctx, exec) if err != nil { return err } for _, o := range os { if o == nil { continue } o.R.DestinationTextJobs = nil } for _, o := range os { if o == nil { continue } for _, rel := range commsTextJobs { if !(o.E164 == rel.Destination) { continue } rel.R.DestinationPhone = o o.R.DestinationTextJobs = append(o.R.DestinationTextJobs, rel) } } return nil } // LoadDestinationTextLogs loads the commsPhone's DestinationTextLogs into the .R struct func (o *CommsPhone) LoadDestinationTextLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } // Reset the relationship o.R.DestinationTextLogs = nil related, err := o.DestinationTextLogs(mods...).All(ctx, exec) if err != nil { return err } for _, rel := range related { rel.R.DestinationPhone = o } o.R.DestinationTextLogs = related return nil } // LoadDestinationTextLogs loads the commsPhone's DestinationTextLogs into the .R struct func (os CommsPhoneSlice) LoadDestinationTextLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if len(os) == 0 { return nil } commsTextLogs, err := os.DestinationTextLogs(mods...).All(ctx, exec) if err != nil { return err } for _, o := range os { if o == nil { continue } o.R.DestinationTextLogs = nil } for _, o := range os { if o == nil { continue } for _, rel := range commsTextLogs { if !(o.E164 == rel.Destination) { continue } rel.R.DestinationPhone = o o.R.DestinationTextLogs = append(o.R.DestinationTextLogs, rel) } } return nil } // LoadSourceTextLogs loads the commsPhone's SourceTextLogs into the .R struct func (o *CommsPhone) LoadSourceTextLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } // Reset the relationship o.R.SourceTextLogs = nil related, err := o.SourceTextLogs(mods...).All(ctx, exec) if err != nil { return err } for _, rel := range related { rel.R.SourcePhone = o } o.R.SourceTextLogs = related return nil } // LoadSourceTextLogs loads the commsPhone's SourceTextLogs into the .R struct func (os CommsPhoneSlice) LoadSourceTextLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if len(os) == 0 { return nil } commsTextLogs, err := os.SourceTextLogs(mods...).All(ctx, exec) if err != nil { return err } for _, o := range os { if o == nil { continue } o.R.SourceTextLogs = nil } for _, o := range os { if o == nil { continue } for _, rel := range commsTextLogs { if !(o.E164 == rel.Source) { continue } rel.R.SourcePhone = o o.R.SourceTextLogs = append(o.R.SourceTextLogs, rel) } } return nil } // commsPhoneC is where relationship counts are stored. type commsPhoneC struct { DestinationTextJobs *int64 DestinationTextLogs *int64 SourceTextLogs *int64 } // PreloadCount sets a count in the C struct by name func (o *CommsPhone) PreloadCount(name string, count int64) error { if o == nil { return nil } switch name { case "DestinationTextJobs": o.C.DestinationTextJobs = &count case "DestinationTextLogs": o.C.DestinationTextLogs = &count case "SourceTextLogs": o.C.SourceTextLogs = &count } return nil } type commsPhoneCountPreloader struct { DestinationTextJobs func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader DestinationTextLogs func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader SourceTextLogs func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader } func buildCommsPhoneCountPreloader() commsPhoneCountPreloader { return commsPhoneCountPreloader{ DestinationTextJobs: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader { return countPreloader[*CommsPhone]("DestinationTextJobs", func(parent string) bob.Expression { // Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk) if parent == "" { parent = CommsPhones.Alias() } subqueryMods := []bob.Mod[*dialect.SelectQuery]{ sm.Columns(psql.Raw("count(*)")), sm.From(CommsTextJobs.Name()), sm.Where(psql.Quote(CommsTextJobs.Alias(), "destination").EQ(psql.Quote(parent, "e164"))), } subqueryMods = append(subqueryMods, mods...) return psql.Group(psql.Select(subqueryMods...).Expression) }) }, DestinationTextLogs: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader { return countPreloader[*CommsPhone]("DestinationTextLogs", func(parent string) bob.Expression { // Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk) if parent == "" { parent = CommsPhones.Alias() } subqueryMods := []bob.Mod[*dialect.SelectQuery]{ sm.Columns(psql.Raw("count(*)")), sm.From(CommsTextLogs.Name()), sm.Where(psql.Quote(CommsTextLogs.Alias(), "destination").EQ(psql.Quote(parent, "e164"))), } subqueryMods = append(subqueryMods, mods...) return psql.Group(psql.Select(subqueryMods...).Expression) }) }, SourceTextLogs: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader { return countPreloader[*CommsPhone]("SourceTextLogs", func(parent string) bob.Expression { // Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk) if parent == "" { parent = CommsPhones.Alias() } subqueryMods := []bob.Mod[*dialect.SelectQuery]{ sm.Columns(psql.Raw("count(*)")), sm.From(CommsTextLogs.Name()), sm.Where(psql.Quote(CommsTextLogs.Alias(), "source").EQ(psql.Quote(parent, "e164"))), } subqueryMods = append(subqueryMods, mods...) return psql.Group(psql.Select(subqueryMods...).Expression) }) }, } } type commsPhoneCountThenLoader[Q orm.Loadable] struct { DestinationTextJobs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] DestinationTextLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] SourceTextLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildCommsPhoneCountThenLoader[Q orm.Loadable]() commsPhoneCountThenLoader[Q] { type DestinationTextJobsCountInterface interface { LoadCountDestinationTextJobs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } type DestinationTextLogsCountInterface interface { LoadCountDestinationTextLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } type SourceTextLogsCountInterface interface { LoadCountSourceTextLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } return commsPhoneCountThenLoader[Q]{ DestinationTextJobs: countThenLoadBuilder[Q]( "DestinationTextJobs", func(ctx context.Context, exec bob.Executor, retrieved DestinationTextJobsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadCountDestinationTextJobs(ctx, exec, mods...) }, ), DestinationTextLogs: countThenLoadBuilder[Q]( "DestinationTextLogs", func(ctx context.Context, exec bob.Executor, retrieved DestinationTextLogsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadCountDestinationTextLogs(ctx, exec, mods...) }, ), SourceTextLogs: countThenLoadBuilder[Q]( "SourceTextLogs", func(ctx context.Context, exec bob.Executor, retrieved SourceTextLogsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadCountSourceTextLogs(ctx, exec, mods...) }, ), } } // LoadCountDestinationTextJobs loads the count of DestinationTextJobs into the C struct func (o *CommsPhone) LoadCountDestinationTextJobs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } count, err := o.DestinationTextJobs(mods...).Count(ctx, exec) if err != nil { return err } o.C.DestinationTextJobs = &count return nil } // LoadCountDestinationTextJobs loads the count of DestinationTextJobs for a slice func (os CommsPhoneSlice) LoadCountDestinationTextJobs(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.LoadCountDestinationTextJobs(ctx, exec, mods...); err != nil { return err } } return nil } // LoadCountDestinationTextLogs loads the count of DestinationTextLogs into the C struct func (o *CommsPhone) LoadCountDestinationTextLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } count, err := o.DestinationTextLogs(mods...).Count(ctx, exec) if err != nil { return err } o.C.DestinationTextLogs = &count return nil } // LoadCountDestinationTextLogs loads the count of DestinationTextLogs for a slice func (os CommsPhoneSlice) LoadCountDestinationTextLogs(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.LoadCountDestinationTextLogs(ctx, exec, mods...); err != nil { return err } } return nil } // LoadCountSourceTextLogs loads the count of SourceTextLogs into the C struct func (o *CommsPhone) LoadCountSourceTextLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } count, err := o.SourceTextLogs(mods...).Count(ctx, exec) if err != nil { return err } o.C.SourceTextLogs = &count return nil } // LoadCountSourceTextLogs loads the count of SourceTextLogs for a slice func (os CommsPhoneSlice) LoadCountSourceTextLogs(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.LoadCountSourceTextLogs(ctx, exec, mods...); err != nil { return err } } return nil } type commsPhoneJoins[Q dialect.Joinable] struct { typ string DestinationTextJobs modAs[Q, commsTextJobColumns] DestinationTextLogs modAs[Q, commsTextLogColumns] SourceTextLogs modAs[Q, commsTextLogColumns] } func (j commsPhoneJoins[Q]) aliasedAs(alias string) commsPhoneJoins[Q] { return buildCommsPhoneJoins[Q](buildCommsPhoneColumns(alias), j.typ) } func buildCommsPhoneJoins[Q dialect.Joinable](cols commsPhoneColumns, typ string) commsPhoneJoins[Q] { return commsPhoneJoins[Q]{ typ: typ, DestinationTextJobs: modAs[Q, commsTextJobColumns]{ c: CommsTextJobs.Columns, f: func(to commsTextJobColumns) bob.Mod[Q] { mods := make(mods.QueryMods[Q], 0, 1) { mods = append(mods, dialect.Join[Q](typ, CommsTextJobs.Name().As(to.Alias())).On( to.Destination.EQ(cols.E164), )) } return mods }, }, DestinationTextLogs: modAs[Q, commsTextLogColumns]{ c: CommsTextLogs.Columns, f: func(to commsTextLogColumns) bob.Mod[Q] { mods := make(mods.QueryMods[Q], 0, 1) { mods = append(mods, dialect.Join[Q](typ, CommsTextLogs.Name().As(to.Alias())).On( to.Destination.EQ(cols.E164), )) } return mods }, }, SourceTextLogs: modAs[Q, commsTextLogColumns]{ c: CommsTextLogs.Columns, f: func(to commsTextLogColumns) bob.Mod[Q] { mods := make(mods.QueryMods[Q], 0, 1) { mods = append(mods, dialect.Join[Q](typ, CommsTextLogs.Name().As(to.Alias())).On( to.Source.EQ(cols.E164), )) } return mods }, }, } }