859 lines
25 KiB
Go
859 lines
25 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"
|
|
"time"
|
|
|
|
enums "github.com/Gleipnir-Technology/nidus-sync/enums"
|
|
"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"
|
|
)
|
|
|
|
// User is an object representing the database table.
|
|
type User struct {
|
|
ID int32 `db:"id,pk" `
|
|
ArcgisAccessToken null.Val[string] `db:"arcgis_access_token" `
|
|
ArcgisLicense null.Val[enums.ArcgisLicenseType] `db:"arcgis_license" `
|
|
ArcgisRefreshToken null.Val[string] `db:"arcgis_refresh_token" `
|
|
ArcgisRefreshTokenExpires null.Val[time.Time] `db:"arcgis_refresh_token_expires" `
|
|
ArcgisRole null.Val[string] `db:"arcgis_role" `
|
|
DisplayName null.Val[string] `db:"display_name" `
|
|
Email null.Val[string] `db:"email" `
|
|
OrganizationID null.Val[int32] `db:"organization_id" `
|
|
Username string `db:"username" `
|
|
PasswordHashType null.Val[enums.Hashtype] `db:"password_hash_type" `
|
|
PasswordHash null.Val[string] `db:"password_hash" `
|
|
|
|
R userR `db:"-" `
|
|
}
|
|
|
|
// UserSlice is an alias for a slice of pointers to User.
|
|
// This should almost always be used instead of []*User.
|
|
type UserSlice []*User
|
|
|
|
// Users contains methods to work with the user_ table
|
|
var Users = psql.NewTablex[*User, UserSlice, *UserSetter]("", "user_", buildUserColumns("user_"))
|
|
|
|
// UsersQuery is a query on the user_ table
|
|
type UsersQuery = *psql.ViewQuery[*User, UserSlice]
|
|
|
|
// userR is where relationships are stored.
|
|
type userR struct {
|
|
Organization *Organization // user_.user__organization_id_fkey
|
|
}
|
|
|
|
func buildUserColumns(alias string) userColumns {
|
|
return userColumns{
|
|
ColumnsExpr: expr.NewColumnsExpr(
|
|
"id", "arcgis_access_token", "arcgis_license", "arcgis_refresh_token", "arcgis_refresh_token_expires", "arcgis_role", "display_name", "email", "organization_id", "username", "password_hash_type", "password_hash",
|
|
).WithParent("user_"),
|
|
tableAlias: alias,
|
|
ID: psql.Quote(alias, "id"),
|
|
ArcgisAccessToken: psql.Quote(alias, "arcgis_access_token"),
|
|
ArcgisLicense: psql.Quote(alias, "arcgis_license"),
|
|
ArcgisRefreshToken: psql.Quote(alias, "arcgis_refresh_token"),
|
|
ArcgisRefreshTokenExpires: psql.Quote(alias, "arcgis_refresh_token_expires"),
|
|
ArcgisRole: psql.Quote(alias, "arcgis_role"),
|
|
DisplayName: psql.Quote(alias, "display_name"),
|
|
Email: psql.Quote(alias, "email"),
|
|
OrganizationID: psql.Quote(alias, "organization_id"),
|
|
Username: psql.Quote(alias, "username"),
|
|
PasswordHashType: psql.Quote(alias, "password_hash_type"),
|
|
PasswordHash: psql.Quote(alias, "password_hash"),
|
|
}
|
|
}
|
|
|
|
type userColumns struct {
|
|
expr.ColumnsExpr
|
|
tableAlias string
|
|
ID psql.Expression
|
|
ArcgisAccessToken psql.Expression
|
|
ArcgisLicense psql.Expression
|
|
ArcgisRefreshToken psql.Expression
|
|
ArcgisRefreshTokenExpires psql.Expression
|
|
ArcgisRole psql.Expression
|
|
DisplayName psql.Expression
|
|
Email psql.Expression
|
|
OrganizationID psql.Expression
|
|
Username psql.Expression
|
|
PasswordHashType psql.Expression
|
|
PasswordHash psql.Expression
|
|
}
|
|
|
|
func (c userColumns) Alias() string {
|
|
return c.tableAlias
|
|
}
|
|
|
|
func (userColumns) AliasedAs(alias string) userColumns {
|
|
return buildUserColumns(alias)
|
|
}
|
|
|
|
// UserSetter is used for insert/upsert/update operations
|
|
// All values are optional, and do not have to be set
|
|
// Generated columns are not included
|
|
type UserSetter struct {
|
|
ID omit.Val[int32] `db:"id,pk" `
|
|
ArcgisAccessToken omitnull.Val[string] `db:"arcgis_access_token" `
|
|
ArcgisLicense omitnull.Val[enums.ArcgisLicenseType] `db:"arcgis_license" `
|
|
ArcgisRefreshToken omitnull.Val[string] `db:"arcgis_refresh_token" `
|
|
ArcgisRefreshTokenExpires omitnull.Val[time.Time] `db:"arcgis_refresh_token_expires" `
|
|
ArcgisRole omitnull.Val[string] `db:"arcgis_role" `
|
|
DisplayName omitnull.Val[string] `db:"display_name" `
|
|
Email omitnull.Val[string] `db:"email" `
|
|
OrganizationID omitnull.Val[int32] `db:"organization_id" `
|
|
Username omit.Val[string] `db:"username" `
|
|
PasswordHashType omitnull.Val[enums.Hashtype] `db:"password_hash_type" `
|
|
PasswordHash omitnull.Val[string] `db:"password_hash" `
|
|
}
|
|
|
|
func (s UserSetter) SetColumns() []string {
|
|
vals := make([]string, 0, 12)
|
|
if s.ID.IsValue() {
|
|
vals = append(vals, "id")
|
|
}
|
|
if !s.ArcgisAccessToken.IsUnset() {
|
|
vals = append(vals, "arcgis_access_token")
|
|
}
|
|
if !s.ArcgisLicense.IsUnset() {
|
|
vals = append(vals, "arcgis_license")
|
|
}
|
|
if !s.ArcgisRefreshToken.IsUnset() {
|
|
vals = append(vals, "arcgis_refresh_token")
|
|
}
|
|
if !s.ArcgisRefreshTokenExpires.IsUnset() {
|
|
vals = append(vals, "arcgis_refresh_token_expires")
|
|
}
|
|
if !s.ArcgisRole.IsUnset() {
|
|
vals = append(vals, "arcgis_role")
|
|
}
|
|
if !s.DisplayName.IsUnset() {
|
|
vals = append(vals, "display_name")
|
|
}
|
|
if !s.Email.IsUnset() {
|
|
vals = append(vals, "email")
|
|
}
|
|
if !s.OrganizationID.IsUnset() {
|
|
vals = append(vals, "organization_id")
|
|
}
|
|
if s.Username.IsValue() {
|
|
vals = append(vals, "username")
|
|
}
|
|
if !s.PasswordHashType.IsUnset() {
|
|
vals = append(vals, "password_hash_type")
|
|
}
|
|
if !s.PasswordHash.IsUnset() {
|
|
vals = append(vals, "password_hash")
|
|
}
|
|
return vals
|
|
}
|
|
|
|
func (s UserSetter) Overwrite(t *User) {
|
|
if s.ID.IsValue() {
|
|
t.ID = s.ID.MustGet()
|
|
}
|
|
if !s.ArcgisAccessToken.IsUnset() {
|
|
t.ArcgisAccessToken = s.ArcgisAccessToken.MustGetNull()
|
|
}
|
|
if !s.ArcgisLicense.IsUnset() {
|
|
t.ArcgisLicense = s.ArcgisLicense.MustGetNull()
|
|
}
|
|
if !s.ArcgisRefreshToken.IsUnset() {
|
|
t.ArcgisRefreshToken = s.ArcgisRefreshToken.MustGetNull()
|
|
}
|
|
if !s.ArcgisRefreshTokenExpires.IsUnset() {
|
|
t.ArcgisRefreshTokenExpires = s.ArcgisRefreshTokenExpires.MustGetNull()
|
|
}
|
|
if !s.ArcgisRole.IsUnset() {
|
|
t.ArcgisRole = s.ArcgisRole.MustGetNull()
|
|
}
|
|
if !s.DisplayName.IsUnset() {
|
|
t.DisplayName = s.DisplayName.MustGetNull()
|
|
}
|
|
if !s.Email.IsUnset() {
|
|
t.Email = s.Email.MustGetNull()
|
|
}
|
|
if !s.OrganizationID.IsUnset() {
|
|
t.OrganizationID = s.OrganizationID.MustGetNull()
|
|
}
|
|
if s.Username.IsValue() {
|
|
t.Username = s.Username.MustGet()
|
|
}
|
|
if !s.PasswordHashType.IsUnset() {
|
|
t.PasswordHashType = s.PasswordHashType.MustGetNull()
|
|
}
|
|
if !s.PasswordHash.IsUnset() {
|
|
t.PasswordHash = s.PasswordHash.MustGetNull()
|
|
}
|
|
}
|
|
|
|
func (s *UserSetter) Apply(q *dialect.InsertQuery) {
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
|
return Users.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, 12)
|
|
if s.ID.IsValue() {
|
|
vals[0] = psql.Arg(s.ID.MustGet())
|
|
} else {
|
|
vals[0] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.ArcgisAccessToken.IsUnset() {
|
|
vals[1] = psql.Arg(s.ArcgisAccessToken.MustGetNull())
|
|
} else {
|
|
vals[1] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.ArcgisLicense.IsUnset() {
|
|
vals[2] = psql.Arg(s.ArcgisLicense.MustGetNull())
|
|
} else {
|
|
vals[2] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.ArcgisRefreshToken.IsUnset() {
|
|
vals[3] = psql.Arg(s.ArcgisRefreshToken.MustGetNull())
|
|
} else {
|
|
vals[3] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.ArcgisRefreshTokenExpires.IsUnset() {
|
|
vals[4] = psql.Arg(s.ArcgisRefreshTokenExpires.MustGetNull())
|
|
} else {
|
|
vals[4] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.ArcgisRole.IsUnset() {
|
|
vals[5] = psql.Arg(s.ArcgisRole.MustGetNull())
|
|
} else {
|
|
vals[5] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.DisplayName.IsUnset() {
|
|
vals[6] = psql.Arg(s.DisplayName.MustGetNull())
|
|
} else {
|
|
vals[6] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.Email.IsUnset() {
|
|
vals[7] = psql.Arg(s.Email.MustGetNull())
|
|
} else {
|
|
vals[7] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.OrganizationID.IsUnset() {
|
|
vals[8] = psql.Arg(s.OrganizationID.MustGetNull())
|
|
} else {
|
|
vals[8] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if s.Username.IsValue() {
|
|
vals[9] = psql.Arg(s.Username.MustGet())
|
|
} else {
|
|
vals[9] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.PasswordHashType.IsUnset() {
|
|
vals[10] = psql.Arg(s.PasswordHashType.MustGetNull())
|
|
} else {
|
|
vals[10] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if !s.PasswordHash.IsUnset() {
|
|
vals[11] = psql.Arg(s.PasswordHash.MustGetNull())
|
|
} else {
|
|
vals[11] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
|
}))
|
|
}
|
|
|
|
func (s UserSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
|
return um.Set(s.Expressions()...)
|
|
}
|
|
|
|
func (s UserSetter) Expressions(prefix ...string) []bob.Expression {
|
|
exprs := make([]bob.Expression, 0, 12)
|
|
|
|
if s.ID.IsValue() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "id")...),
|
|
psql.Arg(s.ID),
|
|
}})
|
|
}
|
|
|
|
if !s.ArcgisAccessToken.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "arcgis_access_token")...),
|
|
psql.Arg(s.ArcgisAccessToken),
|
|
}})
|
|
}
|
|
|
|
if !s.ArcgisLicense.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "arcgis_license")...),
|
|
psql.Arg(s.ArcgisLicense),
|
|
}})
|
|
}
|
|
|
|
if !s.ArcgisRefreshToken.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "arcgis_refresh_token")...),
|
|
psql.Arg(s.ArcgisRefreshToken),
|
|
}})
|
|
}
|
|
|
|
if !s.ArcgisRefreshTokenExpires.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "arcgis_refresh_token_expires")...),
|
|
psql.Arg(s.ArcgisRefreshTokenExpires),
|
|
}})
|
|
}
|
|
|
|
if !s.ArcgisRole.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "arcgis_role")...),
|
|
psql.Arg(s.ArcgisRole),
|
|
}})
|
|
}
|
|
|
|
if !s.DisplayName.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "display_name")...),
|
|
psql.Arg(s.DisplayName),
|
|
}})
|
|
}
|
|
|
|
if !s.Email.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "email")...),
|
|
psql.Arg(s.Email),
|
|
}})
|
|
}
|
|
|
|
if !s.OrganizationID.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "organization_id")...),
|
|
psql.Arg(s.OrganizationID),
|
|
}})
|
|
}
|
|
|
|
if s.Username.IsValue() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "username")...),
|
|
psql.Arg(s.Username),
|
|
}})
|
|
}
|
|
|
|
if !s.PasswordHashType.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "password_hash_type")...),
|
|
psql.Arg(s.PasswordHashType),
|
|
}})
|
|
}
|
|
|
|
if !s.PasswordHash.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "password_hash")...),
|
|
psql.Arg(s.PasswordHash),
|
|
}})
|
|
}
|
|
|
|
return exprs
|
|
}
|
|
|
|
// FindUser retrieves a single record by primary key
|
|
// If cols is empty Find will return all columns.
|
|
func FindUser(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*User, error) {
|
|
if len(cols) == 0 {
|
|
return Users.Query(
|
|
sm.Where(Users.Columns.ID.EQ(psql.Arg(IDPK))),
|
|
).One(ctx, exec)
|
|
}
|
|
|
|
return Users.Query(
|
|
sm.Where(Users.Columns.ID.EQ(psql.Arg(IDPK))),
|
|
sm.Columns(Users.Columns.Only(cols...)),
|
|
).One(ctx, exec)
|
|
}
|
|
|
|
// UserExists checks the presence of a single record by primary key
|
|
func UserExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
|
|
return Users.Query(
|
|
sm.Where(Users.Columns.ID.EQ(psql.Arg(IDPK))),
|
|
).Exists(ctx, exec)
|
|
}
|
|
|
|
// AfterQueryHook is called after User is retrieved from the database
|
|
func (o *User) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
|
var err error
|
|
|
|
switch queryType {
|
|
case bob.QueryTypeSelect:
|
|
ctx, err = Users.AfterSelectHooks.RunHooks(ctx, exec, UserSlice{o})
|
|
case bob.QueryTypeInsert:
|
|
ctx, err = Users.AfterInsertHooks.RunHooks(ctx, exec, UserSlice{o})
|
|
case bob.QueryTypeUpdate:
|
|
ctx, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, UserSlice{o})
|
|
case bob.QueryTypeDelete:
|
|
ctx, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, UserSlice{o})
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// primaryKeyVals returns the primary key values of the User
|
|
func (o *User) primaryKeyVals() bob.Expression {
|
|
return psql.Arg(o.ID)
|
|
}
|
|
|
|
func (o *User) pkEQ() dialect.Expression {
|
|
return psql.Quote("user_", "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 User
|
|
func (o *User) Update(ctx context.Context, exec bob.Executor, s *UserSetter) error {
|
|
v, err := Users.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 User record with an executor
|
|
func (o *User) Delete(ctx context.Context, exec bob.Executor) error {
|
|
_, err := Users.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
// Reload refreshes the User using the executor
|
|
func (o *User) Reload(ctx context.Context, exec bob.Executor) error {
|
|
o2, err := Users.Query(
|
|
sm.Where(Users.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 UserSlice is retrieved from the database
|
|
func (o UserSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
|
var err error
|
|
|
|
switch queryType {
|
|
case bob.QueryTypeSelect:
|
|
ctx, err = Users.AfterSelectHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeInsert:
|
|
ctx, err = Users.AfterInsertHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeUpdate:
|
|
ctx, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeDelete:
|
|
ctx, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (o UserSlice) pkIN() dialect.Expression {
|
|
if len(o) == 0 {
|
|
return psql.Raw("NULL")
|
|
}
|
|
|
|
return psql.Quote("user_", "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 UserSlice) copyMatchingRows(from ...*User) {
|
|
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 UserSlice) 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 Users.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 *User:
|
|
o.copyMatchingRows(retrieved)
|
|
case []*User:
|
|
o.copyMatchingRows(retrieved...)
|
|
case UserSlice:
|
|
o.copyMatchingRows(retrieved...)
|
|
default:
|
|
// If the retrieved value is not a User or a slice of User
|
|
// then run the AfterUpdateHooks on the slice
|
|
_, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}))
|
|
|
|
q.AppendWhere(o.pkIN())
|
|
})
|
|
}
|
|
|
|
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
|
func (o UserSlice) 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 Users.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 *User:
|
|
o.copyMatchingRows(retrieved)
|
|
case []*User:
|
|
o.copyMatchingRows(retrieved...)
|
|
case UserSlice:
|
|
o.copyMatchingRows(retrieved...)
|
|
default:
|
|
// If the retrieved value is not a User or a slice of User
|
|
// then run the AfterDeleteHooks on the slice
|
|
_, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}))
|
|
|
|
q.AppendWhere(o.pkIN())
|
|
})
|
|
}
|
|
|
|
func (o UserSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals UserSetter) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, err := Users.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
func (o UserSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, err := Users.Delete(o.DeleteMod()).Exec(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
func (o UserSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
o2, err := Users.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
o.copyMatchingRows(o2...)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Organization starts a query for related objects on organization
|
|
func (o *User) Organization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery {
|
|
return Organizations.Query(append(mods,
|
|
sm.Where(Organizations.Columns.ID.EQ(psql.Arg(o.OrganizationID))),
|
|
)...)
|
|
}
|
|
|
|
func (os UserSlice) Organization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery {
|
|
pkOrganizationID := make(pgtypes.Array[null.Val[int32]], 0, len(os))
|
|
for _, o := range os {
|
|
if o == nil {
|
|
continue
|
|
}
|
|
pkOrganizationID = append(pkOrganizationID, o.OrganizationID)
|
|
}
|
|
PKArgExpr := psql.Select(sm.Columns(
|
|
psql.F("unnest", psql.Cast(psql.Arg(pkOrganizationID), "integer[]")),
|
|
))
|
|
|
|
return Organizations.Query(append(mods,
|
|
sm.Where(psql.Group(Organizations.Columns.ID).OP("IN", PKArgExpr)),
|
|
)...)
|
|
}
|
|
|
|
func attachUserOrganization0(ctx context.Context, exec bob.Executor, count int, user0 *User, organization1 *Organization) (*User, error) {
|
|
setter := &UserSetter{
|
|
OrganizationID: omitnull.From(organization1.ID),
|
|
}
|
|
|
|
err := user0.Update(ctx, exec, setter)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("attachUserOrganization0: %w", err)
|
|
}
|
|
|
|
return user0, nil
|
|
}
|
|
|
|
func (user0 *User) InsertOrganization(ctx context.Context, exec bob.Executor, related *OrganizationSetter) error {
|
|
var err error
|
|
|
|
organization1, err := Organizations.Insert(related).One(ctx, exec)
|
|
if err != nil {
|
|
return fmt.Errorf("inserting related objects: %w", err)
|
|
}
|
|
|
|
_, err = attachUserOrganization0(ctx, exec, 1, user0, organization1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user0.R.Organization = organization1
|
|
|
|
organization1.R.User = append(organization1.R.User, user0)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (user0 *User) AttachOrganization(ctx context.Context, exec bob.Executor, organization1 *Organization) error {
|
|
var err error
|
|
|
|
_, err = attachUserOrganization0(ctx, exec, 1, user0, organization1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user0.R.Organization = organization1
|
|
|
|
organization1.R.User = append(organization1.R.User, user0)
|
|
|
|
return nil
|
|
}
|
|
|
|
type userWhere[Q psql.Filterable] struct {
|
|
ID psql.WhereMod[Q, int32]
|
|
ArcgisAccessToken psql.WhereNullMod[Q, string]
|
|
ArcgisLicense psql.WhereNullMod[Q, enums.ArcgisLicenseType]
|
|
ArcgisRefreshToken psql.WhereNullMod[Q, string]
|
|
ArcgisRefreshTokenExpires psql.WhereNullMod[Q, time.Time]
|
|
ArcgisRole psql.WhereNullMod[Q, string]
|
|
DisplayName psql.WhereNullMod[Q, string]
|
|
Email psql.WhereNullMod[Q, string]
|
|
OrganizationID psql.WhereNullMod[Q, int32]
|
|
Username psql.WhereMod[Q, string]
|
|
PasswordHashType psql.WhereNullMod[Q, enums.Hashtype]
|
|
PasswordHash psql.WhereNullMod[Q, string]
|
|
}
|
|
|
|
func (userWhere[Q]) AliasedAs(alias string) userWhere[Q] {
|
|
return buildUserWhere[Q](buildUserColumns(alias))
|
|
}
|
|
|
|
func buildUserWhere[Q psql.Filterable](cols userColumns) userWhere[Q] {
|
|
return userWhere[Q]{
|
|
ID: psql.Where[Q, int32](cols.ID),
|
|
ArcgisAccessToken: psql.WhereNull[Q, string](cols.ArcgisAccessToken),
|
|
ArcgisLicense: psql.WhereNull[Q, enums.ArcgisLicenseType](cols.ArcgisLicense),
|
|
ArcgisRefreshToken: psql.WhereNull[Q, string](cols.ArcgisRefreshToken),
|
|
ArcgisRefreshTokenExpires: psql.WhereNull[Q, time.Time](cols.ArcgisRefreshTokenExpires),
|
|
ArcgisRole: psql.WhereNull[Q, string](cols.ArcgisRole),
|
|
DisplayName: psql.WhereNull[Q, string](cols.DisplayName),
|
|
Email: psql.WhereNull[Q, string](cols.Email),
|
|
OrganizationID: psql.WhereNull[Q, int32](cols.OrganizationID),
|
|
Username: psql.Where[Q, string](cols.Username),
|
|
PasswordHashType: psql.WhereNull[Q, enums.Hashtype](cols.PasswordHashType),
|
|
PasswordHash: psql.WhereNull[Q, string](cols.PasswordHash),
|
|
}
|
|
}
|
|
|
|
func (o *User) Preload(name string, retrieved any) error {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
|
|
switch name {
|
|
case "Organization":
|
|
rel, ok := retrieved.(*Organization)
|
|
if !ok {
|
|
return fmt.Errorf("user cannot load %T as %q", retrieved, name)
|
|
}
|
|
|
|
o.R.Organization = rel
|
|
|
|
if rel != nil {
|
|
rel.R.User = UserSlice{o}
|
|
}
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("user has no relationship %q", name)
|
|
}
|
|
}
|
|
|
|
type userPreloader struct {
|
|
Organization func(...psql.PreloadOption) psql.Preloader
|
|
}
|
|
|
|
func buildUserPreloader() userPreloader {
|
|
return userPreloader{
|
|
Organization: func(opts ...psql.PreloadOption) psql.Preloader {
|
|
return psql.Preload[*Organization, OrganizationSlice](psql.PreloadRel{
|
|
Name: "Organization",
|
|
Sides: []psql.PreloadSide{
|
|
{
|
|
From: Users,
|
|
To: Organizations,
|
|
FromColumns: []string{"organization_id"},
|
|
ToColumns: []string{"id"},
|
|
},
|
|
},
|
|
}, Organizations.Columns.Names(), opts...)
|
|
},
|
|
}
|
|
}
|
|
|
|
type userThenLoader[Q orm.Loadable] struct {
|
|
Organization func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
|
|
}
|
|
|
|
func buildUserThenLoader[Q orm.Loadable]() userThenLoader[Q] {
|
|
type OrganizationLoadInterface interface {
|
|
LoadOrganization(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
|
}
|
|
|
|
return userThenLoader[Q]{
|
|
Organization: thenLoadBuilder[Q](
|
|
"Organization",
|
|
func(ctx context.Context, exec bob.Executor, retrieved OrganizationLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
|
return retrieved.LoadOrganization(ctx, exec, mods...)
|
|
},
|
|
),
|
|
}
|
|
}
|
|
|
|
// LoadOrganization loads the user's Organization into the .R struct
|
|
func (o *User) LoadOrganization(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
|
|
// Reset the relationship
|
|
o.R.Organization = nil
|
|
|
|
related, err := o.Organization(mods...).One(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
related.R.User = UserSlice{o}
|
|
|
|
o.R.Organization = related
|
|
return nil
|
|
}
|
|
|
|
// LoadOrganization loads the user's Organization into the .R struct
|
|
func (os UserSlice) LoadOrganization(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
|
if len(os) == 0 {
|
|
return nil
|
|
}
|
|
|
|
organizations, err := os.Organization(mods...).All(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, o := range os {
|
|
if o == nil {
|
|
continue
|
|
}
|
|
|
|
for _, rel := range organizations {
|
|
if !o.OrganizationID.IsValue() {
|
|
continue
|
|
}
|
|
|
|
if !(o.OrganizationID.IsValue() && o.OrganizationID.MustGet() == rel.ID) {
|
|
continue
|
|
}
|
|
|
|
rel.R.User = append(rel.R.User, o)
|
|
|
|
o.R.Organization = rel
|
|
break
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type userJoins[Q dialect.Joinable] struct {
|
|
typ string
|
|
Organization modAs[Q, organizationColumns]
|
|
}
|
|
|
|
func (j userJoins[Q]) aliasedAs(alias string) userJoins[Q] {
|
|
return buildUserJoins[Q](buildUserColumns(alias), j.typ)
|
|
}
|
|
|
|
func buildUserJoins[Q dialect.Joinable](cols userColumns, typ string) userJoins[Q] {
|
|
return userJoins[Q]{
|
|
typ: typ,
|
|
Organization: modAs[Q, organizationColumns]{
|
|
c: Organizations.Columns,
|
|
f: func(to organizationColumns) bob.Mod[Q] {
|
|
mods := make(mods.QueryMods[Q], 0, 1)
|
|
|
|
{
|
|
mods = append(mods, dialect.Join[Q](typ, Organizations.Name().As(to.Alias())).On(
|
|
to.ID.EQ(cols.OrganizationID),
|
|
))
|
|
}
|
|
|
|
return mods
|
|
},
|
|
},
|
|
}
|
|
}
|