Create a log for impersonation activities
This commit is contained in:
parent
b2eb98a66c
commit
d287fa44df
7 changed files with 1360 additions and 0 deletions
811
db/models/log_impersonation.bob.go
Normal file
811
db/models/log_impersonation.bob.go
Normal file
|
|
@ -0,0 +1,811 @@
|
|||
// 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/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
)
|
||||
|
||||
// LogImpersonation is an object representing the database table.
|
||||
type LogImpersonation struct {
|
||||
BeginAt time.Time `db:"begin_at" `
|
||||
EndAt null.Val[time.Time] `db:"end_at" `
|
||||
ID int32 `db:"id,pk" `
|
||||
ImpersonatorID int32 `db:"impersonator_id" `
|
||||
TargetID int32 `db:"target_id" `
|
||||
|
||||
R logImpersonationR `db:"-" `
|
||||
}
|
||||
|
||||
// LogImpersonationSlice is an alias for a slice of pointers to LogImpersonation.
|
||||
// This should almost always be used instead of []*LogImpersonation.
|
||||
type LogImpersonationSlice []*LogImpersonation
|
||||
|
||||
// LogImpersonations contains methods to work with the log_impersonation table
|
||||
var LogImpersonations = psql.NewTablex[*LogImpersonation, LogImpersonationSlice, *LogImpersonationSetter]("", "log_impersonation", buildLogImpersonationColumns("log_impersonation"))
|
||||
|
||||
// LogImpersonationsQuery is a query on the log_impersonation table
|
||||
type LogImpersonationsQuery = *psql.ViewQuery[*LogImpersonation, LogImpersonationSlice]
|
||||
|
||||
// logImpersonationR is where relationships are stored.
|
||||
type logImpersonationR struct {
|
||||
ImpersonatorUser *User // log_impersonation.log_impersonation_impersonator_id_fkey
|
||||
TargetUser *User // log_impersonation.log_impersonation_target_id_fkey
|
||||
}
|
||||
|
||||
func buildLogImpersonationColumns(alias string) logImpersonationColumns {
|
||||
return logImpersonationColumns{
|
||||
ColumnsExpr: expr.NewColumnsExpr(
|
||||
"begin_at", "end_at", "id", "impersonator_id", "target_id",
|
||||
).WithParent("log_impersonation"),
|
||||
tableAlias: alias,
|
||||
BeginAt: psql.Quote(alias, "begin_at"),
|
||||
EndAt: psql.Quote(alias, "end_at"),
|
||||
ID: psql.Quote(alias, "id"),
|
||||
ImpersonatorID: psql.Quote(alias, "impersonator_id"),
|
||||
TargetID: psql.Quote(alias, "target_id"),
|
||||
}
|
||||
}
|
||||
|
||||
type logImpersonationColumns struct {
|
||||
expr.ColumnsExpr
|
||||
tableAlias string
|
||||
BeginAt psql.Expression
|
||||
EndAt psql.Expression
|
||||
ID psql.Expression
|
||||
ImpersonatorID psql.Expression
|
||||
TargetID psql.Expression
|
||||
}
|
||||
|
||||
func (c logImpersonationColumns) Alias() string {
|
||||
return c.tableAlias
|
||||
}
|
||||
|
||||
func (logImpersonationColumns) AliasedAs(alias string) logImpersonationColumns {
|
||||
return buildLogImpersonationColumns(alias)
|
||||
}
|
||||
|
||||
// LogImpersonationSetter is used for insert/upsert/update operations
|
||||
// All values are optional, and do not have to be set
|
||||
// Generated columns are not included
|
||||
type LogImpersonationSetter struct {
|
||||
BeginAt omit.Val[time.Time] `db:"begin_at" `
|
||||
EndAt omitnull.Val[time.Time] `db:"end_at" `
|
||||
ID omit.Val[int32] `db:"id,pk" `
|
||||
ImpersonatorID omit.Val[int32] `db:"impersonator_id" `
|
||||
TargetID omit.Val[int32] `db:"target_id" `
|
||||
}
|
||||
|
||||
func (s LogImpersonationSetter) SetColumns() []string {
|
||||
vals := make([]string, 0, 5)
|
||||
if s.BeginAt.IsValue() {
|
||||
vals = append(vals, "begin_at")
|
||||
}
|
||||
if !s.EndAt.IsUnset() {
|
||||
vals = append(vals, "end_at")
|
||||
}
|
||||
if s.ID.IsValue() {
|
||||
vals = append(vals, "id")
|
||||
}
|
||||
if s.ImpersonatorID.IsValue() {
|
||||
vals = append(vals, "impersonator_id")
|
||||
}
|
||||
if s.TargetID.IsValue() {
|
||||
vals = append(vals, "target_id")
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
func (s LogImpersonationSetter) Overwrite(t *LogImpersonation) {
|
||||
if s.BeginAt.IsValue() {
|
||||
t.BeginAt = s.BeginAt.MustGet()
|
||||
}
|
||||
if !s.EndAt.IsUnset() {
|
||||
t.EndAt = s.EndAt.MustGetNull()
|
||||
}
|
||||
if s.ID.IsValue() {
|
||||
t.ID = s.ID.MustGet()
|
||||
}
|
||||
if s.ImpersonatorID.IsValue() {
|
||||
t.ImpersonatorID = s.ImpersonatorID.MustGet()
|
||||
}
|
||||
if s.TargetID.IsValue() {
|
||||
t.TargetID = s.TargetID.MustGet()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LogImpersonationSetter) Apply(q *dialect.InsertQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return LogImpersonations.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, 5)
|
||||
if s.BeginAt.IsValue() {
|
||||
vals[0] = psql.Arg(s.BeginAt.MustGet())
|
||||
} else {
|
||||
vals[0] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if !s.EndAt.IsUnset() {
|
||||
vals[1] = psql.Arg(s.EndAt.MustGetNull())
|
||||
} else {
|
||||
vals[1] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if s.ID.IsValue() {
|
||||
vals[2] = psql.Arg(s.ID.MustGet())
|
||||
} else {
|
||||
vals[2] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if s.ImpersonatorID.IsValue() {
|
||||
vals[3] = psql.Arg(s.ImpersonatorID.MustGet())
|
||||
} else {
|
||||
vals[3] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if s.TargetID.IsValue() {
|
||||
vals[4] = psql.Arg(s.TargetID.MustGet())
|
||||
} else {
|
||||
vals[4] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
||||
func (s LogImpersonationSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||
return um.Set(s.Expressions()...)
|
||||
}
|
||||
|
||||
func (s LogImpersonationSetter) Expressions(prefix ...string) []bob.Expression {
|
||||
exprs := make([]bob.Expression, 0, 5)
|
||||
|
||||
if s.BeginAt.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "begin_at")...),
|
||||
psql.Arg(s.BeginAt),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.EndAt.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "end_at")...),
|
||||
psql.Arg(s.EndAt),
|
||||
}})
|
||||
}
|
||||
|
||||
if s.ID.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "id")...),
|
||||
psql.Arg(s.ID),
|
||||
}})
|
||||
}
|
||||
|
||||
if s.ImpersonatorID.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "impersonator_id")...),
|
||||
psql.Arg(s.ImpersonatorID),
|
||||
}})
|
||||
}
|
||||
|
||||
if s.TargetID.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "target_id")...),
|
||||
psql.Arg(s.TargetID),
|
||||
}})
|
||||
}
|
||||
|
||||
return exprs
|
||||
}
|
||||
|
||||
// FindLogImpersonation retrieves a single record by primary key
|
||||
// If cols is empty Find will return all columns.
|
||||
func FindLogImpersonation(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*LogImpersonation, error) {
|
||||
if len(cols) == 0 {
|
||||
return LogImpersonations.Query(
|
||||
sm.Where(LogImpersonations.Columns.ID.EQ(psql.Arg(IDPK))),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
return LogImpersonations.Query(
|
||||
sm.Where(LogImpersonations.Columns.ID.EQ(psql.Arg(IDPK))),
|
||||
sm.Columns(LogImpersonations.Columns.Only(cols...)),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
// LogImpersonationExists checks the presence of a single record by primary key
|
||||
func LogImpersonationExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
|
||||
return LogImpersonations.Query(
|
||||
sm.Where(LogImpersonations.Columns.ID.EQ(psql.Arg(IDPK))),
|
||||
).Exists(ctx, exec)
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after LogImpersonation is retrieved from the database
|
||||
func (o *LogImpersonation) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = LogImpersonations.AfterSelectHooks.RunHooks(ctx, exec, LogImpersonationSlice{o})
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = LogImpersonations.AfterInsertHooks.RunHooks(ctx, exec, LogImpersonationSlice{o})
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = LogImpersonations.AfterUpdateHooks.RunHooks(ctx, exec, LogImpersonationSlice{o})
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = LogImpersonations.AfterDeleteHooks.RunHooks(ctx, exec, LogImpersonationSlice{o})
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// primaryKeyVals returns the primary key values of the LogImpersonation
|
||||
func (o *LogImpersonation) primaryKeyVals() bob.Expression {
|
||||
return psql.Arg(o.ID)
|
||||
}
|
||||
|
||||
func (o *LogImpersonation) pkEQ() dialect.Expression {
|
||||
return psql.Quote("log_impersonation", "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 LogImpersonation
|
||||
func (o *LogImpersonation) Update(ctx context.Context, exec bob.Executor, s *LogImpersonationSetter) error {
|
||||
v, err := LogImpersonations.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 LogImpersonation record with an executor
|
||||
func (o *LogImpersonation) Delete(ctx context.Context, exec bob.Executor) error {
|
||||
_, err := LogImpersonations.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
// Reload refreshes the LogImpersonation using the executor
|
||||
func (o *LogImpersonation) Reload(ctx context.Context, exec bob.Executor) error {
|
||||
o2, err := LogImpersonations.Query(
|
||||
sm.Where(LogImpersonations.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 LogImpersonationSlice is retrieved from the database
|
||||
func (o LogImpersonationSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = LogImpersonations.AfterSelectHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = LogImpersonations.AfterInsertHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = LogImpersonations.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = LogImpersonations.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (o LogImpersonationSlice) pkIN() dialect.Expression {
|
||||
if len(o) == 0 {
|
||||
return psql.Raw("NULL")
|
||||
}
|
||||
|
||||
return psql.Quote("log_impersonation", "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 LogImpersonationSlice) copyMatchingRows(from ...*LogImpersonation) {
|
||||
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 LogImpersonationSlice) 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 LogImpersonations.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 *LogImpersonation:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*LogImpersonation:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case LogImpersonationSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a LogImpersonation or a slice of LogImpersonation
|
||||
// then run the AfterUpdateHooks on the slice
|
||||
_, err = LogImpersonations.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
||||
func (o LogImpersonationSlice) 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 LogImpersonations.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 *LogImpersonation:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*LogImpersonation:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case LogImpersonationSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a LogImpersonation or a slice of LogImpersonation
|
||||
// then run the AfterDeleteHooks on the slice
|
||||
_, err = LogImpersonations.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
func (o LogImpersonationSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals LogImpersonationSetter) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := LogImpersonations.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o LogImpersonationSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := LogImpersonations.Delete(o.DeleteMod()).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o LogImpersonationSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
o2, err := LogImpersonations.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.copyMatchingRows(o2...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ImpersonatorUser starts a query for related objects on user_
|
||||
func (o *LogImpersonation) ImpersonatorUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||||
return Users.Query(append(mods,
|
||||
sm.Where(Users.Columns.ID.EQ(psql.Arg(o.ImpersonatorID))),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (os LogImpersonationSlice) ImpersonatorUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||||
pkImpersonatorID := make(pgtypes.Array[int32], 0, len(os))
|
||||
for _, o := range os {
|
||||
if o == nil {
|
||||
continue
|
||||
}
|
||||
pkImpersonatorID = append(pkImpersonatorID, o.ImpersonatorID)
|
||||
}
|
||||
PKArgExpr := psql.Select(sm.Columns(
|
||||
psql.F("unnest", psql.Cast(psql.Arg(pkImpersonatorID), "integer[]")),
|
||||
))
|
||||
|
||||
return Users.Query(append(mods,
|
||||
sm.Where(psql.Group(Users.Columns.ID).OP("IN", PKArgExpr)),
|
||||
)...)
|
||||
}
|
||||
|
||||
// TargetUser starts a query for related objects on user_
|
||||
func (o *LogImpersonation) TargetUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||||
return Users.Query(append(mods,
|
||||
sm.Where(Users.Columns.ID.EQ(psql.Arg(o.TargetID))),
|
||||
)...)
|
||||
}
|
||||
|
||||
func (os LogImpersonationSlice) TargetUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
||||
pkTargetID := make(pgtypes.Array[int32], 0, len(os))
|
||||
for _, o := range os {
|
||||
if o == nil {
|
||||
continue
|
||||
}
|
||||
pkTargetID = append(pkTargetID, o.TargetID)
|
||||
}
|
||||
PKArgExpr := psql.Select(sm.Columns(
|
||||
psql.F("unnest", psql.Cast(psql.Arg(pkTargetID), "integer[]")),
|
||||
))
|
||||
|
||||
return Users.Query(append(mods,
|
||||
sm.Where(psql.Group(Users.Columns.ID).OP("IN", PKArgExpr)),
|
||||
)...)
|
||||
}
|
||||
|
||||
func attachLogImpersonationImpersonatorUser0(ctx context.Context, exec bob.Executor, count int, logImpersonation0 *LogImpersonation, user1 *User) (*LogImpersonation, error) {
|
||||
setter := &LogImpersonationSetter{
|
||||
ImpersonatorID: omit.From(user1.ID),
|
||||
}
|
||||
|
||||
err := logImpersonation0.Update(ctx, exec, setter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("attachLogImpersonationImpersonatorUser0: %w", err)
|
||||
}
|
||||
|
||||
return logImpersonation0, nil
|
||||
}
|
||||
|
||||
func (logImpersonation0 *LogImpersonation) InsertImpersonatorUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
|
||||
var err error
|
||||
|
||||
user1, err := Users.Insert(related).One(ctx, exec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("inserting related objects: %w", err)
|
||||
}
|
||||
|
||||
_, err = attachLogImpersonationImpersonatorUser0(ctx, exec, 1, logImpersonation0, user1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logImpersonation0.R.ImpersonatorUser = user1
|
||||
|
||||
user1.R.ImpersonatorLogImpersonations = append(user1.R.ImpersonatorLogImpersonations, logImpersonation0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (logImpersonation0 *LogImpersonation) AttachImpersonatorUser(ctx context.Context, exec bob.Executor, user1 *User) error {
|
||||
var err error
|
||||
|
||||
_, err = attachLogImpersonationImpersonatorUser0(ctx, exec, 1, logImpersonation0, user1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logImpersonation0.R.ImpersonatorUser = user1
|
||||
|
||||
user1.R.ImpersonatorLogImpersonations = append(user1.R.ImpersonatorLogImpersonations, logImpersonation0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func attachLogImpersonationTargetUser0(ctx context.Context, exec bob.Executor, count int, logImpersonation0 *LogImpersonation, user1 *User) (*LogImpersonation, error) {
|
||||
setter := &LogImpersonationSetter{
|
||||
TargetID: omit.From(user1.ID),
|
||||
}
|
||||
|
||||
err := logImpersonation0.Update(ctx, exec, setter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("attachLogImpersonationTargetUser0: %w", err)
|
||||
}
|
||||
|
||||
return logImpersonation0, nil
|
||||
}
|
||||
|
||||
func (logImpersonation0 *LogImpersonation) InsertTargetUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
|
||||
var err error
|
||||
|
||||
user1, err := Users.Insert(related).One(ctx, exec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("inserting related objects: %w", err)
|
||||
}
|
||||
|
||||
_, err = attachLogImpersonationTargetUser0(ctx, exec, 1, logImpersonation0, user1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logImpersonation0.R.TargetUser = user1
|
||||
|
||||
user1.R.TargetLogImpersonations = append(user1.R.TargetLogImpersonations, logImpersonation0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (logImpersonation0 *LogImpersonation) AttachTargetUser(ctx context.Context, exec bob.Executor, user1 *User) error {
|
||||
var err error
|
||||
|
||||
_, err = attachLogImpersonationTargetUser0(ctx, exec, 1, logImpersonation0, user1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logImpersonation0.R.TargetUser = user1
|
||||
|
||||
user1.R.TargetLogImpersonations = append(user1.R.TargetLogImpersonations, logImpersonation0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type logImpersonationWhere[Q psql.Filterable] struct {
|
||||
BeginAt psql.WhereMod[Q, time.Time]
|
||||
EndAt psql.WhereNullMod[Q, time.Time]
|
||||
ID psql.WhereMod[Q, int32]
|
||||
ImpersonatorID psql.WhereMod[Q, int32]
|
||||
TargetID psql.WhereMod[Q, int32]
|
||||
}
|
||||
|
||||
func (logImpersonationWhere[Q]) AliasedAs(alias string) logImpersonationWhere[Q] {
|
||||
return buildLogImpersonationWhere[Q](buildLogImpersonationColumns(alias))
|
||||
}
|
||||
|
||||
func buildLogImpersonationWhere[Q psql.Filterable](cols logImpersonationColumns) logImpersonationWhere[Q] {
|
||||
return logImpersonationWhere[Q]{
|
||||
BeginAt: psql.Where[Q, time.Time](cols.BeginAt),
|
||||
EndAt: psql.WhereNull[Q, time.Time](cols.EndAt),
|
||||
ID: psql.Where[Q, int32](cols.ID),
|
||||
ImpersonatorID: psql.Where[Q, int32](cols.ImpersonatorID),
|
||||
TargetID: psql.Where[Q, int32](cols.TargetID),
|
||||
}
|
||||
}
|
||||
|
||||
func (o *LogImpersonation) Preload(name string, retrieved any) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch name {
|
||||
case "ImpersonatorUser":
|
||||
rel, ok := retrieved.(*User)
|
||||
if !ok {
|
||||
return fmt.Errorf("logImpersonation cannot load %T as %q", retrieved, name)
|
||||
}
|
||||
|
||||
o.R.ImpersonatorUser = rel
|
||||
|
||||
if rel != nil {
|
||||
rel.R.ImpersonatorLogImpersonations = LogImpersonationSlice{o}
|
||||
}
|
||||
return nil
|
||||
case "TargetUser":
|
||||
rel, ok := retrieved.(*User)
|
||||
if !ok {
|
||||
return fmt.Errorf("logImpersonation cannot load %T as %q", retrieved, name)
|
||||
}
|
||||
|
||||
o.R.TargetUser = rel
|
||||
|
||||
if rel != nil {
|
||||
rel.R.TargetLogImpersonations = LogImpersonationSlice{o}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("logImpersonation has no relationship %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
type logImpersonationPreloader struct {
|
||||
ImpersonatorUser func(...psql.PreloadOption) psql.Preloader
|
||||
TargetUser func(...psql.PreloadOption) psql.Preloader
|
||||
}
|
||||
|
||||
func buildLogImpersonationPreloader() logImpersonationPreloader {
|
||||
return logImpersonationPreloader{
|
||||
ImpersonatorUser: func(opts ...psql.PreloadOption) psql.Preloader {
|
||||
return psql.Preload[*User, UserSlice](psql.PreloadRel{
|
||||
Name: "ImpersonatorUser",
|
||||
Sides: []psql.PreloadSide{
|
||||
{
|
||||
From: LogImpersonations,
|
||||
To: Users,
|
||||
FromColumns: []string{"impersonator_id"},
|
||||
ToColumns: []string{"id"},
|
||||
},
|
||||
},
|
||||
}, Users.Columns.Names(), opts...)
|
||||
},
|
||||
TargetUser: func(opts ...psql.PreloadOption) psql.Preloader {
|
||||
return psql.Preload[*User, UserSlice](psql.PreloadRel{
|
||||
Name: "TargetUser",
|
||||
Sides: []psql.PreloadSide{
|
||||
{
|
||||
From: LogImpersonations,
|
||||
To: Users,
|
||||
FromColumns: []string{"target_id"},
|
||||
ToColumns: []string{"id"},
|
||||
},
|
||||
},
|
||||
}, Users.Columns.Names(), opts...)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type logImpersonationThenLoader[Q orm.Loadable] struct {
|
||||
ImpersonatorUser func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
|
||||
TargetUser func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
|
||||
}
|
||||
|
||||
func buildLogImpersonationThenLoader[Q orm.Loadable]() logImpersonationThenLoader[Q] {
|
||||
type ImpersonatorUserLoadInterface interface {
|
||||
LoadImpersonatorUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
||||
}
|
||||
type TargetUserLoadInterface interface {
|
||||
LoadTargetUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
||||
}
|
||||
|
||||
return logImpersonationThenLoader[Q]{
|
||||
ImpersonatorUser: thenLoadBuilder[Q](
|
||||
"ImpersonatorUser",
|
||||
func(ctx context.Context, exec bob.Executor, retrieved ImpersonatorUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
return retrieved.LoadImpersonatorUser(ctx, exec, mods...)
|
||||
},
|
||||
),
|
||||
TargetUser: thenLoadBuilder[Q](
|
||||
"TargetUser",
|
||||
func(ctx context.Context, exec bob.Executor, retrieved TargetUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
return retrieved.LoadTargetUser(ctx, exec, mods...)
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// LoadImpersonatorUser loads the logImpersonation's ImpersonatorUser into the .R struct
|
||||
func (o *LogImpersonation) LoadImpersonatorUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset the relationship
|
||||
o.R.ImpersonatorUser = nil
|
||||
|
||||
related, err := o.ImpersonatorUser(mods...).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
related.R.ImpersonatorLogImpersonations = LogImpersonationSlice{o}
|
||||
|
||||
o.R.ImpersonatorUser = related
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadImpersonatorUser loads the logImpersonation's ImpersonatorUser into the .R struct
|
||||
func (os LogImpersonationSlice) LoadImpersonatorUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if len(os) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
users, err := os.ImpersonatorUser(mods...).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, o := range os {
|
||||
if o == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, rel := range users {
|
||||
|
||||
if !(o.ImpersonatorID == rel.ID) {
|
||||
continue
|
||||
}
|
||||
|
||||
rel.R.ImpersonatorLogImpersonations = append(rel.R.ImpersonatorLogImpersonations, o)
|
||||
|
||||
o.R.ImpersonatorUser = rel
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadTargetUser loads the logImpersonation's TargetUser into the .R struct
|
||||
func (o *LogImpersonation) LoadTargetUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset the relationship
|
||||
o.R.TargetUser = nil
|
||||
|
||||
related, err := o.TargetUser(mods...).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
related.R.TargetLogImpersonations = LogImpersonationSlice{o}
|
||||
|
||||
o.R.TargetUser = related
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadTargetUser loads the logImpersonation's TargetUser into the .R struct
|
||||
func (os LogImpersonationSlice) LoadTargetUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
||||
if len(os) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
users, err := os.TargetUser(mods...).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, o := range os {
|
||||
if o == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, rel := range users {
|
||||
|
||||
if !(o.TargetID == rel.ID) {
|
||||
continue
|
||||
}
|
||||
|
||||
rel.R.TargetLogImpersonations = append(rel.R.TargetLogImpersonations, o)
|
||||
|
||||
o.R.TargetUser = rel
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue