nidus-sync/db/models/comms.email_log.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

848 lines
25 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"
"time"
enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
"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"
)
// CommsEmailLog is an object representing the database table.
type CommsEmailLog struct {
Created time.Time `db:"created" `
Destination string `db:"destination,pk" `
Source string `db:"source,pk" `
Type enums.CommsEmailmessagetype `db:"type,pk" `
R commsEmailLogR `db:"-" `
}
// CommsEmailLogSlice is an alias for a slice of pointers to CommsEmailLog.
// This should almost always be used instead of []*CommsEmailLog.
type CommsEmailLogSlice []*CommsEmailLog
// CommsEmailLogs contains methods to work with the email_log table
var CommsEmailLogs = psql.NewTablex[*CommsEmailLog, CommsEmailLogSlice, *CommsEmailLogSetter]("comms", "email_log", buildCommsEmailLogColumns("comms.email_log"))
// CommsEmailLogsQuery is a query on the email_log table
type CommsEmailLogsQuery = *psql.ViewQuery[*CommsEmailLog, CommsEmailLogSlice]
// commsEmailLogR is where relationships are stored.
type commsEmailLogR struct {
DestinationEmail *CommsEmail // comms.email_log.email_log_destination_fkey
SourcePhone *CommsPhone // comms.email_log.email_log_source_fkey
}
func buildCommsEmailLogColumns(alias string) commsEmailLogColumns {
return commsEmailLogColumns{
ColumnsExpr: expr.NewColumnsExpr(
"created", "destination", "source", "type",
).WithParent("comms.email_log"),
tableAlias: alias,
Created: psql.Quote(alias, "created"),
Destination: psql.Quote(alias, "destination"),
Source: psql.Quote(alias, "source"),
Type: psql.Quote(alias, "type"),
}
}
type commsEmailLogColumns struct {
expr.ColumnsExpr
tableAlias string
Created psql.Expression
Destination psql.Expression
Source psql.Expression
Type psql.Expression
}
func (c commsEmailLogColumns) Alias() string {
return c.tableAlias
}
func (commsEmailLogColumns) AliasedAs(alias string) commsEmailLogColumns {
return buildCommsEmailLogColumns(alias)
}
// CommsEmailLogSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type CommsEmailLogSetter struct {
Created omit.Val[time.Time] `db:"created" `
Destination omit.Val[string] `db:"destination,pk" `
Source omit.Val[string] `db:"source,pk" `
Type omit.Val[enums.CommsEmailmessagetype] `db:"type,pk" `
}
func (s CommsEmailLogSetter) SetColumns() []string {
vals := make([]string, 0, 4)
if s.Created.IsValue() {
vals = append(vals, "created")
}
if s.Destination.IsValue() {
vals = append(vals, "destination")
}
if s.Source.IsValue() {
vals = append(vals, "source")
}
if s.Type.IsValue() {
vals = append(vals, "type")
}
return vals
}
func (s CommsEmailLogSetter) Overwrite(t *CommsEmailLog) {
if s.Created.IsValue() {
t.Created = s.Created.MustGet()
}
if s.Destination.IsValue() {
t.Destination = s.Destination.MustGet()
}
if s.Source.IsValue() {
t.Source = s.Source.MustGet()
}
if s.Type.IsValue() {
t.Type = s.Type.MustGet()
}
}
func (s *CommsEmailLogSetter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return CommsEmailLogs.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.Created.IsValue() {
vals[0] = psql.Arg(s.Created.MustGet())
} else {
vals[0] = psql.Raw("DEFAULT")
}
if s.Destination.IsValue() {
vals[1] = psql.Arg(s.Destination.MustGet())
} else {
vals[1] = psql.Raw("DEFAULT")
}
if s.Source.IsValue() {
vals[2] = psql.Arg(s.Source.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 CommsEmailLogSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
return um.Set(s.Expressions()...)
}
func (s CommsEmailLogSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 4)
if s.Created.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "created")...),
psql.Arg(s.Created),
}})
}
if s.Destination.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "destination")...),
psql.Arg(s.Destination),
}})
}
if s.Source.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "source")...),
psql.Arg(s.Source),
}})
}
if s.Type.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "type")...),
psql.Arg(s.Type),
}})
}
return exprs
}
// FindCommsEmailLog retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindCommsEmailLog(ctx context.Context, exec bob.Executor, DestinationPK string, SourcePK string, TypePK enums.CommsEmailmessagetype, cols ...string) (*CommsEmailLog, error) {
if len(cols) == 0 {
return CommsEmailLogs.Query(
sm.Where(CommsEmailLogs.Columns.Destination.EQ(psql.Arg(DestinationPK))),
sm.Where(CommsEmailLogs.Columns.Source.EQ(psql.Arg(SourcePK))),
sm.Where(CommsEmailLogs.Columns.Type.EQ(psql.Arg(TypePK))),
).One(ctx, exec)
}
return CommsEmailLogs.Query(
sm.Where(CommsEmailLogs.Columns.Destination.EQ(psql.Arg(DestinationPK))),
sm.Where(CommsEmailLogs.Columns.Source.EQ(psql.Arg(SourcePK))),
sm.Where(CommsEmailLogs.Columns.Type.EQ(psql.Arg(TypePK))),
sm.Columns(CommsEmailLogs.Columns.Only(cols...)),
).One(ctx, exec)
}
// CommsEmailLogExists checks the presence of a single record by primary key
func CommsEmailLogExists(ctx context.Context, exec bob.Executor, DestinationPK string, SourcePK string, TypePK enums.CommsEmailmessagetype) (bool, error) {
return CommsEmailLogs.Query(
sm.Where(CommsEmailLogs.Columns.Destination.EQ(psql.Arg(DestinationPK))),
sm.Where(CommsEmailLogs.Columns.Source.EQ(psql.Arg(SourcePK))),
sm.Where(CommsEmailLogs.Columns.Type.EQ(psql.Arg(TypePK))),
).Exists(ctx, exec)
}
// AfterQueryHook is called after CommsEmailLog is retrieved from the database
func (o *CommsEmailLog) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = CommsEmailLogs.AfterSelectHooks.RunHooks(ctx, exec, CommsEmailLogSlice{o})
case bob.QueryTypeInsert:
ctx, err = CommsEmailLogs.AfterInsertHooks.RunHooks(ctx, exec, CommsEmailLogSlice{o})
case bob.QueryTypeUpdate:
ctx, err = CommsEmailLogs.AfterUpdateHooks.RunHooks(ctx, exec, CommsEmailLogSlice{o})
case bob.QueryTypeDelete:
ctx, err = CommsEmailLogs.AfterDeleteHooks.RunHooks(ctx, exec, CommsEmailLogSlice{o})
}
return err
}
// primaryKeyVals returns the primary key values of the CommsEmailLog
func (o *CommsEmailLog) primaryKeyVals() bob.Expression {
return psql.ArgGroup(
o.Destination,
o.Source,
o.Type,
)
}
func (o *CommsEmailLog) pkEQ() dialect.Expression {
return psql.Group(psql.Quote("comms.email_log", "destination"), psql.Quote("comms.email_log", "source"), psql.Quote("comms.email_log", "type")).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 CommsEmailLog
func (o *CommsEmailLog) Update(ctx context.Context, exec bob.Executor, s *CommsEmailLogSetter) error {
v, err := CommsEmailLogs.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 CommsEmailLog record with an executor
func (o *CommsEmailLog) Delete(ctx context.Context, exec bob.Executor) error {
_, err := CommsEmailLogs.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
return err
}
// Reload refreshes the CommsEmailLog using the executor
func (o *CommsEmailLog) Reload(ctx context.Context, exec bob.Executor) error {
o2, err := CommsEmailLogs.Query(
sm.Where(CommsEmailLogs.Columns.Destination.EQ(psql.Arg(o.Destination))),
sm.Where(CommsEmailLogs.Columns.Source.EQ(psql.Arg(o.Source))),
sm.Where(CommsEmailLogs.Columns.Type.EQ(psql.Arg(o.Type))),
).One(ctx, exec)
if err != nil {
return err
}
o2.R = o.R
*o = *o2
return nil
}
// AfterQueryHook is called after CommsEmailLogSlice is retrieved from the database
func (o CommsEmailLogSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = CommsEmailLogs.AfterSelectHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeInsert:
ctx, err = CommsEmailLogs.AfterInsertHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeUpdate:
ctx, err = CommsEmailLogs.AfterUpdateHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeDelete:
ctx, err = CommsEmailLogs.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}
func (o CommsEmailLogSlice) pkIN() dialect.Expression {
if len(o) == 0 {
return psql.Raw("NULL")
}
return psql.Group(psql.Quote("comms.email_log", "destination"), psql.Quote("comms.email_log", "source"), psql.Quote("comms.email_log", "type")).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 CommsEmailLogSlice) copyMatchingRows(from ...*CommsEmailLog) {
for i, old := range o {
for _, new := range from {
if new.Destination != old.Destination {
continue
}
if new.Source != old.Source {
continue
}
if new.Type != old.Type {
continue
}
new.R = old.R
o[i] = new
break
}
}
}
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o CommsEmailLogSlice) 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 CommsEmailLogs.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 *CommsEmailLog:
o.copyMatchingRows(retrieved)
case []*CommsEmailLog:
o.copyMatchingRows(retrieved...)
case CommsEmailLogSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a CommsEmailLog or a slice of CommsEmailLog
// then run the AfterUpdateHooks on the slice
_, err = CommsEmailLogs.AfterUpdateHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o CommsEmailLogSlice) 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 CommsEmailLogs.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 *CommsEmailLog:
o.copyMatchingRows(retrieved)
case []*CommsEmailLog:
o.copyMatchingRows(retrieved...)
case CommsEmailLogSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a CommsEmailLog or a slice of CommsEmailLog
// then run the AfterDeleteHooks on the slice
_, err = CommsEmailLogs.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
func (o CommsEmailLogSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals CommsEmailLogSetter) error {
if len(o) == 0 {
return nil
}
_, err := CommsEmailLogs.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
return err
}
func (o CommsEmailLogSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
_, err := CommsEmailLogs.Delete(o.DeleteMod()).Exec(ctx, exec)
return err
}
func (o CommsEmailLogSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
o2, err := CommsEmailLogs.Query(sm.Where(o.pkIN())).All(ctx, exec)
if err != nil {
return err
}
o.copyMatchingRows(o2...)
return nil
}
// DestinationEmail starts a query for related objects on comms.email
func (o *CommsEmailLog) DestinationEmail(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailsQuery {
return CommsEmails.Query(append(mods,
sm.Where(CommsEmails.Columns.Address.EQ(psql.Arg(o.Destination))),
)...)
}
func (os CommsEmailLogSlice) DestinationEmail(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailsQuery {
pkDestination := make(pgtypes.Array[string], 0, len(os))
for _, o := range os {
if o == nil {
continue
}
pkDestination = append(pkDestination, o.Destination)
}
PKArgExpr := psql.Select(sm.Columns(
psql.F("unnest", psql.Cast(psql.Arg(pkDestination), "text[]")),
))
return CommsEmails.Query(append(mods,
sm.Where(psql.Group(CommsEmails.Columns.Address).OP("IN", PKArgExpr)),
)...)
}
// SourcePhone starts a query for related objects on comms.phone
func (o *CommsEmailLog) SourcePhone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
return CommsPhones.Query(append(mods,
sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(o.Source))),
)...)
}
func (os CommsEmailLogSlice) SourcePhone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
pkSource := make(pgtypes.Array[string], 0, len(os))
for _, o := range os {
if o == nil {
continue
}
pkSource = append(pkSource, o.Source)
}
PKArgExpr := psql.Select(sm.Columns(
psql.F("unnest", psql.Cast(psql.Arg(pkSource), "text[]")),
))
return CommsPhones.Query(append(mods,
sm.Where(psql.Group(CommsPhones.Columns.E164).OP("IN", PKArgExpr)),
)...)
}
func attachCommsEmailLogDestinationEmail0(ctx context.Context, exec bob.Executor, count int, commsEmailLog0 *CommsEmailLog, commsEmail1 *CommsEmail) (*CommsEmailLog, error) {
setter := &CommsEmailLogSetter{
Destination: omit.From(commsEmail1.Address),
}
err := commsEmailLog0.Update(ctx, exec, setter)
if err != nil {
return nil, fmt.Errorf("attachCommsEmailLogDestinationEmail0: %w", err)
}
return commsEmailLog0, nil
}
func (commsEmailLog0 *CommsEmailLog) InsertDestinationEmail(ctx context.Context, exec bob.Executor, related *CommsEmailSetter) error {
var err error
commsEmail1, err := CommsEmails.Insert(related).One(ctx, exec)
if err != nil {
return fmt.Errorf("inserting related objects: %w", err)
}
_, err = attachCommsEmailLogDestinationEmail0(ctx, exec, 1, commsEmailLog0, commsEmail1)
if err != nil {
return err
}
commsEmailLog0.R.DestinationEmail = commsEmail1
commsEmail1.R.DestinationEmailLogs = append(commsEmail1.R.DestinationEmailLogs, commsEmailLog0)
return nil
}
func (commsEmailLog0 *CommsEmailLog) AttachDestinationEmail(ctx context.Context, exec bob.Executor, commsEmail1 *CommsEmail) error {
var err error
_, err = attachCommsEmailLogDestinationEmail0(ctx, exec, 1, commsEmailLog0, commsEmail1)
if err != nil {
return err
}
commsEmailLog0.R.DestinationEmail = commsEmail1
commsEmail1.R.DestinationEmailLogs = append(commsEmail1.R.DestinationEmailLogs, commsEmailLog0)
return nil
}
func attachCommsEmailLogSourcePhone0(ctx context.Context, exec bob.Executor, count int, commsEmailLog0 *CommsEmailLog, commsPhone1 *CommsPhone) (*CommsEmailLog, error) {
setter := &CommsEmailLogSetter{
Source: omit.From(commsPhone1.E164),
}
err := commsEmailLog0.Update(ctx, exec, setter)
if err != nil {
return nil, fmt.Errorf("attachCommsEmailLogSourcePhone0: %w", err)
}
return commsEmailLog0, nil
}
func (commsEmailLog0 *CommsEmailLog) InsertSourcePhone(ctx context.Context, exec bob.Executor, related *CommsPhoneSetter) error {
var err error
commsPhone1, err := CommsPhones.Insert(related).One(ctx, exec)
if err != nil {
return fmt.Errorf("inserting related objects: %w", err)
}
_, err = attachCommsEmailLogSourcePhone0(ctx, exec, 1, commsEmailLog0, commsPhone1)
if err != nil {
return err
}
commsEmailLog0.R.SourcePhone = commsPhone1
commsPhone1.R.SourceEmailLogs = append(commsPhone1.R.SourceEmailLogs, commsEmailLog0)
return nil
}
func (commsEmailLog0 *CommsEmailLog) AttachSourcePhone(ctx context.Context, exec bob.Executor, commsPhone1 *CommsPhone) error {
var err error
_, err = attachCommsEmailLogSourcePhone0(ctx, exec, 1, commsEmailLog0, commsPhone1)
if err != nil {
return err
}
commsEmailLog0.R.SourcePhone = commsPhone1
commsPhone1.R.SourceEmailLogs = append(commsPhone1.R.SourceEmailLogs, commsEmailLog0)
return nil
}
type commsEmailLogWhere[Q psql.Filterable] struct {
Created psql.WhereMod[Q, time.Time]
Destination psql.WhereMod[Q, string]
Source psql.WhereMod[Q, string]
Type psql.WhereMod[Q, enums.CommsEmailmessagetype]
}
func (commsEmailLogWhere[Q]) AliasedAs(alias string) commsEmailLogWhere[Q] {
return buildCommsEmailLogWhere[Q](buildCommsEmailLogColumns(alias))
}
func buildCommsEmailLogWhere[Q psql.Filterable](cols commsEmailLogColumns) commsEmailLogWhere[Q] {
return commsEmailLogWhere[Q]{
Created: psql.Where[Q, time.Time](cols.Created),
Destination: psql.Where[Q, string](cols.Destination),
Source: psql.Where[Q, string](cols.Source),
Type: psql.Where[Q, enums.CommsEmailmessagetype](cols.Type),
}
}
func (o *CommsEmailLog) Preload(name string, retrieved any) error {
if o == nil {
return nil
}
switch name {
case "DestinationEmail":
rel, ok := retrieved.(*CommsEmail)
if !ok {
return fmt.Errorf("commsEmailLog cannot load %T as %q", retrieved, name)
}
o.R.DestinationEmail = rel
if rel != nil {
rel.R.DestinationEmailLogs = CommsEmailLogSlice{o}
}
return nil
case "SourcePhone":
rel, ok := retrieved.(*CommsPhone)
if !ok {
return fmt.Errorf("commsEmailLog cannot load %T as %q", retrieved, name)
}
o.R.SourcePhone = rel
if rel != nil {
rel.R.SourceEmailLogs = CommsEmailLogSlice{o}
}
return nil
default:
return fmt.Errorf("commsEmailLog has no relationship %q", name)
}
}
type commsEmailLogPreloader struct {
DestinationEmail func(...psql.PreloadOption) psql.Preloader
SourcePhone func(...psql.PreloadOption) psql.Preloader
}
func buildCommsEmailLogPreloader() commsEmailLogPreloader {
return commsEmailLogPreloader{
DestinationEmail: func(opts ...psql.PreloadOption) psql.Preloader {
return psql.Preload[*CommsEmail, CommsEmailSlice](psql.PreloadRel{
Name: "DestinationEmail",
Sides: []psql.PreloadSide{
{
From: CommsEmailLogs,
To: CommsEmails,
FromColumns: []string{"destination"},
ToColumns: []string{"address"},
},
},
}, CommsEmails.Columns.Names(), opts...)
},
SourcePhone: func(opts ...psql.PreloadOption) psql.Preloader {
return psql.Preload[*CommsPhone, CommsPhoneSlice](psql.PreloadRel{
Name: "SourcePhone",
Sides: []psql.PreloadSide{
{
From: CommsEmailLogs,
To: CommsPhones,
FromColumns: []string{"source"},
ToColumns: []string{"e164"},
},
},
}, CommsPhones.Columns.Names(), opts...)
},
}
}
type commsEmailLogThenLoader[Q orm.Loadable] struct {
DestinationEmail func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
SourcePhone func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildCommsEmailLogThenLoader[Q orm.Loadable]() commsEmailLogThenLoader[Q] {
type DestinationEmailLoadInterface interface {
LoadDestinationEmail(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
type SourcePhoneLoadInterface interface {
LoadSourcePhone(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return commsEmailLogThenLoader[Q]{
DestinationEmail: thenLoadBuilder[Q](
"DestinationEmail",
func(ctx context.Context, exec bob.Executor, retrieved DestinationEmailLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadDestinationEmail(ctx, exec, mods...)
},
),
SourcePhone: thenLoadBuilder[Q](
"SourcePhone",
func(ctx context.Context, exec bob.Executor, retrieved SourcePhoneLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadSourcePhone(ctx, exec, mods...)
},
),
}
}
// LoadDestinationEmail loads the commsEmailLog's DestinationEmail into the .R struct
func (o *CommsEmailLog) LoadDestinationEmail(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.DestinationEmail = nil
related, err := o.DestinationEmail(mods...).One(ctx, exec)
if err != nil {
return err
}
related.R.DestinationEmailLogs = CommsEmailLogSlice{o}
o.R.DestinationEmail = related
return nil
}
// LoadDestinationEmail loads the commsEmailLog's DestinationEmail into the .R struct
func (os CommsEmailLogSlice) LoadDestinationEmail(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
commsEmails, err := os.DestinationEmail(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range commsEmails {
if !(o.Destination == rel.Address) {
continue
}
rel.R.DestinationEmailLogs = append(rel.R.DestinationEmailLogs, o)
o.R.DestinationEmail = rel
break
}
}
return nil
}
// LoadSourcePhone loads the commsEmailLog's SourcePhone into the .R struct
func (o *CommsEmailLog) LoadSourcePhone(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.SourcePhone = nil
related, err := o.SourcePhone(mods...).One(ctx, exec)
if err != nil {
return err
}
related.R.SourceEmailLogs = CommsEmailLogSlice{o}
o.R.SourcePhone = related
return nil
}
// LoadSourcePhone loads the commsEmailLog's SourcePhone into the .R struct
func (os CommsEmailLogSlice) LoadSourcePhone(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
commsPhones, err := os.SourcePhone(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range commsPhones {
if !(o.Source == rel.E164) {
continue
}
rel.R.SourceEmailLogs = append(rel.R.SourceEmailLogs, o)
o.R.SourcePhone = rel
break
}
}
return nil
}
type commsEmailLogJoins[Q dialect.Joinable] struct {
typ string
DestinationEmail modAs[Q, commsEmailColumns]
SourcePhone modAs[Q, commsPhoneColumns]
}
func (j commsEmailLogJoins[Q]) aliasedAs(alias string) commsEmailLogJoins[Q] {
return buildCommsEmailLogJoins[Q](buildCommsEmailLogColumns(alias), j.typ)
}
func buildCommsEmailLogJoins[Q dialect.Joinable](cols commsEmailLogColumns, typ string) commsEmailLogJoins[Q] {
return commsEmailLogJoins[Q]{
typ: typ,
DestinationEmail: modAs[Q, commsEmailColumns]{
c: CommsEmails.Columns,
f: func(to commsEmailColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, CommsEmails.Name().As(to.Alias())).On(
to.Address.EQ(cols.Destination),
))
}
return mods
},
},
SourcePhone: modAs[Q, commsPhoneColumns]{
c: CommsPhones.Columns,
f: func(to commsPhoneColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, CommsPhones.Name().As(to.Alias())).On(
to.E164.EQ(cols.Source),
))
}
return mods
},
},
}
}