// 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/dm" "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/orm" "github.com/Gleipnir-Technology/bob/types/pgtypes" "github.com/aarondl/opt/omit" "github.com/aarondl/opt/omitnull" "github.com/google/uuid" ) // PublicreportClient is an object representing the database table. type PublicreportClient struct { Created time.Time `db:"created" ` UserAgent string `db:"user_agent" ` UUID uuid.UUID `db:"uuid,pk" ` R publicreportClientR `db:"-" ` } // PublicreportClientSlice is an alias for a slice of pointers to PublicreportClient. // This should almost always be used instead of []*PublicreportClient. type PublicreportClientSlice []*PublicreportClient // PublicreportClients contains methods to work with the client table var PublicreportClients = psql.NewTablex[*PublicreportClient, PublicreportClientSlice, *PublicreportClientSetter]("publicreport", "client", buildPublicreportClientColumns("publicreport.client")) // PublicreportClientsQuery is a query on the client table type PublicreportClientsQuery = *psql.ViewQuery[*PublicreportClient, PublicreportClientSlice] // publicreportClientR is where relationships are stored. type publicreportClientR struct { Reports PublicreportReportSlice // publicreport.report.report_client_uuid_fkey } func buildPublicreportClientColumns(alias string) publicreportClientColumns { return publicreportClientColumns{ ColumnsExpr: expr.NewColumnsExpr( "created", "user_agent", "uuid", ).WithParent("publicreport.client"), tableAlias: alias, Created: psql.Quote(alias, "created"), UserAgent: psql.Quote(alias, "user_agent"), UUID: psql.Quote(alias, "uuid"), } } type publicreportClientColumns struct { expr.ColumnsExpr tableAlias string Created psql.Expression UserAgent psql.Expression UUID psql.Expression } func (c publicreportClientColumns) Alias() string { return c.tableAlias } func (publicreportClientColumns) AliasedAs(alias string) publicreportClientColumns { return buildPublicreportClientColumns(alias) } // PublicreportClientSetter is used for insert/upsert/update operations // All values are optional, and do not have to be set // Generated columns are not included type PublicreportClientSetter struct { Created omit.Val[time.Time] `db:"created" ` UserAgent omit.Val[string] `db:"user_agent" ` UUID omit.Val[uuid.UUID] `db:"uuid,pk" ` } func (s PublicreportClientSetter) SetColumns() []string { vals := make([]string, 0, 3) if s.Created.IsValue() { vals = append(vals, "created") } if s.UserAgent.IsValue() { vals = append(vals, "user_agent") } if s.UUID.IsValue() { vals = append(vals, "uuid") } return vals } func (s PublicreportClientSetter) Overwrite(t *PublicreportClient) { if s.Created.IsValue() { t.Created = s.Created.MustGet() } if s.UserAgent.IsValue() { t.UserAgent = s.UserAgent.MustGet() } if s.UUID.IsValue() { t.UUID = s.UUID.MustGet() } } func (s *PublicreportClientSetter) Apply(q *dialect.InsertQuery) { q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { return PublicreportClients.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.Created.IsValue() { vals[0] = psql.Arg(s.Created.MustGet()) } else { vals[0] = psql.Raw("DEFAULT") } if s.UserAgent.IsValue() { vals[1] = psql.Arg(s.UserAgent.MustGet()) } else { vals[1] = psql.Raw("DEFAULT") } if s.UUID.IsValue() { vals[2] = psql.Arg(s.UUID.MustGet()) } else { vals[2] = psql.Raw("DEFAULT") } return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "") })) } func (s PublicreportClientSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] { return um.Set(s.Expressions()...) } func (s PublicreportClientSetter) 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.UserAgent.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ psql.Quote(append(prefix, "user_agent")...), psql.Arg(s.UserAgent), }}) } if s.UUID.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ psql.Quote(append(prefix, "uuid")...), psql.Arg(s.UUID), }}) } return exprs } // FindPublicreportClient retrieves a single record by primary key // If cols is empty Find will return all columns. func FindPublicreportClient(ctx context.Context, exec bob.Executor, UUIDPK uuid.UUID, cols ...string) (*PublicreportClient, error) { if len(cols) == 0 { return PublicreportClients.Query( sm.Where(PublicreportClients.Columns.UUID.EQ(psql.Arg(UUIDPK))), ).One(ctx, exec) } return PublicreportClients.Query( sm.Where(PublicreportClients.Columns.UUID.EQ(psql.Arg(UUIDPK))), sm.Columns(PublicreportClients.Columns.Only(cols...)), ).One(ctx, exec) } // PublicreportClientExists checks the presence of a single record by primary key func PublicreportClientExists(ctx context.Context, exec bob.Executor, UUIDPK uuid.UUID) (bool, error) { return PublicreportClients.Query( sm.Where(PublicreportClients.Columns.UUID.EQ(psql.Arg(UUIDPK))), ).Exists(ctx, exec) } // AfterQueryHook is called after PublicreportClient is retrieved from the database func (o *PublicreportClient) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error switch queryType { case bob.QueryTypeSelect: ctx, err = PublicreportClients.AfterSelectHooks.RunHooks(ctx, exec, PublicreportClientSlice{o}) case bob.QueryTypeInsert: ctx, err = PublicreportClients.AfterInsertHooks.RunHooks(ctx, exec, PublicreportClientSlice{o}) case bob.QueryTypeUpdate: ctx, err = PublicreportClients.AfterUpdateHooks.RunHooks(ctx, exec, PublicreportClientSlice{o}) case bob.QueryTypeDelete: ctx, err = PublicreportClients.AfterDeleteHooks.RunHooks(ctx, exec, PublicreportClientSlice{o}) } return err } // primaryKeyVals returns the primary key values of the PublicreportClient func (o *PublicreportClient) primaryKeyVals() bob.Expression { return psql.Arg(o.UUID) } func (o *PublicreportClient) pkEQ() dialect.Expression { return psql.Quote("publicreport.client", "uuid").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 PublicreportClient func (o *PublicreportClient) Update(ctx context.Context, exec bob.Executor, s *PublicreportClientSetter) error { v, err := PublicreportClients.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 PublicreportClient record with an executor func (o *PublicreportClient) Delete(ctx context.Context, exec bob.Executor) error { _, err := PublicreportClients.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec) return err } // Reload refreshes the PublicreportClient using the executor func (o *PublicreportClient) Reload(ctx context.Context, exec bob.Executor) error { o2, err := PublicreportClients.Query( sm.Where(PublicreportClients.Columns.UUID.EQ(psql.Arg(o.UUID))), ).One(ctx, exec) if err != nil { return err } o2.R = o.R *o = *o2 return nil } // AfterQueryHook is called after PublicreportClientSlice is retrieved from the database func (o PublicreportClientSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error switch queryType { case bob.QueryTypeSelect: ctx, err = PublicreportClients.AfterSelectHooks.RunHooks(ctx, exec, o) case bob.QueryTypeInsert: ctx, err = PublicreportClients.AfterInsertHooks.RunHooks(ctx, exec, o) case bob.QueryTypeUpdate: ctx, err = PublicreportClients.AfterUpdateHooks.RunHooks(ctx, exec, o) case bob.QueryTypeDelete: ctx, err = PublicreportClients.AfterDeleteHooks.RunHooks(ctx, exec, o) } return err } func (o PublicreportClientSlice) pkIN() dialect.Expression { if len(o) == 0 { return psql.Raw("NULL") } return psql.Quote("publicreport.client", "uuid").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 PublicreportClientSlice) copyMatchingRows(from ...*PublicreportClient) { for i, old := range o { for _, new := range from { if new.UUID != old.UUID { continue } new.R = old.R o[i] = new break } } } // UpdateMod modifies an update query with "WHERE primary_key IN (o...)" func (o PublicreportClientSlice) 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 PublicreportClients.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 *PublicreportClient: o.copyMatchingRows(retrieved) case []*PublicreportClient: o.copyMatchingRows(retrieved...) case PublicreportClientSlice: o.copyMatchingRows(retrieved...) default: // If the retrieved value is not a PublicreportClient or a slice of PublicreportClient // then run the AfterUpdateHooks on the slice _, err = PublicreportClients.AfterUpdateHooks.RunHooks(ctx, exec, o) } return err })) q.AppendWhere(o.pkIN()) }) } // DeleteMod modifies an delete query with "WHERE primary_key IN (o...)" func (o PublicreportClientSlice) 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 PublicreportClients.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 *PublicreportClient: o.copyMatchingRows(retrieved) case []*PublicreportClient: o.copyMatchingRows(retrieved...) case PublicreportClientSlice: o.copyMatchingRows(retrieved...) default: // If the retrieved value is not a PublicreportClient or a slice of PublicreportClient // then run the AfterDeleteHooks on the slice _, err = PublicreportClients.AfterDeleteHooks.RunHooks(ctx, exec, o) } return err })) q.AppendWhere(o.pkIN()) }) } func (o PublicreportClientSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals PublicreportClientSetter) error { if len(o) == 0 { return nil } _, err := PublicreportClients.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec) return err } func (o PublicreportClientSlice) DeleteAll(ctx context.Context, exec bob.Executor) error { if len(o) == 0 { return nil } _, err := PublicreportClients.Delete(o.DeleteMod()).Exec(ctx, exec) return err } func (o PublicreportClientSlice) ReloadAll(ctx context.Context, exec bob.Executor) error { if len(o) == 0 { return nil } o2, err := PublicreportClients.Query(sm.Where(o.pkIN())).All(ctx, exec) if err != nil { return err } o.copyMatchingRows(o2...) return nil } // Reports starts a query for related objects on publicreport.report func (o *PublicreportClient) Reports(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery { return PublicreportReports.Query(append(mods, sm.Where(PublicreportReports.Columns.ClientUUID.EQ(psql.Arg(o.UUID))), )...) } func (os PublicreportClientSlice) Reports(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery { pkUUID := make(pgtypes.Array[uuid.UUID], 0, len(os)) for _, o := range os { if o == nil { continue } pkUUID = append(pkUUID, o.UUID) } PKArgExpr := psql.Select(sm.Columns( psql.F("unnest", psql.Cast(psql.Arg(pkUUID), "uuid[]")), )) return PublicreportReports.Query(append(mods, sm.Where(psql.Group(PublicreportReports.Columns.ClientUUID).OP("IN", PKArgExpr)), )...) } func insertPublicreportClientReports0(ctx context.Context, exec bob.Executor, publicreportReports1 []*PublicreportReportSetter, publicreportClient0 *PublicreportClient) (PublicreportReportSlice, error) { for i := range publicreportReports1 { publicreportReports1[i].ClientUUID = omitnull.From(publicreportClient0.UUID) } ret, err := PublicreportReports.Insert(bob.ToMods(publicreportReports1...)).All(ctx, exec) if err != nil { return ret, fmt.Errorf("insertPublicreportClientReports0: %w", err) } return ret, nil } func attachPublicreportClientReports0(ctx context.Context, exec bob.Executor, count int, publicreportReports1 PublicreportReportSlice, publicreportClient0 *PublicreportClient) (PublicreportReportSlice, error) { setter := &PublicreportReportSetter{ ClientUUID: omitnull.From(publicreportClient0.UUID), } err := publicreportReports1.UpdateAll(ctx, exec, *setter) if err != nil { return nil, fmt.Errorf("attachPublicreportClientReports0: %w", err) } return publicreportReports1, nil } func (publicreportClient0 *PublicreportClient) InsertReports(ctx context.Context, exec bob.Executor, related ...*PublicreportReportSetter) error { if len(related) == 0 { return nil } var err error publicreportReports1, err := insertPublicreportClientReports0(ctx, exec, related, publicreportClient0) if err != nil { return err } publicreportClient0.R.Reports = append(publicreportClient0.R.Reports, publicreportReports1...) for _, rel := range publicreportReports1 { rel.R.Client = publicreportClient0 } return nil } func (publicreportClient0 *PublicreportClient) AttachReports(ctx context.Context, exec bob.Executor, related ...*PublicreportReport) error { if len(related) == 0 { return nil } var err error publicreportReports1 := PublicreportReportSlice(related) _, err = attachPublicreportClientReports0(ctx, exec, len(related), publicreportReports1, publicreportClient0) if err != nil { return err } publicreportClient0.R.Reports = append(publicreportClient0.R.Reports, publicreportReports1...) for _, rel := range related { rel.R.Client = publicreportClient0 } return nil } type publicreportClientWhere[Q psql.Filterable] struct { Created psql.WhereMod[Q, time.Time] UserAgent psql.WhereMod[Q, string] UUID psql.WhereMod[Q, uuid.UUID] } func (publicreportClientWhere[Q]) AliasedAs(alias string) publicreportClientWhere[Q] { return buildPublicreportClientWhere[Q](buildPublicreportClientColumns(alias)) } func buildPublicreportClientWhere[Q psql.Filterable](cols publicreportClientColumns) publicreportClientWhere[Q] { return publicreportClientWhere[Q]{ Created: psql.Where[Q, time.Time](cols.Created), UserAgent: psql.Where[Q, string](cols.UserAgent), UUID: psql.Where[Q, uuid.UUID](cols.UUID), } } func (o *PublicreportClient) Preload(name string, retrieved any) error { if o == nil { return nil } switch name { case "Reports": rels, ok := retrieved.(PublicreportReportSlice) if !ok { return fmt.Errorf("publicreportClient cannot load %T as %q", retrieved, name) } o.R.Reports = rels for _, rel := range rels { if rel != nil { rel.R.Client = o } } return nil default: return fmt.Errorf("publicreportClient has no relationship %q", name) } } type publicreportClientPreloader struct{} func buildPublicreportClientPreloader() publicreportClientPreloader { return publicreportClientPreloader{} } type publicreportClientThenLoader[Q orm.Loadable] struct { Reports func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildPublicreportClientThenLoader[Q orm.Loadable]() publicreportClientThenLoader[Q] { type ReportsLoadInterface interface { LoadReports(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } return publicreportClientThenLoader[Q]{ Reports: thenLoadBuilder[Q]( "Reports", func(ctx context.Context, exec bob.Executor, retrieved ReportsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadReports(ctx, exec, mods...) }, ), } } // LoadReports loads the publicreportClient's Reports into the .R struct func (o *PublicreportClient) LoadReports(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } // Reset the relationship o.R.Reports = nil related, err := o.Reports(mods...).All(ctx, exec) if err != nil { return err } for _, rel := range related { rel.R.Client = o } o.R.Reports = related return nil } // LoadReports loads the publicreportClient's Reports into the .R struct func (os PublicreportClientSlice) LoadReports(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if len(os) == 0 { return nil } publicreportReports, err := os.Reports(mods...).All(ctx, exec) if err != nil { return err } for _, o := range os { if o == nil { continue } o.R.Reports = nil } for _, o := range os { if o == nil { continue } for _, rel := range publicreportReports { if !rel.ClientUUID.IsValue() { continue } if !(rel.ClientUUID.IsValue() && o.UUID == rel.ClientUUID.MustGet()) { continue } rel.R.Client = o o.R.Reports = append(o.R.Reports, rel) } } return nil }