623 lines
16 KiB
Go
623 lines
16 KiB
Go
|
|
// Code generated by BobGen psql v0.41.1. DO NOT EDIT.
|
||
|
|
// This file is meant to be re-generated in place and/or deleted at any time.
|
||
|
|
|
||
|
|
package models
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
|
||
|
|
"github.com/aarondl/opt/null"
|
||
|
|
"github.com/aarondl/opt/omit"
|
||
|
|
"github.com/aarondl/opt/omitnull"
|
||
|
|
"github.com/stephenafamo/bob"
|
||
|
|
"github.com/stephenafamo/bob/dialect/psql"
|
||
|
|
"github.com/stephenafamo/bob/dialect/psql/dialect"
|
||
|
|
"github.com/stephenafamo/bob/dialect/psql/dm"
|
||
|
|
"github.com/stephenafamo/bob/dialect/psql/sm"
|
||
|
|
"github.com/stephenafamo/bob/dialect/psql/um"
|
||
|
|
"github.com/stephenafamo/bob/expr"
|
||
|
|
"github.com/stephenafamo/bob/mods"
|
||
|
|
"github.com/stephenafamo/bob/orm"
|
||
|
|
"github.com/stephenafamo/bob/types/pgtypes"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Organization is an object representing the database table.
|
||
|
|
type Organization struct {
|
||
|
|
ID int32 `db:"id,pk" `
|
||
|
|
Name null.Val[string] `db:"name" `
|
||
|
|
|
||
|
|
R organizationR `db:"-" `
|
||
|
|
}
|
||
|
|
|
||
|
|
// OrganizationSlice is an alias for a slice of pointers to Organization.
|
||
|
|
// This should almost always be used instead of []*Organization.
|
||
|
|
type OrganizationSlice []*Organization
|
||
|
|
|
||
|
|
// Organizations contains methods to work with the organization table
|
||
|
|
var Organizations = psql.NewTablex[*Organization, OrganizationSlice, *OrganizationSetter]("", "organization", buildOrganizationColumns("organization"))
|
||
|
|
|
||
|
|
// OrganizationsQuery is a query on the organization table
|
||
|
|
type OrganizationsQuery = *psql.ViewQuery[*Organization, OrganizationSlice]
|
||
|
|
|
||
|
|
// organizationR is where relationships are stored.
|
||
|
|
type organizationR struct {
|
||
|
|
User UserSlice // user_.user__organization_id_fkey
|
||
|
|
}
|
||
|
|
|
||
|
|
func buildOrganizationColumns(alias string) organizationColumns {
|
||
|
|
return organizationColumns{
|
||
|
|
ColumnsExpr: expr.NewColumnsExpr(
|
||
|
|
"id", "name",
|
||
|
|
).WithParent("organization"),
|
||
|
|
tableAlias: alias,
|
||
|
|
ID: psql.Quote(alias, "id"),
|
||
|
|
Name: psql.Quote(alias, "name"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type organizationColumns struct {
|
||
|
|
expr.ColumnsExpr
|
||
|
|
tableAlias string
|
||
|
|
ID psql.Expression
|
||
|
|
Name psql.Expression
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c organizationColumns) Alias() string {
|
||
|
|
return c.tableAlias
|
||
|
|
}
|
||
|
|
|
||
|
|
func (organizationColumns) AliasedAs(alias string) organizationColumns {
|
||
|
|
return buildOrganizationColumns(alias)
|
||
|
|
}
|
||
|
|
|
||
|
|
// OrganizationSetter is used for insert/upsert/update operations
|
||
|
|
// All values are optional, and do not have to be set
|
||
|
|
// Generated columns are not included
|
||
|
|
type OrganizationSetter struct {
|
||
|
|
ID omit.Val[int32] `db:"id,pk" `
|
||
|
|
Name omitnull.Val[string] `db:"name" `
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s OrganizationSetter) SetColumns() []string {
|
||
|
|
vals := make([]string, 0, 2)
|
||
|
|
if s.ID.IsValue() {
|
||
|
|
vals = append(vals, "id")
|
||
|
|
}
|
||
|
|
if !s.Name.IsUnset() {
|
||
|
|
vals = append(vals, "name")
|
||
|
|
}
|
||
|
|
return vals
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s OrganizationSetter) Overwrite(t *Organization) {
|
||
|
|
if s.ID.IsValue() {
|
||
|
|
t.ID = s.ID.MustGet()
|
||
|
|
}
|
||
|
|
if !s.Name.IsUnset() {
|
||
|
|
t.Name = s.Name.MustGetNull()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *OrganizationSetter) Apply(q *dialect.InsertQuery) {
|
||
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||
|
|
return Organizations.BeforeInsertHooks.RunHooks(ctx, exec, s)
|
||
|
|
})
|
||
|
|
|
||
|
|
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||
|
|
vals := make([]bob.Expression, 2)
|
||
|
|
if s.ID.IsValue() {
|
||
|
|
vals[0] = psql.Arg(s.ID.MustGet())
|
||
|
|
} else {
|
||
|
|
vals[0] = psql.Raw("DEFAULT")
|
||
|
|
}
|
||
|
|
|
||
|
|
if !s.Name.IsUnset() {
|
||
|
|
vals[1] = psql.Arg(s.Name.MustGetNull())
|
||
|
|
} else {
|
||
|
|
vals[1] = psql.Raw("DEFAULT")
|
||
|
|
}
|
||
|
|
|
||
|
|
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s OrganizationSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||
|
|
return um.Set(s.Expressions()...)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s OrganizationSetter) Expressions(prefix ...string) []bob.Expression {
|
||
|
|
exprs := make([]bob.Expression, 0, 2)
|
||
|
|
|
||
|
|
if s.ID.IsValue() {
|
||
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||
|
|
psql.Quote(append(prefix, "id")...),
|
||
|
|
psql.Arg(s.ID),
|
||
|
|
}})
|
||
|
|
}
|
||
|
|
|
||
|
|
if !s.Name.IsUnset() {
|
||
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||
|
|
psql.Quote(append(prefix, "name")...),
|
||
|
|
psql.Arg(s.Name),
|
||
|
|
}})
|
||
|
|
}
|
||
|
|
|
||
|
|
return exprs
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindOrganization retrieves a single record by primary key
|
||
|
|
// If cols is empty Find will return all columns.
|
||
|
|
func FindOrganization(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*Organization, error) {
|
||
|
|
if len(cols) == 0 {
|
||
|
|
return Organizations.Query(
|
||
|
|
sm.Where(Organizations.Columns.ID.EQ(psql.Arg(IDPK))),
|
||
|
|
).One(ctx, exec)
|
||
|
|
}
|
||
|
|
|
||
|
|
return Organizations.Query(
|
||
|
|
sm.Where(Organizations.Columns.ID.EQ(psql.Arg(IDPK))),
|
||
|
|
sm.Columns(Organizations.Columns.Only(cols...)),
|
||
|
|
).One(ctx, exec)
|
||
|
|
}
|
||
|
|
|
||
|
|
// OrganizationExists checks the presence of a single record by primary key
|
||
|
|
func OrganizationExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
|
||
|
|
return Organizations.Query(
|
||
|
|
sm.Where(Organizations.Columns.ID.EQ(psql.Arg(IDPK))),
|
||
|
|
).Exists(ctx, exec)
|
||
|
|
}
|
||
|
|
|
||
|
|
// AfterQueryHook is called after Organization is retrieved from the database
|
||
|
|
func (o *Organization) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||
|
|
var err error
|
||
|
|
|
||
|
|
switch queryType {
|
||
|
|
case bob.QueryTypeSelect:
|
||
|
|
ctx, err = Organizations.AfterSelectHooks.RunHooks(ctx, exec, OrganizationSlice{o})
|
||
|
|
case bob.QueryTypeInsert:
|
||
|
|
ctx, err = Organizations.AfterInsertHooks.RunHooks(ctx, exec, OrganizationSlice{o})
|
||
|
|
case bob.QueryTypeUpdate:
|
||
|
|
ctx, err = Organizations.AfterUpdateHooks.RunHooks(ctx, exec, OrganizationSlice{o})
|
||
|
|
case bob.QueryTypeDelete:
|
||
|
|
ctx, err = Organizations.AfterDeleteHooks.RunHooks(ctx, exec, OrganizationSlice{o})
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// primaryKeyVals returns the primary key values of the Organization
|
||
|
|
func (o *Organization) primaryKeyVals() bob.Expression {
|
||
|
|
return psql.Arg(o.ID)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o *Organization) pkEQ() dialect.Expression {
|
||
|
|
return psql.Quote("organization", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
||
|
|
return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update uses an executor to update the Organization
|
||
|
|
func (o *Organization) Update(ctx context.Context, exec bob.Executor, s *OrganizationSetter) error {
|
||
|
|
v, err := Organizations.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 Organization record with an executor
|
||
|
|
func (o *Organization) Delete(ctx context.Context, exec bob.Executor) error {
|
||
|
|
_, err := Organizations.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Reload refreshes the Organization using the executor
|
||
|
|
func (o *Organization) Reload(ctx context.Context, exec bob.Executor) error {
|
||
|
|
o2, err := Organizations.Query(
|
||
|
|
sm.Where(Organizations.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 OrganizationSlice is retrieved from the database
|
||
|
|
func (o OrganizationSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||
|
|
var err error
|
||
|
|
|
||
|
|
switch queryType {
|
||
|
|
case bob.QueryTypeSelect:
|
||
|
|
ctx, err = Organizations.AfterSelectHooks.RunHooks(ctx, exec, o)
|
||
|
|
case bob.QueryTypeInsert:
|
||
|
|
ctx, err = Organizations.AfterInsertHooks.RunHooks(ctx, exec, o)
|
||
|
|
case bob.QueryTypeUpdate:
|
||
|
|
ctx, err = Organizations.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||
|
|
case bob.QueryTypeDelete:
|
||
|
|
ctx, err = Organizations.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o OrganizationSlice) pkIN() dialect.Expression {
|
||
|
|
if len(o) == 0 {
|
||
|
|
return psql.Raw("NULL")
|
||
|
|
}
|
||
|
|
|
||
|
|
return psql.Quote("organization", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, 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 OrganizationSlice) copyMatchingRows(from ...*Organization) {
|
||
|
|
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 OrganizationSlice) 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 Organizations.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 *Organization:
|
||
|
|
o.copyMatchingRows(retrieved)
|
||
|
|
case []*Organization:
|
||
|
|
o.copyMatchingRows(retrieved...)
|
||
|
|
case OrganizationSlice:
|
||
|
|
o.copyMatchingRows(retrieved...)
|
||
|
|
default:
|
||
|
|
// If the retrieved value is not a Organization or a slice of Organization
|
||
|
|
// then run the AfterUpdateHooks on the slice
|
||
|
|
_, err = Organizations.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}))
|
||
|
|
|
||
|
|
q.AppendWhere(o.pkIN())
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
||
|
|
func (o OrganizationSlice) 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 Organizations.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 *Organization:
|
||
|
|
o.copyMatchingRows(retrieved)
|
||
|
|
case []*Organization:
|
||
|
|
o.copyMatchingRows(retrieved...)
|
||
|
|
case OrganizationSlice:
|
||
|
|
o.copyMatchingRows(retrieved...)
|
||
|
|
default:
|
||
|
|
// If the retrieved value is not a Organization or a slice of Organization
|
||
|
|
// then run the AfterDeleteHooks on the slice
|
||
|
|
_, err = Organizations.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}))
|
||
|
|
|
||
|
|
q.AppendWhere(o.pkIN())
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o OrganizationSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals OrganizationSetter) error {
|
||
|
|
if len(o) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
_, err := Organizations.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o OrganizationSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
||
|
|
if len(o) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
_, err := Organizations.Delete(o.DeleteMod()).Exec(ctx, exec)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o OrganizationSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
||
|
|
if len(o) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
o2, err := Organizations.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
o.copyMatchingRows(o2...)
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// User starts a query for related objects on user_
|
||
|
|
func (o *Organization) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||
|
|
return Users.Query(append(mods,
|
||
|
|
sm.Where(Users.Columns.OrganizationID.EQ(psql.Arg(o.ID))),
|
||
|
|
)...)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (os OrganizationSlice) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||
|
|
pkID := make(pgtypes.Array[int32], 0, len(os))
|
||
|
|
for _, o := range os {
|
||
|
|
if o == nil {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
pkID = append(pkID, o.ID)
|
||
|
|
}
|
||
|
|
PKArgExpr := psql.Select(sm.Columns(
|
||
|
|
psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
|
||
|
|
))
|
||
|
|
|
||
|
|
return Users.Query(append(mods,
|
||
|
|
sm.Where(psql.Group(Users.Columns.OrganizationID).OP("IN", PKArgExpr)),
|
||
|
|
)...)
|
||
|
|
}
|
||
|
|
|
||
|
|
func insertOrganizationUser0(ctx context.Context, exec bob.Executor, users1 []*UserSetter, organization0 *Organization) (UserSlice, error) {
|
||
|
|
for i := range users1 {
|
||
|
|
users1[i].OrganizationID = omitnull.From(organization0.ID)
|
||
|
|
}
|
||
|
|
|
||
|
|
ret, err := Users.Insert(bob.ToMods(users1...)).All(ctx, exec)
|
||
|
|
if err != nil {
|
||
|
|
return ret, fmt.Errorf("insertOrganizationUser0: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return ret, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func attachOrganizationUser0(ctx context.Context, exec bob.Executor, count int, users1 UserSlice, organization0 *Organization) (UserSlice, error) {
|
||
|
|
setter := &UserSetter{
|
||
|
|
OrganizationID: omitnull.From(organization0.ID),
|
||
|
|
}
|
||
|
|
|
||
|
|
err := users1.UpdateAll(ctx, exec, *setter)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("attachOrganizationUser0: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return users1, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (organization0 *Organization) InsertUser(ctx context.Context, exec bob.Executor, related ...*UserSetter) error {
|
||
|
|
if len(related) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
var err error
|
||
|
|
|
||
|
|
users1, err := insertOrganizationUser0(ctx, exec, related, organization0)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
organization0.R.User = append(organization0.R.User, users1...)
|
||
|
|
|
||
|
|
for _, rel := range users1 {
|
||
|
|
rel.R.Organization = organization0
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (organization0 *Organization) AttachUser(ctx context.Context, exec bob.Executor, related ...*User) error {
|
||
|
|
if len(related) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
var err error
|
||
|
|
users1 := UserSlice(related)
|
||
|
|
|
||
|
|
_, err = attachOrganizationUser0(ctx, exec, len(related), users1, organization0)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
organization0.R.User = append(organization0.R.User, users1...)
|
||
|
|
|
||
|
|
for _, rel := range related {
|
||
|
|
rel.R.Organization = organization0
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type organizationWhere[Q psql.Filterable] struct {
|
||
|
|
ID psql.WhereMod[Q, int32]
|
||
|
|
Name psql.WhereNullMod[Q, string]
|
||
|
|
}
|
||
|
|
|
||
|
|
func (organizationWhere[Q]) AliasedAs(alias string) organizationWhere[Q] {
|
||
|
|
return buildOrganizationWhere[Q](buildOrganizationColumns(alias))
|
||
|
|
}
|
||
|
|
|
||
|
|
func buildOrganizationWhere[Q psql.Filterable](cols organizationColumns) organizationWhere[Q] {
|
||
|
|
return organizationWhere[Q]{
|
||
|
|
ID: psql.Where[Q, int32](cols.ID),
|
||
|
|
Name: psql.WhereNull[Q, string](cols.Name),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o *Organization) Preload(name string, retrieved any) error {
|
||
|
|
if o == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
switch name {
|
||
|
|
case "User":
|
||
|
|
rels, ok := retrieved.(UserSlice)
|
||
|
|
if !ok {
|
||
|
|
return fmt.Errorf("organization cannot load %T as %q", retrieved, name)
|
||
|
|
}
|
||
|
|
|
||
|
|
o.R.User = rels
|
||
|
|
|
||
|
|
for _, rel := range rels {
|
||
|
|
if rel != nil {
|
||
|
|
rel.R.Organization = o
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
default:
|
||
|
|
return fmt.Errorf("organization has no relationship %q", name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type organizationPreloader struct{}
|
||
|
|
|
||
|
|
func buildOrganizationPreloader() organizationPreloader {
|
||
|
|
return organizationPreloader{}
|
||
|
|
}
|
||
|
|
|
||
|
|
type organizationThenLoader[Q orm.Loadable] struct {
|
||
|
|
User func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
|
||
|
|
}
|
||
|
|
|
||
|
|
func buildOrganizationThenLoader[Q orm.Loadable]() organizationThenLoader[Q] {
|
||
|
|
type UserLoadInterface interface {
|
||
|
|
LoadUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
||
|
|
}
|
||
|
|
|
||
|
|
return organizationThenLoader[Q]{
|
||
|
|
User: thenLoadBuilder[Q](
|
||
|
|
"User",
|
||
|
|
func(ctx context.Context, exec bob.Executor, retrieved UserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||
|
|
return retrieved.LoadUser(ctx, exec, mods...)
|
||
|
|
},
|
||
|
|
),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// LoadUser loads the organization's User into the .R struct
|
||
|
|
func (o *Organization) LoadUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||
|
|
if o == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Reset the relationship
|
||
|
|
o.R.User = nil
|
||
|
|
|
||
|
|
related, err := o.User(mods...).All(ctx, exec)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, rel := range related {
|
||
|
|
rel.R.Organization = o
|
||
|
|
}
|
||
|
|
|
||
|
|
o.R.User = related
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// LoadUser loads the organization's User into the .R struct
|
||
|
|
func (os OrganizationSlice) LoadUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||
|
|
if len(os) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
users, err := os.User(mods...).All(ctx, exec)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, o := range os {
|
||
|
|
if o == nil {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
o.R.User = nil
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, o := range os {
|
||
|
|
if o == nil {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, rel := range users {
|
||
|
|
|
||
|
|
if !rel.OrganizationID.IsValue() {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if !(rel.OrganizationID.IsValue() && o.ID == rel.OrganizationID.MustGet()) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
rel.R.Organization = o
|
||
|
|
|
||
|
|
o.R.User = append(o.R.User, rel)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type organizationJoins[Q dialect.Joinable] struct {
|
||
|
|
typ string
|
||
|
|
User modAs[Q, userColumns]
|
||
|
|
}
|
||
|
|
|
||
|
|
func (j organizationJoins[Q]) aliasedAs(alias string) organizationJoins[Q] {
|
||
|
|
return buildOrganizationJoins[Q](buildOrganizationColumns(alias), j.typ)
|
||
|
|
}
|
||
|
|
|
||
|
|
func buildOrganizationJoins[Q dialect.Joinable](cols organizationColumns, typ string) organizationJoins[Q] {
|
||
|
|
return organizationJoins[Q]{
|
||
|
|
typ: typ,
|
||
|
|
User: modAs[Q, userColumns]{
|
||
|
|
c: Users.Columns,
|
||
|
|
f: func(to userColumns) bob.Mod[Q] {
|
||
|
|
mods := make(mods.QueryMods[Q], 0, 1)
|
||
|
|
|
||
|
|
{
|
||
|
|
mods = append(mods, dialect.Join[Q](typ, Users.Name().As(to.Alias())).On(
|
||
|
|
to.OrganizationID.EQ(cols.ID),
|
||
|
|
))
|
||
|
|
}
|
||
|
|
|
||
|
|
return mods
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|