733 lines
18 KiB
Go
733 lines
18 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"
|
|
"time"
|
|
|
|
"github.com/aarondl/opt/omit"
|
|
"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"
|
|
)
|
|
|
|
// Item is an object representing the database table.
|
|
type Item struct {
|
|
ID int64 `db:"id,pk" `
|
|
Name string `db:"name" `
|
|
Added time.Time `db:"added" `
|
|
Description string `db:"description" `
|
|
Price float32 `db:"price" `
|
|
Quantity int64 `db:"quantity" `
|
|
UserID int64 `db:"user_id" `
|
|
|
|
R itemR `db:"-" `
|
|
}
|
|
|
|
// ItemSlice is an alias for a slice of pointers to Item.
|
|
// This should almost always be used instead of []*Item.
|
|
type ItemSlice []*Item
|
|
|
|
// Items contains methods to work with the item table
|
|
var Items = sqlite.NewTablex[*Item, ItemSlice, *ItemSetter]("", "item")
|
|
|
|
// ItemsQuery is a query on the item table
|
|
type ItemsQuery = *sqlite.ViewQuery[*Item, ItemSlice]
|
|
|
|
// itemR is where relationships are stored.
|
|
type itemR struct {
|
|
User *User // fk_item_0
|
|
}
|
|
|
|
type itemColumnNames struct {
|
|
ID string
|
|
Name string
|
|
Added string
|
|
Description string
|
|
Price string
|
|
Quantity string
|
|
UserID string
|
|
}
|
|
|
|
var ItemColumns = buildItemColumns("item")
|
|
|
|
type itemColumns struct {
|
|
tableAlias string
|
|
ID sqlite.Expression
|
|
Name sqlite.Expression
|
|
Added sqlite.Expression
|
|
Description sqlite.Expression
|
|
Price sqlite.Expression
|
|
Quantity sqlite.Expression
|
|
UserID sqlite.Expression
|
|
}
|
|
|
|
func (c itemColumns) Alias() string {
|
|
return c.tableAlias
|
|
}
|
|
|
|
func (itemColumns) AliasedAs(alias string) itemColumns {
|
|
return buildItemColumns(alias)
|
|
}
|
|
|
|
func buildItemColumns(alias string) itemColumns {
|
|
return itemColumns{
|
|
tableAlias: alias,
|
|
ID: sqlite.Quote(alias, "id"),
|
|
Name: sqlite.Quote(alias, "name"),
|
|
Added: sqlite.Quote(alias, "added"),
|
|
Description: sqlite.Quote(alias, "description"),
|
|
Price: sqlite.Quote(alias, "price"),
|
|
Quantity: sqlite.Quote(alias, "quantity"),
|
|
UserID: sqlite.Quote(alias, "user_id"),
|
|
}
|
|
}
|
|
|
|
type itemWhere[Q sqlite.Filterable] struct {
|
|
ID sqlite.WhereMod[Q, int64]
|
|
Name sqlite.WhereMod[Q, string]
|
|
Added sqlite.WhereMod[Q, time.Time]
|
|
Description sqlite.WhereMod[Q, string]
|
|
Price sqlite.WhereMod[Q, float32]
|
|
Quantity sqlite.WhereMod[Q, int64]
|
|
UserID sqlite.WhereMod[Q, int64]
|
|
}
|
|
|
|
func (itemWhere[Q]) AliasedAs(alias string) itemWhere[Q] {
|
|
return buildItemWhere[Q](buildItemColumns(alias))
|
|
}
|
|
|
|
func buildItemWhere[Q sqlite.Filterable](cols itemColumns) itemWhere[Q] {
|
|
return itemWhere[Q]{
|
|
ID: sqlite.Where[Q, int64](cols.ID),
|
|
Name: sqlite.Where[Q, string](cols.Name),
|
|
Added: sqlite.Where[Q, time.Time](cols.Added),
|
|
Description: sqlite.Where[Q, string](cols.Description),
|
|
Price: sqlite.Where[Q, float32](cols.Price),
|
|
Quantity: sqlite.Where[Q, int64](cols.Quantity),
|
|
UserID: sqlite.Where[Q, int64](cols.UserID),
|
|
}
|
|
}
|
|
|
|
var ItemErrors = &itemErrors{
|
|
ErrUniquePkMainItem: &UniqueConstraintError{s: "pk_main_item"},
|
|
}
|
|
|
|
type itemErrors struct {
|
|
ErrUniquePkMainItem *UniqueConstraintError
|
|
}
|
|
|
|
// ItemSetter is used for insert/upsert/update operations
|
|
// All values are optional, and do not have to be set
|
|
// Generated columns are not included
|
|
type ItemSetter struct {
|
|
ID omit.Val[int64] `db:"id,pk" `
|
|
Name omit.Val[string] `db:"name" `
|
|
Added omit.Val[time.Time] `db:"added" `
|
|
Description omit.Val[string] `db:"description" `
|
|
Price omit.Val[float32] `db:"price" `
|
|
Quantity omit.Val[int64] `db:"quantity" `
|
|
UserID omit.Val[int64] `db:"user_id" `
|
|
}
|
|
|
|
func (s ItemSetter) SetColumns() []string {
|
|
vals := make([]string, 0, 7)
|
|
if !s.ID.IsUnset() {
|
|
vals = append(vals, "id")
|
|
}
|
|
|
|
if !s.Name.IsUnset() {
|
|
vals = append(vals, "name")
|
|
}
|
|
|
|
if !s.Added.IsUnset() {
|
|
vals = append(vals, "added")
|
|
}
|
|
|
|
if !s.Description.IsUnset() {
|
|
vals = append(vals, "description")
|
|
}
|
|
|
|
if !s.Price.IsUnset() {
|
|
vals = append(vals, "price")
|
|
}
|
|
|
|
if !s.Quantity.IsUnset() {
|
|
vals = append(vals, "quantity")
|
|
}
|
|
|
|
if !s.UserID.IsUnset() {
|
|
vals = append(vals, "user_id")
|
|
}
|
|
|
|
return vals
|
|
}
|
|
|
|
func (s ItemSetter) Overwrite(t *Item) {
|
|
if !s.ID.IsUnset() {
|
|
t.ID, _ = s.ID.Get()
|
|
}
|
|
if !s.Name.IsUnset() {
|
|
t.Name, _ = s.Name.Get()
|
|
}
|
|
if !s.Added.IsUnset() {
|
|
t.Added, _ = s.Added.Get()
|
|
}
|
|
if !s.Description.IsUnset() {
|
|
t.Description, _ = s.Description.Get()
|
|
}
|
|
if !s.Price.IsUnset() {
|
|
t.Price, _ = s.Price.Get()
|
|
}
|
|
if !s.Quantity.IsUnset() {
|
|
t.Quantity, _ = s.Quantity.Get()
|
|
}
|
|
if !s.UserID.IsUnset() {
|
|
t.UserID, _ = s.UserID.Get()
|
|
}
|
|
}
|
|
|
|
func (s *ItemSetter) Apply(q *dialect.InsertQuery) {
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
|
return Items.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, 7)
|
|
if !s.ID.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.ID))
|
|
}
|
|
|
|
if !s.Name.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.Name))
|
|
}
|
|
|
|
if !s.Added.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.Added))
|
|
}
|
|
|
|
if !s.Description.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.Description))
|
|
}
|
|
|
|
if !s.Price.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.Price))
|
|
}
|
|
|
|
if !s.Quantity.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.Quantity))
|
|
}
|
|
|
|
if !s.UserID.IsUnset() {
|
|
vals = append(vals, sqlite.Arg(s.UserID))
|
|
}
|
|
|
|
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
|
}))
|
|
}
|
|
|
|
func (s ItemSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
|
return um.Set(s.Expressions()...)
|
|
}
|
|
|
|
func (s ItemSetter) Expressions(prefix ...string) []bob.Expression {
|
|
exprs := make([]bob.Expression, 0, 7)
|
|
|
|
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.Added.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
sqlite.Quote(append(prefix, "added")...),
|
|
sqlite.Arg(s.Added),
|
|
}})
|
|
}
|
|
|
|
if !s.Description.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
sqlite.Quote(append(prefix, "description")...),
|
|
sqlite.Arg(s.Description),
|
|
}})
|
|
}
|
|
|
|
if !s.Price.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
sqlite.Quote(append(prefix, "price")...),
|
|
sqlite.Arg(s.Price),
|
|
}})
|
|
}
|
|
|
|
if !s.Quantity.IsUnset() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
sqlite.Quote(append(prefix, "quantity")...),
|
|
sqlite.Arg(s.Quantity),
|
|
}})
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// FindItem retrieves a single record by primary key
|
|
// If cols is empty Find will return all columns.
|
|
func FindItem(ctx context.Context, exec bob.Executor, IDPK int64, cols ...string) (*Item, error) {
|
|
if len(cols) == 0 {
|
|
return Items.Query(
|
|
SelectWhere.Items.ID.EQ(IDPK),
|
|
).One(ctx, exec)
|
|
}
|
|
|
|
return Items.Query(
|
|
SelectWhere.Items.ID.EQ(IDPK),
|
|
sm.Columns(Items.Columns().Only(cols...)),
|
|
).One(ctx, exec)
|
|
}
|
|
|
|
// ItemExists checks the presence of a single record by primary key
|
|
func ItemExists(ctx context.Context, exec bob.Executor, IDPK int64) (bool, error) {
|
|
return Items.Query(
|
|
SelectWhere.Items.ID.EQ(IDPK),
|
|
).Exists(ctx, exec)
|
|
}
|
|
|
|
// AfterQueryHook is called after Item is retrieved from the database
|
|
func (o *Item) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
|
var err error
|
|
|
|
switch queryType {
|
|
case bob.QueryTypeSelect:
|
|
ctx, err = Items.AfterSelectHooks.RunHooks(ctx, exec, ItemSlice{o})
|
|
case bob.QueryTypeInsert:
|
|
ctx, err = Items.AfterInsertHooks.RunHooks(ctx, exec, ItemSlice{o})
|
|
case bob.QueryTypeUpdate:
|
|
ctx, err = Items.AfterUpdateHooks.RunHooks(ctx, exec, ItemSlice{o})
|
|
case bob.QueryTypeDelete:
|
|
ctx, err = Items.AfterDeleteHooks.RunHooks(ctx, exec, ItemSlice{o})
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// PrimaryKeyVals returns the primary key values of the Item
|
|
func (o *Item) PrimaryKeyVals() bob.Expression {
|
|
return sqlite.Arg(o.ID)
|
|
}
|
|
|
|
func (o *Item) pkEQ() dialect.Expression {
|
|
return sqlite.Quote("item", "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 Item
|
|
func (o *Item) Update(ctx context.Context, exec bob.Executor, s *ItemSetter) error {
|
|
v, err := Items.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 Item record with an executor
|
|
func (o *Item) Delete(ctx context.Context, exec bob.Executor) error {
|
|
_, err := Items.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
// Reload refreshes the Item using the executor
|
|
func (o *Item) Reload(ctx context.Context, exec bob.Executor) error {
|
|
o2, err := Items.Query(
|
|
SelectWhere.Items.ID.EQ(o.ID),
|
|
).One(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
o2.R = o.R
|
|
*o = *o2
|
|
|
|
return nil
|
|
}
|
|
|
|
// AfterQueryHook is called after ItemSlice is retrieved from the database
|
|
func (o ItemSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
|
var err error
|
|
|
|
switch queryType {
|
|
case bob.QueryTypeSelect:
|
|
ctx, err = Items.AfterSelectHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeInsert:
|
|
ctx, err = Items.AfterInsertHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeUpdate:
|
|
ctx, err = Items.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeDelete:
|
|
ctx, err = Items.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (o ItemSlice) pkIN() dialect.Expression {
|
|
if len(o) == 0 {
|
|
return sqlite.Raw("NULL")
|
|
}
|
|
|
|
return sqlite.Quote("item", "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 ItemSlice) copyMatchingRows(from ...*Item) {
|
|
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 ItemSlice) 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 Items.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 *Item:
|
|
o.copyMatchingRows(retrieved)
|
|
case []*Item:
|
|
o.copyMatchingRows(retrieved...)
|
|
case ItemSlice:
|
|
o.copyMatchingRows(retrieved...)
|
|
default:
|
|
// If the retrieved value is not a Item or a slice of Item
|
|
// then run the AfterUpdateHooks on the slice
|
|
_, err = Items.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}))
|
|
|
|
q.AppendWhere(o.pkIN())
|
|
})
|
|
}
|
|
|
|
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
|
func (o ItemSlice) 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 Items.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 *Item:
|
|
o.copyMatchingRows(retrieved)
|
|
case []*Item:
|
|
o.copyMatchingRows(retrieved...)
|
|
case ItemSlice:
|
|
o.copyMatchingRows(retrieved...)
|
|
default:
|
|
// If the retrieved value is not a Item or a slice of Item
|
|
// then run the AfterDeleteHooks on the slice
|
|
_, err = Items.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}))
|
|
|
|
q.AppendWhere(o.pkIN())
|
|
})
|
|
}
|
|
|
|
func (o ItemSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals ItemSetter) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, err := Items.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
func (o ItemSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, err := Items.Delete(o.DeleteMod()).Exec(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
func (o ItemSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
o2, err := Items.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
o.copyMatchingRows(o2...)
|
|
|
|
return nil
|
|
}
|
|
|
|
type itemJoins[Q dialect.Joinable] struct {
|
|
typ string
|
|
User func(context.Context) modAs[Q, userColumns]
|
|
}
|
|
|
|
func (j itemJoins[Q]) aliasedAs(alias string) itemJoins[Q] {
|
|
return buildItemJoins[Q](buildItemColumns(alias), j.typ)
|
|
}
|
|
|
|
func buildItemJoins[Q dialect.Joinable](cols itemColumns, typ string) itemJoins[Q] {
|
|
return itemJoins[Q]{
|
|
typ: typ,
|
|
User: itemsJoinUser[Q](cols, typ),
|
|
}
|
|
}
|
|
|
|
func itemsJoinUser[Q dialect.Joinable](from itemColumns, 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
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
// User starts a query for related objects on user
|
|
func (o *Item) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
|
|
return Users.Query(append(mods,
|
|
sm.Where(UserColumns.ID.EQ(sqlite.Arg(o.UserID))),
|
|
)...)
|
|
}
|
|
|
|
func (os ItemSlice) 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...)),
|
|
)...)
|
|
}
|
|
|
|
func (o *Item) Preload(name string, retrieved any) error {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
|
|
switch name {
|
|
case "User":
|
|
rel, ok := retrieved.(*User)
|
|
if !ok {
|
|
return fmt.Errorf("item cannot load %T as %q", retrieved, name)
|
|
}
|
|
|
|
o.R.User = rel
|
|
|
|
if rel != nil {
|
|
rel.R.Items = ItemSlice{o}
|
|
}
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("item has no relationship %q", name)
|
|
}
|
|
}
|
|
|
|
func PreloadItemUser(opts ...sqlite.PreloadOption) sqlite.Preloader {
|
|
return sqlite.Preload[*User, UserSlice](orm.Relationship{
|
|
Name: "User",
|
|
Sides: []orm.RelSide{
|
|
{
|
|
From: TableNames.Items,
|
|
To: TableNames.Users,
|
|
FromColumns: []string{
|
|
ColumnNames.Items.UserID,
|
|
},
|
|
ToColumns: []string{
|
|
ColumnNames.Users.ID,
|
|
},
|
|
},
|
|
},
|
|
}, Users.Columns().Names(), opts...)
|
|
}
|
|
|
|
func ThenLoadItemUser(queryMods ...bob.Mod[*dialect.SelectQuery]) sqlite.Loader {
|
|
return sqlite.Loader(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
|
loader, isLoader := retrieved.(interface {
|
|
LoadItemUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
|
|
})
|
|
if !isLoader {
|
|
return fmt.Errorf("object %T cannot load ItemUser", retrieved)
|
|
}
|
|
|
|
err := loader.LoadItemUser(ctx, exec, queryMods...)
|
|
|
|
// Don't cause an issue due to missing relationships
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
})
|
|
}
|
|
|
|
// LoadItemUser loads the item's User into the .R struct
|
|
func (o *Item) LoadItemUser(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.Items = ItemSlice{o}
|
|
|
|
o.R.User = related
|
|
return nil
|
|
}
|
|
|
|
// LoadItemUser loads the item's User into the .R struct
|
|
func (os ItemSlice) LoadItemUser(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.Items = append(rel.R.Items, o)
|
|
|
|
o.R.User = rel
|
|
break
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func attachItemUser0(ctx context.Context, exec bob.Executor, count int, item0 *Item, user1 *User) (*Item, error) {
|
|
setter := &ItemSetter{
|
|
UserID: omit.From(user1.ID),
|
|
}
|
|
|
|
err := item0.Update(ctx, exec, setter)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("attachItemUser0: %w", err)
|
|
}
|
|
|
|
return item0, nil
|
|
}
|
|
|
|
func (item0 *Item) 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 = attachItemUser0(ctx, exec, 1, item0, user1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
item0.R.User = user1
|
|
|
|
user1.R.Items = append(user1.R.Items, item0)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (item0 *Item) AttachUser(ctx context.Context, exec bob.Executor, user1 *User) error {
|
|
var err error
|
|
|
|
_, err = attachItemUser0(ctx, exec, 1, item0, user1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
item0.R.User = user1
|
|
|
|
user1.R.Items = append(user1.R.Items, item0)
|
|
|
|
return nil
|
|
}
|