nidus-sync/db/models/comms.phone.bob.go
Eli Ribble 842e6cff43
Move comms work to background goroutine
This is a sort of random checkpoint of work
 * add schema for tracking messages sent to DB
 * add terms of service and privacy policy for RCS compliance
 * standardize some things about background workers
 * update some missing stuff from generated DB code
2026-01-20 17:10:22 +00:00

1220 lines
34 KiB
Go

// 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/omit"
"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 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 {
SourceEmailLogs CommsEmailLogSlice // comms.email_log.email_log_source_fkey
DestinationSMSLogs CommsSMSLogSlice // comms.sms_log.sms_log_destination_fkey
SourceSMSLogs CommsSMSLogSlice // comms.sms_log.sms_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 omit.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.IsValue() {
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.IsValue() {
t.IsSubscribed = s.IsSubscribed.MustGet()
}
}
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.IsValue() {
vals[1] = psql.Arg(s.IsSubscribed.MustGet())
} 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.IsValue() {
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
}
// SourceEmailLogs starts a query for related objects on comms.email_log
func (o *CommsPhone) SourceEmailLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailLogsQuery {
return CommsEmailLogs.Query(append(mods,
sm.Where(CommsEmailLogs.Columns.Source.EQ(psql.Arg(o.E164))),
)...)
}
func (os CommsPhoneSlice) SourceEmailLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailLogsQuery {
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 CommsEmailLogs.Query(append(mods,
sm.Where(psql.Group(CommsEmailLogs.Columns.Source).OP("IN", PKArgExpr)),
)...)
}
// DestinationSMSLogs starts a query for related objects on comms.sms_log
func (o *CommsPhone) DestinationSMSLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsSMSLogsQuery {
return CommsSMSLogs.Query(append(mods,
sm.Where(CommsSMSLogs.Columns.Destination.EQ(psql.Arg(o.E164))),
)...)
}
func (os CommsPhoneSlice) DestinationSMSLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsSMSLogsQuery {
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 CommsSMSLogs.Query(append(mods,
sm.Where(psql.Group(CommsSMSLogs.Columns.Destination).OP("IN", PKArgExpr)),
)...)
}
// SourceSMSLogs starts a query for related objects on comms.sms_log
func (o *CommsPhone) SourceSMSLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsSMSLogsQuery {
return CommsSMSLogs.Query(append(mods,
sm.Where(CommsSMSLogs.Columns.Source.EQ(psql.Arg(o.E164))),
)...)
}
func (os CommsPhoneSlice) SourceSMSLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsSMSLogsQuery {
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 CommsSMSLogs.Query(append(mods,
sm.Where(psql.Group(CommsSMSLogs.Columns.Source).OP("IN", PKArgExpr)),
)...)
}
func insertCommsPhoneSourceEmailLogs0(ctx context.Context, exec bob.Executor, commsEmailLogs1 []*CommsEmailLogSetter, commsPhone0 *CommsPhone) (CommsEmailLogSlice, error) {
for i := range commsEmailLogs1 {
commsEmailLogs1[i].Source = omit.From(commsPhone0.E164)
}
ret, err := CommsEmailLogs.Insert(bob.ToMods(commsEmailLogs1...)).All(ctx, exec)
if err != nil {
return ret, fmt.Errorf("insertCommsPhoneSourceEmailLogs0: %w", err)
}
return ret, nil
}
func attachCommsPhoneSourceEmailLogs0(ctx context.Context, exec bob.Executor, count int, commsEmailLogs1 CommsEmailLogSlice, commsPhone0 *CommsPhone) (CommsEmailLogSlice, error) {
setter := &CommsEmailLogSetter{
Source: omit.From(commsPhone0.E164),
}
err := commsEmailLogs1.UpdateAll(ctx, exec, *setter)
if err != nil {
return nil, fmt.Errorf("attachCommsPhoneSourceEmailLogs0: %w", err)
}
return commsEmailLogs1, nil
}
func (commsPhone0 *CommsPhone) InsertSourceEmailLogs(ctx context.Context, exec bob.Executor, related ...*CommsEmailLogSetter) error {
if len(related) == 0 {
return nil
}
var err error
commsEmailLogs1, err := insertCommsPhoneSourceEmailLogs0(ctx, exec, related, commsPhone0)
if err != nil {
return err
}
commsPhone0.R.SourceEmailLogs = append(commsPhone0.R.SourceEmailLogs, commsEmailLogs1...)
for _, rel := range commsEmailLogs1 {
rel.R.SourcePhone = commsPhone0
}
return nil
}
func (commsPhone0 *CommsPhone) AttachSourceEmailLogs(ctx context.Context, exec bob.Executor, related ...*CommsEmailLog) error {
if len(related) == 0 {
return nil
}
var err error
commsEmailLogs1 := CommsEmailLogSlice(related)
_, err = attachCommsPhoneSourceEmailLogs0(ctx, exec, len(related), commsEmailLogs1, commsPhone0)
if err != nil {
return err
}
commsPhone0.R.SourceEmailLogs = append(commsPhone0.R.SourceEmailLogs, commsEmailLogs1...)
for _, rel := range related {
rel.R.SourcePhone = commsPhone0
}
return nil
}
func insertCommsPhoneDestinationSMSLogs0(ctx context.Context, exec bob.Executor, commsSMSLogs1 []*CommsSMSLogSetter, commsPhone0 *CommsPhone) (CommsSMSLogSlice, error) {
for i := range commsSMSLogs1 {
commsSMSLogs1[i].Destination = omit.From(commsPhone0.E164)
}
ret, err := CommsSMSLogs.Insert(bob.ToMods(commsSMSLogs1...)).All(ctx, exec)
if err != nil {
return ret, fmt.Errorf("insertCommsPhoneDestinationSMSLogs0: %w", err)
}
return ret, nil
}
func attachCommsPhoneDestinationSMSLogs0(ctx context.Context, exec bob.Executor, count int, commsSMSLogs1 CommsSMSLogSlice, commsPhone0 *CommsPhone) (CommsSMSLogSlice, error) {
setter := &CommsSMSLogSetter{
Destination: omit.From(commsPhone0.E164),
}
err := commsSMSLogs1.UpdateAll(ctx, exec, *setter)
if err != nil {
return nil, fmt.Errorf("attachCommsPhoneDestinationSMSLogs0: %w", err)
}
return commsSMSLogs1, nil
}
func (commsPhone0 *CommsPhone) InsertDestinationSMSLogs(ctx context.Context, exec bob.Executor, related ...*CommsSMSLogSetter) error {
if len(related) == 0 {
return nil
}
var err error
commsSMSLogs1, err := insertCommsPhoneDestinationSMSLogs0(ctx, exec, related, commsPhone0)
if err != nil {
return err
}
commsPhone0.R.DestinationSMSLogs = append(commsPhone0.R.DestinationSMSLogs, commsSMSLogs1...)
for _, rel := range commsSMSLogs1 {
rel.R.DestinationPhone = commsPhone0
}
return nil
}
func (commsPhone0 *CommsPhone) AttachDestinationSMSLogs(ctx context.Context, exec bob.Executor, related ...*CommsSMSLog) error {
if len(related) == 0 {
return nil
}
var err error
commsSMSLogs1 := CommsSMSLogSlice(related)
_, err = attachCommsPhoneDestinationSMSLogs0(ctx, exec, len(related), commsSMSLogs1, commsPhone0)
if err != nil {
return err
}
commsPhone0.R.DestinationSMSLogs = append(commsPhone0.R.DestinationSMSLogs, commsSMSLogs1...)
for _, rel := range related {
rel.R.DestinationPhone = commsPhone0
}
return nil
}
func insertCommsPhoneSourceSMSLogs0(ctx context.Context, exec bob.Executor, commsSMSLogs1 []*CommsSMSLogSetter, commsPhone0 *CommsPhone) (CommsSMSLogSlice, error) {
for i := range commsSMSLogs1 {
commsSMSLogs1[i].Source = omit.From(commsPhone0.E164)
}
ret, err := CommsSMSLogs.Insert(bob.ToMods(commsSMSLogs1...)).All(ctx, exec)
if err != nil {
return ret, fmt.Errorf("insertCommsPhoneSourceSMSLogs0: %w", err)
}
return ret, nil
}
func attachCommsPhoneSourceSMSLogs0(ctx context.Context, exec bob.Executor, count int, commsSMSLogs1 CommsSMSLogSlice, commsPhone0 *CommsPhone) (CommsSMSLogSlice, error) {
setter := &CommsSMSLogSetter{
Source: omit.From(commsPhone0.E164),
}
err := commsSMSLogs1.UpdateAll(ctx, exec, *setter)
if err != nil {
return nil, fmt.Errorf("attachCommsPhoneSourceSMSLogs0: %w", err)
}
return commsSMSLogs1, nil
}
func (commsPhone0 *CommsPhone) InsertSourceSMSLogs(ctx context.Context, exec bob.Executor, related ...*CommsSMSLogSetter) error {
if len(related) == 0 {
return nil
}
var err error
commsSMSLogs1, err := insertCommsPhoneSourceSMSLogs0(ctx, exec, related, commsPhone0)
if err != nil {
return err
}
commsPhone0.R.SourceSMSLogs = append(commsPhone0.R.SourceSMSLogs, commsSMSLogs1...)
for _, rel := range commsSMSLogs1 {
rel.R.SourcePhone = commsPhone0
}
return nil
}
func (commsPhone0 *CommsPhone) AttachSourceSMSLogs(ctx context.Context, exec bob.Executor, related ...*CommsSMSLog) error {
if len(related) == 0 {
return nil
}
var err error
commsSMSLogs1 := CommsSMSLogSlice(related)
_, err = attachCommsPhoneSourceSMSLogs0(ctx, exec, len(related), commsSMSLogs1, commsPhone0)
if err != nil {
return err
}
commsPhone0.R.SourceSMSLogs = append(commsPhone0.R.SourceSMSLogs, commsSMSLogs1...)
for _, rel := range related {
rel.R.SourcePhone = commsPhone0
}
return nil
}
type commsPhoneWhere[Q psql.Filterable] struct {
E164 psql.WhereMod[Q, string]
IsSubscribed psql.WhereMod[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.Where[Q, bool](cols.IsSubscribed),
}
}
func (o *CommsPhone) Preload(name string, retrieved any) error {
if o == nil {
return nil
}
switch name {
case "SourceEmailLogs":
rels, ok := retrieved.(CommsEmailLogSlice)
if !ok {
return fmt.Errorf("commsPhone cannot load %T as %q", retrieved, name)
}
o.R.SourceEmailLogs = rels
for _, rel := range rels {
if rel != nil {
rel.R.SourcePhone = o
}
}
return nil
case "DestinationSMSLogs":
rels, ok := retrieved.(CommsSMSLogSlice)
if !ok {
return fmt.Errorf("commsPhone cannot load %T as %q", retrieved, name)
}
o.R.DestinationSMSLogs = rels
for _, rel := range rels {
if rel != nil {
rel.R.DestinationPhone = o
}
}
return nil
case "SourceSMSLogs":
rels, ok := retrieved.(CommsSMSLogSlice)
if !ok {
return fmt.Errorf("commsPhone cannot load %T as %q", retrieved, name)
}
o.R.SourceSMSLogs = 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 {
SourceEmailLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
DestinationSMSLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
SourceSMSLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildCommsPhoneThenLoader[Q orm.Loadable]() commsPhoneThenLoader[Q] {
type SourceEmailLogsLoadInterface interface {
LoadSourceEmailLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
type DestinationSMSLogsLoadInterface interface {
LoadDestinationSMSLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
type SourceSMSLogsLoadInterface interface {
LoadSourceSMSLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return commsPhoneThenLoader[Q]{
SourceEmailLogs: thenLoadBuilder[Q](
"SourceEmailLogs",
func(ctx context.Context, exec bob.Executor, retrieved SourceEmailLogsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadSourceEmailLogs(ctx, exec, mods...)
},
),
DestinationSMSLogs: thenLoadBuilder[Q](
"DestinationSMSLogs",
func(ctx context.Context, exec bob.Executor, retrieved DestinationSMSLogsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadDestinationSMSLogs(ctx, exec, mods...)
},
),
SourceSMSLogs: thenLoadBuilder[Q](
"SourceSMSLogs",
func(ctx context.Context, exec bob.Executor, retrieved SourceSMSLogsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadSourceSMSLogs(ctx, exec, mods...)
},
),
}
}
// LoadSourceEmailLogs loads the commsPhone's SourceEmailLogs into the .R struct
func (o *CommsPhone) LoadSourceEmailLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.SourceEmailLogs = nil
related, err := o.SourceEmailLogs(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, rel := range related {
rel.R.SourcePhone = o
}
o.R.SourceEmailLogs = related
return nil
}
// LoadSourceEmailLogs loads the commsPhone's SourceEmailLogs into the .R struct
func (os CommsPhoneSlice) LoadSourceEmailLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
commsEmailLogs, err := os.SourceEmailLogs(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
o.R.SourceEmailLogs = nil
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range commsEmailLogs {
if !(o.E164 == rel.Source) {
continue
}
rel.R.SourcePhone = o
o.R.SourceEmailLogs = append(o.R.SourceEmailLogs, rel)
}
}
return nil
}
// LoadDestinationSMSLogs loads the commsPhone's DestinationSMSLogs into the .R struct
func (o *CommsPhone) LoadDestinationSMSLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.DestinationSMSLogs = nil
related, err := o.DestinationSMSLogs(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, rel := range related {
rel.R.DestinationPhone = o
}
o.R.DestinationSMSLogs = related
return nil
}
// LoadDestinationSMSLogs loads the commsPhone's DestinationSMSLogs into the .R struct
func (os CommsPhoneSlice) LoadDestinationSMSLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
commsSMSLogs, err := os.DestinationSMSLogs(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
o.R.DestinationSMSLogs = nil
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range commsSMSLogs {
if !(o.E164 == rel.Destination) {
continue
}
rel.R.DestinationPhone = o
o.R.DestinationSMSLogs = append(o.R.DestinationSMSLogs, rel)
}
}
return nil
}
// LoadSourceSMSLogs loads the commsPhone's SourceSMSLogs into the .R struct
func (o *CommsPhone) LoadSourceSMSLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.SourceSMSLogs = nil
related, err := o.SourceSMSLogs(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, rel := range related {
rel.R.SourcePhone = o
}
o.R.SourceSMSLogs = related
return nil
}
// LoadSourceSMSLogs loads the commsPhone's SourceSMSLogs into the .R struct
func (os CommsPhoneSlice) LoadSourceSMSLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
commsSMSLogs, err := os.SourceSMSLogs(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
o.R.SourceSMSLogs = nil
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range commsSMSLogs {
if !(o.E164 == rel.Source) {
continue
}
rel.R.SourcePhone = o
o.R.SourceSMSLogs = append(o.R.SourceSMSLogs, rel)
}
}
return nil
}
// commsPhoneC is where relationship counts are stored.
type commsPhoneC struct {
SourceEmailLogs *int64
DestinationSMSLogs *int64
SourceSMSLogs *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 "SourceEmailLogs":
o.C.SourceEmailLogs = &count
case "DestinationSMSLogs":
o.C.DestinationSMSLogs = &count
case "SourceSMSLogs":
o.C.SourceSMSLogs = &count
}
return nil
}
type commsPhoneCountPreloader struct {
SourceEmailLogs func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
DestinationSMSLogs func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
SourceSMSLogs func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
}
func buildCommsPhoneCountPreloader() commsPhoneCountPreloader {
return commsPhoneCountPreloader{
SourceEmailLogs: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
return countPreloader[*CommsPhone]("SourceEmailLogs", 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(CommsEmailLogs.Name()),
sm.Where(psql.Quote(CommsEmailLogs.Alias(), "source").EQ(psql.Quote(parent, "e164"))),
}
subqueryMods = append(subqueryMods, mods...)
return psql.Group(psql.Select(subqueryMods...).Expression)
})
},
DestinationSMSLogs: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
return countPreloader[*CommsPhone]("DestinationSMSLogs", 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(CommsSMSLogs.Name()),
sm.Where(psql.Quote(CommsSMSLogs.Alias(), "destination").EQ(psql.Quote(parent, "e164"))),
}
subqueryMods = append(subqueryMods, mods...)
return psql.Group(psql.Select(subqueryMods...).Expression)
})
},
SourceSMSLogs: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
return countPreloader[*CommsPhone]("SourceSMSLogs", 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(CommsSMSLogs.Name()),
sm.Where(psql.Quote(CommsSMSLogs.Alias(), "source").EQ(psql.Quote(parent, "e164"))),
}
subqueryMods = append(subqueryMods, mods...)
return psql.Group(psql.Select(subqueryMods...).Expression)
})
},
}
}
type commsPhoneCountThenLoader[Q orm.Loadable] struct {
SourceEmailLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
DestinationSMSLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
SourceSMSLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildCommsPhoneCountThenLoader[Q orm.Loadable]() commsPhoneCountThenLoader[Q] {
type SourceEmailLogsCountInterface interface {
LoadCountSourceEmailLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
type DestinationSMSLogsCountInterface interface {
LoadCountDestinationSMSLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
type SourceSMSLogsCountInterface interface {
LoadCountSourceSMSLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return commsPhoneCountThenLoader[Q]{
SourceEmailLogs: countThenLoadBuilder[Q](
"SourceEmailLogs",
func(ctx context.Context, exec bob.Executor, retrieved SourceEmailLogsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadCountSourceEmailLogs(ctx, exec, mods...)
},
),
DestinationSMSLogs: countThenLoadBuilder[Q](
"DestinationSMSLogs",
func(ctx context.Context, exec bob.Executor, retrieved DestinationSMSLogsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadCountDestinationSMSLogs(ctx, exec, mods...)
},
),
SourceSMSLogs: countThenLoadBuilder[Q](
"SourceSMSLogs",
func(ctx context.Context, exec bob.Executor, retrieved SourceSMSLogsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadCountSourceSMSLogs(ctx, exec, mods...)
},
),
}
}
// LoadCountSourceEmailLogs loads the count of SourceEmailLogs into the C struct
func (o *CommsPhone) LoadCountSourceEmailLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
count, err := o.SourceEmailLogs(mods...).Count(ctx, exec)
if err != nil {
return err
}
o.C.SourceEmailLogs = &count
return nil
}
// LoadCountSourceEmailLogs loads the count of SourceEmailLogs for a slice
func (os CommsPhoneSlice) LoadCountSourceEmailLogs(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.LoadCountSourceEmailLogs(ctx, exec, mods...); err != nil {
return err
}
}
return nil
}
// LoadCountDestinationSMSLogs loads the count of DestinationSMSLogs into the C struct
func (o *CommsPhone) LoadCountDestinationSMSLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
count, err := o.DestinationSMSLogs(mods...).Count(ctx, exec)
if err != nil {
return err
}
o.C.DestinationSMSLogs = &count
return nil
}
// LoadCountDestinationSMSLogs loads the count of DestinationSMSLogs for a slice
func (os CommsPhoneSlice) LoadCountDestinationSMSLogs(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.LoadCountDestinationSMSLogs(ctx, exec, mods...); err != nil {
return err
}
}
return nil
}
// LoadCountSourceSMSLogs loads the count of SourceSMSLogs into the C struct
func (o *CommsPhone) LoadCountSourceSMSLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
count, err := o.SourceSMSLogs(mods...).Count(ctx, exec)
if err != nil {
return err
}
o.C.SourceSMSLogs = &count
return nil
}
// LoadCountSourceSMSLogs loads the count of SourceSMSLogs for a slice
func (os CommsPhoneSlice) LoadCountSourceSMSLogs(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.LoadCountSourceSMSLogs(ctx, exec, mods...); err != nil {
return err
}
}
return nil
}
type commsPhoneJoins[Q dialect.Joinable] struct {
typ string
SourceEmailLogs modAs[Q, commsEmailLogColumns]
DestinationSMSLogs modAs[Q, commsSMSLogColumns]
SourceSMSLogs modAs[Q, commsSMSLogColumns]
}
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,
SourceEmailLogs: modAs[Q, commsEmailLogColumns]{
c: CommsEmailLogs.Columns,
f: func(to commsEmailLogColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, CommsEmailLogs.Name().As(to.Alias())).On(
to.Source.EQ(cols.E164),
))
}
return mods
},
},
DestinationSMSLogs: modAs[Q, commsSMSLogColumns]{
c: CommsSMSLogs.Columns,
f: func(to commsSMSLogColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, CommsSMSLogs.Name().As(to.Alias())).On(
to.Destination.EQ(cols.E164),
))
}
return mods
},
},
SourceSMSLogs: modAs[Q, commsSMSLogColumns]{
c: CommsSMSLogs.Columns,
f: func(to commsSMSLogColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, CommsSMSLogs.Name().As(to.Alias())).On(
to.Source.EQ(cols.E164),
))
}
return mods
},
},
}
}