nidus-sync/db/models/fileupload.error.bob.go

652 lines
18 KiB
Go

// 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"
"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/mods"
"github.com/Gleipnir-Technology/bob/orm"
"github.com/Gleipnir-Technology/bob/types/pgtypes"
"github.com/aarondl/opt/omit"
)
// FileuploadError is an object representing the database table.
type FileuploadError struct {
FileID int32 `db:"file_id" `
ID int32 `db:"id,pk" `
Line int32 `db:"line" `
Message string `db:"message" `
R fileuploadErrorR `db:"-" `
}
// FileuploadErrorSlice is an alias for a slice of pointers to FileuploadError.
// This should almost always be used instead of []*FileuploadError.
type FileuploadErrorSlice []*FileuploadError
// FileuploadErrors contains methods to work with the error table
var FileuploadErrors = psql.NewTablex[*FileuploadError, FileuploadErrorSlice, *FileuploadErrorSetter]("fileupload", "error", buildFileuploadErrorColumns("fileupload.error"))
// FileuploadErrorsQuery is a query on the error table
type FileuploadErrorsQuery = *psql.ViewQuery[*FileuploadError, FileuploadErrorSlice]
// fileuploadErrorR is where relationships are stored.
type fileuploadErrorR struct {
File *FileuploadFile // fileupload.error.error_file_id_fkey
}
func buildFileuploadErrorColumns(alias string) fileuploadErrorColumns {
return fileuploadErrorColumns{
ColumnsExpr: expr.NewColumnsExpr(
"file_id", "id", "line", "message",
).WithParent("fileupload.error"),
tableAlias: alias,
FileID: psql.Quote(alias, "file_id"),
ID: psql.Quote(alias, "id"),
Line: psql.Quote(alias, "line"),
Message: psql.Quote(alias, "message"),
}
}
type fileuploadErrorColumns struct {
expr.ColumnsExpr
tableAlias string
FileID psql.Expression
ID psql.Expression
Line psql.Expression
Message psql.Expression
}
func (c fileuploadErrorColumns) Alias() string {
return c.tableAlias
}
func (fileuploadErrorColumns) AliasedAs(alias string) fileuploadErrorColumns {
return buildFileuploadErrorColumns(alias)
}
// FileuploadErrorSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type FileuploadErrorSetter struct {
FileID omit.Val[int32] `db:"file_id" `
ID omit.Val[int32] `db:"id,pk" `
Line omit.Val[int32] `db:"line" `
Message omit.Val[string] `db:"message" `
}
func (s FileuploadErrorSetter) SetColumns() []string {
vals := make([]string, 0, 4)
if s.FileID.IsValue() {
vals = append(vals, "file_id")
}
if s.ID.IsValue() {
vals = append(vals, "id")
}
if s.Line.IsValue() {
vals = append(vals, "line")
}
if s.Message.IsValue() {
vals = append(vals, "message")
}
return vals
}
func (s FileuploadErrorSetter) Overwrite(t *FileuploadError) {
if s.FileID.IsValue() {
t.FileID = s.FileID.MustGet()
}
if s.ID.IsValue() {
t.ID = s.ID.MustGet()
}
if s.Line.IsValue() {
t.Line = s.Line.MustGet()
}
if s.Message.IsValue() {
t.Message = s.Message.MustGet()
}
}
func (s *FileuploadErrorSetter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return FileuploadErrors.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, 4)
if s.FileID.IsValue() {
vals[0] = psql.Arg(s.FileID.MustGet())
} else {
vals[0] = psql.Raw("DEFAULT")
}
if s.ID.IsValue() {
vals[1] = psql.Arg(s.ID.MustGet())
} else {
vals[1] = psql.Raw("DEFAULT")
}
if s.Line.IsValue() {
vals[2] = psql.Arg(s.Line.MustGet())
} else {
vals[2] = psql.Raw("DEFAULT")
}
if s.Message.IsValue() {
vals[3] = psql.Arg(s.Message.MustGet())
} else {
vals[3] = psql.Raw("DEFAULT")
}
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
func (s FileuploadErrorSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
return um.Set(s.Expressions()...)
}
func (s FileuploadErrorSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 4)
if s.FileID.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "file_id")...),
psql.Arg(s.FileID),
}})
}
if s.ID.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "id")...),
psql.Arg(s.ID),
}})
}
if s.Line.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "line")...),
psql.Arg(s.Line),
}})
}
if s.Message.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "message")...),
psql.Arg(s.Message),
}})
}
return exprs
}
// FindFileuploadError retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindFileuploadError(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*FileuploadError, error) {
if len(cols) == 0 {
return FileuploadErrors.Query(
sm.Where(FileuploadErrors.Columns.ID.EQ(psql.Arg(IDPK))),
).One(ctx, exec)
}
return FileuploadErrors.Query(
sm.Where(FileuploadErrors.Columns.ID.EQ(psql.Arg(IDPK))),
sm.Columns(FileuploadErrors.Columns.Only(cols...)),
).One(ctx, exec)
}
// FileuploadErrorExists checks the presence of a single record by primary key
func FileuploadErrorExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
return FileuploadErrors.Query(
sm.Where(FileuploadErrors.Columns.ID.EQ(psql.Arg(IDPK))),
).Exists(ctx, exec)
}
// AfterQueryHook is called after FileuploadError is retrieved from the database
func (o *FileuploadError) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = FileuploadErrors.AfterSelectHooks.RunHooks(ctx, exec, FileuploadErrorSlice{o})
case bob.QueryTypeInsert:
ctx, err = FileuploadErrors.AfterInsertHooks.RunHooks(ctx, exec, FileuploadErrorSlice{o})
case bob.QueryTypeUpdate:
ctx, err = FileuploadErrors.AfterUpdateHooks.RunHooks(ctx, exec, FileuploadErrorSlice{o})
case bob.QueryTypeDelete:
ctx, err = FileuploadErrors.AfterDeleteHooks.RunHooks(ctx, exec, FileuploadErrorSlice{o})
}
return err
}
// primaryKeyVals returns the primary key values of the FileuploadError
func (o *FileuploadError) primaryKeyVals() bob.Expression {
return psql.Arg(o.ID)
}
func (o *FileuploadError) pkEQ() dialect.Expression {
return psql.Quote("fileupload.error", "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 FileuploadError
func (o *FileuploadError) Update(ctx context.Context, exec bob.Executor, s *FileuploadErrorSetter) error {
v, err := FileuploadErrors.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 FileuploadError record with an executor
func (o *FileuploadError) Delete(ctx context.Context, exec bob.Executor) error {
_, err := FileuploadErrors.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
return err
}
// Reload refreshes the FileuploadError using the executor
func (o *FileuploadError) Reload(ctx context.Context, exec bob.Executor) error {
o2, err := FileuploadErrors.Query(
sm.Where(FileuploadErrors.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 FileuploadErrorSlice is retrieved from the database
func (o FileuploadErrorSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = FileuploadErrors.AfterSelectHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeInsert:
ctx, err = FileuploadErrors.AfterInsertHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeUpdate:
ctx, err = FileuploadErrors.AfterUpdateHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeDelete:
ctx, err = FileuploadErrors.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}
func (o FileuploadErrorSlice) pkIN() dialect.Expression {
if len(o) == 0 {
return psql.Raw("NULL")
}
return psql.Quote("fileupload.error", "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 FileuploadErrorSlice) copyMatchingRows(from ...*FileuploadError) {
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 FileuploadErrorSlice) 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 FileuploadErrors.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 *FileuploadError:
o.copyMatchingRows(retrieved)
case []*FileuploadError:
o.copyMatchingRows(retrieved...)
case FileuploadErrorSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a FileuploadError or a slice of FileuploadError
// then run the AfterUpdateHooks on the slice
_, err = FileuploadErrors.AfterUpdateHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o FileuploadErrorSlice) 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 FileuploadErrors.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 *FileuploadError:
o.copyMatchingRows(retrieved)
case []*FileuploadError:
o.copyMatchingRows(retrieved...)
case FileuploadErrorSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a FileuploadError or a slice of FileuploadError
// then run the AfterDeleteHooks on the slice
_, err = FileuploadErrors.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
func (o FileuploadErrorSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals FileuploadErrorSetter) error {
if len(o) == 0 {
return nil
}
_, err := FileuploadErrors.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
return err
}
func (o FileuploadErrorSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
_, err := FileuploadErrors.Delete(o.DeleteMod()).Exec(ctx, exec)
return err
}
func (o FileuploadErrorSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
o2, err := FileuploadErrors.Query(sm.Where(o.pkIN())).All(ctx, exec)
if err != nil {
return err
}
o.copyMatchingRows(o2...)
return nil
}
// File starts a query for related objects on fileupload.file
func (o *FileuploadError) File(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadFilesQuery {
return FileuploadFiles.Query(append(mods,
sm.Where(FileuploadFiles.Columns.ID.EQ(psql.Arg(o.FileID))),
)...)
}
func (os FileuploadErrorSlice) File(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadFilesQuery {
pkFileID := make(pgtypes.Array[int32], 0, len(os))
for _, o := range os {
if o == nil {
continue
}
pkFileID = append(pkFileID, o.FileID)
}
PKArgExpr := psql.Select(sm.Columns(
psql.F("unnest", psql.Cast(psql.Arg(pkFileID), "integer[]")),
))
return FileuploadFiles.Query(append(mods,
sm.Where(psql.Group(FileuploadFiles.Columns.ID).OP("IN", PKArgExpr)),
)...)
}
func attachFileuploadErrorFile0(ctx context.Context, exec bob.Executor, count int, fileuploadError0 *FileuploadError, fileuploadFile1 *FileuploadFile) (*FileuploadError, error) {
setter := &FileuploadErrorSetter{
FileID: omit.From(fileuploadFile1.ID),
}
err := fileuploadError0.Update(ctx, exec, setter)
if err != nil {
return nil, fmt.Errorf("attachFileuploadErrorFile0: %w", err)
}
return fileuploadError0, nil
}
func (fileuploadError0 *FileuploadError) InsertFile(ctx context.Context, exec bob.Executor, related *FileuploadFileSetter) error {
var err error
fileuploadFile1, err := FileuploadFiles.Insert(related).One(ctx, exec)
if err != nil {
return fmt.Errorf("inserting related objects: %w", err)
}
_, err = attachFileuploadErrorFile0(ctx, exec, 1, fileuploadError0, fileuploadFile1)
if err != nil {
return err
}
fileuploadError0.R.File = fileuploadFile1
fileuploadFile1.R.Errors = append(fileuploadFile1.R.Errors, fileuploadError0)
return nil
}
func (fileuploadError0 *FileuploadError) AttachFile(ctx context.Context, exec bob.Executor, fileuploadFile1 *FileuploadFile) error {
var err error
_, err = attachFileuploadErrorFile0(ctx, exec, 1, fileuploadError0, fileuploadFile1)
if err != nil {
return err
}
fileuploadError0.R.File = fileuploadFile1
fileuploadFile1.R.Errors = append(fileuploadFile1.R.Errors, fileuploadError0)
return nil
}
type fileuploadErrorWhere[Q psql.Filterable] struct {
FileID psql.WhereMod[Q, int32]
ID psql.WhereMod[Q, int32]
Line psql.WhereMod[Q, int32]
Message psql.WhereMod[Q, string]
}
func (fileuploadErrorWhere[Q]) AliasedAs(alias string) fileuploadErrorWhere[Q] {
return buildFileuploadErrorWhere[Q](buildFileuploadErrorColumns(alias))
}
func buildFileuploadErrorWhere[Q psql.Filterable](cols fileuploadErrorColumns) fileuploadErrorWhere[Q] {
return fileuploadErrorWhere[Q]{
FileID: psql.Where[Q, int32](cols.FileID),
ID: psql.Where[Q, int32](cols.ID),
Line: psql.Where[Q, int32](cols.Line),
Message: psql.Where[Q, string](cols.Message),
}
}
func (o *FileuploadError) Preload(name string, retrieved any) error {
if o == nil {
return nil
}
switch name {
case "File":
rel, ok := retrieved.(*FileuploadFile)
if !ok {
return fmt.Errorf("fileuploadError cannot load %T as %q", retrieved, name)
}
o.R.File = rel
if rel != nil {
rel.R.Errors = FileuploadErrorSlice{o}
}
return nil
default:
return fmt.Errorf("fileuploadError has no relationship %q", name)
}
}
type fileuploadErrorPreloader struct {
File func(...psql.PreloadOption) psql.Preloader
}
func buildFileuploadErrorPreloader() fileuploadErrorPreloader {
return fileuploadErrorPreloader{
File: func(opts ...psql.PreloadOption) psql.Preloader {
return psql.Preload[*FileuploadFile, FileuploadFileSlice](psql.PreloadRel{
Name: "File",
Sides: []psql.PreloadSide{
{
From: FileuploadErrors,
To: FileuploadFiles,
FromColumns: []string{"file_id"},
ToColumns: []string{"id"},
},
},
}, FileuploadFiles.Columns.Names(), opts...)
},
}
}
type fileuploadErrorThenLoader[Q orm.Loadable] struct {
File func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildFileuploadErrorThenLoader[Q orm.Loadable]() fileuploadErrorThenLoader[Q] {
type FileLoadInterface interface {
LoadFile(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return fileuploadErrorThenLoader[Q]{
File: thenLoadBuilder[Q](
"File",
func(ctx context.Context, exec bob.Executor, retrieved FileLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadFile(ctx, exec, mods...)
},
),
}
}
// LoadFile loads the fileuploadError's File into the .R struct
func (o *FileuploadError) LoadFile(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.File = nil
related, err := o.File(mods...).One(ctx, exec)
if err != nil {
return err
}
related.R.Errors = FileuploadErrorSlice{o}
o.R.File = related
return nil
}
// LoadFile loads the fileuploadError's File into the .R struct
func (os FileuploadErrorSlice) LoadFile(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
fileuploadFiles, err := os.File(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range fileuploadFiles {
if !(o.FileID == rel.ID) {
continue
}
rel.R.Errors = append(rel.R.Errors, o)
o.R.File = rel
break
}
}
return nil
}
type fileuploadErrorJoins[Q dialect.Joinable] struct {
typ string
File modAs[Q, fileuploadFileColumns]
}
func (j fileuploadErrorJoins[Q]) aliasedAs(alias string) fileuploadErrorJoins[Q] {
return buildFileuploadErrorJoins[Q](buildFileuploadErrorColumns(alias), j.typ)
}
func buildFileuploadErrorJoins[Q dialect.Joinable](cols fileuploadErrorColumns, typ string) fileuploadErrorJoins[Q] {
return fileuploadErrorJoins[Q]{
typ: typ,
File: modAs[Q, fileuploadFileColumns]{
c: FileuploadFiles.Columns,
f: func(to fileuploadFileColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, FileuploadFiles.Name().As(to.Alias())).On(
to.ID.EQ(cols.FileID),
))
}
return mods
},
},
}
}