852 lines
21 KiB
Go
852 lines
21 KiB
Go
// Code generated by BobGen sql (devel). DO NOT EDIT.
|
|
// This file is meant to be re-generated in place and/or deleted at any time.
|
|
|
|
package models
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/aarondl/opt/omit"
|
|
"github.com/aarondl/opt/omitnull"
|
|
"github.com/stephenafamo/bob"
|
|
"github.com/stephenafamo/bob/dialect/sqlite"
|
|
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
|
|
"github.com/stephenafamo/bob/dialect/sqlite/dm"
|
|
"github.com/stephenafamo/bob/dialect/sqlite/sm"
|
|
"github.com/stephenafamo/bob/dialect/sqlite/um"
|
|
"github.com/stephenafamo/bob/expr"
|
|
"github.com/stephenafamo/bob/mods"
|
|
"github.com/stephenafamo/bob/orm"
|
|
)
|
|
|
|
// File is an object representing the database table.
|
|
type File struct {
|
|
ID int64 `db:"id,pk" `
|
|
Name string `db:"name" `
|
|
Data []byte `db:"data" `
|
|
UserID int64 `db:"user_id" `
|
|
|
|
R fileR `db:"-" `
|
|
}
|
|
|
|
// FileSlice is an alias for a slice of pointers to File.
|
|
// This should almost always be used instead of []*File.
|
|
type FileSlice []*File
|
|
|
|
// Files contains methods to work with the file table
|
|
var Files = sqlite.NewTablex[*File, FileSlice, *FileSetter]("", "file")
|
|
|
|
// FilesQuery is a query on the file table
|
|
type FilesQuery = *sqlite.ViewQuery[*File, FileSlice]
|
|
|
|
// fileR is where relationships are stored.
|
|
type fileR struct {
|
|
User *User // fk_file_0
|
|
ProfilePictureUsers UserSlice // fk_user_0
|
|
}
|
|
|
|
type fileColumnNames struct {
|
|
ID string
|
|
Name string
|
|
Data string
|
|
UserID string
|
|
}
|
|
|
|
var FileColumns = buildFileColumns("file")
|
|
|
|
type fileColumns struct {
|
|
tableAlias string
|
|
ID sqlite.Expression
|
|
Name sqlite.Expression
|
|
Data sqlite.Expression
|
|
UserID sqlite.Expression
|
|
}
|
|
|
|
func (c fileColumns) Alias() string {
|
|
return c.tableAlias
|
|
}
|
|
|
|
func (fileColumns) AliasedAs(alias string) fileColumns {
|
|
return buildFileColumns(alias)
|
|
}
|
|
|
|
func buildFileColumns(alias string) fileColumns {
|
|
return fileColumns{
|
|
tableAlias: alias,
|
|
ID: sqlite.Quote(alias, "id"),
|
|
Name: sqlite.Quote(alias, "name"),
|
|
Data: sqlite.Quote(alias, "data"),
|
|
UserID: sqlite.Quote(alias, "user_id"),
|
|
}
|
|
}
|
|
|
|
type fileWhere[Q sqlite.Filterable] struct {
|
|
ID sqlite.WhereMod[Q, int64]
|
|
Name sqlite.WhereMod[Q, string]
|
|
Data sqlite.WhereMod[Q, []byte]
|
|
UserID sqlite.WhereMod[Q, int64]
|
|
}
|
|
|
|
func (fileWhere[Q]) AliasedAs(alias string) fileWhere[Q] {
|
|
return buildFileWhere[Q](buildFileColumns(alias))
|
|
}
|
|
|
|
func buildFileWhere[Q sqlite.Filterable](cols fileColumns) fileWhere[Q] {
|
|
return fileWhere[Q]{
|
|
ID: sqlite.Where[Q, int64](cols.ID),
|
|
Name: sqlite.Where[Q, string](cols.Name),
|
|
Data: sqlite.Where[Q, []byte](cols.Data),
|
|
UserID: sqlite.Where[Q, int64](cols.UserID),
|
|
}
|
|
}
|
|
|
|
var FileErrors = &fileErrors{
|
|
ErrUniquePkMainFile: &UniqueConstraintError{s: "pk_main_file"},
|
|
}
|
|
|
|
type fileErrors struct {
|
|
ErrUniquePkMainFile *UniqueConstraintError
|
|
}
|
|
|
|
// FileSetter is used for insert/upsert/update operations
|
|
// All values are optional, and do not have to be set
|
|
// Generated columns are not included
|
|
type FileSetter struct {
|
|
ID omit.Val[int64] `db:"id,pk" `
|
|
Name omit.Val[string] `db:"name" `
|
|
Data omit.Val[[]byte] `db:"data" `
|
|
UserID omit.Val[int64] `db:"user_id" `
|
|
}
|
|
|
|
func (s FileSetter) SetColumns() []string {
|
|
vals := make([]string, 0, 4)
|
|
if !s.ID.IsUnset() {
|
|
vals = append(vals, "id")
|
|
}
|
|
|
|
if !s.Name.IsUnset() {
|
|
vals = append(vals, "name")
|
|
}
|
|
|
|
if !s.Data.IsUnset() {
|
|
vals = append(vals, "data")
|
|
}
|
|
|
|
if !s.UserID.IsUnset() {
|
|
vals = append(vals, "user_id")
|
|
}
|
|
|
|
return vals
|
|
}
|
|
|
|
func (s FileSetter) Overwrite(t *File) {
|
|
if !s.ID.IsUnset() {
|
|
t.ID, _ = s.ID.Get()
|
|
}
|
|
if !s.Name.IsUnset() {
|
|
t.Name, _ = s.Name.Get()
|
|
}
|
|
if !s.Data.IsUnset() {
|
|
t.Data, _ = s.Data.Get()
|
|
}
|
|
if !s.UserID.IsUnset() {
|
|
t.UserID, _ = s.UserID.Get()
|
|
}
|
|
}
|
|
|
|
func (s *FileSetter) Apply(q *dialect.InsertQuery) {
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
|
return Files.BeforeInsertHooks.RunHooks(ctx, exec, s)
|
|
})
|
|
|
|
if len(q.Table.Columns) == 0 {
|
|
q.Table.Columns = s.SetColumns()
|
|
}
|
|
|
|
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
|
|
vals := make([]bob.Expression, 0, 4)
|
|
if !s.ID.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.ID))
|
|
}
|
|
|
|
if !s.Name.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.Name))
|
|
}
|
|
|
|
if !s.Data.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.Data))
|
|
}
|
|
|
|
if !s.UserID.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.UserID))
|
|
}
|
|
|
|
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
|
}))
|
|
}
|
|
|
|
func (s FileSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
|
return um.Set(s.Expressions()...)
|
|
}
|
|
|
|
func (s FileSetter) Expressions(prefix ...string) []bob.Expression {
|
|
exprs := make([]bob.Expression, 0, 4)
|
|
|
|
if !s.ID.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
sqlite.Quote(append(prefix, "id")...),
|
|
sqlite.Arg(s.ID),
|
|
}})
|
|
}
|
|
|
|
if !s.Name.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
sqlite.Quote(append(prefix, "name")...),
|
|
sqlite.Arg(s.Name),
|
|
}})
|
|
}
|
|
|
|
if !s.Data.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
sqlite.Quote(append(prefix, "data")...),
|
|
sqlite.Arg(s.Data),
|
|
}})
|
|
}
|
|
|
|
if !s.UserID.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
sqlite.Quote(append(prefix, "user_id")...),
|
|
sqlite.Arg(s.UserID),
|
|
}})
|
|
}
|
|
|
|
return exprs
|
|
}
|
|
|
|
// FindFile retrieves a single record by primary key
|
|
// If cols is empty Find will return all columns.
|
|
func FindFile(ctx context.Context, exec bob.Executor, IDPK int64, cols ...string) (*File, error) {
|
|
if len(cols) == 0 {
|
|
return Files.Query(
|
|
SelectWhere.Files.ID.EQ(IDPK),
|
|
).One(ctx, exec)
|
|
}
|
|
|
|
return Files.Query(
|
|
SelectWhere.Files.ID.EQ(IDPK),
|
|
sm.Columns(Files.Columns().Only(cols...)),
|
|
).One(ctx, exec)
|
|
}
|
|
|
|
// FileExists checks the presence of a single record by primary key
|
|
func FileExists(ctx context.Context, exec bob.Executor, IDPK int64) (bool, error) {
|
|
return Files.Query(
|
|
SelectWhere.Files.ID.EQ(IDPK),
|
|
).Exists(ctx, exec)
|
|
}
|
|
|
|
// AfterQueryHook is called after File is retrieved from the database
|
|
func (o *File) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
|
var err error
|
|
|
|
switch queryType {
|
|
case bob.QueryTypeSelect:
|
|
ctx, err = Files.AfterSelectHooks.RunHooks(ctx, exec, FileSlice{o})
|
|
case bob.QueryTypeInsert:
|
|
ctx, err = Files.AfterInsertHooks.RunHooks(ctx, exec, FileSlice{o})
|
|
case bob.QueryTypeUpdate:
|
|
ctx, err = Files.AfterUpdateHooks.RunHooks(ctx, exec, FileSlice{o})
|
|
case bob.QueryTypeDelete:
|
|
ctx, err = Files.AfterDeleteHooks.RunHooks(ctx, exec, FileSlice{o})
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// PrimaryKeyVals returns the primary key values of the File
|
|
func (o *File) PrimaryKeyVals() bob.Expression {
|
|
return sqlite.Arg(o.ID)
|
|
}
|
|
|
|
func (o *File) pkEQ() dialect.Expression {
|
|
return sqlite.Quote("file", "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 File
|
|
func (o *File) Update(ctx context.Context, exec bob.Executor, s *FileSetter) error {
|
|
v, err := Files.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 File record with an executor
|
|
func (o *File) Delete(ctx context.Context, exec bob.Executor) error {
|
|
_, err := Files.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
// Reload refreshes the File using the executor
|
|
func (o *File) Reload(ctx context.Context, exec bob.Executor) error {
|
|
o2, err := Files.Query(
|
|
SelectWhere.Files.ID.EQ(o.ID),
|
|
).One(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
o2.R = o.R
|
|
*o = *o2
|
|
|
|
return nil
|
|
}
|
|
|
|
// AfterQueryHook is called after FileSlice is retrieved from the database
|
|
func (o FileSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
|
var err error
|
|
|
|
switch queryType {
|
|
case bob.QueryTypeSelect:
|
|
ctx, err = Files.AfterSelectHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeInsert:
|
|
ctx, err = Files.AfterInsertHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeUpdate:
|
|
ctx, err = Files.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeDelete:
|
|
ctx, err = Files.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (o FileSlice) pkIN() dialect.Expression {
|
|
if len(o) == 0 {
|
|
return sqlite.Raw("NULL")
|
|
}
|
|
|
|
return sqlite.Quote("file", "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 FileSlice) copyMatchingRows(from ...*File) {
|
|
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 FileSlice) 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 Files.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 *File:
|
|
o.copyMatchingRows(retrieved)
|
|
case []*File:
|
|
o.copyMatchingRows(retrieved...)
|
|
case FileSlice:
|
|
o.copyMatchingRows(retrieved...)
|
|
default:
|
|
// If the retrieved value is not a File or a slice of File
|
|
// then run the AfterUpdateHooks on the slice
|
|
_, err = Files.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}))
|
|
|
|
q.AppendWhere(o.pkIN())
|
|
})
|
|
}
|
|
|
|
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
|
func (o FileSlice) 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 Files.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 *File:
|
|
o.copyMatchingRows(retrieved)
|
|
case []*File:
|
|
o.copyMatchingRows(retrieved...)
|
|
case FileSlice:
|
|
o.copyMatchingRows(retrieved...)
|
|
default:
|
|
// If the retrieved value is not a File or a slice of File
|
|
// then run the AfterDeleteHooks on the slice
|
|
_, err = Files.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}))
|
|
|
|
q.AppendWhere(o.pkIN())
|
|
})
|
|
}
|
|
|
|
func (o FileSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals FileSetter) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, err := Files.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
func (o FileSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, err := Files.Delete(o.DeleteMod()).Exec(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
func (o FileSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
o2, err := Files.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
o.copyMatchingRows(o2...)
|
|
|
|
return nil
|
|
}
|
|
|
|
type fileJoins[Q dialect.Joinable] struct {
|
|
typ string
|
|
User func(context.Context) modAs[Q, userColumns]
|
|
ProfilePictureUsers func(context.Context) modAs[Q, userColumns]
|
|
}
|
|
|
|
func (j fileJoins[Q]) aliasedAs(alias string) fileJoins[Q] {
|
|
return buildFileJoins[Q](buildFileColumns(alias), j.typ)
|
|
}
|
|
|
|
func buildFileJoins[Q dialect.Joinable](cols fileColumns, typ string) fileJoins[Q] {
|
|
return fileJoins[Q]{
|
|
typ: typ,
|
|
User: filesJoinUser[Q](cols, typ),
|
|
ProfilePictureUsers: filesJoinProfilePictureUsers[Q](cols, typ),
|
|
}
|
|
}
|
|
|
|
func filesJoinUser[Q dialect.Joinable](from fileColumns, typ string) func(context.Context) modAs[Q, userColumns] {
|
|
return func(ctx context.Context) modAs[Q, userColumns] {
|
|
return modAs[Q, userColumns]{
|
|
c: UserColumns,
|
|
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.ID.EQ(from.UserID),
|
|
))
|
|
}
|
|
|
|
return mods
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
func filesJoinProfilePictureUsers[Q dialect.Joinable](from fileColumns, typ string) func(context.Context) modAs[Q, userColumns] {
|
|
return func(ctx context.Context) modAs[Q, userColumns] {
|
|
return modAs[Q, userColumns]{
|
|
c: UserColumns,
|
|
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.ProfilePictureID.EQ(from.ID),
|
|
))
|
|
}
|
|
|
|
return mods
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
// User starts a query for related objects on user
|
|
func (o *File) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
|
return Users.Query(append(mods,
|
|
sm.Where(UserColumns.ID.EQ(sqlite.Arg(o.UserID))),
|
|
)...)
|
|
}
|
|
|
|
func (os FileSlice) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
|
PKArgs := make([]bob.Expression, len(os))
|
|
for i, o := range os {
|
|
PKArgs[i] = sqlite.ArgGroup(o.UserID)
|
|
}
|
|
|
|
return Users.Query(append(mods,
|
|
sm.Where(sqlite.Group(UserColumns.ID).In(PKArgs...)),
|
|
)...)
|
|
}
|
|
|
|
// ProfilePictureUsers starts a query for related objects on user
|
|
func (o *File) ProfilePictureUsers(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
|
return Users.Query(append(mods,
|
|
sm.Where(UserColumns.ProfilePictureID.EQ(sqlite.Arg(o.ID))),
|
|
)...)
|
|
}
|
|
|
|
func (os FileSlice) ProfilePictureUsers(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
|
PKArgs := make([]bob.Expression, len(os))
|
|
for i, o := range os {
|
|
PKArgs[i] = sqlite.ArgGroup(o.ID)
|
|
}
|
|
|
|
return Users.Query(append(mods,
|
|
sm.Where(sqlite.Group(UserColumns.ProfilePictureID).In(PKArgs...)),
|
|
)...)
|
|
}
|
|
|
|
func (o *File) Preload(name string, retrieved any) error {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
|
|
switch name {
|
|
case "User":
|
|
rel, ok := retrieved.(*User)
|
|
if !ok {
|
|
return fmt.Errorf("file cannot load %T as %q", retrieved, name)
|
|
}
|
|
|
|
o.R.User = rel
|
|
|
|
if rel != nil {
|
|
rel.R.Files = FileSlice{o}
|
|
}
|
|
return nil
|
|
case "ProfilePictureUsers":
|
|
rels, ok := retrieved.(UserSlice)
|
|
if !ok {
|
|
return fmt.Errorf("file cannot load %T as %q", retrieved, name)
|
|
}
|
|
|
|
o.R.ProfilePictureUsers = rels
|
|
|
|
for _, rel := range rels {
|
|
if rel != nil {
|
|
rel.R.ProfilePictureFile = o
|
|
}
|
|
}
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("file has no relationship %q", name)
|
|
}
|
|
}
|
|
|
|
func PreloadFileUser(opts ...sqlite.PreloadOption) sqlite.Preloader {
|
|
return sqlite.Preload[*User, UserSlice](orm.Relationship{
|
|
Name: "User",
|
|
Sides: []orm.RelSide{
|
|
{
|
|
From: TableNames.Files,
|
|
To: TableNames.Users,
|
|
FromColumns: []string{
|
|
ColumnNames.Files.UserID,
|
|
},
|
|
ToColumns: []string{
|
|
ColumnNames.Users.ID,
|
|
},
|
|
},
|
|
},
|
|
}, Users.Columns().Names(), opts...)
|
|
}
|
|
|
|
func ThenLoadFileUser(queryMods ...bob.Mod[*dialect.SelectQuery]) sqlite.Loader {
|
|
return sqlite.Loader(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
|
loader, isLoader := retrieved.(interface {
|
|
LoadFileUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
|
})
|
|
if !isLoader {
|
|
return fmt.Errorf("object %T cannot load FileUser", retrieved)
|
|
}
|
|
|
|
err := loader.LoadFileUser(ctx, exec, queryMods...)
|
|
|
|
// Don't cause an issue due to missing relationships
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
})
|
|
}
|
|
|
|
// LoadFileUser loads the file's User into the .R struct
|
|
func (o *File) LoadFileUser(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...).One(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
related.R.Files = FileSlice{o}
|
|
|
|
o.R.User = related
|
|
return nil
|
|
}
|
|
|
|
// LoadFileUser loads the file's User into the .R struct
|
|
func (os FileSlice) LoadFileUser(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 {
|
|
for _, rel := range users {
|
|
if o.UserID != rel.ID {
|
|
continue
|
|
}
|
|
|
|
rel.R.Files = append(rel.R.Files, o)
|
|
|
|
o.R.User = rel
|
|
break
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func ThenLoadFileProfilePictureUsers(queryMods ...bob.Mod[*dialect.SelectQuery]) sqlite.Loader {
|
|
return sqlite.Loader(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
|
loader, isLoader := retrieved.(interface {
|
|
LoadFileProfilePictureUsers(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
|
})
|
|
if !isLoader {
|
|
return fmt.Errorf("object %T cannot load FileProfilePictureUsers", retrieved)
|
|
}
|
|
|
|
err := loader.LoadFileProfilePictureUsers(ctx, exec, queryMods...)
|
|
|
|
// Don't cause an issue due to missing relationships
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
})
|
|
}
|
|
|
|
// LoadFileProfilePictureUsers loads the file's ProfilePictureUsers into the .R struct
|
|
func (o *File) LoadFileProfilePictureUsers(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
|
|
// Reset the relationship
|
|
o.R.ProfilePictureUsers = nil
|
|
|
|
related, err := o.ProfilePictureUsers(mods...).All(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, rel := range related {
|
|
rel.R.ProfilePictureFile = o
|
|
}
|
|
|
|
o.R.ProfilePictureUsers = related
|
|
return nil
|
|
}
|
|
|
|
// LoadFileProfilePictureUsers loads the file's ProfilePictureUsers into the .R struct
|
|
func (os FileSlice) LoadFileProfilePictureUsers(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
|
|
if len(os) == 0 {
|
|
return nil
|
|
}
|
|
|
|
users, err := os.ProfilePictureUsers(mods...).All(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, o := range os {
|
|
o.R.ProfilePictureUsers = nil
|
|
}
|
|
|
|
for _, o := range os {
|
|
for _, rel := range users {
|
|
if o.ID != rel.ProfilePictureID.GetOrZero() {
|
|
continue
|
|
}
|
|
|
|
rel.R.ProfilePictureFile = o
|
|
|
|
o.R.ProfilePictureUsers = append(o.R.ProfilePictureUsers, rel)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func attachFileUser0(ctx context.Context, exec bob.Executor, count int, file0 *File, user1 *User) (*File, error) {
|
|
setter := &FileSetter{
|
|
UserID: omit.From(user1.ID),
|
|
}
|
|
|
|
err := file0.Update(ctx, exec, setter)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("attachFileUser0: %w", err)
|
|
}
|
|
|
|
return file0, nil
|
|
}
|
|
|
|
func (file0 *File) InsertUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
|
|
user1, err := Users.Insert(related).One(ctx, exec)
|
|
if err != nil {
|
|
return fmt.Errorf("inserting related objects: %w", err)
|
|
}
|
|
|
|
_, err = attachFileUser0(ctx, exec, 1, file0, user1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
file0.R.User = user1
|
|
|
|
user1.R.Files = append(user1.R.Files, file0)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (file0 *File) AttachUser(ctx context.Context, exec bob.Executor, user1 *User) error {
|
|
var err error
|
|
|
|
_, err = attachFileUser0(ctx, exec, 1, file0, user1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
file0.R.User = user1
|
|
|
|
user1.R.Files = append(user1.R.Files, file0)
|
|
|
|
return nil
|
|
}
|
|
|
|
func insertFileProfilePictureUsers0(ctx context.Context, exec bob.Executor, users1 []*UserSetter, file0 *File) (UserSlice, error) {
|
|
for i := range users1 {
|
|
users1[i].ProfilePictureID = omitnull.From(file0.ID)
|
|
}
|
|
|
|
ret, err := Users.Insert(bob.ToMods(users1...)).All(ctx, exec)
|
|
if err != nil {
|
|
return ret, fmt.Errorf("insertFileProfilePictureUsers0: %w", err)
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func attachFileProfilePictureUsers0(ctx context.Context, exec bob.Executor, count int, users1 UserSlice, file0 *File) (UserSlice, error) {
|
|
setter := &UserSetter{
|
|
ProfilePictureID: omitnull.From(file0.ID),
|
|
}
|
|
|
|
err := users1.UpdateAll(ctx, exec, *setter)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("attachFileProfilePictureUsers0: %w", err)
|
|
}
|
|
|
|
return users1, nil
|
|
}
|
|
|
|
func (file0 *File) InsertProfilePictureUsers(ctx context.Context, exec bob.Executor, related ...*UserSetter) error {
|
|
if len(related) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var err error
|
|
|
|
users1, err := insertFileProfilePictureUsers0(ctx, exec, related, file0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
file0.R.ProfilePictureUsers = append(file0.R.ProfilePictureUsers, users1...)
|
|
|
|
for _, rel := range users1 {
|
|
rel.R.ProfilePictureFile = file0
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (file0 *File) AttachProfilePictureUsers(ctx context.Context, exec bob.Executor, related ...*User) error {
|
|
if len(related) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var err error
|
|
users1 := UserSlice(related)
|
|
|
|
_, err = attachFileProfilePictureUsers0(ctx, exec, len(related), users1, file0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
file0.R.ProfilePictureUsers = append(file0.R.ProfilePictureUsers, users1...)
|
|
|
|
for _, rel := range related {
|
|
rel.R.ProfilePictureFile = file0
|
|
}
|
|
|
|
return nil
|
|
}
|