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
This commit is contained in:
Eli Ribble 2026-01-20 17:10:22 +00:00
parent 8f44e57c72
commit 842e6cff43
No known key found for this signature in database
47 changed files with 7361 additions and 179 deletions

View file

@ -22,6 +22,8 @@ var (
type preloadCounts struct {
ArcgisUser arcgisuserCountPreloader
CommsEmail commsEmailCountPreloader
CommsPhone commsPhoneCountPreloader
NoteAudio noteAudioCountPreloader
NoteImage noteImageCountPreloader
Organization organizationCountPreloader
@ -34,6 +36,8 @@ type preloadCounts struct {
func getPreloadCount() preloadCounts {
return preloadCounts{
ArcgisUser: buildArcgisUserCountPreloader(),
CommsEmail: buildCommsEmailCountPreloader(),
CommsPhone: buildCommsPhoneCountPreloader(),
NoteAudio: buildNoteAudioCountPreloader(),
NoteImage: buildNoteImageCountPreloader(),
Organization: buildOrganizationCountPreloader(),
@ -46,6 +50,8 @@ func getPreloadCount() preloadCounts {
type thenLoadCounts[Q orm.Loadable] struct {
ArcgisUser arcgisuserCountThenLoader[Q]
CommsEmail commsEmailCountThenLoader[Q]
CommsPhone commsPhoneCountThenLoader[Q]
NoteAudio noteAudioCountThenLoader[Q]
NoteImage noteImageCountThenLoader[Q]
Organization organizationCountThenLoader[Q]
@ -58,6 +64,8 @@ type thenLoadCounts[Q orm.Loadable] struct {
func getThenLoadCount[Q orm.Loadable]() thenLoadCounts[Q] {
return thenLoadCounts[Q]{
ArcgisUser: buildArcgisUserCountThenLoader[Q](),
CommsEmail: buildCommsEmailCountThenLoader[Q](),
CommsPhone: buildCommsPhoneCountThenLoader[Q](),
NoteAudio: buildNoteAudioCountThenLoader[Q](),
NoteImage: buildNoteImageCountThenLoader[Q](),
Organization: buildOrganizationCountThenLoader[Q](),

View file

@ -34,6 +34,10 @@ func (j joinSet[Q]) AliasedAs(alias string) joinSet[Q] {
type joins[Q dialect.Joinable] struct {
ArcgisUsers joinSet[arcgisuserJoins[Q]]
ArcgisUserPrivileges joinSet[arcgisUserPrivilegeJoins[Q]]
CommsEmails joinSet[commsEmailJoins[Q]]
CommsEmailLogs joinSet[commsEmailLogJoins[Q]]
CommsPhones joinSet[commsPhoneJoins[Q]]
CommsSMSLogs joinSet[commsSMSLogJoins[Q]]
FieldseekerContainerrelates joinSet[fieldseekerContainerrelateJoins[Q]]
FieldseekerFieldscoutinglogs joinSet[fieldseekerFieldscoutinglogJoins[Q]]
FieldseekerHabitatrelates joinSet[fieldseekerHabitatrelateJoins[Q]]
@ -94,6 +98,10 @@ func getJoins[Q dialect.Joinable]() joins[Q] {
return joins[Q]{
ArcgisUsers: buildJoinSet[arcgisuserJoins[Q]](ArcgisUsers.Columns, buildArcgisUserJoins),
ArcgisUserPrivileges: buildJoinSet[arcgisUserPrivilegeJoins[Q]](ArcgisUserPrivileges.Columns, buildArcgisUserPrivilegeJoins),
CommsEmails: buildJoinSet[commsEmailJoins[Q]](CommsEmails.Columns, buildCommsEmailJoins),
CommsEmailLogs: buildJoinSet[commsEmailLogJoins[Q]](CommsEmailLogs.Columns, buildCommsEmailLogJoins),
CommsPhones: buildJoinSet[commsPhoneJoins[Q]](CommsPhones.Columns, buildCommsPhoneJoins),
CommsSMSLogs: buildJoinSet[commsSMSLogJoins[Q]](CommsSMSLogs.Columns, buildCommsSMSLogJoins),
FieldseekerContainerrelates: buildJoinSet[fieldseekerContainerrelateJoins[Q]](FieldseekerContainerrelates.Columns, buildFieldseekerContainerrelateJoins),
FieldseekerFieldscoutinglogs: buildJoinSet[fieldseekerFieldscoutinglogJoins[Q]](FieldseekerFieldscoutinglogs.Columns, buildFieldseekerFieldscoutinglogJoins),
FieldseekerHabitatrelates: buildJoinSet[fieldseekerHabitatrelateJoins[Q]](FieldseekerHabitatrelates.Columns, buildFieldseekerHabitatrelateJoins),

View file

@ -19,6 +19,10 @@ var Preload = getPreloaders()
type preloaders struct {
ArcgisUser arcgisuserPreloader
ArcgisUserPrivilege arcgisUserPrivilegePreloader
CommsEmail commsEmailPreloader
CommsEmailLog commsEmailLogPreloader
CommsPhone commsPhonePreloader
CommsSMSLog commsSMSLogPreloader
FieldseekerContainerrelate fieldseekerContainerrelatePreloader
FieldseekerFieldscoutinglog fieldseekerFieldscoutinglogPreloader
FieldseekerHabitatrelate fieldseekerHabitatrelatePreloader
@ -71,6 +75,10 @@ func getPreloaders() preloaders {
return preloaders{
ArcgisUser: buildArcgisUserPreloader(),
ArcgisUserPrivilege: buildArcgisUserPrivilegePreloader(),
CommsEmail: buildCommsEmailPreloader(),
CommsEmailLog: buildCommsEmailLogPreloader(),
CommsPhone: buildCommsPhonePreloader(),
CommsSMSLog: buildCommsSMSLogPreloader(),
FieldseekerContainerrelate: buildFieldseekerContainerrelatePreloader(),
FieldseekerFieldscoutinglog: buildFieldseekerFieldscoutinglogPreloader(),
FieldseekerHabitatrelate: buildFieldseekerHabitatrelatePreloader(),
@ -129,6 +137,10 @@ var (
type thenLoaders[Q orm.Loadable] struct {
ArcgisUser arcgisuserThenLoader[Q]
ArcgisUserPrivilege arcgisUserPrivilegeThenLoader[Q]
CommsEmail commsEmailThenLoader[Q]
CommsEmailLog commsEmailLogThenLoader[Q]
CommsPhone commsPhoneThenLoader[Q]
CommsSMSLog commsSMSLogThenLoader[Q]
FieldseekerContainerrelate fieldseekerContainerrelateThenLoader[Q]
FieldseekerFieldscoutinglog fieldseekerFieldscoutinglogThenLoader[Q]
FieldseekerHabitatrelate fieldseekerHabitatrelateThenLoader[Q]
@ -181,6 +193,10 @@ func getThenLoaders[Q orm.Loadable]() thenLoaders[Q] {
return thenLoaders[Q]{
ArcgisUser: buildArcgisUserThenLoader[Q](),
ArcgisUserPrivilege: buildArcgisUserPrivilegeThenLoader[Q](),
CommsEmail: buildCommsEmailThenLoader[Q](),
CommsEmailLog: buildCommsEmailLogThenLoader[Q](),
CommsPhone: buildCommsPhoneThenLoader[Q](),
CommsSMSLog: buildCommsSMSLogThenLoader[Q](),
FieldseekerContainerrelate: buildFieldseekerContainerrelateThenLoader[Q](),
FieldseekerFieldscoutinglog: buildFieldseekerFieldscoutinglogThenLoader[Q](),
FieldseekerHabitatrelate: buildFieldseekerHabitatrelateThenLoader[Q](),

View file

@ -19,6 +19,10 @@ var (
func Where[Q psql.Filterable]() struct {
ArcgisUsers arcgisuserWhere[Q]
ArcgisUserPrivileges arcgisUserPrivilegeWhere[Q]
CommsEmails commsEmailWhere[Q]
CommsEmailLogs commsEmailLogWhere[Q]
CommsPhones commsPhoneWhere[Q]
CommsSMSLogs commsSMSLogWhere[Q]
FieldseekerContainerrelates fieldseekerContainerrelateWhere[Q]
FieldseekerFieldscoutinglogs fieldseekerFieldscoutinglogWhere[Q]
FieldseekerHabitatrelates fieldseekerHabitatrelateWhere[Q]
@ -78,6 +82,10 @@ func Where[Q psql.Filterable]() struct {
return struct {
ArcgisUsers arcgisuserWhere[Q]
ArcgisUserPrivileges arcgisUserPrivilegeWhere[Q]
CommsEmails commsEmailWhere[Q]
CommsEmailLogs commsEmailLogWhere[Q]
CommsPhones commsPhoneWhere[Q]
CommsSMSLogs commsSMSLogWhere[Q]
FieldseekerContainerrelates fieldseekerContainerrelateWhere[Q]
FieldseekerFieldscoutinglogs fieldseekerFieldscoutinglogWhere[Q]
FieldseekerHabitatrelates fieldseekerHabitatrelateWhere[Q]
@ -136,6 +144,10 @@ func Where[Q psql.Filterable]() struct {
}{
ArcgisUsers: buildArcgisUserWhere[Q](ArcgisUsers.Columns),
ArcgisUserPrivileges: buildArcgisUserPrivilegeWhere[Q](ArcgisUserPrivileges.Columns),
CommsEmails: buildCommsEmailWhere[Q](CommsEmails.Columns),
CommsEmailLogs: buildCommsEmailLogWhere[Q](CommsEmailLogs.Columns),
CommsPhones: buildCommsPhoneWhere[Q](CommsPhones.Columns),
CommsSMSLogs: buildCommsSMSLogWhere[Q](CommsSMSLogs.Columns),
FieldseekerContainerrelates: buildFieldseekerContainerrelateWhere[Q](FieldseekerContainerrelates.Columns),
FieldseekerFieldscoutinglogs: buildFieldseekerFieldscoutinglogWhere[Q](FieldseekerFieldscoutinglogs.Columns),
FieldseekerHabitatrelates: buildFieldseekerHabitatrelateWhere[Q](FieldseekerHabitatrelates.Columns),

View file

@ -0,0 +1,737 @@
// 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"
)
// CommsEmail is an object representing the database table.
type CommsEmail struct {
Address string `db:"address,pk" `
Confirmed bool `db:"confirmed" `
IsSubscribed bool `db:"is_subscribed" `
R commsEmailR `db:"-" `
C commsEmailC `db:"-" `
}
// CommsEmailSlice is an alias for a slice of pointers to CommsEmail.
// This should almost always be used instead of []*CommsEmail.
type CommsEmailSlice []*CommsEmail
// CommsEmails contains methods to work with the email table
var CommsEmails = psql.NewTablex[*CommsEmail, CommsEmailSlice, *CommsEmailSetter]("comms", "email", buildCommsEmailColumns("comms.email"))
// CommsEmailsQuery is a query on the email table
type CommsEmailsQuery = *psql.ViewQuery[*CommsEmail, CommsEmailSlice]
// commsEmailR is where relationships are stored.
type commsEmailR struct {
DestinationEmailLogs CommsEmailLogSlice // comms.email_log.email_log_destination_fkey
}
func buildCommsEmailColumns(alias string) commsEmailColumns {
return commsEmailColumns{
ColumnsExpr: expr.NewColumnsExpr(
"address", "confirmed", "is_subscribed",
).WithParent("comms.email"),
tableAlias: alias,
Address: psql.Quote(alias, "address"),
Confirmed: psql.Quote(alias, "confirmed"),
IsSubscribed: psql.Quote(alias, "is_subscribed"),
}
}
type commsEmailColumns struct {
expr.ColumnsExpr
tableAlias string
Address psql.Expression
Confirmed psql.Expression
IsSubscribed psql.Expression
}
func (c commsEmailColumns) Alias() string {
return c.tableAlias
}
func (commsEmailColumns) AliasedAs(alias string) commsEmailColumns {
return buildCommsEmailColumns(alias)
}
// CommsEmailSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type CommsEmailSetter struct {
Address omit.Val[string] `db:"address,pk" `
Confirmed omit.Val[bool] `db:"confirmed" `
IsSubscribed omit.Val[bool] `db:"is_subscribed" `
}
func (s CommsEmailSetter) SetColumns() []string {
vals := make([]string, 0, 3)
if s.Address.IsValue() {
vals = append(vals, "address")
}
if s.Confirmed.IsValue() {
vals = append(vals, "confirmed")
}
if s.IsSubscribed.IsValue() {
vals = append(vals, "is_subscribed")
}
return vals
}
func (s CommsEmailSetter) Overwrite(t *CommsEmail) {
if s.Address.IsValue() {
t.Address = s.Address.MustGet()
}
if s.Confirmed.IsValue() {
t.Confirmed = s.Confirmed.MustGet()
}
if s.IsSubscribed.IsValue() {
t.IsSubscribed = s.IsSubscribed.MustGet()
}
}
func (s *CommsEmailSetter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return CommsEmails.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, 3)
if s.Address.IsValue() {
vals[0] = psql.Arg(s.Address.MustGet())
} else {
vals[0] = psql.Raw("DEFAULT")
}
if s.Confirmed.IsValue() {
vals[1] = psql.Arg(s.Confirmed.MustGet())
} else {
vals[1] = psql.Raw("DEFAULT")
}
if s.IsSubscribed.IsValue() {
vals[2] = psql.Arg(s.IsSubscribed.MustGet())
} else {
vals[2] = psql.Raw("DEFAULT")
}
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
func (s CommsEmailSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
return um.Set(s.Expressions()...)
}
func (s CommsEmailSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 3)
if s.Address.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "address")...),
psql.Arg(s.Address),
}})
}
if s.Confirmed.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "confirmed")...),
psql.Arg(s.Confirmed),
}})
}
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
}
// FindCommsEmail retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindCommsEmail(ctx context.Context, exec bob.Executor, AddressPK string, cols ...string) (*CommsEmail, error) {
if len(cols) == 0 {
return CommsEmails.Query(
sm.Where(CommsEmails.Columns.Address.EQ(psql.Arg(AddressPK))),
).One(ctx, exec)
}
return CommsEmails.Query(
sm.Where(CommsEmails.Columns.Address.EQ(psql.Arg(AddressPK))),
sm.Columns(CommsEmails.Columns.Only(cols...)),
).One(ctx, exec)
}
// CommsEmailExists checks the presence of a single record by primary key
func CommsEmailExists(ctx context.Context, exec bob.Executor, AddressPK string) (bool, error) {
return CommsEmails.Query(
sm.Where(CommsEmails.Columns.Address.EQ(psql.Arg(AddressPK))),
).Exists(ctx, exec)
}
// AfterQueryHook is called after CommsEmail is retrieved from the database
func (o *CommsEmail) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = CommsEmails.AfterSelectHooks.RunHooks(ctx, exec, CommsEmailSlice{o})
case bob.QueryTypeInsert:
ctx, err = CommsEmails.AfterInsertHooks.RunHooks(ctx, exec, CommsEmailSlice{o})
case bob.QueryTypeUpdate:
ctx, err = CommsEmails.AfterUpdateHooks.RunHooks(ctx, exec, CommsEmailSlice{o})
case bob.QueryTypeDelete:
ctx, err = CommsEmails.AfterDeleteHooks.RunHooks(ctx, exec, CommsEmailSlice{o})
}
return err
}
// primaryKeyVals returns the primary key values of the CommsEmail
func (o *CommsEmail) primaryKeyVals() bob.Expression {
return psql.Arg(o.Address)
}
func (o *CommsEmail) pkEQ() dialect.Expression {
return psql.Quote("comms.email", "address").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 CommsEmail
func (o *CommsEmail) Update(ctx context.Context, exec bob.Executor, s *CommsEmailSetter) error {
v, err := CommsEmails.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 CommsEmail record with an executor
func (o *CommsEmail) Delete(ctx context.Context, exec bob.Executor) error {
_, err := CommsEmails.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
return err
}
// Reload refreshes the CommsEmail using the executor
func (o *CommsEmail) Reload(ctx context.Context, exec bob.Executor) error {
o2, err := CommsEmails.Query(
sm.Where(CommsEmails.Columns.Address.EQ(psql.Arg(o.Address))),
).One(ctx, exec)
if err != nil {
return err
}
o2.R = o.R
*o = *o2
return nil
}
// AfterQueryHook is called after CommsEmailSlice is retrieved from the database
func (o CommsEmailSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = CommsEmails.AfterSelectHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeInsert:
ctx, err = CommsEmails.AfterInsertHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeUpdate:
ctx, err = CommsEmails.AfterUpdateHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeDelete:
ctx, err = CommsEmails.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}
func (o CommsEmailSlice) pkIN() dialect.Expression {
if len(o) == 0 {
return psql.Raw("NULL")
}
return psql.Quote("comms.email", "address").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 CommsEmailSlice) copyMatchingRows(from ...*CommsEmail) {
for i, old := range o {
for _, new := range from {
if new.Address != old.Address {
continue
}
new.R = old.R
o[i] = new
break
}
}
}
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o CommsEmailSlice) 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 CommsEmails.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 *CommsEmail:
o.copyMatchingRows(retrieved)
case []*CommsEmail:
o.copyMatchingRows(retrieved...)
case CommsEmailSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a CommsEmail or a slice of CommsEmail
// then run the AfterUpdateHooks on the slice
_, err = CommsEmails.AfterUpdateHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o CommsEmailSlice) 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 CommsEmails.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 *CommsEmail:
o.copyMatchingRows(retrieved)
case []*CommsEmail:
o.copyMatchingRows(retrieved...)
case CommsEmailSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a CommsEmail or a slice of CommsEmail
// then run the AfterDeleteHooks on the slice
_, err = CommsEmails.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
func (o CommsEmailSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals CommsEmailSetter) error {
if len(o) == 0 {
return nil
}
_, err := CommsEmails.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
return err
}
func (o CommsEmailSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
_, err := CommsEmails.Delete(o.DeleteMod()).Exec(ctx, exec)
return err
}
func (o CommsEmailSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
o2, err := CommsEmails.Query(sm.Where(o.pkIN())).All(ctx, exec)
if err != nil {
return err
}
o.copyMatchingRows(o2...)
return nil
}
// DestinationEmailLogs starts a query for related objects on comms.email_log
func (o *CommsEmail) DestinationEmailLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailLogsQuery {
return CommsEmailLogs.Query(append(mods,
sm.Where(CommsEmailLogs.Columns.Destination.EQ(psql.Arg(o.Address))),
)...)
}
func (os CommsEmailSlice) DestinationEmailLogs(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailLogsQuery {
pkAddress := make(pgtypes.Array[string], 0, len(os))
for _, o := range os {
if o == nil {
continue
}
pkAddress = append(pkAddress, o.Address)
}
PKArgExpr := psql.Select(sm.Columns(
psql.F("unnest", psql.Cast(psql.Arg(pkAddress), "text[]")),
))
return CommsEmailLogs.Query(append(mods,
sm.Where(psql.Group(CommsEmailLogs.Columns.Destination).OP("IN", PKArgExpr)),
)...)
}
func insertCommsEmailDestinationEmailLogs0(ctx context.Context, exec bob.Executor, commsEmailLogs1 []*CommsEmailLogSetter, commsEmail0 *CommsEmail) (CommsEmailLogSlice, error) {
for i := range commsEmailLogs1 {
commsEmailLogs1[i].Destination = omit.From(commsEmail0.Address)
}
ret, err := CommsEmailLogs.Insert(bob.ToMods(commsEmailLogs1...)).All(ctx, exec)
if err != nil {
return ret, fmt.Errorf("insertCommsEmailDestinationEmailLogs0: %w", err)
}
return ret, nil
}
func attachCommsEmailDestinationEmailLogs0(ctx context.Context, exec bob.Executor, count int, commsEmailLogs1 CommsEmailLogSlice, commsEmail0 *CommsEmail) (CommsEmailLogSlice, error) {
setter := &CommsEmailLogSetter{
Destination: omit.From(commsEmail0.Address),
}
err := commsEmailLogs1.UpdateAll(ctx, exec, *setter)
if err != nil {
return nil, fmt.Errorf("attachCommsEmailDestinationEmailLogs0: %w", err)
}
return commsEmailLogs1, nil
}
func (commsEmail0 *CommsEmail) InsertDestinationEmailLogs(ctx context.Context, exec bob.Executor, related ...*CommsEmailLogSetter) error {
if len(related) == 0 {
return nil
}
var err error
commsEmailLogs1, err := insertCommsEmailDestinationEmailLogs0(ctx, exec, related, commsEmail0)
if err != nil {
return err
}
commsEmail0.R.DestinationEmailLogs = append(commsEmail0.R.DestinationEmailLogs, commsEmailLogs1...)
for _, rel := range commsEmailLogs1 {
rel.R.DestinationEmail = commsEmail0
}
return nil
}
func (commsEmail0 *CommsEmail) AttachDestinationEmailLogs(ctx context.Context, exec bob.Executor, related ...*CommsEmailLog) error {
if len(related) == 0 {
return nil
}
var err error
commsEmailLogs1 := CommsEmailLogSlice(related)
_, err = attachCommsEmailDestinationEmailLogs0(ctx, exec, len(related), commsEmailLogs1, commsEmail0)
if err != nil {
return err
}
commsEmail0.R.DestinationEmailLogs = append(commsEmail0.R.DestinationEmailLogs, commsEmailLogs1...)
for _, rel := range related {
rel.R.DestinationEmail = commsEmail0
}
return nil
}
type commsEmailWhere[Q psql.Filterable] struct {
Address psql.WhereMod[Q, string]
Confirmed psql.WhereMod[Q, bool]
IsSubscribed psql.WhereMod[Q, bool]
}
func (commsEmailWhere[Q]) AliasedAs(alias string) commsEmailWhere[Q] {
return buildCommsEmailWhere[Q](buildCommsEmailColumns(alias))
}
func buildCommsEmailWhere[Q psql.Filterable](cols commsEmailColumns) commsEmailWhere[Q] {
return commsEmailWhere[Q]{
Address: psql.Where[Q, string](cols.Address),
Confirmed: psql.Where[Q, bool](cols.Confirmed),
IsSubscribed: psql.Where[Q, bool](cols.IsSubscribed),
}
}
func (o *CommsEmail) Preload(name string, retrieved any) error {
if o == nil {
return nil
}
switch name {
case "DestinationEmailLogs":
rels, ok := retrieved.(CommsEmailLogSlice)
if !ok {
return fmt.Errorf("commsEmail cannot load %T as %q", retrieved, name)
}
o.R.DestinationEmailLogs = rels
for _, rel := range rels {
if rel != nil {
rel.R.DestinationEmail = o
}
}
return nil
default:
return fmt.Errorf("commsEmail has no relationship %q", name)
}
}
type commsEmailPreloader struct{}
func buildCommsEmailPreloader() commsEmailPreloader {
return commsEmailPreloader{}
}
type commsEmailThenLoader[Q orm.Loadable] struct {
DestinationEmailLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildCommsEmailThenLoader[Q orm.Loadable]() commsEmailThenLoader[Q] {
type DestinationEmailLogsLoadInterface interface {
LoadDestinationEmailLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return commsEmailThenLoader[Q]{
DestinationEmailLogs: thenLoadBuilder[Q](
"DestinationEmailLogs",
func(ctx context.Context, exec bob.Executor, retrieved DestinationEmailLogsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadDestinationEmailLogs(ctx, exec, mods...)
},
),
}
}
// LoadDestinationEmailLogs loads the commsEmail's DestinationEmailLogs into the .R struct
func (o *CommsEmail) LoadDestinationEmailLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.DestinationEmailLogs = nil
related, err := o.DestinationEmailLogs(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, rel := range related {
rel.R.DestinationEmail = o
}
o.R.DestinationEmailLogs = related
return nil
}
// LoadDestinationEmailLogs loads the commsEmail's DestinationEmailLogs into the .R struct
func (os CommsEmailSlice) LoadDestinationEmailLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
commsEmailLogs, err := os.DestinationEmailLogs(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
o.R.DestinationEmailLogs = nil
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range commsEmailLogs {
if !(o.Address == rel.Destination) {
continue
}
rel.R.DestinationEmail = o
o.R.DestinationEmailLogs = append(o.R.DestinationEmailLogs, rel)
}
}
return nil
}
// commsEmailC is where relationship counts are stored.
type commsEmailC struct {
DestinationEmailLogs *int64
}
// PreloadCount sets a count in the C struct by name
func (o *CommsEmail) PreloadCount(name string, count int64) error {
if o == nil {
return nil
}
switch name {
case "DestinationEmailLogs":
o.C.DestinationEmailLogs = &count
}
return nil
}
type commsEmailCountPreloader struct {
DestinationEmailLogs func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
}
func buildCommsEmailCountPreloader() commsEmailCountPreloader {
return commsEmailCountPreloader{
DestinationEmailLogs: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
return countPreloader[*CommsEmail]("DestinationEmailLogs", func(parent string) bob.Expression {
// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
if parent == "" {
parent = CommsEmails.Alias()
}
subqueryMods := []bob.Mod[*dialect.SelectQuery]{
sm.Columns(psql.Raw("count(*)")),
sm.From(CommsEmailLogs.Name()),
sm.Where(psql.Quote(CommsEmailLogs.Alias(), "destination").EQ(psql.Quote(parent, "address"))),
}
subqueryMods = append(subqueryMods, mods...)
return psql.Group(psql.Select(subqueryMods...).Expression)
})
},
}
}
type commsEmailCountThenLoader[Q orm.Loadable] struct {
DestinationEmailLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildCommsEmailCountThenLoader[Q orm.Loadable]() commsEmailCountThenLoader[Q] {
type DestinationEmailLogsCountInterface interface {
LoadCountDestinationEmailLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return commsEmailCountThenLoader[Q]{
DestinationEmailLogs: countThenLoadBuilder[Q](
"DestinationEmailLogs",
func(ctx context.Context, exec bob.Executor, retrieved DestinationEmailLogsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadCountDestinationEmailLogs(ctx, exec, mods...)
},
),
}
}
// LoadCountDestinationEmailLogs loads the count of DestinationEmailLogs into the C struct
func (o *CommsEmail) LoadCountDestinationEmailLogs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
count, err := o.DestinationEmailLogs(mods...).Count(ctx, exec)
if err != nil {
return err
}
o.C.DestinationEmailLogs = &count
return nil
}
// LoadCountDestinationEmailLogs loads the count of DestinationEmailLogs for a slice
func (os CommsEmailSlice) LoadCountDestinationEmailLogs(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.LoadCountDestinationEmailLogs(ctx, exec, mods...); err != nil {
return err
}
}
return nil
}
type commsEmailJoins[Q dialect.Joinable] struct {
typ string
DestinationEmailLogs modAs[Q, commsEmailLogColumns]
}
func (j commsEmailJoins[Q]) aliasedAs(alias string) commsEmailJoins[Q] {
return buildCommsEmailJoins[Q](buildCommsEmailColumns(alias), j.typ)
}
func buildCommsEmailJoins[Q dialect.Joinable](cols commsEmailColumns, typ string) commsEmailJoins[Q] {
return commsEmailJoins[Q]{
typ: typ,
DestinationEmailLogs: 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.Destination.EQ(cols.Address),
))
}
return mods
},
},
}
}

View file

@ -0,0 +1,848 @@
// 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
},
},
}
}

1220
db/models/comms.phone.bob.go Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,848 @@
// 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"
)
// CommsSMSLog is an object representing the database table.
type CommsSMSLog struct {
Created time.Time `db:"created" `
Destination string `db:"destination,pk" `
Source string `db:"source,pk" `
Type enums.CommsSmsmessagetype `db:"type,pk" `
R commsSMSLogR `db:"-" `
}
// CommsSMSLogSlice is an alias for a slice of pointers to CommsSMSLog.
// This should almost always be used instead of []*CommsSMSLog.
type CommsSMSLogSlice []*CommsSMSLog
// CommsSMSLogs contains methods to work with the sms_log table
var CommsSMSLogs = psql.NewTablex[*CommsSMSLog, CommsSMSLogSlice, *CommsSMSLogSetter]("comms", "sms_log", buildCommsSMSLogColumns("comms.sms_log"))
// CommsSMSLogsQuery is a query on the sms_log table
type CommsSMSLogsQuery = *psql.ViewQuery[*CommsSMSLog, CommsSMSLogSlice]
// commsSMSLogR is where relationships are stored.
type commsSMSLogR struct {
DestinationPhone *CommsPhone // comms.sms_log.sms_log_destination_fkey
SourcePhone *CommsPhone // comms.sms_log.sms_log_source_fkey
}
func buildCommsSMSLogColumns(alias string) commsSMSLogColumns {
return commsSMSLogColumns{
ColumnsExpr: expr.NewColumnsExpr(
"created", "destination", "source", "type",
).WithParent("comms.sms_log"),
tableAlias: alias,
Created: psql.Quote(alias, "created"),
Destination: psql.Quote(alias, "destination"),
Source: psql.Quote(alias, "source"),
Type: psql.Quote(alias, "type"),
}
}
type commsSMSLogColumns struct {
expr.ColumnsExpr
tableAlias string
Created psql.Expression
Destination psql.Expression
Source psql.Expression
Type psql.Expression
}
func (c commsSMSLogColumns) Alias() string {
return c.tableAlias
}
func (commsSMSLogColumns) AliasedAs(alias string) commsSMSLogColumns {
return buildCommsSMSLogColumns(alias)
}
// CommsSMSLogSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type CommsSMSLogSetter 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.CommsSmsmessagetype] `db:"type,pk" `
}
func (s CommsSMSLogSetter) 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 CommsSMSLogSetter) Overwrite(t *CommsSMSLog) {
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 *CommsSMSLogSetter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return CommsSMSLogs.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 CommsSMSLogSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
return um.Set(s.Expressions()...)
}
func (s CommsSMSLogSetter) 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
}
// FindCommsSMSLog retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindCommsSMSLog(ctx context.Context, exec bob.Executor, DestinationPK string, SourcePK string, TypePK enums.CommsSmsmessagetype, cols ...string) (*CommsSMSLog, error) {
if len(cols) == 0 {
return CommsSMSLogs.Query(
sm.Where(CommsSMSLogs.Columns.Destination.EQ(psql.Arg(DestinationPK))),
sm.Where(CommsSMSLogs.Columns.Source.EQ(psql.Arg(SourcePK))),
sm.Where(CommsSMSLogs.Columns.Type.EQ(psql.Arg(TypePK))),
).One(ctx, exec)
}
return CommsSMSLogs.Query(
sm.Where(CommsSMSLogs.Columns.Destination.EQ(psql.Arg(DestinationPK))),
sm.Where(CommsSMSLogs.Columns.Source.EQ(psql.Arg(SourcePK))),
sm.Where(CommsSMSLogs.Columns.Type.EQ(psql.Arg(TypePK))),
sm.Columns(CommsSMSLogs.Columns.Only(cols...)),
).One(ctx, exec)
}
// CommsSMSLogExists checks the presence of a single record by primary key
func CommsSMSLogExists(ctx context.Context, exec bob.Executor, DestinationPK string, SourcePK string, TypePK enums.CommsSmsmessagetype) (bool, error) {
return CommsSMSLogs.Query(
sm.Where(CommsSMSLogs.Columns.Destination.EQ(psql.Arg(DestinationPK))),
sm.Where(CommsSMSLogs.Columns.Source.EQ(psql.Arg(SourcePK))),
sm.Where(CommsSMSLogs.Columns.Type.EQ(psql.Arg(TypePK))),
).Exists(ctx, exec)
}
// AfterQueryHook is called after CommsSMSLog is retrieved from the database
func (o *CommsSMSLog) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = CommsSMSLogs.AfterSelectHooks.RunHooks(ctx, exec, CommsSMSLogSlice{o})
case bob.QueryTypeInsert:
ctx, err = CommsSMSLogs.AfterInsertHooks.RunHooks(ctx, exec, CommsSMSLogSlice{o})
case bob.QueryTypeUpdate:
ctx, err = CommsSMSLogs.AfterUpdateHooks.RunHooks(ctx, exec, CommsSMSLogSlice{o})
case bob.QueryTypeDelete:
ctx, err = CommsSMSLogs.AfterDeleteHooks.RunHooks(ctx, exec, CommsSMSLogSlice{o})
}
return err
}
// primaryKeyVals returns the primary key values of the CommsSMSLog
func (o *CommsSMSLog) primaryKeyVals() bob.Expression {
return psql.ArgGroup(
o.Destination,
o.Source,
o.Type,
)
}
func (o *CommsSMSLog) pkEQ() dialect.Expression {
return psql.Group(psql.Quote("comms.sms_log", "destination"), psql.Quote("comms.sms_log", "source"), psql.Quote("comms.sms_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 CommsSMSLog
func (o *CommsSMSLog) Update(ctx context.Context, exec bob.Executor, s *CommsSMSLogSetter) error {
v, err := CommsSMSLogs.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 CommsSMSLog record with an executor
func (o *CommsSMSLog) Delete(ctx context.Context, exec bob.Executor) error {
_, err := CommsSMSLogs.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
return err
}
// Reload refreshes the CommsSMSLog using the executor
func (o *CommsSMSLog) Reload(ctx context.Context, exec bob.Executor) error {
o2, err := CommsSMSLogs.Query(
sm.Where(CommsSMSLogs.Columns.Destination.EQ(psql.Arg(o.Destination))),
sm.Where(CommsSMSLogs.Columns.Source.EQ(psql.Arg(o.Source))),
sm.Where(CommsSMSLogs.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 CommsSMSLogSlice is retrieved from the database
func (o CommsSMSLogSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = CommsSMSLogs.AfterSelectHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeInsert:
ctx, err = CommsSMSLogs.AfterInsertHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeUpdate:
ctx, err = CommsSMSLogs.AfterUpdateHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeDelete:
ctx, err = CommsSMSLogs.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}
func (o CommsSMSLogSlice) pkIN() dialect.Expression {
if len(o) == 0 {
return psql.Raw("NULL")
}
return psql.Group(psql.Quote("comms.sms_log", "destination"), psql.Quote("comms.sms_log", "source"), psql.Quote("comms.sms_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 CommsSMSLogSlice) copyMatchingRows(from ...*CommsSMSLog) {
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 CommsSMSLogSlice) 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 CommsSMSLogs.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 *CommsSMSLog:
o.copyMatchingRows(retrieved)
case []*CommsSMSLog:
o.copyMatchingRows(retrieved...)
case CommsSMSLogSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a CommsSMSLog or a slice of CommsSMSLog
// then run the AfterUpdateHooks on the slice
_, err = CommsSMSLogs.AfterUpdateHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o CommsSMSLogSlice) 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 CommsSMSLogs.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 *CommsSMSLog:
o.copyMatchingRows(retrieved)
case []*CommsSMSLog:
o.copyMatchingRows(retrieved...)
case CommsSMSLogSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a CommsSMSLog or a slice of CommsSMSLog
// then run the AfterDeleteHooks on the slice
_, err = CommsSMSLogs.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
func (o CommsSMSLogSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals CommsSMSLogSetter) error {
if len(o) == 0 {
return nil
}
_, err := CommsSMSLogs.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
return err
}
func (o CommsSMSLogSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
_, err := CommsSMSLogs.Delete(o.DeleteMod()).Exec(ctx, exec)
return err
}
func (o CommsSMSLogSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
o2, err := CommsSMSLogs.Query(sm.Where(o.pkIN())).All(ctx, exec)
if err != nil {
return err
}
o.copyMatchingRows(o2...)
return nil
}
// DestinationPhone starts a query for related objects on comms.phone
func (o *CommsSMSLog) DestinationPhone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
return CommsPhones.Query(append(mods,
sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(o.Destination))),
)...)
}
func (os CommsSMSLogSlice) DestinationPhone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
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 CommsPhones.Query(append(mods,
sm.Where(psql.Group(CommsPhones.Columns.E164).OP("IN", PKArgExpr)),
)...)
}
// SourcePhone starts a query for related objects on comms.phone
func (o *CommsSMSLog) SourcePhone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery {
return CommsPhones.Query(append(mods,
sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(o.Source))),
)...)
}
func (os CommsSMSLogSlice) 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 attachCommsSMSLogDestinationPhone0(ctx context.Context, exec bob.Executor, count int, commsSMSLog0 *CommsSMSLog, commsPhone1 *CommsPhone) (*CommsSMSLog, error) {
setter := &CommsSMSLogSetter{
Destination: omit.From(commsPhone1.E164),
}
err := commsSMSLog0.Update(ctx, exec, setter)
if err != nil {
return nil, fmt.Errorf("attachCommsSMSLogDestinationPhone0: %w", err)
}
return commsSMSLog0, nil
}
func (commsSMSLog0 *CommsSMSLog) InsertDestinationPhone(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 = attachCommsSMSLogDestinationPhone0(ctx, exec, 1, commsSMSLog0, commsPhone1)
if err != nil {
return err
}
commsSMSLog0.R.DestinationPhone = commsPhone1
commsPhone1.R.DestinationSMSLogs = append(commsPhone1.R.DestinationSMSLogs, commsSMSLog0)
return nil
}
func (commsSMSLog0 *CommsSMSLog) AttachDestinationPhone(ctx context.Context, exec bob.Executor, commsPhone1 *CommsPhone) error {
var err error
_, err = attachCommsSMSLogDestinationPhone0(ctx, exec, 1, commsSMSLog0, commsPhone1)
if err != nil {
return err
}
commsSMSLog0.R.DestinationPhone = commsPhone1
commsPhone1.R.DestinationSMSLogs = append(commsPhone1.R.DestinationSMSLogs, commsSMSLog0)
return nil
}
func attachCommsSMSLogSourcePhone0(ctx context.Context, exec bob.Executor, count int, commsSMSLog0 *CommsSMSLog, commsPhone1 *CommsPhone) (*CommsSMSLog, error) {
setter := &CommsSMSLogSetter{
Source: omit.From(commsPhone1.E164),
}
err := commsSMSLog0.Update(ctx, exec, setter)
if err != nil {
return nil, fmt.Errorf("attachCommsSMSLogSourcePhone0: %w", err)
}
return commsSMSLog0, nil
}
func (commsSMSLog0 *CommsSMSLog) 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 = attachCommsSMSLogSourcePhone0(ctx, exec, 1, commsSMSLog0, commsPhone1)
if err != nil {
return err
}
commsSMSLog0.R.SourcePhone = commsPhone1
commsPhone1.R.SourceSMSLogs = append(commsPhone1.R.SourceSMSLogs, commsSMSLog0)
return nil
}
func (commsSMSLog0 *CommsSMSLog) AttachSourcePhone(ctx context.Context, exec bob.Executor, commsPhone1 *CommsPhone) error {
var err error
_, err = attachCommsSMSLogSourcePhone0(ctx, exec, 1, commsSMSLog0, commsPhone1)
if err != nil {
return err
}
commsSMSLog0.R.SourcePhone = commsPhone1
commsPhone1.R.SourceSMSLogs = append(commsPhone1.R.SourceSMSLogs, commsSMSLog0)
return nil
}
type commsSMSLogWhere[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.CommsSmsmessagetype]
}
func (commsSMSLogWhere[Q]) AliasedAs(alias string) commsSMSLogWhere[Q] {
return buildCommsSMSLogWhere[Q](buildCommsSMSLogColumns(alias))
}
func buildCommsSMSLogWhere[Q psql.Filterable](cols commsSMSLogColumns) commsSMSLogWhere[Q] {
return commsSMSLogWhere[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.CommsSmsmessagetype](cols.Type),
}
}
func (o *CommsSMSLog) Preload(name string, retrieved any) error {
if o == nil {
return nil
}
switch name {
case "DestinationPhone":
rel, ok := retrieved.(*CommsPhone)
if !ok {
return fmt.Errorf("commsSMSLog cannot load %T as %q", retrieved, name)
}
o.R.DestinationPhone = rel
if rel != nil {
rel.R.DestinationSMSLogs = CommsSMSLogSlice{o}
}
return nil
case "SourcePhone":
rel, ok := retrieved.(*CommsPhone)
if !ok {
return fmt.Errorf("commsSMSLog cannot load %T as %q", retrieved, name)
}
o.R.SourcePhone = rel
if rel != nil {
rel.R.SourceSMSLogs = CommsSMSLogSlice{o}
}
return nil
default:
return fmt.Errorf("commsSMSLog has no relationship %q", name)
}
}
type commsSMSLogPreloader struct {
DestinationPhone func(...psql.PreloadOption) psql.Preloader
SourcePhone func(...psql.PreloadOption) psql.Preloader
}
func buildCommsSMSLogPreloader() commsSMSLogPreloader {
return commsSMSLogPreloader{
DestinationPhone: func(opts ...psql.PreloadOption) psql.Preloader {
return psql.Preload[*CommsPhone, CommsPhoneSlice](psql.PreloadRel{
Name: "DestinationPhone",
Sides: []psql.PreloadSide{
{
From: CommsSMSLogs,
To: CommsPhones,
FromColumns: []string{"destination"},
ToColumns: []string{"e164"},
},
},
}, CommsPhones.Columns.Names(), opts...)
},
SourcePhone: func(opts ...psql.PreloadOption) psql.Preloader {
return psql.Preload[*CommsPhone, CommsPhoneSlice](psql.PreloadRel{
Name: "SourcePhone",
Sides: []psql.PreloadSide{
{
From: CommsSMSLogs,
To: CommsPhones,
FromColumns: []string{"source"},
ToColumns: []string{"e164"},
},
},
}, CommsPhones.Columns.Names(), opts...)
},
}
}
type commsSMSLogThenLoader[Q orm.Loadable] struct {
DestinationPhone func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
SourcePhone func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildCommsSMSLogThenLoader[Q orm.Loadable]() commsSMSLogThenLoader[Q] {
type DestinationPhoneLoadInterface interface {
LoadDestinationPhone(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
type SourcePhoneLoadInterface interface {
LoadSourcePhone(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return commsSMSLogThenLoader[Q]{
DestinationPhone: thenLoadBuilder[Q](
"DestinationPhone",
func(ctx context.Context, exec bob.Executor, retrieved DestinationPhoneLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadDestinationPhone(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...)
},
),
}
}
// LoadDestinationPhone loads the commsSMSLog's DestinationPhone into the .R struct
func (o *CommsSMSLog) LoadDestinationPhone(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.DestinationPhone = nil
related, err := o.DestinationPhone(mods...).One(ctx, exec)
if err != nil {
return err
}
related.R.DestinationSMSLogs = CommsSMSLogSlice{o}
o.R.DestinationPhone = related
return nil
}
// LoadDestinationPhone loads the commsSMSLog's DestinationPhone into the .R struct
func (os CommsSMSLogSlice) LoadDestinationPhone(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
commsPhones, err := os.DestinationPhone(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range commsPhones {
if !(o.Destination == rel.E164) {
continue
}
rel.R.DestinationSMSLogs = append(rel.R.DestinationSMSLogs, o)
o.R.DestinationPhone = rel
break
}
}
return nil
}
// LoadSourcePhone loads the commsSMSLog's SourcePhone into the .R struct
func (o *CommsSMSLog) 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.SourceSMSLogs = CommsSMSLogSlice{o}
o.R.SourcePhone = related
return nil
}
// LoadSourcePhone loads the commsSMSLog's SourcePhone into the .R struct
func (os CommsSMSLogSlice) 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.SourceSMSLogs = append(rel.R.SourceSMSLogs, o)
o.R.SourcePhone = rel
break
}
}
return nil
}
type commsSMSLogJoins[Q dialect.Joinable] struct {
typ string
DestinationPhone modAs[Q, commsPhoneColumns]
SourcePhone modAs[Q, commsPhoneColumns]
}
func (j commsSMSLogJoins[Q]) aliasedAs(alias string) commsSMSLogJoins[Q] {
return buildCommsSMSLogJoins[Q](buildCommsSMSLogColumns(alias), j.typ)
}
func buildCommsSMSLogJoins[Q dialect.Joinable](cols commsSMSLogColumns, typ string) commsSMSLogJoins[Q] {
return commsSMSLogJoins[Q]{
typ: typ,
DestinationPhone: 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.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
},
},
}
}

View file

@ -10,7 +10,9 @@ import (
"strconv"
"time"
"github.com/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/google/uuid"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/psql"
@ -27,14 +29,15 @@ import (
// PublicreportImage is an object representing the database table.
type PublicreportImage struct {
ID int32 `db:"id,pk" `
ContentType string `db:"content_type" `
Created time.Time `db:"created" `
ResolutionX int32 `db:"resolution_x" `
ResolutionY int32 `db:"resolution_y" `
StorageUUID uuid.UUID `db:"storage_uuid" `
StorageSize int64 `db:"storage_size" `
UploadedFilename string `db:"uploaded_filename" `
ID int32 `db:"id,pk" `
ContentType string `db:"content_type" `
Created time.Time `db:"created" `
Location null.Val[string] `db:"location" `
ResolutionX int32 `db:"resolution_x" `
ResolutionY int32 `db:"resolution_y" `
StorageUUID uuid.UUID `db:"storage_uuid" `
StorageSize int64 `db:"storage_size" `
UploadedFilename string `db:"uploaded_filename" `
R publicreportImageR `db:"-" `
@ -61,12 +64,13 @@ type publicreportImageR struct {
func buildPublicreportImageColumns(alias string) publicreportImageColumns {
return publicreportImageColumns{
ColumnsExpr: expr.NewColumnsExpr(
"id", "content_type", "created", "resolution_x", "resolution_y", "storage_uuid", "storage_size", "uploaded_filename",
"id", "content_type", "created", "location", "resolution_x", "resolution_y", "storage_uuid", "storage_size", "uploaded_filename",
).WithParent("publicreport.image"),
tableAlias: alias,
ID: psql.Quote(alias, "id"),
ContentType: psql.Quote(alias, "content_type"),
Created: psql.Quote(alias, "created"),
Location: psql.Quote(alias, "location"),
ResolutionX: psql.Quote(alias, "resolution_x"),
ResolutionY: psql.Quote(alias, "resolution_y"),
StorageUUID: psql.Quote(alias, "storage_uuid"),
@ -81,6 +85,7 @@ type publicreportImageColumns struct {
ID psql.Expression
ContentType psql.Expression
Created psql.Expression
Location psql.Expression
ResolutionX psql.Expression
ResolutionY psql.Expression
StorageUUID psql.Expression
@ -100,18 +105,19 @@ func (publicreportImageColumns) AliasedAs(alias string) publicreportImageColumns
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportImageSetter struct {
ID omit.Val[int32] `db:"id,pk" `
ContentType omit.Val[string] `db:"content_type" `
Created omit.Val[time.Time] `db:"created" `
ResolutionX omit.Val[int32] `db:"resolution_x" `
ResolutionY omit.Val[int32] `db:"resolution_y" `
StorageUUID omit.Val[uuid.UUID] `db:"storage_uuid" `
StorageSize omit.Val[int64] `db:"storage_size" `
UploadedFilename omit.Val[string] `db:"uploaded_filename" `
ID omit.Val[int32] `db:"id,pk" `
ContentType omit.Val[string] `db:"content_type" `
Created omit.Val[time.Time] `db:"created" `
Location omitnull.Val[string] `db:"location" `
ResolutionX omit.Val[int32] `db:"resolution_x" `
ResolutionY omit.Val[int32] `db:"resolution_y" `
StorageUUID omit.Val[uuid.UUID] `db:"storage_uuid" `
StorageSize omit.Val[int64] `db:"storage_size" `
UploadedFilename omit.Val[string] `db:"uploaded_filename" `
}
func (s PublicreportImageSetter) SetColumns() []string {
vals := make([]string, 0, 8)
vals := make([]string, 0, 9)
if s.ID.IsValue() {
vals = append(vals, "id")
}
@ -121,6 +127,9 @@ func (s PublicreportImageSetter) SetColumns() []string {
if s.Created.IsValue() {
vals = append(vals, "created")
}
if !s.Location.IsUnset() {
vals = append(vals, "location")
}
if s.ResolutionX.IsValue() {
vals = append(vals, "resolution_x")
}
@ -149,6 +158,9 @@ func (s PublicreportImageSetter) Overwrite(t *PublicreportImage) {
if s.Created.IsValue() {
t.Created = s.Created.MustGet()
}
if !s.Location.IsUnset() {
t.Location = s.Location.MustGetNull()
}
if s.ResolutionX.IsValue() {
t.ResolutionX = s.ResolutionX.MustGet()
}
@ -172,7 +184,7 @@ func (s *PublicreportImageSetter) Apply(q *dialect.InsertQuery) {
})
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
vals := make([]bob.Expression, 8)
vals := make([]bob.Expression, 9)
if s.ID.IsValue() {
vals[0] = psql.Arg(s.ID.MustGet())
} else {
@ -191,36 +203,42 @@ func (s *PublicreportImageSetter) Apply(q *dialect.InsertQuery) {
vals[2] = psql.Raw("DEFAULT")
}
if s.ResolutionX.IsValue() {
vals[3] = psql.Arg(s.ResolutionX.MustGet())
if !s.Location.IsUnset() {
vals[3] = psql.Arg(s.Location.MustGetNull())
} else {
vals[3] = psql.Raw("DEFAULT")
}
if s.ResolutionY.IsValue() {
vals[4] = psql.Arg(s.ResolutionY.MustGet())
if s.ResolutionX.IsValue() {
vals[4] = psql.Arg(s.ResolutionX.MustGet())
} else {
vals[4] = psql.Raw("DEFAULT")
}
if s.StorageUUID.IsValue() {
vals[5] = psql.Arg(s.StorageUUID.MustGet())
if s.ResolutionY.IsValue() {
vals[5] = psql.Arg(s.ResolutionY.MustGet())
} else {
vals[5] = psql.Raw("DEFAULT")
}
if s.StorageSize.IsValue() {
vals[6] = psql.Arg(s.StorageSize.MustGet())
if s.StorageUUID.IsValue() {
vals[6] = psql.Arg(s.StorageUUID.MustGet())
} else {
vals[6] = psql.Raw("DEFAULT")
}
if s.UploadedFilename.IsValue() {
vals[7] = psql.Arg(s.UploadedFilename.MustGet())
if s.StorageSize.IsValue() {
vals[7] = psql.Arg(s.StorageSize.MustGet())
} else {
vals[7] = psql.Raw("DEFAULT")
}
if s.UploadedFilename.IsValue() {
vals[8] = psql.Arg(s.UploadedFilename.MustGet())
} else {
vals[8] = psql.Raw("DEFAULT")
}
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
@ -230,7 +248,7 @@ func (s PublicreportImageSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
}
func (s PublicreportImageSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 8)
exprs := make([]bob.Expression, 0, 9)
if s.ID.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
@ -253,6 +271,13 @@ func (s PublicreportImageSetter) Expressions(prefix ...string) []bob.Expression
}})
}
if !s.Location.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "location")...),
psql.Arg(s.Location),
}})
}
if s.ResolutionX.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "resolution_x")...),
@ -798,6 +823,7 @@ type publicreportImageWhere[Q psql.Filterable] struct {
ID psql.WhereMod[Q, int32]
ContentType psql.WhereMod[Q, string]
Created psql.WhereMod[Q, time.Time]
Location psql.WhereNullMod[Q, string]
ResolutionX psql.WhereMod[Q, int32]
ResolutionY psql.WhereMod[Q, int32]
StorageUUID psql.WhereMod[Q, uuid.UUID]
@ -814,6 +840,7 @@ func buildPublicreportImageWhere[Q psql.Filterable](cols publicreportImageColumn
ID: psql.Where[Q, int32](cols.ID),
ContentType: psql.Where[Q, string](cols.ContentType),
Created: psql.Where[Q, time.Time](cols.Created),
Location: psql.WhereNull[Q, string](cols.Location),
ResolutionX: psql.Where[Q, int32](cols.ResolutionX),
ResolutionY: psql.Where[Q, int32](cols.ResolutionY),
StorageUUID: psql.Where[Q, uuid.UUID](cols.StorageUUID),