// 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 models import ( "context" "fmt" "io" "time" "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/sm" "github.com/Gleipnir-Technology/bob/dialect/psql/um" "github.com/Gleipnir-Technology/bob/expr" "github.com/Gleipnir-Technology/bob/mods" "github.com/Gleipnir-Technology/bob/orm" "github.com/Gleipnir-Technology/bob/types/pgtypes" "github.com/aarondl/opt/null" "github.com/aarondl/opt/omit" "github.com/aarondl/opt/omitnull" ) // PublicreportSubscribePhone is an object representing the database table. type PublicreportSubscribePhone struct { Created time.Time `db:"created" ` Deleted null.Val[time.Time] `db:"deleted" ` PhoneE164 string `db:"phone_e164" ` R publicreportSubscribePhoneR `db:"-" ` } // PublicreportSubscribePhoneSlice is an alias for a slice of pointers to PublicreportSubscribePhone. // 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")) // PublicreportSubscribePhonesQuery is a query on the subscribe_phone view type PublicreportSubscribePhonesQuery = *psql.ViewQuery[*PublicreportSubscribePhone, PublicreportSubscribePhoneSlice] // publicreportSubscribePhoneR is where relationships are stored. type publicreportSubscribePhoneR struct { PhoneE164Phone *CommsPhone // publicreport.subscribe_phone.subscribe_phone_phone_e164_fkey } func buildPublicreportSubscribePhoneColumns(alias string) publicreportSubscribePhoneColumns { return publicreportSubscribePhoneColumns{ ColumnsExpr: expr.NewColumnsExpr( "created", "deleted", "phone_e164", ).WithParent("publicreport.subscribe_phone"), tableAlias: alias, Created: psql.Quote(alias, "created"), Deleted: psql.Quote(alias, "deleted"), PhoneE164: psql.Quote(alias, "phone_e164"), } } type publicreportSubscribePhoneColumns struct { expr.ColumnsExpr tableAlias string Created psql.Expression Deleted psql.Expression PhoneE164 psql.Expression } func (c publicreportSubscribePhoneColumns) Alias() string { return c.tableAlias } func (publicreportSubscribePhoneColumns) AliasedAs(alias string) publicreportSubscribePhoneColumns { return buildPublicreportSubscribePhoneColumns(alias) } // PublicreportSubscribePhoneSetter is used for insert/upsert/update operations // All values are optional, and do not have to be set // Generated columns are not included type PublicreportSubscribePhoneSetter struct { Created omit.Val[time.Time] `db:"created" ` Deleted omitnull.Val[time.Time] `db:"deleted" ` PhoneE164 omit.Val[string] `db:"phone_e164" ` } func (s PublicreportSubscribePhoneSetter) SetColumns() []string { vals := make([]string, 0, 3) if s.Created.IsValue() { vals = append(vals, "created") } if !s.Deleted.IsUnset() { vals = append(vals, "deleted") } if s.PhoneE164.IsValue() { vals = append(vals, "phone_e164") } return vals } func (s PublicreportSubscribePhoneSetter) Overwrite(t *PublicreportSubscribePhone) { if s.Created.IsValue() { t.Created = s.Created.MustGet() } if !s.Deleted.IsUnset() { t.Deleted = s.Deleted.MustGetNull() } if s.PhoneE164.IsValue() { t.PhoneE164 = s.PhoneE164.MustGet() } } func (s *PublicreportSubscribePhoneSetter) 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, 3) if s.Created.IsValue() { vals[0] = psql.Arg(s.Created.MustGet()) } else { vals[0] = psql.Raw("DEFAULT") } if !s.Deleted.IsUnset() { vals[1] = psql.Arg(s.Deleted.MustGetNull()) } else { vals[1] = psql.Raw("DEFAULT") } if s.PhoneE164.IsValue() { vals[2] = psql.Arg(s.PhoneE164.MustGet()) } else { vals[2] = psql.Raw("DEFAULT") } return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "") })) } func (s PublicreportSubscribePhoneSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] { return um.Set(s.Expressions()...) } func (s PublicreportSubscribePhoneSetter) Expressions(prefix ...string) []bob.Expression { exprs := make([]bob.Expression, 0, 3) if s.Created.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ psql.Quote(append(prefix, "created")...), psql.Arg(s.Created), }}) } if !s.Deleted.IsUnset() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ psql.Quote(append(prefix, "deleted")...), psql.Arg(s.Deleted), }}) } if s.PhoneE164.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ psql.Quote(append(prefix, "phone_e164")...), psql.Arg(s.PhoneE164), }}) } return exprs } // 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 switch queryType { case bob.QueryTypeSelect: ctx, err = PublicreportSubscribePhones.AfterSelectHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) } return err } // 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 switch queryType { case bob.QueryTypeSelect: ctx, err = PublicreportSubscribePhones.AfterSelectHooks.RunHooks(ctx, exec, o) } return err } // 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, sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(o.PhoneE164))), )...) } func (os PublicreportSubscribePhoneSlice) PhoneE164Phone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery { pkPhoneE164 := make(pgtypes.Array[string], 0, len(os)) for _, o := range os { if o == nil { continue } pkPhoneE164 = append(pkPhoneE164, o.PhoneE164) } PKArgExpr := psql.Select(sm.Columns( psql.F("unnest", psql.Cast(psql.Arg(pkPhoneE164), "text[]")), )) return CommsPhones.Query(append(mods, sm.Where(psql.Group(CommsPhones.Columns.E164).OP("IN", PKArgExpr)), )...) } type publicreportSubscribePhoneWhere[Q psql.Filterable] struct { Created psql.WhereMod[Q, time.Time] Deleted psql.WhereNullMod[Q, time.Time] PhoneE164 psql.WhereMod[Q, string] } func (publicreportSubscribePhoneWhere[Q]) AliasedAs(alias string) publicreportSubscribePhoneWhere[Q] { return buildPublicreportSubscribePhoneWhere[Q](buildPublicreportSubscribePhoneColumns(alias)) } func buildPublicreportSubscribePhoneWhere[Q psql.Filterable](cols publicreportSubscribePhoneColumns) publicreportSubscribePhoneWhere[Q] { return publicreportSubscribePhoneWhere[Q]{ Created: psql.Where[Q, time.Time](cols.Created), Deleted: psql.WhereNull[Q, time.Time](cols.Deleted), PhoneE164: psql.Where[Q, string](cols.PhoneE164), } } func (o *PublicreportSubscribePhone) Preload(name string, retrieved any) error { if o == nil { return nil } switch name { case "PhoneE164Phone": rel, ok := retrieved.(*CommsPhone) if !ok { return fmt.Errorf("publicreportSubscribePhone cannot load %T as %q", retrieved, name) } o.R.PhoneE164Phone = rel if rel != nil { rel.R.PhoneE164SubscribePhones = PublicreportSubscribePhoneSlice{o} } return nil default: return fmt.Errorf("publicreportSubscribePhone has no relationship %q", name) } } type publicreportSubscribePhonePreloader struct { PhoneE164Phone func(...psql.PreloadOption) psql.Preloader } func buildPublicreportSubscribePhonePreloader() publicreportSubscribePhonePreloader { return publicreportSubscribePhonePreloader{ PhoneE164Phone: func(opts ...psql.PreloadOption) psql.Preloader { return psql.Preload[*CommsPhone, CommsPhoneSlice](psql.PreloadRel{ Name: "PhoneE164Phone", Sides: []psql.PreloadSide{ { From: PublicreportSubscribePhones, To: CommsPhones, FromColumns: []string{"phone_e164"}, ToColumns: []string{"e164"}, }, }, }, CommsPhones.Columns.Names(), opts...) }, } } type publicreportSubscribePhoneThenLoader[Q orm.Loadable] struct { PhoneE164Phone func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildPublicreportSubscribePhoneThenLoader[Q orm.Loadable]() publicreportSubscribePhoneThenLoader[Q] { type PhoneE164PhoneLoadInterface interface { LoadPhoneE164Phone(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } return publicreportSubscribePhoneThenLoader[Q]{ PhoneE164Phone: thenLoadBuilder[Q]( "PhoneE164Phone", func(ctx context.Context, exec bob.Executor, retrieved PhoneE164PhoneLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadPhoneE164Phone(ctx, exec, mods...) }, ), } } // LoadPhoneE164Phone loads the publicreportSubscribePhone's PhoneE164Phone into the .R struct func (o *PublicreportSubscribePhone) LoadPhoneE164Phone(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } // Reset the relationship o.R.PhoneE164Phone = nil related, err := o.PhoneE164Phone(mods...).One(ctx, exec) if err != nil { return err } related.R.PhoneE164SubscribePhones = PublicreportSubscribePhoneSlice{o} o.R.PhoneE164Phone = related return nil } // LoadPhoneE164Phone loads the publicreportSubscribePhone's PhoneE164Phone into the .R struct func (os PublicreportSubscribePhoneSlice) LoadPhoneE164Phone(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if len(os) == 0 { return nil } commsPhones, err := os.PhoneE164Phone(mods...).All(ctx, exec) if err != nil { return err } for _, o := range os { if o == nil { continue } for _, rel := range commsPhones { if !(o.PhoneE164 == rel.E164) { continue } rel.R.PhoneE164SubscribePhones = append(rel.R.PhoneE164SubscribePhones, o) o.R.PhoneE164Phone = rel break } } return nil } type publicreportSubscribePhoneJoins[Q dialect.Joinable] struct { typ string PhoneE164Phone modAs[Q, commsPhoneColumns] } func (j publicreportSubscribePhoneJoins[Q]) aliasedAs(alias string) publicreportSubscribePhoneJoins[Q] { return buildPublicreportSubscribePhoneJoins[Q](buildPublicreportSubscribePhoneColumns(alias), j.typ) } func buildPublicreportSubscribePhoneJoins[Q dialect.Joinable](cols publicreportSubscribePhoneColumns, typ string) publicreportSubscribePhoneJoins[Q] { return publicreportSubscribePhoneJoins[Q]{ typ: typ, PhoneE164Phone: 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.PhoneE164), )) } return mods }, }, } }