diff --git a/db/dberrors/publicreport.subscribe_email.bob.go b/db/dberrors/publicreport.subscribe_email.bob.go new file mode 100644 index 00000000..1cc242e5 --- /dev/null +++ b/db/dberrors/publicreport.subscribe_email.bob.go @@ -0,0 +1,17 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package dberrors + +var PublicreportSubscribeEmailErrors = &publicreportSubscribeEmailErrors{ + ErrUniqueSubscribeEmailPkey: &UniqueConstraintError{ + schema: "publicreport", + table: "subscribe_email", + columns: []string{"id"}, + s: "subscribe_email_pkey", + }, +} + +type publicreportSubscribeEmailErrors struct { + ErrUniqueSubscribeEmailPkey *UniqueConstraintError +} diff --git a/db/dberrors/publicreport.subscribe_phone.bob.go b/db/dberrors/publicreport.subscribe_phone.bob.go new file mode 100644 index 00000000..ef44fc82 --- /dev/null +++ b/db/dberrors/publicreport.subscribe_phone.bob.go @@ -0,0 +1,17 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package dberrors + +var PublicreportSubscribePhoneErrors = &publicreportSubscribePhoneErrors{ + ErrUniqueSubscribePhonePkey: &UniqueConstraintError{ + schema: "publicreport", + table: "subscribe_phone", + columns: []string{"id"}, + s: "subscribe_phone_pkey", + }, +} + +type publicreportSubscribePhoneErrors struct { + ErrUniqueSubscribePhonePkey *UniqueConstraintError +} diff --git a/db/dbinfo/publicreport.subscribe_email.bob.go b/db/dbinfo/publicreport.subscribe_email.bob.go index 1066ee26..c168b568 100644 --- a/db/dbinfo/publicreport.subscribe_email.bob.go +++ b/db/dbinfo/publicreport.subscribe_email.bob.go @@ -3,6 +3,8 @@ package dbinfo +import "github.com/aarondl/opt/null" + var PublicreportSubscribeEmails = Table[ publicreportSubscribeEmailColumns, publicreportSubscribeEmailIndexes, @@ -40,8 +42,40 @@ var PublicreportSubscribeEmails = Table[ Generated: false, AutoIncr: false, }, + ID: column{ + Name: "id", + DBType: "integer", + Default: "nextval('publicreport.subscribe_email_id_seq'::regclass)", + Comment: "", + Nullable: false, + Generated: false, + AutoIncr: false, + }, + }, + Indexes: publicreportSubscribeEmailIndexes{ + SubscribeEmailPkey: index{ + Type: "btree", + Name: "subscribe_email_pkey", + Columns: []indexColumn{ + { + Name: "id", + Desc: null.FromCond(false, true), + IsExpression: false, + }, + }, + Unique: true, + Comment: "", + NullsFirst: []bool{false}, + NullsDistinct: false, + Where: "", + Include: []string{}, + }, + }, + PrimaryKey: &constraint{ + Name: "subscribe_email_pkey", + Columns: []string{"id"}, + Comment: "", }, - ForeignKeys: publicreportSubscribeEmailForeignKeys{ PublicreportSubscribeEmailSubscribeEmailEmailAddressFkey: foreignKey{ constraint: constraint{ @@ -61,18 +95,23 @@ type publicreportSubscribeEmailColumns struct { Created column Deleted column EmailAddress column + ID column } func (c publicreportSubscribeEmailColumns) AsSlice() []column { return []column{ - c.Created, c.Deleted, c.EmailAddress, + c.Created, c.Deleted, c.EmailAddress, c.ID, } } -type publicreportSubscribeEmailIndexes struct{} +type publicreportSubscribeEmailIndexes struct { + SubscribeEmailPkey index +} func (i publicreportSubscribeEmailIndexes) AsSlice() []index { - return []index{} + return []index{ + i.SubscribeEmailPkey, + } } type publicreportSubscribeEmailForeignKeys struct { diff --git a/db/dbinfo/publicreport.subscribe_phone.bob.go b/db/dbinfo/publicreport.subscribe_phone.bob.go index 30248aaa..3f7df786 100644 --- a/db/dbinfo/publicreport.subscribe_phone.bob.go +++ b/db/dbinfo/publicreport.subscribe_phone.bob.go @@ -3,6 +3,8 @@ package dbinfo +import "github.com/aarondl/opt/null" + var PublicreportSubscribePhones = Table[ publicreportSubscribePhoneColumns, publicreportSubscribePhoneIndexes, @@ -31,6 +33,15 @@ var PublicreportSubscribePhones = Table[ Generated: false, AutoIncr: false, }, + ID: column{ + Name: "id", + DBType: "integer", + Default: "nextval('publicreport.subscribe_phone_id_seq'::regclass)", + Comment: "", + Nullable: false, + Generated: false, + AutoIncr: false, + }, PhoneE164: column{ Name: "phone_e164", DBType: "text", @@ -41,7 +52,30 @@ var PublicreportSubscribePhones = Table[ AutoIncr: false, }, }, - + Indexes: publicreportSubscribePhoneIndexes{ + SubscribePhonePkey: index{ + Type: "btree", + Name: "subscribe_phone_pkey", + Columns: []indexColumn{ + { + Name: "id", + Desc: null.FromCond(false, true), + IsExpression: false, + }, + }, + Unique: true, + Comment: "", + NullsFirst: []bool{false}, + NullsDistinct: false, + Where: "", + Include: []string{}, + }, + }, + PrimaryKey: &constraint{ + Name: "subscribe_phone_pkey", + Columns: []string{"id"}, + Comment: "", + }, ForeignKeys: publicreportSubscribePhoneForeignKeys{ PublicreportSubscribePhoneSubscribePhonePhoneE164Fkey: foreignKey{ constraint: constraint{ @@ -60,19 +94,24 @@ var PublicreportSubscribePhones = Table[ type publicreportSubscribePhoneColumns struct { Created column Deleted column + ID column PhoneE164 column } func (c publicreportSubscribePhoneColumns) AsSlice() []column { return []column{ - c.Created, c.Deleted, c.PhoneE164, + c.Created, c.Deleted, c.ID, c.PhoneE164, } } -type publicreportSubscribePhoneIndexes struct{} +type publicreportSubscribePhoneIndexes struct { + SubscribePhonePkey index +} func (i publicreportSubscribePhoneIndexes) AsSlice() []index { - return []index{} + return []index{ + i.SubscribePhonePkey, + } } type publicreportSubscribePhoneForeignKeys struct { diff --git a/db/factory/bobfactory_main.bob.go b/db/factory/bobfactory_main.bob.go index af47988b..b60f1155 100644 --- a/db/factory/bobfactory_main.bob.go +++ b/db/factory/bobfactory_main.bob.go @@ -3628,6 +3628,7 @@ func (f *Factory) FromExistingPublicreportSubscribeEmail(m *models.PublicreportS o.Created = func() time.Time { return m.Created } o.Deleted = func() null.Val[time.Time] { return m.Deleted } o.EmailAddress = func() string { return m.EmailAddress } + o.ID = func() int32 { return m.ID } ctx := context.Background() if m.R.EmailAddressEmailContact != nil { @@ -3658,6 +3659,7 @@ func (f *Factory) FromExistingPublicreportSubscribePhone(m *models.PublicreportS o.Created = func() time.Time { return m.Created } o.Deleted = func() null.Val[time.Time] { return m.Deleted } + o.ID = func() int32 { return m.ID } o.PhoneE164 = func() string { return m.PhoneE164 } ctx := context.Background() diff --git a/db/factory/comms.email_contact.bob.go b/db/factory/comms.email_contact.bob.go index f751477c..2f89d433 100644 --- a/db/factory/comms.email_contact.bob.go +++ b/db/factory/comms.email_contact.bob.go @@ -328,6 +328,26 @@ func (o *CommsEmailContactTemplate) insertOptRels(ctx context.Context, exec bob. } } + isEmailAddressSubscribeEmailsDone, _ := commsEmailContactRelEmailAddressSubscribeEmailsCtx.Value(ctx) + if !isEmailAddressSubscribeEmailsDone && o.r.EmailAddressSubscribeEmails != nil { + ctx = commsEmailContactRelEmailAddressSubscribeEmailsCtx.WithValue(ctx, true) + for _, r := range o.r.EmailAddressSubscribeEmails { + if r.o.alreadyPersisted { + m.R.EmailAddressSubscribeEmails = append(m.R.EmailAddressSubscribeEmails, r.o.Build()) + } else { + rel4, err := r.o.CreateMany(ctx, exec, r.number) + if err != nil { + return err + } + + err = m.AttachEmailAddressSubscribeEmails(ctx, exec, rel4...) + if err != nil { + return err + } + } + } + } + return err } diff --git a/db/factory/comms.phone.bob.go b/db/factory/comms.phone.bob.go index 6d894e25..23c43c7d 100644 --- a/db/factory/comms.phone.bob.go +++ b/db/factory/comms.phone.bob.go @@ -393,6 +393,26 @@ func (o *CommsPhoneTemplate) insertOptRels(ctx context.Context, exec bob.Executo } } + isPhoneE164SubscribePhonesDone, _ := commsPhoneRelPhoneE164SubscribePhonesCtx.Value(ctx) + if !isPhoneE164SubscribePhonesDone && o.r.PhoneE164SubscribePhones != nil { + ctx = commsPhoneRelPhoneE164SubscribePhonesCtx.WithValue(ctx, true) + for _, r := range o.r.PhoneE164SubscribePhones { + if r.o.alreadyPersisted { + m.R.PhoneE164SubscribePhones = append(m.R.PhoneE164SubscribePhones, r.o.Build()) + } else { + rel6, err := r.o.CreateMany(ctx, exec, r.number) + if err != nil { + return err + } + + err = m.AttachPhoneE164SubscribePhones(ctx, exec, rel6...) + if err != nil { + return err + } + } + } + } + return err } diff --git a/db/factory/publicreport.subscribe_email.bob.go b/db/factory/publicreport.subscribe_email.bob.go index 940f7284..23009620 100644 --- a/db/factory/publicreport.subscribe_email.bob.go +++ b/db/factory/publicreport.subscribe_email.bob.go @@ -5,10 +5,14 @@ package factory import ( "context" + "testing" "time" + "github.com/Gleipnir-Technology/bob" models "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/aarondl/opt/null" + "github.com/aarondl/opt/omit" + "github.com/aarondl/opt/omitnull" "github.com/jaswdr/faker/v2" ) @@ -36,6 +40,7 @@ type PublicreportSubscribeEmailTemplate struct { Created func() time.Time Deleted func() null.Val[time.Time] EmailAddress func() string + ID func() int32 r publicreportSubscribeEmailR f *Factory @@ -69,6 +74,43 @@ func (t PublicreportSubscribeEmailTemplate) setModelRels(o *models.PublicreportS } } +// BuildSetter returns an *models.PublicreportSubscribeEmailSetter +// this does nothing with the relationship templates +func (o PublicreportSubscribeEmailTemplate) BuildSetter() *models.PublicreportSubscribeEmailSetter { + m := &models.PublicreportSubscribeEmailSetter{} + + if o.Created != nil { + val := o.Created() + m.Created = omit.From(val) + } + if o.Deleted != nil { + val := o.Deleted() + m.Deleted = omitnull.FromNull(val) + } + if o.EmailAddress != nil { + val := o.EmailAddress() + m.EmailAddress = omit.From(val) + } + if o.ID != nil { + val := o.ID() + m.ID = omit.From(val) + } + + return m +} + +// BuildManySetter returns an []*models.PublicreportSubscribeEmailSetter +// this does nothing with the relationship templates +func (o PublicreportSubscribeEmailTemplate) BuildManySetter(number int) []*models.PublicreportSubscribeEmailSetter { + m := make([]*models.PublicreportSubscribeEmailSetter, number) + + for i := range m { + m[i] = o.BuildSetter() + } + + return m +} + // Build returns an *models.PublicreportSubscribeEmail // Related objects are also created and placed in the .R field // NOTE: Objects are not inserted into the database. Use PublicreportSubscribeEmailTemplate.Create @@ -84,6 +126,9 @@ func (o PublicreportSubscribeEmailTemplate) Build() *models.PublicreportSubscrib if o.EmailAddress != nil { m.EmailAddress = o.EmailAddress() } + if o.ID != nil { + m.ID = o.ID() + } o.setModelRels(m) @@ -103,6 +148,127 @@ func (o PublicreportSubscribeEmailTemplate) BuildMany(number int) models.Publicr return m } +func ensureCreatablePublicreportSubscribeEmail(m *models.PublicreportSubscribeEmailSetter) { + if !(m.Created.IsValue()) { + val := random_time_Time(nil) + m.Created = omit.From(val) + } + if !(m.EmailAddress.IsValue()) { + val := random_string(nil) + m.EmailAddress = omit.From(val) + } +} + +// insertOptRels creates and inserts any optional the relationships on *models.PublicreportSubscribeEmail +// according to the relationships in the template. +// any required relationship should have already exist on the model +func (o *PublicreportSubscribeEmailTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.PublicreportSubscribeEmail) error { + var err error + + return err +} + +// Create builds a publicreportSubscribeEmail and inserts it into the database +// Relations objects are also inserted and placed in the .R field +func (o *PublicreportSubscribeEmailTemplate) Create(ctx context.Context, exec bob.Executor) (*models.PublicreportSubscribeEmail, error) { + var err error + opt := o.BuildSetter() + ensureCreatablePublicreportSubscribeEmail(opt) + + if o.r.EmailAddressEmailContact == nil { + PublicreportSubscribeEmailMods.WithNewEmailAddressEmailContact().Apply(ctx, o) + } + + var rel0 *models.CommsEmailContact + + if o.r.EmailAddressEmailContact.o.alreadyPersisted { + rel0 = o.r.EmailAddressEmailContact.o.Build() + } else { + rel0, err = o.r.EmailAddressEmailContact.o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + opt.EmailAddress = omit.From(rel0.Address) + + m, err := models.PublicreportSubscribeEmails.Insert(opt).One(ctx, exec) + if err != nil { + return nil, err + } + + m.R.EmailAddressEmailContact = rel0 + + if err := o.insertOptRels(ctx, exec, m); err != nil { + return nil, err + } + return m, err +} + +// MustCreate builds a publicreportSubscribeEmail and inserts it into the database +// Relations objects are also inserted and placed in the .R field +// panics if an error occurs +func (o *PublicreportSubscribeEmailTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.PublicreportSubscribeEmail { + m, err := o.Create(ctx, exec) + if err != nil { + panic(err) + } + return m +} + +// CreateOrFail builds a publicreportSubscribeEmail and inserts it into the database +// Relations objects are also inserted and placed in the .R field +// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs +func (o *PublicreportSubscribeEmailTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.PublicreportSubscribeEmail { + tb.Helper() + m, err := o.Create(ctx, exec) + if err != nil { + tb.Fatal(err) + return nil + } + return m +} + +// CreateMany builds multiple publicreportSubscribeEmails and inserts them into the database +// Relations objects are also inserted and placed in the .R field +func (o PublicreportSubscribeEmailTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.PublicreportSubscribeEmailSlice, error) { + var err error + m := make(models.PublicreportSubscribeEmailSlice, number) + + for i := range m { + m[i], err = o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + return m, nil +} + +// MustCreateMany builds multiple publicreportSubscribeEmails and inserts them into the database +// Relations objects are also inserted and placed in the .R field +// panics if an error occurs +func (o PublicreportSubscribeEmailTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.PublicreportSubscribeEmailSlice { + m, err := o.CreateMany(ctx, exec, number) + if err != nil { + panic(err) + } + return m +} + +// CreateManyOrFail builds multiple publicreportSubscribeEmails and inserts them into the database +// Relations objects are also inserted and placed in the .R field +// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs +func (o PublicreportSubscribeEmailTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.PublicreportSubscribeEmailSlice { + tb.Helper() + m, err := o.CreateMany(ctx, exec, number) + if err != nil { + tb.Fatal(err) + return nil + } + return m +} + // PublicreportSubscribeEmail has methods that act as mods for the PublicreportSubscribeEmailTemplate var PublicreportSubscribeEmailMods publicreportSubscribeEmailMods @@ -113,6 +279,7 @@ func (m publicreportSubscribeEmailMods) RandomizeAllColumns(f *faker.Faker) Publ PublicreportSubscribeEmailMods.RandomCreated(f), PublicreportSubscribeEmailMods.RandomDeleted(f), PublicreportSubscribeEmailMods.RandomEmailAddress(f), + PublicreportSubscribeEmailMods.RandomID(f), } } @@ -231,6 +398,37 @@ func (m publicreportSubscribeEmailMods) RandomEmailAddress(f *faker.Faker) Publi }) } +// Set the model columns to this value +func (m publicreportSubscribeEmailMods) ID(val int32) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.ID = func() int32 { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribeEmailMods) IDFunc(f func() int32) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.ID = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribeEmailMods) UnsetID() PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.ID = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +func (m publicreportSubscribeEmailMods) RandomID(f *faker.Faker) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.ID = func() int32 { + return random_int32(f) + } + }) +} + func (m publicreportSubscribeEmailMods) WithParentsCascading() PublicreportSubscribeEmailMod { return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { if isDone, _ := publicreportSubscribeEmailWithParentsCascadingCtx.Value(ctx); isDone { diff --git a/db/factory/publicreport.subscribe_phone.bob.go b/db/factory/publicreport.subscribe_phone.bob.go index 76f6d4a8..4988af0a 100644 --- a/db/factory/publicreport.subscribe_phone.bob.go +++ b/db/factory/publicreport.subscribe_phone.bob.go @@ -5,10 +5,14 @@ package factory import ( "context" + "testing" "time" + "github.com/Gleipnir-Technology/bob" models "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/aarondl/opt/null" + "github.com/aarondl/opt/omit" + "github.com/aarondl/opt/omitnull" "github.com/jaswdr/faker/v2" ) @@ -35,6 +39,7 @@ func (mods PublicreportSubscribePhoneModSlice) Apply(ctx context.Context, n *Pub type PublicreportSubscribePhoneTemplate struct { Created func() time.Time Deleted func() null.Val[time.Time] + ID func() int32 PhoneE164 func() string r publicreportSubscribePhoneR @@ -69,6 +74,43 @@ func (t PublicreportSubscribePhoneTemplate) setModelRels(o *models.PublicreportS } } +// BuildSetter returns an *models.PublicreportSubscribePhoneSetter +// this does nothing with the relationship templates +func (o PublicreportSubscribePhoneTemplate) BuildSetter() *models.PublicreportSubscribePhoneSetter { + m := &models.PublicreportSubscribePhoneSetter{} + + if o.Created != nil { + val := o.Created() + m.Created = omit.From(val) + } + if o.Deleted != nil { + val := o.Deleted() + m.Deleted = omitnull.FromNull(val) + } + if o.ID != nil { + val := o.ID() + m.ID = omit.From(val) + } + if o.PhoneE164 != nil { + val := o.PhoneE164() + m.PhoneE164 = omit.From(val) + } + + return m +} + +// BuildManySetter returns an []*models.PublicreportSubscribePhoneSetter +// this does nothing with the relationship templates +func (o PublicreportSubscribePhoneTemplate) BuildManySetter(number int) []*models.PublicreportSubscribePhoneSetter { + m := make([]*models.PublicreportSubscribePhoneSetter, number) + + for i := range m { + m[i] = o.BuildSetter() + } + + return m +} + // Build returns an *models.PublicreportSubscribePhone // Related objects are also created and placed in the .R field // NOTE: Objects are not inserted into the database. Use PublicreportSubscribePhoneTemplate.Create @@ -81,6 +123,9 @@ func (o PublicreportSubscribePhoneTemplate) Build() *models.PublicreportSubscrib if o.Deleted != nil { m.Deleted = o.Deleted() } + if o.ID != nil { + m.ID = o.ID() + } if o.PhoneE164 != nil { m.PhoneE164 = o.PhoneE164() } @@ -103,6 +148,127 @@ func (o PublicreportSubscribePhoneTemplate) BuildMany(number int) models.Publicr return m } +func ensureCreatablePublicreportSubscribePhone(m *models.PublicreportSubscribePhoneSetter) { + if !(m.Created.IsValue()) { + val := random_time_Time(nil) + m.Created = omit.From(val) + } + if !(m.PhoneE164.IsValue()) { + val := random_string(nil) + m.PhoneE164 = omit.From(val) + } +} + +// insertOptRels creates and inserts any optional the relationships on *models.PublicreportSubscribePhone +// according to the relationships in the template. +// any required relationship should have already exist on the model +func (o *PublicreportSubscribePhoneTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.PublicreportSubscribePhone) error { + var err error + + return err +} + +// Create builds a publicreportSubscribePhone and inserts it into the database +// Relations objects are also inserted and placed in the .R field +func (o *PublicreportSubscribePhoneTemplate) Create(ctx context.Context, exec bob.Executor) (*models.PublicreportSubscribePhone, error) { + var err error + opt := o.BuildSetter() + ensureCreatablePublicreportSubscribePhone(opt) + + if o.r.PhoneE164Phone == nil { + PublicreportSubscribePhoneMods.WithNewPhoneE164Phone().Apply(ctx, o) + } + + var rel0 *models.CommsPhone + + if o.r.PhoneE164Phone.o.alreadyPersisted { + rel0 = o.r.PhoneE164Phone.o.Build() + } else { + rel0, err = o.r.PhoneE164Phone.o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + opt.PhoneE164 = omit.From(rel0.E164) + + m, err := models.PublicreportSubscribePhones.Insert(opt).One(ctx, exec) + if err != nil { + return nil, err + } + + m.R.PhoneE164Phone = rel0 + + if err := o.insertOptRels(ctx, exec, m); err != nil { + return nil, err + } + return m, err +} + +// MustCreate builds a publicreportSubscribePhone and inserts it into the database +// Relations objects are also inserted and placed in the .R field +// panics if an error occurs +func (o *PublicreportSubscribePhoneTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.PublicreportSubscribePhone { + m, err := o.Create(ctx, exec) + if err != nil { + panic(err) + } + return m +} + +// CreateOrFail builds a publicreportSubscribePhone and inserts it into the database +// Relations objects are also inserted and placed in the .R field +// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs +func (o *PublicreportSubscribePhoneTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.PublicreportSubscribePhone { + tb.Helper() + m, err := o.Create(ctx, exec) + if err != nil { + tb.Fatal(err) + return nil + } + return m +} + +// CreateMany builds multiple publicreportSubscribePhones and inserts them into the database +// Relations objects are also inserted and placed in the .R field +func (o PublicreportSubscribePhoneTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.PublicreportSubscribePhoneSlice, error) { + var err error + m := make(models.PublicreportSubscribePhoneSlice, number) + + for i := range m { + m[i], err = o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + return m, nil +} + +// MustCreateMany builds multiple publicreportSubscribePhones and inserts them into the database +// Relations objects are also inserted and placed in the .R field +// panics if an error occurs +func (o PublicreportSubscribePhoneTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.PublicreportSubscribePhoneSlice { + m, err := o.CreateMany(ctx, exec, number) + if err != nil { + panic(err) + } + return m +} + +// CreateManyOrFail builds multiple publicreportSubscribePhones and inserts them into the database +// Relations objects are also inserted and placed in the .R field +// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs +func (o PublicreportSubscribePhoneTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.PublicreportSubscribePhoneSlice { + tb.Helper() + m, err := o.CreateMany(ctx, exec, number) + if err != nil { + tb.Fatal(err) + return nil + } + return m +} + // PublicreportSubscribePhone has methods that act as mods for the PublicreportSubscribePhoneTemplate var PublicreportSubscribePhoneMods publicreportSubscribePhoneMods @@ -112,6 +278,7 @@ func (m publicreportSubscribePhoneMods) RandomizeAllColumns(f *faker.Faker) Publ return PublicreportSubscribePhoneModSlice{ PublicreportSubscribePhoneMods.RandomCreated(f), PublicreportSubscribePhoneMods.RandomDeleted(f), + PublicreportSubscribePhoneMods.RandomID(f), PublicreportSubscribePhoneMods.RandomPhoneE164(f), } } @@ -200,6 +367,37 @@ func (m publicreportSubscribePhoneMods) RandomDeletedNotNull(f *faker.Faker) Pub }) } +// Set the model columns to this value +func (m publicreportSubscribePhoneMods) ID(val int32) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.ID = func() int32 { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribePhoneMods) IDFunc(f func() int32) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.ID = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribePhoneMods) UnsetID() PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.ID = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +func (m publicreportSubscribePhoneMods) RandomID(f *faker.Faker) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.ID = func() int32 { + return random_int32(f) + } + }) +} + // Set the model columns to this value func (m publicreportSubscribePhoneMods) PhoneE164(val string) PublicreportSubscribePhoneMod { return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { diff --git a/db/migrations/00058_district_subscription.sql b/db/migrations/00058_district_subscription.sql index 21f0ede3..6dae8240 100644 --- a/db/migrations/00058_district_subscription.sql +++ b/db/migrations/00058_district_subscription.sql @@ -13,3 +13,6 @@ CREATE TABLE publicreport.subscribe_phone ( phone_e164 TEXT NOT NULL REFERENCES comms.phone(e164), PRIMARY KEY(district_id, phone_e164) ); +-- +goose Down +DROP TABLE publicreport.subscribe_phone; +DROP TABLE publicreport.subscribe_email; diff --git a/db/migrations/00059_subscription_drop_district.sql b/db/migrations/00059_subscription_drop_district.sql index 94c255e2..26e1dfa6 100644 --- a/db/migrations/00059_subscription_drop_district.sql +++ b/db/migrations/00059_subscription_drop_district.sql @@ -1,3 +1,36 @@ -- +goose Up -ALTER TABLE publicreport.subscribe_email DROP COLUMN district_id; -ALTER TABLE publicreport.subscribe_phone DROP COLUMN district_id; +DROP TABLE publicreport.subscribe_email; +DROP TABLE publicreport.subscribe_phone; + +CREATE TABLE publicreport.subscribe_email ( + created TIMESTAMP WITHOUT TIME ZONE NOT NULL, + deleted TIMESTAMP WITHOUT TIME ZONE, + email_address TEXT NOT NULL REFERENCES comms.email_contact(address), + id SERIAL, + PRIMARY KEY(id) +); +CREATE TABLE publicreport.subscribe_phone ( + created TIMESTAMP WITHOUT TIME ZONE NOT NULL, + deleted TIMESTAMP WITHOUT TIME ZONE, + id SERIAL, + phone_e164 TEXT NOT NULL REFERENCES comms.phone(e164), + PRIMARY KEY(id) +); +-- +goose Down +DROP TABLE publicreport.subscribe_email; +DROP TABLE publicreport.subscribe_phone; + +CREATE TABLE publicreport.subscribe_email ( + created TIMESTAMP WITHOUT TIME ZONE NOT NULL, + deleted TIMESTAMP WITHOUT TIME ZONE, + district_id INTEGER REFERENCES organization(id), + email_address TEXT NOT NULL REFERENCES comms.email_contact(address), + PRIMARY KEY(district_id, email_address) +); +CREATE TABLE publicreport.subscribe_phone ( + created TIMESTAMP WITHOUT TIME ZONE NOT NULL, + deleted TIMESTAMP WITHOUT TIME ZONE, + district_id INTEGER REFERENCES organization(id), + phone_e164 TEXT NOT NULL REFERENCES comms.phone(e164), + PRIMARY KEY(district_id, phone_e164) +); diff --git a/db/models/comms.email_contact.bob.go b/db/models/comms.email_contact.bob.go index 56fd4b60..5d534c2a 100644 --- a/db/models/comms.email_contact.bob.go +++ b/db/models/comms.email_contact.bob.go @@ -816,6 +816,74 @@ func (commsEmailContact0 *CommsEmailContact) AttachEmailAddressNotifyEmailPools( return nil } +func insertCommsEmailContactEmailAddressSubscribeEmails0(ctx context.Context, exec bob.Executor, publicreportSubscribeEmails1 []*PublicreportSubscribeEmailSetter, commsEmailContact0 *CommsEmailContact) (PublicreportSubscribeEmailSlice, error) { + for i := range publicreportSubscribeEmails1 { + publicreportSubscribeEmails1[i].EmailAddress = omit.From(commsEmailContact0.Address) + } + + ret, err := PublicreportSubscribeEmails.Insert(bob.ToMods(publicreportSubscribeEmails1...)).All(ctx, exec) + if err != nil { + return ret, fmt.Errorf("insertCommsEmailContactEmailAddressSubscribeEmails0: %w", err) + } + + return ret, nil +} + +func attachCommsEmailContactEmailAddressSubscribeEmails0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribeEmails1 PublicreportSubscribeEmailSlice, commsEmailContact0 *CommsEmailContact) (PublicreportSubscribeEmailSlice, error) { + setter := &PublicreportSubscribeEmailSetter{ + EmailAddress: omit.From(commsEmailContact0.Address), + } + + err := publicreportSubscribeEmails1.UpdateAll(ctx, exec, *setter) + if err != nil { + return nil, fmt.Errorf("attachCommsEmailContactEmailAddressSubscribeEmails0: %w", err) + } + + return publicreportSubscribeEmails1, nil +} + +func (commsEmailContact0 *CommsEmailContact) InsertEmailAddressSubscribeEmails(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribeEmailSetter) error { + if len(related) == 0 { + return nil + } + + var err error + + publicreportSubscribeEmails1, err := insertCommsEmailContactEmailAddressSubscribeEmails0(ctx, exec, related, commsEmailContact0) + if err != nil { + return err + } + + commsEmailContact0.R.EmailAddressSubscribeEmails = append(commsEmailContact0.R.EmailAddressSubscribeEmails, publicreportSubscribeEmails1...) + + for _, rel := range publicreportSubscribeEmails1 { + rel.R.EmailAddressEmailContact = commsEmailContact0 + } + return nil +} + +func (commsEmailContact0 *CommsEmailContact) AttachEmailAddressSubscribeEmails(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribeEmail) error { + if len(related) == 0 { + return nil + } + + var err error + publicreportSubscribeEmails1 := PublicreportSubscribeEmailSlice(related) + + _, err = attachCommsEmailContactEmailAddressSubscribeEmails0(ctx, exec, len(related), publicreportSubscribeEmails1, commsEmailContact0) + if err != nil { + return err + } + + commsEmailContact0.R.EmailAddressSubscribeEmails = append(commsEmailContact0.R.EmailAddressSubscribeEmails, publicreportSubscribeEmails1...) + + for _, rel := range related { + rel.R.EmailAddressEmailContact = commsEmailContact0 + } + + return nil +} + type commsEmailContactWhere[Q psql.Filterable] struct { Address psql.WhereMod[Q, string] Confirmed psql.WhereMod[Q, bool] diff --git a/db/models/comms.phone.bob.go b/db/models/comms.phone.bob.go index 7eabf758..4e4045e4 100644 --- a/db/models/comms.phone.bob.go +++ b/db/models/comms.phone.bob.go @@ -980,6 +980,74 @@ func (commsPhone0 *CommsPhone) AttachPhoneE164NotifyPhonePools(ctx context.Conte return nil } +func insertCommsPhonePhoneE164SubscribePhones0(ctx context.Context, exec bob.Executor, publicreportSubscribePhones1 []*PublicreportSubscribePhoneSetter, commsPhone0 *CommsPhone) (PublicreportSubscribePhoneSlice, error) { + for i := range publicreportSubscribePhones1 { + publicreportSubscribePhones1[i].PhoneE164 = omit.From(commsPhone0.E164) + } + + ret, err := PublicreportSubscribePhones.Insert(bob.ToMods(publicreportSubscribePhones1...)).All(ctx, exec) + if err != nil { + return ret, fmt.Errorf("insertCommsPhonePhoneE164SubscribePhones0: %w", err) + } + + return ret, nil +} + +func attachCommsPhonePhoneE164SubscribePhones0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribePhones1 PublicreportSubscribePhoneSlice, commsPhone0 *CommsPhone) (PublicreportSubscribePhoneSlice, error) { + setter := &PublicreportSubscribePhoneSetter{ + PhoneE164: omit.From(commsPhone0.E164), + } + + err := publicreportSubscribePhones1.UpdateAll(ctx, exec, *setter) + if err != nil { + return nil, fmt.Errorf("attachCommsPhonePhoneE164SubscribePhones0: %w", err) + } + + return publicreportSubscribePhones1, nil +} + +func (commsPhone0 *CommsPhone) InsertPhoneE164SubscribePhones(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribePhoneSetter) error { + if len(related) == 0 { + return nil + } + + var err error + + publicreportSubscribePhones1, err := insertCommsPhonePhoneE164SubscribePhones0(ctx, exec, related, commsPhone0) + if err != nil { + return err + } + + commsPhone0.R.PhoneE164SubscribePhones = append(commsPhone0.R.PhoneE164SubscribePhones, publicreportSubscribePhones1...) + + for _, rel := range publicreportSubscribePhones1 { + rel.R.PhoneE164Phone = commsPhone0 + } + return nil +} + +func (commsPhone0 *CommsPhone) AttachPhoneE164SubscribePhones(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribePhone) error { + if len(related) == 0 { + return nil + } + + var err error + publicreportSubscribePhones1 := PublicreportSubscribePhoneSlice(related) + + _, err = attachCommsPhonePhoneE164SubscribePhones0(ctx, exec, len(related), publicreportSubscribePhones1, commsPhone0) + if err != nil { + return err + } + + commsPhone0.R.PhoneE164SubscribePhones = append(commsPhone0.R.PhoneE164SubscribePhones, publicreportSubscribePhones1...) + + for _, rel := range related { + rel.R.PhoneE164Phone = commsPhone0 + } + + return nil +} + type commsPhoneWhere[Q psql.Filterable] struct { E164 psql.WhereMod[Q, string] IsSubscribed psql.WhereMod[Q, bool] diff --git a/db/models/publicreport.subscribe_email.bob.go b/db/models/publicreport.subscribe_email.bob.go index 8a49fce3..82f5b547 100644 --- a/db/models/publicreport.subscribe_email.bob.go +++ b/db/models/publicreport.subscribe_email.bob.go @@ -12,6 +12,7 @@ import ( "github.com/Gleipnir-Technology/bob" "github.com/Gleipnir-Technology/bob/dialect/psql" "github.com/Gleipnir-Technology/bob/dialect/psql/dialect" + "github.com/Gleipnir-Technology/bob/dialect/psql/dm" "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/bob/dialect/psql/um" "github.com/Gleipnir-Technology/bob/expr" @@ -28,6 +29,7 @@ type PublicreportSubscribeEmail struct { Created time.Time `db:"created" ` Deleted null.Val[time.Time] `db:"deleted" ` EmailAddress string `db:"email_address" ` + ID int32 `db:"id,pk" ` R publicreportSubscribeEmailR `db:"-" ` } @@ -36,10 +38,10 @@ type PublicreportSubscribeEmail struct { // This should almost always be used instead of []*PublicreportSubscribeEmail. type PublicreportSubscribeEmailSlice []*PublicreportSubscribeEmail -// PublicreportSubscribeEmails contains methods to work with the subscribe_email view -var PublicreportSubscribeEmails = psql.NewViewx[*PublicreportSubscribeEmail, PublicreportSubscribeEmailSlice]("publicreport", "subscribe_email", buildPublicreportSubscribeEmailColumns("publicreport.subscribe_email")) +// PublicreportSubscribeEmails contains methods to work with the subscribe_email table +var PublicreportSubscribeEmails = psql.NewTablex[*PublicreportSubscribeEmail, PublicreportSubscribeEmailSlice, *PublicreportSubscribeEmailSetter]("publicreport", "subscribe_email", buildPublicreportSubscribeEmailColumns("publicreport.subscribe_email")) -// PublicreportSubscribeEmailsQuery is a query on the subscribe_email view +// PublicreportSubscribeEmailsQuery is a query on the subscribe_email table type PublicreportSubscribeEmailsQuery = *psql.ViewQuery[*PublicreportSubscribeEmail, PublicreportSubscribeEmailSlice] // publicreportSubscribeEmailR is where relationships are stored. @@ -50,12 +52,13 @@ type publicreportSubscribeEmailR struct { func buildPublicreportSubscribeEmailColumns(alias string) publicreportSubscribeEmailColumns { return publicreportSubscribeEmailColumns{ ColumnsExpr: expr.NewColumnsExpr( - "created", "deleted", "email_address", + "created", "deleted", "email_address", "id", ).WithParent("publicreport.subscribe_email"), tableAlias: alias, Created: psql.Quote(alias, "created"), Deleted: psql.Quote(alias, "deleted"), EmailAddress: psql.Quote(alias, "email_address"), + ID: psql.Quote(alias, "id"), } } @@ -65,6 +68,7 @@ type publicreportSubscribeEmailColumns struct { Created psql.Expression Deleted psql.Expression EmailAddress psql.Expression + ID psql.Expression } func (c publicreportSubscribeEmailColumns) Alias() string { @@ -82,10 +86,11 @@ type PublicreportSubscribeEmailSetter struct { Created omit.Val[time.Time] `db:"created" ` Deleted omitnull.Val[time.Time] `db:"deleted" ` EmailAddress omit.Val[string] `db:"email_address" ` + ID omit.Val[int32] `db:"id,pk" ` } func (s PublicreportSubscribeEmailSetter) SetColumns() []string { - vals := make([]string, 0, 3) + vals := make([]string, 0, 4) if s.Created.IsValue() { vals = append(vals, "created") } @@ -95,6 +100,9 @@ func (s PublicreportSubscribeEmailSetter) SetColumns() []string { if s.EmailAddress.IsValue() { vals = append(vals, "email_address") } + if s.ID.IsValue() { + vals = append(vals, "id") + } return vals } @@ -108,11 +116,18 @@ func (s PublicreportSubscribeEmailSetter) Overwrite(t *PublicreportSubscribeEmai if s.EmailAddress.IsValue() { t.EmailAddress = s.EmailAddress.MustGet() } + if s.ID.IsValue() { + t.ID = s.ID.MustGet() + } } func (s *PublicreportSubscribeEmailSetter) Apply(q *dialect.InsertQuery) { + q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { + return PublicreportSubscribeEmails.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) + vals := make([]bob.Expression, 4) if s.Created.IsValue() { vals[0] = psql.Arg(s.Created.MustGet()) } else { @@ -131,6 +146,12 @@ func (s *PublicreportSubscribeEmailSetter) Apply(q *dialect.InsertQuery) { vals[2] = psql.Raw("DEFAULT") } + if s.ID.IsValue() { + vals[3] = psql.Arg(s.ID.MustGet()) + } else { + vals[3] = psql.Raw("DEFAULT") + } + return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "") })) } @@ -140,7 +161,7 @@ func (s PublicreportSubscribeEmailSetter) UpdateMod() bob.Mod[*dialect.UpdateQue } func (s PublicreportSubscribeEmailSetter) Expressions(prefix ...string) []bob.Expression { - exprs := make([]bob.Expression, 0, 3) + exprs := make([]bob.Expression, 0, 4) if s.Created.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ @@ -163,9 +184,38 @@ func (s PublicreportSubscribeEmailSetter) Expressions(prefix ...string) []bob.Ex }}) } + if s.ID.IsValue() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "id")...), + psql.Arg(s.ID), + }}) + } + return exprs } +// FindPublicreportSubscribeEmail retrieves a single record by primary key +// If cols is empty Find will return all columns. +func FindPublicreportSubscribeEmail(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*PublicreportSubscribeEmail, error) { + if len(cols) == 0 { + return PublicreportSubscribeEmails.Query( + sm.Where(PublicreportSubscribeEmails.Columns.ID.EQ(psql.Arg(IDPK))), + ).One(ctx, exec) + } + + return PublicreportSubscribeEmails.Query( + sm.Where(PublicreportSubscribeEmails.Columns.ID.EQ(psql.Arg(IDPK))), + sm.Columns(PublicreportSubscribeEmails.Columns.Only(cols...)), + ).One(ctx, exec) +} + +// PublicreportSubscribeEmailExists checks the presence of a single record by primary key +func PublicreportSubscribeEmailExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) { + return PublicreportSubscribeEmails.Query( + sm.Where(PublicreportSubscribeEmails.Columns.ID.EQ(psql.Arg(IDPK))), + ).Exists(ctx, exec) +} + // AfterQueryHook is called after PublicreportSubscribeEmail is retrieved from the database func (o *PublicreportSubscribeEmail) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error @@ -173,11 +223,61 @@ func (o *PublicreportSubscribeEmail) AfterQueryHook(ctx context.Context, exec bo switch queryType { case bob.QueryTypeSelect: ctx, err = PublicreportSubscribeEmails.AfterSelectHooks.RunHooks(ctx, exec, PublicreportSubscribeEmailSlice{o}) + case bob.QueryTypeInsert: + ctx, err = PublicreportSubscribeEmails.AfterInsertHooks.RunHooks(ctx, exec, PublicreportSubscribeEmailSlice{o}) + case bob.QueryTypeUpdate: + ctx, err = PublicreportSubscribeEmails.AfterUpdateHooks.RunHooks(ctx, exec, PublicreportSubscribeEmailSlice{o}) + case bob.QueryTypeDelete: + ctx, err = PublicreportSubscribeEmails.AfterDeleteHooks.RunHooks(ctx, exec, PublicreportSubscribeEmailSlice{o}) } return err } +// primaryKeyVals returns the primary key values of the PublicreportSubscribeEmail +func (o *PublicreportSubscribeEmail) primaryKeyVals() bob.Expression { + return psql.Arg(o.ID) +} + +func (o *PublicreportSubscribeEmail) pkEQ() dialect.Expression { + return psql.Quote("publicreport.subscribe_email", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + return o.primaryKeyVals().WriteSQL(ctx, w, d, start) + })) +} + +// Update uses an executor to update the PublicreportSubscribeEmail +func (o *PublicreportSubscribeEmail) Update(ctx context.Context, exec bob.Executor, s *PublicreportSubscribeEmailSetter) error { + v, err := PublicreportSubscribeEmails.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 PublicreportSubscribeEmail record with an executor +func (o *PublicreportSubscribeEmail) Delete(ctx context.Context, exec bob.Executor) error { + _, err := PublicreportSubscribeEmails.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec) + return err +} + +// Reload refreshes the PublicreportSubscribeEmail using the executor +func (o *PublicreportSubscribeEmail) Reload(ctx context.Context, exec bob.Executor) error { + o2, err := PublicreportSubscribeEmails.Query( + sm.Where(PublicreportSubscribeEmails.Columns.ID.EQ(psql.Arg(o.ID))), + ).One(ctx, exec) + if err != nil { + return err + } + o2.R = o.R + *o = *o2 + + return nil +} + // AfterQueryHook is called after PublicreportSubscribeEmailSlice is retrieved from the database func (o PublicreportSubscribeEmailSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error @@ -185,11 +285,138 @@ func (o PublicreportSubscribeEmailSlice) AfterQueryHook(ctx context.Context, exe switch queryType { case bob.QueryTypeSelect: ctx, err = PublicreportSubscribeEmails.AfterSelectHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeInsert: + ctx, err = PublicreportSubscribeEmails.AfterInsertHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeUpdate: + ctx, err = PublicreportSubscribeEmails.AfterUpdateHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeDelete: + ctx, err = PublicreportSubscribeEmails.AfterDeleteHooks.RunHooks(ctx, exec, o) } return err } +func (o PublicreportSubscribeEmailSlice) pkIN() dialect.Expression { + if len(o) == 0 { + return psql.Raw("NULL") + } + + return psql.Quote("publicreport.subscribe_email", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + pkPairs := make([]bob.Expression, len(o)) + for i, row := range o { + pkPairs[i] = row.primaryKeyVals() + } + return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "") + })) +} + +// copyMatchingRows finds models in the given slice that have the same primary key +// then it first copies the existing relationships from the old model to the new model +// and then replaces the old model in the slice with the new model +func (o PublicreportSubscribeEmailSlice) copyMatchingRows(from ...*PublicreportSubscribeEmail) { + for i, old := range o { + for _, new := range from { + if new.ID != old.ID { + continue + } + new.R = old.R + o[i] = new + break + } + } +} + +// UpdateMod modifies an update query with "WHERE primary_key IN (o...)" +func (o PublicreportSubscribeEmailSlice) 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 PublicreportSubscribeEmails.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 *PublicreportSubscribeEmail: + o.copyMatchingRows(retrieved) + case []*PublicreportSubscribeEmail: + o.copyMatchingRows(retrieved...) + case PublicreportSubscribeEmailSlice: + o.copyMatchingRows(retrieved...) + default: + // If the retrieved value is not a PublicreportSubscribeEmail or a slice of PublicreportSubscribeEmail + // then run the AfterUpdateHooks on the slice + _, err = PublicreportSubscribeEmails.AfterUpdateHooks.RunHooks(ctx, exec, o) + } + + return err + })) + + q.AppendWhere(o.pkIN()) + }) +} + +// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)" +func (o PublicreportSubscribeEmailSlice) 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 PublicreportSubscribeEmails.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 *PublicreportSubscribeEmail: + o.copyMatchingRows(retrieved) + case []*PublicreportSubscribeEmail: + o.copyMatchingRows(retrieved...) + case PublicreportSubscribeEmailSlice: + o.copyMatchingRows(retrieved...) + default: + // If the retrieved value is not a PublicreportSubscribeEmail or a slice of PublicreportSubscribeEmail + // then run the AfterDeleteHooks on the slice + _, err = PublicreportSubscribeEmails.AfterDeleteHooks.RunHooks(ctx, exec, o) + } + + return err + })) + + q.AppendWhere(o.pkIN()) + }) +} + +func (o PublicreportSubscribeEmailSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals PublicreportSubscribeEmailSetter) error { + if len(o) == 0 { + return nil + } + + _, err := PublicreportSubscribeEmails.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec) + return err +} + +func (o PublicreportSubscribeEmailSlice) DeleteAll(ctx context.Context, exec bob.Executor) error { + if len(o) == 0 { + return nil + } + + _, err := PublicreportSubscribeEmails.Delete(o.DeleteMod()).Exec(ctx, exec) + return err +} + +func (o PublicreportSubscribeEmailSlice) ReloadAll(ctx context.Context, exec bob.Executor) error { + if len(o) == 0 { + return nil + } + + o2, err := PublicreportSubscribeEmails.Query(sm.Where(o.pkIN())).All(ctx, exec) + if err != nil { + return err + } + + o.copyMatchingRows(o2...) + + return nil +} + // EmailAddressEmailContact starts a query for related objects on comms.email_contact func (o *PublicreportSubscribeEmail) EmailAddressEmailContact(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailContactsQuery { return CommsEmailContacts.Query(append(mods, @@ -214,10 +441,59 @@ func (os PublicreportSubscribeEmailSlice) EmailAddressEmailContact(mods ...bob.M )...) } +func attachPublicreportSubscribeEmailEmailAddressEmailContact0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribeEmail0 *PublicreportSubscribeEmail, commsEmailContact1 *CommsEmailContact) (*PublicreportSubscribeEmail, error) { + setter := &PublicreportSubscribeEmailSetter{ + EmailAddress: omit.From(commsEmailContact1.Address), + } + + err := publicreportSubscribeEmail0.Update(ctx, exec, setter) + if err != nil { + return nil, fmt.Errorf("attachPublicreportSubscribeEmailEmailAddressEmailContact0: %w", err) + } + + return publicreportSubscribeEmail0, nil +} + +func (publicreportSubscribeEmail0 *PublicreportSubscribeEmail) InsertEmailAddressEmailContact(ctx context.Context, exec bob.Executor, related *CommsEmailContactSetter) error { + var err error + + commsEmailContact1, err := CommsEmailContacts.Insert(related).One(ctx, exec) + if err != nil { + return fmt.Errorf("inserting related objects: %w", err) + } + + _, err = attachPublicreportSubscribeEmailEmailAddressEmailContact0(ctx, exec, 1, publicreportSubscribeEmail0, commsEmailContact1) + if err != nil { + return err + } + + publicreportSubscribeEmail0.R.EmailAddressEmailContact = commsEmailContact1 + + commsEmailContact1.R.EmailAddressSubscribeEmails = append(commsEmailContact1.R.EmailAddressSubscribeEmails, publicreportSubscribeEmail0) + + return nil +} + +func (publicreportSubscribeEmail0 *PublicreportSubscribeEmail) AttachEmailAddressEmailContact(ctx context.Context, exec bob.Executor, commsEmailContact1 *CommsEmailContact) error { + var err error + + _, err = attachPublicreportSubscribeEmailEmailAddressEmailContact0(ctx, exec, 1, publicreportSubscribeEmail0, commsEmailContact1) + if err != nil { + return err + } + + publicreportSubscribeEmail0.R.EmailAddressEmailContact = commsEmailContact1 + + commsEmailContact1.R.EmailAddressSubscribeEmails = append(commsEmailContact1.R.EmailAddressSubscribeEmails, publicreportSubscribeEmail0) + + return nil +} + type publicreportSubscribeEmailWhere[Q psql.Filterable] struct { Created psql.WhereMod[Q, time.Time] Deleted psql.WhereNullMod[Q, time.Time] EmailAddress psql.WhereMod[Q, string] + ID psql.WhereMod[Q, int32] } func (publicreportSubscribeEmailWhere[Q]) AliasedAs(alias string) publicreportSubscribeEmailWhere[Q] { @@ -229,6 +505,7 @@ func buildPublicreportSubscribeEmailWhere[Q psql.Filterable](cols publicreportSu Created: psql.Where[Q, time.Time](cols.Created), Deleted: psql.WhereNull[Q, time.Time](cols.Deleted), EmailAddress: psql.Where[Q, string](cols.EmailAddress), + ID: psql.Where[Q, int32](cols.ID), } } diff --git a/db/models/publicreport.subscribe_phone.bob.go b/db/models/publicreport.subscribe_phone.bob.go index 0a6fa8e0..b9d60783 100644 --- a/db/models/publicreport.subscribe_phone.bob.go +++ b/db/models/publicreport.subscribe_phone.bob.go @@ -12,6 +12,7 @@ import ( "github.com/Gleipnir-Technology/bob" "github.com/Gleipnir-Technology/bob/dialect/psql" "github.com/Gleipnir-Technology/bob/dialect/psql/dialect" + "github.com/Gleipnir-Technology/bob/dialect/psql/dm" "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/bob/dialect/psql/um" "github.com/Gleipnir-Technology/bob/expr" @@ -27,6 +28,7 @@ import ( type PublicreportSubscribePhone struct { Created time.Time `db:"created" ` Deleted null.Val[time.Time] `db:"deleted" ` + ID int32 `db:"id,pk" ` PhoneE164 string `db:"phone_e164" ` R publicreportSubscribePhoneR `db:"-" ` @@ -36,10 +38,10 @@ type PublicreportSubscribePhone struct { // This should almost always be used instead of []*PublicreportSubscribePhone. type PublicreportSubscribePhoneSlice []*PublicreportSubscribePhone -// PublicreportSubscribePhones contains methods to work with the subscribe_phone view -var PublicreportSubscribePhones = psql.NewViewx[*PublicreportSubscribePhone, PublicreportSubscribePhoneSlice]("publicreport", "subscribe_phone", buildPublicreportSubscribePhoneColumns("publicreport.subscribe_phone")) +// PublicreportSubscribePhones contains methods to work with the subscribe_phone table +var PublicreportSubscribePhones = psql.NewTablex[*PublicreportSubscribePhone, PublicreportSubscribePhoneSlice, *PublicreportSubscribePhoneSetter]("publicreport", "subscribe_phone", buildPublicreportSubscribePhoneColumns("publicreport.subscribe_phone")) -// PublicreportSubscribePhonesQuery is a query on the subscribe_phone view +// PublicreportSubscribePhonesQuery is a query on the subscribe_phone table type PublicreportSubscribePhonesQuery = *psql.ViewQuery[*PublicreportSubscribePhone, PublicreportSubscribePhoneSlice] // publicreportSubscribePhoneR is where relationships are stored. @@ -50,11 +52,12 @@ type publicreportSubscribePhoneR struct { func buildPublicreportSubscribePhoneColumns(alias string) publicreportSubscribePhoneColumns { return publicreportSubscribePhoneColumns{ ColumnsExpr: expr.NewColumnsExpr( - "created", "deleted", "phone_e164", + "created", "deleted", "id", "phone_e164", ).WithParent("publicreport.subscribe_phone"), tableAlias: alias, Created: psql.Quote(alias, "created"), Deleted: psql.Quote(alias, "deleted"), + ID: psql.Quote(alias, "id"), PhoneE164: psql.Quote(alias, "phone_e164"), } } @@ -64,6 +67,7 @@ type publicreportSubscribePhoneColumns struct { tableAlias string Created psql.Expression Deleted psql.Expression + ID psql.Expression PhoneE164 psql.Expression } @@ -81,17 +85,21 @@ func (publicreportSubscribePhoneColumns) AliasedAs(alias string) publicreportSub type PublicreportSubscribePhoneSetter struct { Created omit.Val[time.Time] `db:"created" ` Deleted omitnull.Val[time.Time] `db:"deleted" ` + ID omit.Val[int32] `db:"id,pk" ` PhoneE164 omit.Val[string] `db:"phone_e164" ` } func (s PublicreportSubscribePhoneSetter) SetColumns() []string { - vals := make([]string, 0, 3) + vals := make([]string, 0, 4) if s.Created.IsValue() { vals = append(vals, "created") } if !s.Deleted.IsUnset() { vals = append(vals, "deleted") } + if s.ID.IsValue() { + vals = append(vals, "id") + } if s.PhoneE164.IsValue() { vals = append(vals, "phone_e164") } @@ -105,14 +113,21 @@ func (s PublicreportSubscribePhoneSetter) Overwrite(t *PublicreportSubscribePhon if !s.Deleted.IsUnset() { t.Deleted = s.Deleted.MustGetNull() } + if s.ID.IsValue() { + t.ID = s.ID.MustGet() + } if s.PhoneE164.IsValue() { t.PhoneE164 = s.PhoneE164.MustGet() } } func (s *PublicreportSubscribePhoneSetter) Apply(q *dialect.InsertQuery) { + q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { + return PublicreportSubscribePhones.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) + vals := make([]bob.Expression, 4) if s.Created.IsValue() { vals[0] = psql.Arg(s.Created.MustGet()) } else { @@ -125,12 +140,18 @@ func (s *PublicreportSubscribePhoneSetter) Apply(q *dialect.InsertQuery) { vals[1] = psql.Raw("DEFAULT") } - if s.PhoneE164.IsValue() { - vals[2] = psql.Arg(s.PhoneE164.MustGet()) + if s.ID.IsValue() { + vals[2] = psql.Arg(s.ID.MustGet()) } else { vals[2] = psql.Raw("DEFAULT") } + if s.PhoneE164.IsValue() { + vals[3] = psql.Arg(s.PhoneE164.MustGet()) + } else { + vals[3] = psql.Raw("DEFAULT") + } + return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "") })) } @@ -140,7 +161,7 @@ func (s PublicreportSubscribePhoneSetter) UpdateMod() bob.Mod[*dialect.UpdateQue } func (s PublicreportSubscribePhoneSetter) Expressions(prefix ...string) []bob.Expression { - exprs := make([]bob.Expression, 0, 3) + exprs := make([]bob.Expression, 0, 4) if s.Created.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ @@ -156,6 +177,13 @@ func (s PublicreportSubscribePhoneSetter) Expressions(prefix ...string) []bob.Ex }}) } + if s.ID.IsValue() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "id")...), + psql.Arg(s.ID), + }}) + } + if s.PhoneE164.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ psql.Quote(append(prefix, "phone_e164")...), @@ -166,6 +194,28 @@ func (s PublicreportSubscribePhoneSetter) Expressions(prefix ...string) []bob.Ex return exprs } +// FindPublicreportSubscribePhone retrieves a single record by primary key +// If cols is empty Find will return all columns. +func FindPublicreportSubscribePhone(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*PublicreportSubscribePhone, error) { + if len(cols) == 0 { + return PublicreportSubscribePhones.Query( + sm.Where(PublicreportSubscribePhones.Columns.ID.EQ(psql.Arg(IDPK))), + ).One(ctx, exec) + } + + return PublicreportSubscribePhones.Query( + sm.Where(PublicreportSubscribePhones.Columns.ID.EQ(psql.Arg(IDPK))), + sm.Columns(PublicreportSubscribePhones.Columns.Only(cols...)), + ).One(ctx, exec) +} + +// PublicreportSubscribePhoneExists checks the presence of a single record by primary key +func PublicreportSubscribePhoneExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) { + return PublicreportSubscribePhones.Query( + sm.Where(PublicreportSubscribePhones.Columns.ID.EQ(psql.Arg(IDPK))), + ).Exists(ctx, exec) +} + // AfterQueryHook is called after PublicreportSubscribePhone is retrieved from the database func (o *PublicreportSubscribePhone) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error @@ -173,11 +223,61 @@ func (o *PublicreportSubscribePhone) AfterQueryHook(ctx context.Context, exec bo switch queryType { case bob.QueryTypeSelect: ctx, err = PublicreportSubscribePhones.AfterSelectHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) + case bob.QueryTypeInsert: + ctx, err = PublicreportSubscribePhones.AfterInsertHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) + case bob.QueryTypeUpdate: + ctx, err = PublicreportSubscribePhones.AfterUpdateHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) + case bob.QueryTypeDelete: + ctx, err = PublicreportSubscribePhones.AfterDeleteHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) } return err } +// primaryKeyVals returns the primary key values of the PublicreportSubscribePhone +func (o *PublicreportSubscribePhone) primaryKeyVals() bob.Expression { + return psql.Arg(o.ID) +} + +func (o *PublicreportSubscribePhone) pkEQ() dialect.Expression { + return psql.Quote("publicreport.subscribe_phone", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + return o.primaryKeyVals().WriteSQL(ctx, w, d, start) + })) +} + +// Update uses an executor to update the PublicreportSubscribePhone +func (o *PublicreportSubscribePhone) Update(ctx context.Context, exec bob.Executor, s *PublicreportSubscribePhoneSetter) error { + v, err := PublicreportSubscribePhones.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 PublicreportSubscribePhone record with an executor +func (o *PublicreportSubscribePhone) Delete(ctx context.Context, exec bob.Executor) error { + _, err := PublicreportSubscribePhones.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec) + return err +} + +// Reload refreshes the PublicreportSubscribePhone using the executor +func (o *PublicreportSubscribePhone) Reload(ctx context.Context, exec bob.Executor) error { + o2, err := PublicreportSubscribePhones.Query( + sm.Where(PublicreportSubscribePhones.Columns.ID.EQ(psql.Arg(o.ID))), + ).One(ctx, exec) + if err != nil { + return err + } + o2.R = o.R + *o = *o2 + + return nil +} + // AfterQueryHook is called after PublicreportSubscribePhoneSlice is retrieved from the database func (o PublicreportSubscribePhoneSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error @@ -185,11 +285,138 @@ func (o PublicreportSubscribePhoneSlice) AfterQueryHook(ctx context.Context, exe switch queryType { case bob.QueryTypeSelect: ctx, err = PublicreportSubscribePhones.AfterSelectHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeInsert: + ctx, err = PublicreportSubscribePhones.AfterInsertHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeUpdate: + ctx, err = PublicreportSubscribePhones.AfterUpdateHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeDelete: + ctx, err = PublicreportSubscribePhones.AfterDeleteHooks.RunHooks(ctx, exec, o) } return err } +func (o PublicreportSubscribePhoneSlice) pkIN() dialect.Expression { + if len(o) == 0 { + return psql.Raw("NULL") + } + + return psql.Quote("publicreport.subscribe_phone", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + pkPairs := make([]bob.Expression, len(o)) + for i, row := range o { + pkPairs[i] = row.primaryKeyVals() + } + return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "") + })) +} + +// copyMatchingRows finds models in the given slice that have the same primary key +// then it first copies the existing relationships from the old model to the new model +// and then replaces the old model in the slice with the new model +func (o PublicreportSubscribePhoneSlice) copyMatchingRows(from ...*PublicreportSubscribePhone) { + for i, old := range o { + for _, new := range from { + if new.ID != old.ID { + continue + } + new.R = old.R + o[i] = new + break + } + } +} + +// UpdateMod modifies an update query with "WHERE primary_key IN (o...)" +func (o PublicreportSubscribePhoneSlice) 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 PublicreportSubscribePhones.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 *PublicreportSubscribePhone: + o.copyMatchingRows(retrieved) + case []*PublicreportSubscribePhone: + o.copyMatchingRows(retrieved...) + case PublicreportSubscribePhoneSlice: + o.copyMatchingRows(retrieved...) + default: + // If the retrieved value is not a PublicreportSubscribePhone or a slice of PublicreportSubscribePhone + // then run the AfterUpdateHooks on the slice + _, err = PublicreportSubscribePhones.AfterUpdateHooks.RunHooks(ctx, exec, o) + } + + return err + })) + + q.AppendWhere(o.pkIN()) + }) +} + +// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)" +func (o PublicreportSubscribePhoneSlice) 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 PublicreportSubscribePhones.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 *PublicreportSubscribePhone: + o.copyMatchingRows(retrieved) + case []*PublicreportSubscribePhone: + o.copyMatchingRows(retrieved...) + case PublicreportSubscribePhoneSlice: + o.copyMatchingRows(retrieved...) + default: + // If the retrieved value is not a PublicreportSubscribePhone or a slice of PublicreportSubscribePhone + // then run the AfterDeleteHooks on the slice + _, err = PublicreportSubscribePhones.AfterDeleteHooks.RunHooks(ctx, exec, o) + } + + return err + })) + + q.AppendWhere(o.pkIN()) + }) +} + +func (o PublicreportSubscribePhoneSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals PublicreportSubscribePhoneSetter) error { + if len(o) == 0 { + return nil + } + + _, err := PublicreportSubscribePhones.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec) + return err +} + +func (o PublicreportSubscribePhoneSlice) DeleteAll(ctx context.Context, exec bob.Executor) error { + if len(o) == 0 { + return nil + } + + _, err := PublicreportSubscribePhones.Delete(o.DeleteMod()).Exec(ctx, exec) + return err +} + +func (o PublicreportSubscribePhoneSlice) ReloadAll(ctx context.Context, exec bob.Executor) error { + if len(o) == 0 { + return nil + } + + o2, err := PublicreportSubscribePhones.Query(sm.Where(o.pkIN())).All(ctx, exec) + if err != nil { + return err + } + + o.copyMatchingRows(o2...) + + return nil +} + // PhoneE164Phone starts a query for related objects on comms.phone func (o *PublicreportSubscribePhone) PhoneE164Phone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery { return CommsPhones.Query(append(mods, @@ -214,9 +441,58 @@ func (os PublicreportSubscribePhoneSlice) PhoneE164Phone(mods ...bob.Mod[*dialec )...) } +func attachPublicreportSubscribePhonePhoneE164Phone0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribePhone0 *PublicreportSubscribePhone, commsPhone1 *CommsPhone) (*PublicreportSubscribePhone, error) { + setter := &PublicreportSubscribePhoneSetter{ + PhoneE164: omit.From(commsPhone1.E164), + } + + err := publicreportSubscribePhone0.Update(ctx, exec, setter) + if err != nil { + return nil, fmt.Errorf("attachPublicreportSubscribePhonePhoneE164Phone0: %w", err) + } + + return publicreportSubscribePhone0, nil +} + +func (publicreportSubscribePhone0 *PublicreportSubscribePhone) InsertPhoneE164Phone(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 = attachPublicreportSubscribePhonePhoneE164Phone0(ctx, exec, 1, publicreportSubscribePhone0, commsPhone1) + if err != nil { + return err + } + + publicreportSubscribePhone0.R.PhoneE164Phone = commsPhone1 + + commsPhone1.R.PhoneE164SubscribePhones = append(commsPhone1.R.PhoneE164SubscribePhones, publicreportSubscribePhone0) + + return nil +} + +func (publicreportSubscribePhone0 *PublicreportSubscribePhone) AttachPhoneE164Phone(ctx context.Context, exec bob.Executor, commsPhone1 *CommsPhone) error { + var err error + + _, err = attachPublicreportSubscribePhonePhoneE164Phone0(ctx, exec, 1, publicreportSubscribePhone0, commsPhone1) + if err != nil { + return err + } + + publicreportSubscribePhone0.R.PhoneE164Phone = commsPhone1 + + commsPhone1.R.PhoneE164SubscribePhones = append(commsPhone1.R.PhoneE164SubscribePhones, publicreportSubscribePhone0) + + return nil +} + type publicreportSubscribePhoneWhere[Q psql.Filterable] struct { Created psql.WhereMod[Q, time.Time] Deleted psql.WhereNullMod[Q, time.Time] + ID psql.WhereMod[Q, int32] PhoneE164 psql.WhereMod[Q, string] } @@ -228,6 +504,7 @@ func buildPublicreportSubscribePhoneWhere[Q psql.Filterable](cols publicreportSu return publicreportSubscribePhoneWhere[Q]{ Created: psql.Where[Q, time.Time](cols.Created), Deleted: psql.WhereNull[Q, time.Time](cols.Deleted), + ID: psql.Where[Q, int32](cols.ID), PhoneE164: psql.Where[Q, string](cols.PhoneE164), } }