// Code generated by BobGen mysql v0.42.0. DO NOT EDIT. // This file is meant to be re-generated in place and/or deleted at any time. package models import ( "context" "fmt" "io" "github.com/aarondl/opt/omit" "github.com/stephenafamo/bob" "github.com/stephenafamo/bob/dialect/mysql" "github.com/stephenafamo/bob/dialect/mysql/dialect" "github.com/stephenafamo/bob/dialect/mysql/dm" "github.com/stephenafamo/bob/dialect/mysql/sm" "github.com/stephenafamo/bob/dialect/mysql/um" "github.com/stephenafamo/bob/expr" "github.com/stephenafamo/bob/mods" "github.com/stephenafamo/bob/orm" ) // LocationsFedora is an object representing the database table. type LocationsFedora struct { ID int32 `db:"id,pk,autoincr" ` FedoraUser string `db:"fedora_user" ` FedoraPassword string `db:"fedora_password" ` FedoraName string `db:"fedora_name" ` SpaceID string `db:"space_id" ` R locationsFedoraR `db:"-" ` } // LocationsFedoraSlice is an alias for a slice of pointers to LocationsFedora. // This should almost always be used instead of []*LocationsFedora. type LocationsFedoraSlice []*LocationsFedora // LocationsFedoras contains methods to work with the locations_fedora table var LocationsFedoras = mysql.NewTablex[*LocationsFedora, LocationsFedoraSlice, *LocationsFedoraSetter]("locations_fedora", buildLocationsFedoraColumns("locations_fedora"), []string{"id"}, []string{"space_id"}) // LocationsFedorasQuery is a query on the locations_fedora table type LocationsFedorasQuery = *mysql.ViewQuery[*LocationsFedora, LocationsFedoraSlice] // locationsFedoraR is where relationships are stored. type locationsFedoraR struct { SpaceLocationsSpace *LocationsSpace // locations_fedora_space_id_088fe895_fk_locations_space_uuid } func buildLocationsFedoraColumns(alias string) locationsFedoraColumns { return locationsFedoraColumns{ ColumnsExpr: expr.NewColumnsExpr( "id", "fedora_user", "fedora_password", "fedora_name", "space_id", ).WithParent("locations_fedora"), tableAlias: alias, ID: mysql.Quote(alias, "id"), FedoraUser: mysql.Quote(alias, "fedora_user"), FedoraPassword: mysql.Quote(alias, "fedora_password"), FedoraName: mysql.Quote(alias, "fedora_name"), SpaceID: mysql.Quote(alias, "space_id"), } } type locationsFedoraColumns struct { expr.ColumnsExpr tableAlias string ID mysql.Expression FedoraUser mysql.Expression FedoraPassword mysql.Expression FedoraName mysql.Expression SpaceID mysql.Expression } func (c locationsFedoraColumns) Alias() string { return c.tableAlias } func (locationsFedoraColumns) AliasedAs(alias string) locationsFedoraColumns { return buildLocationsFedoraColumns(alias) } // LocationsFedoraSetter is used for insert/upsert/update operations // All values are optional, and do not have to be set // Generated columns are not included type LocationsFedoraSetter struct { ID omit.Val[int32] `db:"id,pk,autoincr" ` FedoraUser omit.Val[string] `db:"fedora_user" ` FedoraPassword omit.Val[string] `db:"fedora_password" ` FedoraName omit.Val[string] `db:"fedora_name" ` SpaceID omit.Val[string] `db:"space_id" ` } func (s LocationsFedoraSetter) SetColumns() []string { vals := make([]string, 0, 5) if s.ID.IsValue() { vals = append(vals, "id") } if s.FedoraUser.IsValue() { vals = append(vals, "fedora_user") } if s.FedoraPassword.IsValue() { vals = append(vals, "fedora_password") } if s.FedoraName.IsValue() { vals = append(vals, "fedora_name") } if s.SpaceID.IsValue() { vals = append(vals, "space_id") } return vals } func (s LocationsFedoraSetter) Overwrite(t *LocationsFedora) { if s.ID.IsValue() { t.ID = s.ID.MustGet() } if s.FedoraUser.IsValue() { t.FedoraUser = s.FedoraUser.MustGet() } if s.FedoraPassword.IsValue() { t.FedoraPassword = s.FedoraPassword.MustGet() } if s.FedoraName.IsValue() { t.FedoraName = s.FedoraName.MustGet() } if s.SpaceID.IsValue() { t.SpaceID = s.SpaceID.MustGet() } } func (s *LocationsFedoraSetter) Apply(q *dialect.InsertQuery) { q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { return LocationsFedoras.BeforeInsertHooks.RunHooks(ctx, exec, s) }) q.AppendValues( bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { if !(s.ID.IsValue()) { return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start) } return mysql.Arg(s.ID.MustGet()).WriteSQL(ctx, w, d, start) }), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { if !(s.FedoraUser.IsValue()) { return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start) } return mysql.Arg(s.FedoraUser.MustGet()).WriteSQL(ctx, w, d, start) }), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { if !(s.FedoraPassword.IsValue()) { return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start) } return mysql.Arg(s.FedoraPassword.MustGet()).WriteSQL(ctx, w, d, start) }), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { if !(s.FedoraName.IsValue()) { return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start) } return mysql.Arg(s.FedoraName.MustGet()).WriteSQL(ctx, w, d, start) }), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { if !(s.SpaceID.IsValue()) { return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start) } return mysql.Arg(s.SpaceID.MustGet()).WriteSQL(ctx, w, d, start) })) } func (s LocationsFedoraSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] { return um.Set(s.Expressions("locations_fedora")...) } func (s LocationsFedoraSetter) Expressions(prefix ...string) []bob.Expression { exprs := make([]bob.Expression, 0, 5) if s.ID.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ mysql.Quote(append(prefix, "id")...), mysql.Arg(s.ID), }}) } if s.FedoraUser.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ mysql.Quote(append(prefix, "fedora_user")...), mysql.Arg(s.FedoraUser), }}) } if s.FedoraPassword.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ mysql.Quote(append(prefix, "fedora_password")...), mysql.Arg(s.FedoraPassword), }}) } if s.FedoraName.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ mysql.Quote(append(prefix, "fedora_name")...), mysql.Arg(s.FedoraName), }}) } if s.SpaceID.IsValue() { exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ mysql.Quote(append(prefix, "space_id")...), mysql.Arg(s.SpaceID), }}) } return exprs } // FindLocationsFedora retrieves a single record by primary key // If cols is empty Find will return all columns. func FindLocationsFedora(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*LocationsFedora, error) { if len(cols) == 0 { return LocationsFedoras.Query( sm.Where(LocationsFedoras.Columns.ID.EQ(mysql.Arg(IDPK))), ).One(ctx, exec) } return LocationsFedoras.Query( sm.Where(LocationsFedoras.Columns.ID.EQ(mysql.Arg(IDPK))), sm.Columns(LocationsFedoras.Columns.Only(cols...)), ).One(ctx, exec) } // LocationsFedoraExists checks the presence of a single record by primary key func LocationsFedoraExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) { return LocationsFedoras.Query( sm.Where(LocationsFedoras.Columns.ID.EQ(mysql.Arg(IDPK))), ).Exists(ctx, exec) } // AfterQueryHook is called after LocationsFedora is retrieved from the database func (o *LocationsFedora) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error switch queryType { case bob.QueryTypeSelect: ctx, err = LocationsFedoras.AfterSelectHooks.RunHooks(ctx, exec, LocationsFedoraSlice{o}) case bob.QueryTypeInsert: ctx, err = LocationsFedoras.AfterInsertHooks.RunHooks(ctx, exec, LocationsFedoraSlice{o}) case bob.QueryTypeUpdate: ctx, err = LocationsFedoras.AfterUpdateHooks.RunHooks(ctx, exec, LocationsFedoraSlice{o}) case bob.QueryTypeDelete: ctx, err = LocationsFedoras.AfterDeleteHooks.RunHooks(ctx, exec, LocationsFedoraSlice{o}) } return err } // primaryKeyVals returns the primary key values of the LocationsFedora func (o *LocationsFedora) primaryKeyVals() bob.Expression { return mysql.Arg(o.ID) } func (o *LocationsFedora) pkEQ() dialect.Expression { return mysql.Quote("locations_fedora", "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 LocationsFedora func (o *LocationsFedora) Update(ctx context.Context, exec bob.Executor, s *LocationsFedoraSetter) error { _, err := LocationsFedoras.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec) if err != nil { return err } s.Overwrite(o) return nil } // Delete deletes a single LocationsFedora record with an executor func (o *LocationsFedora) Delete(ctx context.Context, exec bob.Executor) error { _, err := LocationsFedoras.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec) return err } // Reload refreshes the LocationsFedora using the executor func (o *LocationsFedora) Reload(ctx context.Context, exec bob.Executor) error { o2, err := LocationsFedoras.Query( sm.Where(LocationsFedoras.Columns.ID.EQ(mysql.Arg(o.ID))), ).One(ctx, exec) if err != nil { return err } o2.R = o.R *o = *o2 return nil } // AfterQueryHook is called after LocationsFedoraSlice is retrieved from the database func (o LocationsFedoraSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { var err error switch queryType { case bob.QueryTypeSelect: ctx, err = LocationsFedoras.AfterSelectHooks.RunHooks(ctx, exec, o) case bob.QueryTypeInsert: ctx, err = LocationsFedoras.AfterInsertHooks.RunHooks(ctx, exec, o) case bob.QueryTypeUpdate: ctx, err = LocationsFedoras.AfterUpdateHooks.RunHooks(ctx, exec, o) case bob.QueryTypeDelete: ctx, err = LocationsFedoras.AfterDeleteHooks.RunHooks(ctx, exec, o) } return err } func (o LocationsFedoraSlice) pkIN() dialect.Expression { if len(o) == 0 { return mysql.Raw("NULL") } return mysql.Quote("locations_fedora", "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 LocationsFedoraSlice) copyMatchingRows(from ...*LocationsFedora) { 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 LocationsFedoraSlice) 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 LocationsFedoras.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 *LocationsFedora: o.copyMatchingRows(retrieved) case []*LocationsFedora: o.copyMatchingRows(retrieved...) case LocationsFedoraSlice: o.copyMatchingRows(retrieved...) default: // If the retrieved value is not a LocationsFedora or a slice of LocationsFedora // then run the AfterUpdateHooks on the slice _, err = LocationsFedoras.AfterUpdateHooks.RunHooks(ctx, exec, o) } return err })) q.AppendWhere(o.pkIN()) }) } // DeleteMod modifies an delete query with "WHERE primary_key IN (o...)" func (o LocationsFedoraSlice) 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 LocationsFedoras.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 *LocationsFedora: o.copyMatchingRows(retrieved) case []*LocationsFedora: o.copyMatchingRows(retrieved...) case LocationsFedoraSlice: o.copyMatchingRows(retrieved...) default: // If the retrieved value is not a LocationsFedora or a slice of LocationsFedora // then run the AfterDeleteHooks on the slice _, err = LocationsFedoras.AfterDeleteHooks.RunHooks(ctx, exec, o) } return err })) q.AppendWhere(o.pkIN()) }) } func (o LocationsFedoraSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsFedoraSetter) error { _, err := LocationsFedoras.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec) for i := range o { vals.Overwrite(o[i]) } return err } func (o LocationsFedoraSlice) DeleteAll(ctx context.Context, exec bob.Executor) error { if len(o) == 0 { return nil } _, err := LocationsFedoras.Delete(o.DeleteMod()).Exec(ctx, exec) return err } func (o LocationsFedoraSlice) ReloadAll(ctx context.Context, exec bob.Executor) error { if len(o) == 0 { return nil } o2, err := LocationsFedoras.Query(sm.Where(o.pkIN())).All(ctx, exec) if err != nil { return err } o.copyMatchingRows(o2...) return nil } // SpaceLocationsSpace starts a query for related objects on locations_space func (o *LocationsFedora) SpaceLocationsSpace(mods ...bob.Mod[*dialect.SelectQuery]) LocationsSpacesQuery { return LocationsSpaces.Query(append(mods, sm.Where(LocationsSpaces.Columns.UUID.EQ(mysql.Arg(o.SpaceID))), )...) } func (os LocationsFedoraSlice) SpaceLocationsSpace(mods ...bob.Mod[*dialect.SelectQuery]) LocationsSpacesQuery { PKArgSlice := make([]bob.Expression, len(os)) for i, o := range os { PKArgSlice[i] = mysql.ArgGroup(o.SpaceID) } PKArgExpr := mysql.Group(PKArgSlice...) return LocationsSpaces.Query(append(mods, sm.Where(mysql.Group(LocationsSpaces.Columns.UUID).OP("IN", PKArgExpr)), )...) } func attachLocationsFedoraSpaceLocationsSpace0(ctx context.Context, exec bob.Executor, count int, locationsFedora0 *LocationsFedora, locationsSpace1 *LocationsSpace) (*LocationsFedora, error) { setter := &LocationsFedoraSetter{ SpaceID: omit.From(locationsSpace1.UUID), } err := locationsFedora0.Update(ctx, exec, setter) if err != nil { return nil, fmt.Errorf("attachLocationsFedoraSpaceLocationsSpace0: %w", err) } return locationsFedora0, nil } func (locationsFedora0 *LocationsFedora) InsertSpaceLocationsSpace(ctx context.Context, exec bob.Executor, related *LocationsSpaceSetter) error { var err error locationsSpace1, err := LocationsSpaces.Insert(related).One(ctx, exec) if err != nil { return fmt.Errorf("inserting related objects: %w", err) } _, err = attachLocationsFedoraSpaceLocationsSpace0(ctx, exec, 1, locationsFedora0, locationsSpace1) if err != nil { return err } locationsFedora0.R.SpaceLocationsSpace = locationsSpace1 locationsSpace1.R.SpaceLocationsFedora = locationsFedora0 return nil } func (locationsFedora0 *LocationsFedora) AttachSpaceLocationsSpace(ctx context.Context, exec bob.Executor, locationsSpace1 *LocationsSpace) error { var err error _, err = attachLocationsFedoraSpaceLocationsSpace0(ctx, exec, 1, locationsFedora0, locationsSpace1) if err != nil { return err } locationsFedora0.R.SpaceLocationsSpace = locationsSpace1 locationsSpace1.R.SpaceLocationsFedora = locationsFedora0 return nil } type locationsFedoraWhere[Q mysql.Filterable] struct { ID mysql.WhereMod[Q, int32] FedoraUser mysql.WhereMod[Q, string] FedoraPassword mysql.WhereMod[Q, string] FedoraName mysql.WhereMod[Q, string] SpaceID mysql.WhereMod[Q, string] } func (locationsFedoraWhere[Q]) AliasedAs(alias string) locationsFedoraWhere[Q] { return buildLocationsFedoraWhere[Q](buildLocationsFedoraColumns(alias)) } func buildLocationsFedoraWhere[Q mysql.Filterable](cols locationsFedoraColumns) locationsFedoraWhere[Q] { return locationsFedoraWhere[Q]{ ID: mysql.Where[Q, int32](cols.ID), FedoraUser: mysql.Where[Q, string](cols.FedoraUser), FedoraPassword: mysql.Where[Q, string](cols.FedoraPassword), FedoraName: mysql.Where[Q, string](cols.FedoraName), SpaceID: mysql.Where[Q, string](cols.SpaceID), } } func (o *LocationsFedora) Preload(name string, retrieved any) error { if o == nil { return nil } switch name { case "SpaceLocationsSpace": rel, ok := retrieved.(*LocationsSpace) if !ok { return fmt.Errorf("locationsFedora cannot load %T as %q", retrieved, name) } o.R.SpaceLocationsSpace = rel if rel != nil { rel.R.SpaceLocationsFedora = o } return nil default: return fmt.Errorf("locationsFedora has no relationship %q", name) } } type locationsFedoraPreloader struct { SpaceLocationsSpace func(...mysql.PreloadOption) mysql.Preloader } func buildLocationsFedoraPreloader() locationsFedoraPreloader { return locationsFedoraPreloader{ SpaceLocationsSpace: func(opts ...mysql.PreloadOption) mysql.Preloader { return mysql.Preload[*LocationsSpace, LocationsSpaceSlice](mysql.PreloadRel{ Name: "SpaceLocationsSpace", Sides: []mysql.PreloadSide{ { From: LocationsFedoras, To: LocationsSpaces, FromColumns: []string{"space_id"}, ToColumns: []string{"uuid"}, }, }, }, LocationsSpaces.Columns.Names(), opts...) }, } } type locationsFedoraThenLoader[Q orm.Loadable] struct { SpaceLocationsSpace func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildLocationsFedoraThenLoader[Q orm.Loadable]() locationsFedoraThenLoader[Q] { type SpaceLocationsSpaceLoadInterface interface { LoadSpaceLocationsSpace(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } return locationsFedoraThenLoader[Q]{ SpaceLocationsSpace: thenLoadBuilder[Q]( "SpaceLocationsSpace", func(ctx context.Context, exec bob.Executor, retrieved SpaceLocationsSpaceLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { return retrieved.LoadSpaceLocationsSpace(ctx, exec, mods...) }, ), } } // LoadSpaceLocationsSpace loads the locationsFedora's SpaceLocationsSpace into the .R struct func (o *LocationsFedora) LoadSpaceLocationsSpace(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { return nil } // Reset the relationship o.R.SpaceLocationsSpace = nil related, err := o.SpaceLocationsSpace(mods...).One(ctx, exec) if err != nil { return err } related.R.SpaceLocationsFedora = o o.R.SpaceLocationsSpace = related return nil } // LoadSpaceLocationsSpace loads the locationsFedora's SpaceLocationsSpace into the .R struct func (os LocationsFedoraSlice) LoadSpaceLocationsSpace(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if len(os) == 0 { return nil } locationsSpaces, err := os.SpaceLocationsSpace(mods...).All(ctx, exec) if err != nil { return err } for _, o := range os { if o == nil { continue } for _, rel := range locationsSpaces { if !(o.SpaceID == rel.UUID) { continue } rel.R.SpaceLocationsFedora = o o.R.SpaceLocationsSpace = rel break } } return nil } type locationsFedoraJoins[Q dialect.Joinable] struct { typ string SpaceLocationsSpace modAs[Q, locationsSpaceColumns] } func (j locationsFedoraJoins[Q]) aliasedAs(alias string) locationsFedoraJoins[Q] { return buildLocationsFedoraJoins[Q](buildLocationsFedoraColumns(alias), j.typ) } func buildLocationsFedoraJoins[Q dialect.Joinable](cols locationsFedoraColumns, typ string) locationsFedoraJoins[Q] { return locationsFedoraJoins[Q]{ typ: typ, SpaceLocationsSpace: modAs[Q, locationsSpaceColumns]{ c: LocationsSpaces.Columns, f: func(to locationsSpaceColumns) bob.Mod[Q] { mods := make(mods.QueryMods[Q], 0, 1) { mods = append(mods, dialect.Join[Q](typ, LocationsSpaces.Name().As(to.Alias())).On( to.UUID.EQ(cols.SpaceID), )) } return mods }, }, } }