feat: bob

This commit is contained in:
2025-04-10 00:59:28 -04:00
parent dfd6789aa9
commit e9c44cbc94
58 changed files with 1649 additions and 3104 deletions

View File

@ -19,9 +19,9 @@ var TableNames = struct {
Items string
Users string
}{
Files: "files",
Items: "items",
Users: "users",
Files: "file",
Items: "item",
Users: "user",
}
var ColumnNames = struct {
@ -38,10 +38,10 @@ var ColumnNames = struct {
Items: itemColumnNames{
ID: "id",
Name: "name",
Added: "added",
Description: "description",
Price: "price",
Quantity: "quantity",
Added: "added",
UserID: "user_id",
},
Users: userColumnNames{
@ -49,7 +49,6 @@ var ColumnNames = struct {
Username: "username",
Password: "password",
ProfilePictureID: "profile_picture_id",
Challenge: "challenge",
},
}

View File

@ -28,12 +28,12 @@ func random_float32(f *faker.Faker) float32 {
return f.Float32(10, -1_000_000, 1_000_000)
}
func random_int32(f *faker.Faker) int32 {
func random_int64(f *faker.Faker) int64 {
if f == nil {
f = &defaultFaker
}
return f.Int32()
return f.Int64()
}
func random_string(f *faker.Faker) string {

View File

@ -8,14 +8,14 @@ import (
"testing"
)
func TestRandom_int32(t *testing.T) {
func TestRandom_int64(t *testing.T) {
t.Parallel()
val1 := random_int32(nil)
val2 := random_int32(nil)
val1 := random_int64(nil)
val2 := random_int64(nil)
if val1 == val2 {
t.Fatalf("random_int32() returned the same value twice: %v", val1)
t.Fatalf("random_int64() returned the same value twice: %v", val1)
}
}
@ -41,17 +41,6 @@ func TestRandom___byte(t *testing.T) {
}
}
func TestRandom_float32(t *testing.T) {
t.Parallel()
val1 := random_float32(nil)
val2 := random_float32(nil)
if val1 == val2 {
t.Fatalf("random_float32() returned the same value twice: %v", val1)
}
}
func TestRandom_time_Time(t *testing.T) {
t.Parallel()
@ -62,3 +51,14 @@ func TestRandom_time_Time(t *testing.T) {
t.Fatalf("random_time_Time() returned the same value twice: %v", val1)
}
}
func TestRandom_float32(t *testing.T) {
t.Parallel()
val1 := random_float32(nil)
val2 := random_float32(nil)
if val1 == val2 {
t.Fatalf("random_float32() returned the same value twice: %v", val1)
}
}

View File

@ -9,7 +9,6 @@ import (
"github.com/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/jaswdr/faker/v2"
models "github.com/spotdemo4/trevstack/server/internal/models"
"github.com/stephenafamo/bob"
@ -36,22 +35,27 @@ func (mods FileModSlice) Apply(n *FileTemplate) {
// FileTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type FileTemplate struct {
ID func() int32
Name func() null.Val[string]
Data func() null.Val[[]byte]
UserID func() null.Val[int32]
ID func() int64
Name func() string
Data func() []byte
UserID func() int64
r fileR
f *Factory
}
type fileR struct {
User *fileRUserR
User *fileRUserR
ProfilePictureUsers []*fileRProfilePictureUsersR
}
type fileRUserR struct {
o *UserTemplate
}
type fileRProfilePictureUsersR struct {
number int
o *UserTemplate
}
// Apply mods to the FileTemplate
func (o *FileTemplate) Apply(mods ...FileMod) {
@ -99,9 +103,22 @@ func (t FileTemplate) setModelRels(o *models.File) {
if t.r.User != nil {
rel := t.r.User.o.toModel()
rel.R.Files = append(rel.R.Files, o)
o.UserID = null.From(rel.ID)
o.UserID = rel.ID
o.R.User = rel
}
if t.r.ProfilePictureUsers != nil {
rel := models.UserSlice{}
for _, r := range t.r.ProfilePictureUsers {
related := r.o.toModels(r.number)
for _, rel := range related {
rel.ProfilePictureID = null.From(o.ID)
rel.R.ProfilePictureFile = o
}
rel = append(rel, related...)
}
o.R.ProfilePictureUsers = rel
}
}
// BuildSetter returns an *models.FileSetter
@ -113,13 +130,13 @@ func (o FileTemplate) BuildSetter() *models.FileSetter {
m.ID = omit.From(o.ID())
}
if o.Name != nil {
m.Name = omitnull.FromNull(o.Name())
m.Name = omit.From(o.Name())
}
if o.Data != nil {
m.Data = omitnull.FromNull(o.Data())
m.Data = omit.From(o.Data())
}
if o.UserID != nil {
m.UserID = omitnull.FromNull(o.UserID())
m.UserID = omit.From(o.UserID())
}
return m
@ -161,6 +178,15 @@ func (o FileTemplate) BuildMany(number int) models.FileSlice {
}
func ensureCreatableFile(m *models.FileSetter) {
if m.Name.IsUnset() {
m.Name = omit.From(random_string(nil))
}
if m.Data.IsUnset() {
m.Data = omit.From(random___byte(nil))
}
if m.UserID.IsUnset() {
m.UserID = omit.From(random_int64(nil))
}
}
// insertOptRels creates and inserts any optional the relationships on *models.File
@ -169,15 +195,18 @@ func ensureCreatableFile(m *models.FileSetter) {
func (o *FileTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.File) (context.Context, error) {
var err error
if o.r.User != nil {
var rel0 *models.User
ctx, rel0, err = o.r.User.o.create(ctx, exec)
if err != nil {
return ctx, err
}
err = m.AttachUser(ctx, exec, rel0)
if err != nil {
return ctx, err
if o.r.ProfilePictureUsers != nil {
for _, r := range o.r.ProfilePictureUsers {
var rel1 models.UserSlice
ctx, rel1, err = r.o.createMany(ctx, exec, r.number)
if err != nil {
return ctx, err
}
err = m.AttachProfilePictureUsers(ctx, exec, rel1...)
if err != nil {
return ctx, err
}
}
}
@ -223,12 +252,30 @@ func (o *FileTemplate) create(ctx context.Context, exec bob.Executor) (context.C
opt := o.BuildSetter()
ensureCreatableFile(opt)
var rel0 *models.User
if o.r.User == nil {
var ok bool
rel0, ok = userCtx.Value(ctx)
if !ok {
FileMods.WithNewUser().Apply(o)
}
}
if o.r.User != nil {
ctx, rel0, err = o.r.User.o.create(ctx, exec)
if err != nil {
return ctx, nil, err
}
}
opt.UserID = omit.From(rel0.ID)
m, err := models.Files.Insert(opt).One(ctx, exec)
if err != nil {
return ctx, nil, err
}
ctx = fileCtx.WithValue(ctx, m)
m.R.User = rel0
ctx, err = o.insertOptRels(ctx, exec, m)
return ctx, m, err
}
@ -296,14 +343,14 @@ func (m fileMods) RandomizeAllColumns(f *faker.Faker) FileMod {
}
// Set the model columns to this value
func (m fileMods) ID(val int32) FileMod {
func (m fileMods) ID(val int64) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.ID = func() int32 { return val }
o.ID = func() int64 { return val }
})
}
// Set the Column from the function
func (m fileMods) IDFunc(f func() int32) FileMod {
func (m fileMods) IDFunc(f func() int64) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.ID = f
})
@ -320,21 +367,21 @@ func (m fileMods) UnsetID() FileMod {
// if faker is nil, a default faker is used
func (m fileMods) RandomID(f *faker.Faker) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.ID = func() int32 {
return random_int32(f)
o.ID = func() int64 {
return random_int64(f)
}
})
}
// Set the model columns to this value
func (m fileMods) Name(val null.Val[string]) FileMod {
func (m fileMods) Name(val string) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.Name = func() null.Val[string] { return val }
o.Name = func() string { return val }
})
}
// Set the Column from the function
func (m fileMods) NameFunc(f func() null.Val[string]) FileMod {
func (m fileMods) NameFunc(f func() string) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.Name = f
})
@ -351,29 +398,21 @@ func (m fileMods) UnsetName() FileMod {
// if faker is nil, a default faker is used
func (m fileMods) RandomName(f *faker.Faker) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.Name = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[string](nil)
}
return null.From(random_string(f))
o.Name = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m fileMods) Data(val null.Val[[]byte]) FileMod {
func (m fileMods) Data(val []byte) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.Data = func() null.Val[[]byte] { return val }
o.Data = func() []byte { return val }
})
}
// Set the Column from the function
func (m fileMods) DataFunc(f func() null.Val[[]byte]) FileMod {
func (m fileMods) DataFunc(f func() []byte) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.Data = f
})
@ -390,29 +429,21 @@ func (m fileMods) UnsetData() FileMod {
// if faker is nil, a default faker is used
func (m fileMods) RandomData(f *faker.Faker) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.Data = func() null.Val[[]byte] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[[]byte](nil)
}
return null.From(random___byte(f))
o.Data = func() []byte {
return random___byte(f)
}
})
}
// Set the model columns to this value
func (m fileMods) UserID(val null.Val[int32]) FileMod {
func (m fileMods) UserID(val int64) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.UserID = func() null.Val[int32] { return val }
o.UserID = func() int64 { return val }
})
}
// Set the Column from the function
func (m fileMods) UserIDFunc(f func() null.Val[int32]) FileMod {
func (m fileMods) UserIDFunc(f func() int64) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.UserID = f
})
@ -429,16 +460,8 @@ func (m fileMods) UnsetUserID() FileMod {
// if faker is nil, a default faker is used
func (m fileMods) RandomUserID(f *faker.Faker) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.UserID = func() null.Val[int32] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[int32](nil)
}
return null.From(random_int32(f))
o.UserID = func() int64 {
return random_int64(f)
}
})
}
@ -464,3 +487,41 @@ func (m fileMods) WithoutUser() FileMod {
o.r.User = nil
})
}
func (m fileMods) WithProfilePictureUsers(number int, related *UserTemplate) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.r.ProfilePictureUsers = []*fileRProfilePictureUsersR{{
number: number,
o: related,
}}
})
}
func (m fileMods) WithNewProfilePictureUsers(number int, mods ...UserMod) FileMod {
return FileModFunc(func(o *FileTemplate) {
related := o.f.NewUser(mods...)
m.WithProfilePictureUsers(number, related).Apply(o)
})
}
func (m fileMods) AddProfilePictureUsers(number int, related *UserTemplate) FileMod {
return FileModFunc(func(o *FileTemplate) {
o.r.ProfilePictureUsers = append(o.r.ProfilePictureUsers, &fileRProfilePictureUsersR{
number: number,
o: related,
})
})
}
func (m fileMods) AddNewProfilePictureUsers(number int, mods ...UserMod) FileMod {
return FileModFunc(func(o *FileTemplate) {
related := o.f.NewUser(mods...)
m.AddProfilePictureUsers(number, related).Apply(o)
})
}
func (m fileMods) WithoutProfilePictureUsers() FileMod {
return FileModFunc(func(o *FileTemplate) {
o.r.ProfilePictureUsers = nil
})
}

View File

@ -37,13 +37,13 @@ func (mods ItemModSlice) Apply(n *ItemTemplate) {
// ItemTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type ItemTemplate struct {
ID func() int32
Name func() null.Val[string]
ID func() int64
Name func() string
Added func() time.Time
Description func() null.Val[string]
Price func() null.Val[float32]
Quantity func() null.Val[int32]
Added func() null.Val[time.Time]
UserID func() null.Val[int32]
Quantity func() null.Val[int64]
UserID func() int64
r itemR
f *Factory
@ -75,6 +75,9 @@ func (o ItemTemplate) toModel() *models.Item {
if o.Name != nil {
m.Name = o.Name()
}
if o.Added != nil {
m.Added = o.Added()
}
if o.Description != nil {
m.Description = o.Description()
}
@ -84,9 +87,6 @@ func (o ItemTemplate) toModel() *models.Item {
if o.Quantity != nil {
m.Quantity = o.Quantity()
}
if o.Added != nil {
m.Added = o.Added()
}
if o.UserID != nil {
m.UserID = o.UserID()
}
@ -112,7 +112,7 @@ func (t ItemTemplate) setModelRels(o *models.Item) {
if t.r.User != nil {
rel := t.r.User.o.toModel()
rel.R.Items = append(rel.R.Items, o)
o.UserID = null.From(rel.ID)
o.UserID = rel.ID
o.R.User = rel
}
}
@ -126,7 +126,10 @@ func (o ItemTemplate) BuildSetter() *models.ItemSetter {
m.ID = omit.From(o.ID())
}
if o.Name != nil {
m.Name = omitnull.FromNull(o.Name())
m.Name = omit.From(o.Name())
}
if o.Added != nil {
m.Added = omit.From(o.Added())
}
if o.Description != nil {
m.Description = omitnull.FromNull(o.Description())
@ -137,11 +140,8 @@ func (o ItemTemplate) BuildSetter() *models.ItemSetter {
if o.Quantity != nil {
m.Quantity = omitnull.FromNull(o.Quantity())
}
if o.Added != nil {
m.Added = omitnull.FromNull(o.Added())
}
if o.UserID != nil {
m.UserID = omitnull.FromNull(o.UserID())
m.UserID = omit.From(o.UserID())
}
return m
@ -183,6 +183,15 @@ func (o ItemTemplate) BuildMany(number int) models.ItemSlice {
}
func ensureCreatableItem(m *models.ItemSetter) {
if m.Name.IsUnset() {
m.Name = omit.From(random_string(nil))
}
if m.Added.IsUnset() {
m.Added = omit.From(random_time_Time(nil))
}
if m.UserID.IsUnset() {
m.UserID = omit.From(random_int64(nil))
}
}
// insertOptRels creates and inserts any optional the relationships on *models.Item
@ -191,18 +200,6 @@ func ensureCreatableItem(m *models.ItemSetter) {
func (o *ItemTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Item) (context.Context, error) {
var err error
if o.r.User != nil {
var rel0 *models.User
ctx, rel0, err = o.r.User.o.create(ctx, exec)
if err != nil {
return ctx, err
}
err = m.AttachUser(ctx, exec, rel0)
if err != nil {
return ctx, err
}
}
return ctx, err
}
@ -245,12 +242,30 @@ func (o *ItemTemplate) create(ctx context.Context, exec bob.Executor) (context.C
opt := o.BuildSetter()
ensureCreatableItem(opt)
var rel0 *models.User
if o.r.User == nil {
var ok bool
rel0, ok = userCtx.Value(ctx)
if !ok {
ItemMods.WithNewUser().Apply(o)
}
}
if o.r.User != nil {
ctx, rel0, err = o.r.User.o.create(ctx, exec)
if err != nil {
return ctx, nil, err
}
}
opt.UserID = omit.From(rel0.ID)
m, err := models.Items.Insert(opt).One(ctx, exec)
if err != nil {
return ctx, nil, err
}
ctx = itemCtx.WithValue(ctx, m)
m.R.User = rel0
ctx, err = o.insertOptRels(ctx, exec, m)
return ctx, m, err
}
@ -312,23 +327,23 @@ func (m itemMods) RandomizeAllColumns(f *faker.Faker) ItemMod {
return ItemModSlice{
ItemMods.RandomID(f),
ItemMods.RandomName(f),
ItemMods.RandomAdded(f),
ItemMods.RandomDescription(f),
ItemMods.RandomPrice(f),
ItemMods.RandomQuantity(f),
ItemMods.RandomAdded(f),
ItemMods.RandomUserID(f),
}
}
// Set the model columns to this value
func (m itemMods) ID(val int32) ItemMod {
func (m itemMods) ID(val int64) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.ID = func() int32 { return val }
o.ID = func() int64 { return val }
})
}
// Set the Column from the function
func (m itemMods) IDFunc(f func() int32) ItemMod {
func (m itemMods) IDFunc(f func() int64) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.ID = f
})
@ -345,21 +360,21 @@ func (m itemMods) UnsetID() ItemMod {
// if faker is nil, a default faker is used
func (m itemMods) RandomID(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.ID = func() int32 {
return random_int32(f)
o.ID = func() int64 {
return random_int64(f)
}
})
}
// Set the model columns to this value
func (m itemMods) Name(val null.Val[string]) ItemMod {
func (m itemMods) Name(val string) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Name = func() null.Val[string] { return val }
o.Name = func() string { return val }
})
}
// Set the Column from the function
func (m itemMods) NameFunc(f func() null.Val[string]) ItemMod {
func (m itemMods) NameFunc(f func() string) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Name = f
})
@ -376,16 +391,39 @@ func (m itemMods) UnsetName() ItemMod {
// if faker is nil, a default faker is used
func (m itemMods) RandomName(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Name = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
o.Name = func() string {
return random_string(f)
}
})
}
if f.Bool() {
return null.FromPtr[string](nil)
}
// Set the model columns to this value
func (m itemMods) Added(val time.Time) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = func() time.Time { return val }
})
}
return null.From(random_string(f))
// Set the Column from the function
func (m itemMods) AddedFunc(f func() time.Time) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = f
})
}
// Clear any values for the column
func (m itemMods) UnsetAdded() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m itemMods) RandomAdded(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = func() time.Time {
return random_time_Time(f)
}
})
}
@ -469,14 +507,14 @@ func (m itemMods) RandomPrice(f *faker.Faker) ItemMod {
}
// Set the model columns to this value
func (m itemMods) Quantity(val null.Val[int32]) ItemMod {
func (m itemMods) Quantity(val null.Val[int64]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = func() null.Val[int32] { return val }
o.Quantity = func() null.Val[int64] { return val }
})
}
// Set the Column from the function
func (m itemMods) QuantityFunc(f func() null.Val[int32]) ItemMod {
func (m itemMods) QuantityFunc(f func() null.Val[int64]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = f
})
@ -493,68 +531,29 @@ func (m itemMods) UnsetQuantity() ItemMod {
// if faker is nil, a default faker is used
func (m itemMods) RandomQuantity(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = func() null.Val[int32] {
o.Quantity = func() null.Val[int64] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[int32](nil)
return null.FromPtr[int64](nil)
}
return null.From(random_int32(f))
return null.From(random_int64(f))
}
})
}
// Set the model columns to this value
func (m itemMods) Added(val null.Val[time.Time]) ItemMod {
func (m itemMods) UserID(val int64) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = func() null.Val[time.Time] { return val }
o.UserID = func() int64 { return val }
})
}
// Set the Column from the function
func (m itemMods) AddedFunc(f func() null.Val[time.Time]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = f
})
}
// Clear any values for the column
func (m itemMods) UnsetAdded() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m itemMods) RandomAdded(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = func() null.Val[time.Time] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[time.Time](nil)
}
return null.From(random_time_Time(f))
}
})
}
// Set the model columns to this value
func (m itemMods) UserID(val null.Val[int32]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.UserID = func() null.Val[int32] { return val }
})
}
// Set the Column from the function
func (m itemMods) UserIDFunc(f func() null.Val[int32]) ItemMod {
func (m itemMods) UserIDFunc(f func() int64) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.UserID = f
})
@ -571,16 +570,8 @@ func (m itemMods) UnsetUserID() ItemMod {
// if faker is nil, a default faker is used
func (m itemMods) RandomUserID(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.UserID = func() null.Val[int32] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[int32](nil)
}
return null.From(random_int32(f))
o.UserID = func() int64 {
return random_int64(f)
}
})
}

View File

@ -36,19 +36,19 @@ func (mods UserModSlice) Apply(n *UserTemplate) {
// UserTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type UserTemplate struct {
ID func() int32
Username func() null.Val[string]
Password func() null.Val[string]
ProfilePictureID func() null.Val[int32]
Challenge func() null.Val[string]
ID func() int64
Username func() string
Password func() string
ProfilePictureID func() null.Val[int64]
r userR
f *Factory
}
type userR struct {
Files []*userRFilesR
Items []*userRItemsR
Files []*userRFilesR
Items []*userRItemsR
ProfilePictureFile *userRProfilePictureFileR
}
type userRFilesR struct {
@ -59,6 +59,9 @@ type userRItemsR struct {
number int
o *ItemTemplate
}
type userRProfilePictureFileR struct {
o *FileTemplate
}
// Apply mods to the UserTemplate
func (o *UserTemplate) Apply(mods ...UserMod) {
@ -84,9 +87,6 @@ func (o UserTemplate) toModel() *models.User {
if o.ProfilePictureID != nil {
m.ProfilePictureID = o.ProfilePictureID()
}
if o.Challenge != nil {
m.Challenge = o.Challenge()
}
return m
}
@ -111,7 +111,7 @@ func (t UserTemplate) setModelRels(o *models.User) {
for _, r := range t.r.Files {
related := r.o.toModels(r.number)
for _, rel := range related {
rel.UserID = null.From(o.ID)
rel.UserID = o.ID
rel.R.User = o
}
rel = append(rel, related...)
@ -124,13 +124,20 @@ func (t UserTemplate) setModelRels(o *models.User) {
for _, r := range t.r.Items {
related := r.o.toModels(r.number)
for _, rel := range related {
rel.UserID = null.From(o.ID)
rel.UserID = o.ID
rel.R.User = o
}
rel = append(rel, related...)
}
o.R.Items = rel
}
if t.r.ProfilePictureFile != nil {
rel := t.r.ProfilePictureFile.o.toModel()
rel.R.ProfilePictureUsers = append(rel.R.ProfilePictureUsers, o)
o.ProfilePictureID = null.From(rel.ID)
o.R.ProfilePictureFile = rel
}
}
// BuildSetter returns an *models.UserSetter
@ -142,17 +149,14 @@ func (o UserTemplate) BuildSetter() *models.UserSetter {
m.ID = omit.From(o.ID())
}
if o.Username != nil {
m.Username = omitnull.FromNull(o.Username())
m.Username = omit.From(o.Username())
}
if o.Password != nil {
m.Password = omitnull.FromNull(o.Password())
m.Password = omit.From(o.Password())
}
if o.ProfilePictureID != nil {
m.ProfilePictureID = omitnull.FromNull(o.ProfilePictureID())
}
if o.Challenge != nil {
m.Challenge = omitnull.FromNull(o.Challenge())
}
return m
}
@ -193,6 +197,12 @@ func (o UserTemplate) BuildMany(number int) models.UserSlice {
}
func ensureCreatableUser(m *models.UserSetter) {
if m.Username.IsUnset() {
m.Username = omit.From(random_string(nil))
}
if m.Password.IsUnset() {
m.Password = omit.From(random_string(nil))
}
}
// insertOptRels creates and inserts any optional the relationships on *models.User
@ -231,6 +241,18 @@ func (o *UserTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *
}
}
if o.r.ProfilePictureFile != nil {
var rel2 *models.File
ctx, rel2, err = o.r.ProfilePictureFile.o.create(ctx, exec)
if err != nil {
return ctx, err
}
err = m.AttachProfilePictureFile(ctx, exec, rel2)
if err != nil {
return ctx, err
}
}
return ctx, err
}
@ -342,19 +364,18 @@ func (m userMods) RandomizeAllColumns(f *faker.Faker) UserMod {
UserMods.RandomUsername(f),
UserMods.RandomPassword(f),
UserMods.RandomProfilePictureID(f),
UserMods.RandomChallenge(f),
}
}
// Set the model columns to this value
func (m userMods) ID(val int32) UserMod {
func (m userMods) ID(val int64) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.ID = func() int32 { return val }
o.ID = func() int64 { return val }
})
}
// Set the Column from the function
func (m userMods) IDFunc(f func() int32) UserMod {
func (m userMods) IDFunc(f func() int64) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.ID = f
})
@ -371,21 +392,21 @@ func (m userMods) UnsetID() UserMod {
// if faker is nil, a default faker is used
func (m userMods) RandomID(f *faker.Faker) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.ID = func() int32 {
return random_int32(f)
o.ID = func() int64 {
return random_int64(f)
}
})
}
// Set the model columns to this value
func (m userMods) Username(val null.Val[string]) UserMod {
func (m userMods) Username(val string) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Username = func() null.Val[string] { return val }
o.Username = func() string { return val }
})
}
// Set the Column from the function
func (m userMods) UsernameFunc(f func() null.Val[string]) UserMod {
func (m userMods) UsernameFunc(f func() string) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Username = f
})
@ -402,29 +423,21 @@ func (m userMods) UnsetUsername() UserMod {
// if faker is nil, a default faker is used
func (m userMods) RandomUsername(f *faker.Faker) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Username = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[string](nil)
}
return null.From(random_string(f))
o.Username = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m userMods) Password(val null.Val[string]) UserMod {
func (m userMods) Password(val string) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Password = func() null.Val[string] { return val }
o.Password = func() string { return val }
})
}
// Set the Column from the function
func (m userMods) PasswordFunc(f func() null.Val[string]) UserMod {
func (m userMods) PasswordFunc(f func() string) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Password = f
})
@ -441,29 +454,21 @@ func (m userMods) UnsetPassword() UserMod {
// if faker is nil, a default faker is used
func (m userMods) RandomPassword(f *faker.Faker) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Password = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[string](nil)
}
return null.From(random_string(f))
o.Password = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m userMods) ProfilePictureID(val null.Val[int32]) UserMod {
func (m userMods) ProfilePictureID(val null.Val[int64]) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.ProfilePictureID = func() null.Val[int32] { return val }
o.ProfilePictureID = func() null.Val[int64] { return val }
})
}
// Set the Column from the function
func (m userMods) ProfilePictureIDFunc(f func() null.Val[int32]) UserMod {
func (m userMods) ProfilePictureIDFunc(f func() null.Val[int64]) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.ProfilePictureID = f
})
@ -480,59 +485,42 @@ func (m userMods) UnsetProfilePictureID() UserMod {
// if faker is nil, a default faker is used
func (m userMods) RandomProfilePictureID(f *faker.Faker) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.ProfilePictureID = func() null.Val[int32] {
o.ProfilePictureID = func() null.Val[int64] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[int32](nil)
return null.FromPtr[int64](nil)
}
return null.From(random_int32(f))
return null.From(random_int64(f))
}
})
}
// Set the model columns to this value
func (m userMods) Challenge(val null.Val[string]) UserMod {
func (m userMods) WithProfilePictureFile(rel *FileTemplate) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Challenge = func() null.Val[string] { return val }
})
}
// Set the Column from the function
func (m userMods) ChallengeFunc(f func() null.Val[string]) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Challenge = f
})
}
// Clear any values for the column
func (m userMods) UnsetChallenge() UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Challenge = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m userMods) RandomChallenge(f *faker.Faker) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.Challenge = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[string](nil)
}
return null.From(random_string(f))
o.r.ProfilePictureFile = &userRProfilePictureFileR{
o: rel,
}
})
}
func (m userMods) WithNewProfilePictureFile(mods ...FileMod) UserMod {
return UserModFunc(func(o *UserTemplate) {
related := o.f.NewFile(mods...)
m.WithProfilePictureFile(related).Apply(o)
})
}
func (m userMods) WithoutProfilePictureFile() UserMod {
return UserModFunc(func(o *UserTemplate) {
o.r.ProfilePictureFile = nil
})
}
func (m userMods) WithFiles(number int, related *FileTemplate) UserMod {
return UserModFunc(func(o *UserTemplate) {
o.r.Files = []*userRFilesR{{

View File

@ -10,7 +10,6 @@ import (
"fmt"
"io"
"github.com/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/stephenafamo/bob"
@ -26,10 +25,10 @@ import (
// File is an object representing the database table.
type File struct {
ID int32 `db:"id,pk" `
Name null.Val[string] `db:"name" `
Data null.Val[[]byte] `db:"data" `
UserID null.Val[int32] `db:"user_id" `
ID int64 `db:"id,pk" `
Name string `db:"name" `
Data []byte `db:"data" `
UserID int64 `db:"user_id" `
R fileR `db:"-" `
}
@ -38,15 +37,16 @@ type File struct {
// This should almost always be used instead of []*File.
type FileSlice []*File
// Files contains methods to work with the files table
var Files = sqlite.NewTablex[*File, FileSlice, *FileSetter]("", "files")
// Files contains methods to work with the file table
var Files = sqlite.NewTablex[*File, FileSlice, *FileSetter]("", "file")
// FilesQuery is a query on the files table
// 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_files_0
User *User // fk_file_0
ProfilePictureUsers UserSlice // fk_user_0
}
type fileColumnNames struct {
@ -56,7 +56,7 @@ type fileColumnNames struct {
UserID string
}
var FileColumns = buildFileColumns("files")
var FileColumns = buildFileColumns("file")
type fileColumns struct {
tableAlias string
@ -85,10 +85,10 @@ func buildFileColumns(alias string) fileColumns {
}
type fileWhere[Q sqlite.Filterable] struct {
ID sqlite.WhereMod[Q, int32]
Name sqlite.WhereNullMod[Q, string]
Data sqlite.WhereNullMod[Q, []byte]
UserID sqlite.WhereNullMod[Q, int32]
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] {
@ -97,29 +97,29 @@ func (fileWhere[Q]) AliasedAs(alias string) fileWhere[Q] {
func buildFileWhere[Q sqlite.Filterable](cols fileColumns) fileWhere[Q] {
return fileWhere[Q]{
ID: sqlite.Where[Q, int32](cols.ID),
Name: sqlite.WhereNull[Q, string](cols.Name),
Data: sqlite.WhereNull[Q, []byte](cols.Data),
UserID: sqlite.WhereNull[Q, int32](cols.UserID),
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{
ErrUniquePkMainFiles: &UniqueConstraintError{s: "pk_main_files"},
ErrUniquePkMainFile: &UniqueConstraintError{s: "pk_main_file"},
}
type fileErrors struct {
ErrUniquePkMainFiles *UniqueConstraintError
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[int32] `db:"id,pk" `
Name omitnull.Val[string] `db:"name" `
Data omitnull.Val[[]byte] `db:"data" `
UserID omitnull.Val[int32] `db:"user_id" `
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 {
@ -148,13 +148,13 @@ func (s FileSetter) Overwrite(t *File) {
t.ID, _ = s.ID.Get()
}
if !s.Name.IsUnset() {
t.Name, _ = s.Name.GetNull()
t.Name, _ = s.Name.Get()
}
if !s.Data.IsUnset() {
t.Data, _ = s.Data.GetNull()
t.Data, _ = s.Data.Get()
}
if !s.UserID.IsUnset() {
t.UserID, _ = s.UserID.GetNull()
t.UserID, _ = s.UserID.Get()
}
}
@ -229,7 +229,7 @@ func (s FileSetter) Expressions(prefix ...string) []bob.Expression {
// 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 int32, cols ...string) (*File, error) {
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),
@ -243,7 +243,7 @@ func FindFile(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string
}
// FileExists checks the presence of a single record by primary key
func FileExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
func FileExists(ctx context.Context, exec bob.Executor, IDPK int64) (bool, error) {
return Files.Query(
SelectWhere.Files.ID.EQ(IDPK),
).Exists(ctx, exec)
@ -273,7 +273,7 @@ func (o *File) PrimaryKeyVals() bob.Expression {
}
func (o *File) pkEQ() dialect.Expression {
return sqlite.Quote("files", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
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)
}))
}
@ -334,7 +334,7 @@ func (o FileSlice) pkIN() dialect.Expression {
return sqlite.Raw("NULL")
}
return sqlite.Quote("files", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
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()
@ -451,8 +451,9 @@ func (o FileSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
}
type fileJoins[Q dialect.Joinable] struct {
typ string
User func(context.Context) modAs[Q, userColumns]
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] {
@ -461,8 +462,9 @@ func (j fileJoins[Q]) aliasedAs(alias string) fileJoins[Q] {
func buildFileJoins[Q dialect.Joinable](cols fileColumns, typ string) fileJoins[Q] {
return fileJoins[Q]{
typ: typ,
User: filesJoinUser[Q](cols, typ),
typ: typ,
User: filesJoinUser[Q](cols, typ),
ProfilePictureUsers: filesJoinProfilePictureUsers[Q](cols, typ),
}
}
@ -485,7 +487,26 @@ func filesJoinUser[Q dialect.Joinable](from fileColumns, typ string) func(contex
}
}
// User starts a query for related objects on users
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))),
@ -503,6 +524,24 @@ func (os FileSlice) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
)...)
}
// 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
@ -521,6 +560,20 @@ func (o *File) Preload(name string, retrieved any) error {
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)
}
@ -597,7 +650,7 @@ func (os FileSlice) LoadFileUser(ctx context.Context, exec bob.Executor, mods ..
for _, o := range os {
for _, rel := range users {
if o.UserID.GetOrZero() != rel.ID {
if o.UserID != rel.ID {
continue
}
@ -611,9 +664,81 @@ func (os FileSlice) LoadFileUser(ctx context.Context, exec bob.Executor, mods ..
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: omitnull.From(user1.ID),
UserID: omit.From(user1.ID),
}
err := file0.Update(ctx, exec, setter)
@ -656,3 +781,71 @@ func (file0 *File) AttachUser(ctx context.Context, exec bob.Executor, user1 *Use
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
}

View File

@ -27,13 +27,13 @@ import (
// Item is an object representing the database table.
type Item struct {
ID int32 `db:"id,pk" `
Name null.Val[string] `db:"name" `
Description null.Val[string] `db:"description" `
Price null.Val[float32] `db:"price" `
Quantity null.Val[int32] `db:"quantity" `
Added null.Val[time.Time] `db:"added" `
UserID null.Val[int32] `db:"user_id" `
ID int64 `db:"id,pk" `
Name string `db:"name" `
Added time.Time `db:"added" `
Description null.Val[string] `db:"description" `
Price null.Val[float32] `db:"price" `
Quantity null.Val[int64] `db:"quantity" `
UserID int64 `db:"user_id" `
R itemR `db:"-" `
}
@ -42,37 +42,37 @@ type Item struct {
// This should almost always be used instead of []*Item.
type ItemSlice []*Item
// Items contains methods to work with the items table
var Items = sqlite.NewTablex[*Item, ItemSlice, *ItemSetter]("", "items")
// Items contains methods to work with the item table
var Items = sqlite.NewTablex[*Item, ItemSlice, *ItemSetter]("", "item")
// ItemsQuery is a query on the items table
// 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_items_0
User *User // fk_item_0
}
type itemColumnNames struct {
ID string
Name string
Added string
Description string
Price string
Quantity string
Added string
UserID string
}
var ItemColumns = buildItemColumns("items")
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
Added sqlite.Expression
UserID sqlite.Expression
}
@ -89,22 +89,22 @@ func buildItemColumns(alias string) 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"),
Added: sqlite.Quote(alias, "added"),
UserID: sqlite.Quote(alias, "user_id"),
}
}
type itemWhere[Q sqlite.Filterable] struct {
ID sqlite.WhereMod[Q, int32]
Name sqlite.WhereNullMod[Q, string]
ID sqlite.WhereMod[Q, int64]
Name sqlite.WhereMod[Q, string]
Added sqlite.WhereMod[Q, time.Time]
Description sqlite.WhereNullMod[Q, string]
Price sqlite.WhereNullMod[Q, float32]
Quantity sqlite.WhereNullMod[Q, int32]
Added sqlite.WhereNullMod[Q, time.Time]
UserID sqlite.WhereNullMod[Q, int32]
Quantity sqlite.WhereNullMod[Q, int64]
UserID sqlite.WhereMod[Q, int64]
}
func (itemWhere[Q]) AliasedAs(alias string) itemWhere[Q] {
@ -113,35 +113,35 @@ func (itemWhere[Q]) AliasedAs(alias string) itemWhere[Q] {
func buildItemWhere[Q sqlite.Filterable](cols itemColumns) itemWhere[Q] {
return itemWhere[Q]{
ID: sqlite.Where[Q, int32](cols.ID),
Name: sqlite.WhereNull[Q, string](cols.Name),
ID: sqlite.Where[Q, int64](cols.ID),
Name: sqlite.Where[Q, string](cols.Name),
Added: sqlite.Where[Q, time.Time](cols.Added),
Description: sqlite.WhereNull[Q, string](cols.Description),
Price: sqlite.WhereNull[Q, float32](cols.Price),
Quantity: sqlite.WhereNull[Q, int32](cols.Quantity),
Added: sqlite.WhereNull[Q, time.Time](cols.Added),
UserID: sqlite.WhereNull[Q, int32](cols.UserID),
Quantity: sqlite.WhereNull[Q, int64](cols.Quantity),
UserID: sqlite.Where[Q, int64](cols.UserID),
}
}
var ItemErrors = &itemErrors{
ErrUniquePkMainItems: &UniqueConstraintError{s: "pk_main_items"},
ErrUniquePkMainItem: &UniqueConstraintError{s: "pk_main_item"},
}
type itemErrors struct {
ErrUniquePkMainItems *UniqueConstraintError
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[int32] `db:"id,pk" `
Name omitnull.Val[string] `db:"name" `
Description omitnull.Val[string] `db:"description" `
Price omitnull.Val[float32] `db:"price" `
Quantity omitnull.Val[int32] `db:"quantity" `
Added omitnull.Val[time.Time] `db:"added" `
UserID omitnull.Val[int32] `db:"user_id" `
ID omit.Val[int64] `db:"id,pk" `
Name omit.Val[string] `db:"name" `
Added omit.Val[time.Time] `db:"added" `
Description omitnull.Val[string] `db:"description" `
Price omitnull.Val[float32] `db:"price" `
Quantity omitnull.Val[int64] `db:"quantity" `
UserID omit.Val[int64] `db:"user_id" `
}
func (s ItemSetter) SetColumns() []string {
@ -154,6 +154,10 @@ func (s ItemSetter) SetColumns() []string {
vals = append(vals, "name")
}
if !s.Added.IsUnset() {
vals = append(vals, "added")
}
if !s.Description.IsUnset() {
vals = append(vals, "description")
}
@ -166,10 +170,6 @@ func (s ItemSetter) SetColumns() []string {
vals = append(vals, "quantity")
}
if !s.Added.IsUnset() {
vals = append(vals, "added")
}
if !s.UserID.IsUnset() {
vals = append(vals, "user_id")
}
@ -182,7 +182,10 @@ func (s ItemSetter) Overwrite(t *Item) {
t.ID, _ = s.ID.Get()
}
if !s.Name.IsUnset() {
t.Name, _ = s.Name.GetNull()
t.Name, _ = s.Name.Get()
}
if !s.Added.IsUnset() {
t.Added, _ = s.Added.Get()
}
if !s.Description.IsUnset() {
t.Description, _ = s.Description.GetNull()
@ -193,11 +196,8 @@ func (s ItemSetter) Overwrite(t *Item) {
if !s.Quantity.IsUnset() {
t.Quantity, _ = s.Quantity.GetNull()
}
if !s.Added.IsUnset() {
t.Added, _ = s.Added.GetNull()
}
if !s.UserID.IsUnset() {
t.UserID, _ = s.UserID.GetNull()
t.UserID, _ = s.UserID.Get()
}
}
@ -220,6 +220,10 @@ func (s *ItemSetter) Apply(q *dialect.InsertQuery) {
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))
}
@ -232,10 +236,6 @@ func (s *ItemSetter) Apply(q *dialect.InsertQuery) {
vals = append(vals, sqlite.Arg(s.Quantity))
}
if !s.Added.IsUnset() {
vals = append(vals, sqlite.Arg(s.Added))
}
if !s.UserID.IsUnset() {
vals = append(vals, sqlite.Arg(s.UserID))
}
@ -265,6 +265,13 @@ func (s ItemSetter) Expressions(prefix ...string) []bob.Expression {
}})
}
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")...),
@ -286,13 +293,6 @@ func (s ItemSetter) Expressions(prefix ...string) []bob.Expression {
}})
}
if !s.Added.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "added")...),
sqlite.Arg(s.Added),
}})
}
if !s.UserID.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "user_id")...),
@ -305,7 +305,7 @@ func (s ItemSetter) Expressions(prefix ...string) []bob.Expression {
// 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 int32, cols ...string) (*Item, error) {
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),
@ -319,7 +319,7 @@ func FindItem(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string
}
// ItemExists checks the presence of a single record by primary key
func ItemExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
func ItemExists(ctx context.Context, exec bob.Executor, IDPK int64) (bool, error) {
return Items.Query(
SelectWhere.Items.ID.EQ(IDPK),
).Exists(ctx, exec)
@ -349,7 +349,7 @@ func (o *Item) PrimaryKeyVals() bob.Expression {
}
func (o *Item) pkEQ() dialect.Expression {
return sqlite.Quote("items", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
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)
}))
}
@ -410,7 +410,7 @@ func (o ItemSlice) pkIN() dialect.Expression {
return sqlite.Raw("NULL")
}
return sqlite.Quote("items", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
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()
@ -561,7 +561,7 @@ func itemsJoinUser[Q dialect.Joinable](from itemColumns, typ string) func(contex
}
}
// User starts a query for related objects on users
// 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))),
@ -673,7 +673,7 @@ func (os ItemSlice) LoadItemUser(ctx context.Context, exec bob.Executor, mods ..
for _, o := range os {
for _, rel := range users {
if o.UserID.GetOrZero() != rel.ID {
if o.UserID != rel.ID {
continue
}
@ -689,7 +689,7 @@ func (os ItemSlice) LoadItemUser(ctx context.Context, exec bob.Executor, mods ..
func attachItemUser0(ctx context.Context, exec bob.Executor, count int, item0 *Item, user1 *User) (*Item, error) {
setter := &ItemSetter{
UserID: omitnull.From(user1.ID),
UserID: omit.From(user1.ID),
}
err := item0.Update(ctx, exec, setter)

View File

@ -21,15 +21,15 @@ import (
"github.com/stephenafamo/bob/dialect/sqlite/um"
"github.com/stephenafamo/bob/expr"
"github.com/stephenafamo/bob/mods"
"github.com/stephenafamo/bob/orm"
)
// User is an object representing the database table.
type User struct {
ID int32 `db:"id,pk" `
Username null.Val[string] `db:"username" `
Password null.Val[string] `db:"password" `
ProfilePictureID null.Val[int32] `db:"profile_picture_id" `
Challenge null.Val[string] `db:"challenge" `
ID int64 `db:"id,pk" `
Username string `db:"username" `
Password string `db:"password" `
ProfilePictureID null.Val[int64] `db:"profile_picture_id" `
R userR `db:"-" `
}
@ -38,16 +38,17 @@ type User struct {
// This should almost always be used instead of []*User.
type UserSlice []*User
// Users contains methods to work with the users table
var Users = sqlite.NewTablex[*User, UserSlice, *UserSetter]("", "users")
// Users contains methods to work with the user table
var Users = sqlite.NewTablex[*User, UserSlice, *UserSetter]("", "user")
// UsersQuery is a query on the users table
// UsersQuery is a query on the user table
type UsersQuery = *sqlite.ViewQuery[*User, UserSlice]
// userR is where relationships are stored.
type userR struct {
Files FileSlice // fk_files_0
Items ItemSlice // fk_items_0
Files FileSlice // fk_file_0
Items ItemSlice // fk_item_0
ProfilePictureFile *File // fk_user_0
}
type userColumnNames struct {
@ -55,10 +56,9 @@ type userColumnNames struct {
Username string
Password string
ProfilePictureID string
Challenge string
}
var UserColumns = buildUserColumns("users")
var UserColumns = buildUserColumns("user")
type userColumns struct {
tableAlias string
@ -66,7 +66,6 @@ type userColumns struct {
Username sqlite.Expression
Password sqlite.Expression
ProfilePictureID sqlite.Expression
Challenge sqlite.Expression
}
func (c userColumns) Alias() string {
@ -84,16 +83,14 @@ func buildUserColumns(alias string) userColumns {
Username: sqlite.Quote(alias, "username"),
Password: sqlite.Quote(alias, "password"),
ProfilePictureID: sqlite.Quote(alias, "profile_picture_id"),
Challenge: sqlite.Quote(alias, "challenge"),
}
}
type userWhere[Q sqlite.Filterable] struct {
ID sqlite.WhereMod[Q, int32]
Username sqlite.WhereNullMod[Q, string]
Password sqlite.WhereNullMod[Q, string]
ProfilePictureID sqlite.WhereNullMod[Q, int32]
Challenge sqlite.WhereNullMod[Q, string]
ID sqlite.WhereMod[Q, int64]
Username sqlite.WhereMod[Q, string]
Password sqlite.WhereMod[Q, string]
ProfilePictureID sqlite.WhereNullMod[Q, int64]
}
func (userWhere[Q]) AliasedAs(alias string) userWhere[Q] {
@ -102,35 +99,33 @@ func (userWhere[Q]) AliasedAs(alias string) userWhere[Q] {
func buildUserWhere[Q sqlite.Filterable](cols userColumns) userWhere[Q] {
return userWhere[Q]{
ID: sqlite.Where[Q, int32](cols.ID),
Username: sqlite.WhereNull[Q, string](cols.Username),
Password: sqlite.WhereNull[Q, string](cols.Password),
ProfilePictureID: sqlite.WhereNull[Q, int32](cols.ProfilePictureID),
Challenge: sqlite.WhereNull[Q, string](cols.Challenge),
ID: sqlite.Where[Q, int64](cols.ID),
Username: sqlite.Where[Q, string](cols.Username),
Password: sqlite.Where[Q, string](cols.Password),
ProfilePictureID: sqlite.WhereNull[Q, int64](cols.ProfilePictureID),
}
}
var UserErrors = &userErrors{
ErrUniquePkMainUsers: &UniqueConstraintError{s: "pk_main_users"},
ErrUniquePkMainUser: &UniqueConstraintError{s: "pk_main_user"},
}
type userErrors struct {
ErrUniquePkMainUsers *UniqueConstraintError
ErrUniquePkMainUser *UniqueConstraintError
}
// 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" `
Username omitnull.Val[string] `db:"username" `
Password omitnull.Val[string] `db:"password" `
ProfilePictureID omitnull.Val[int32] `db:"profile_picture_id" `
Challenge omitnull.Val[string] `db:"challenge" `
ID omit.Val[int64] `db:"id,pk" `
Username omit.Val[string] `db:"username" `
Password omit.Val[string] `db:"password" `
ProfilePictureID omitnull.Val[int64] `db:"profile_picture_id" `
}
func (s UserSetter) SetColumns() []string {
vals := make([]string, 0, 5)
vals := make([]string, 0, 4)
if !s.ID.IsUnset() {
vals = append(vals, "id")
}
@ -147,10 +142,6 @@ func (s UserSetter) SetColumns() []string {
vals = append(vals, "profile_picture_id")
}
if !s.Challenge.IsUnset() {
vals = append(vals, "challenge")
}
return vals
}
@ -159,17 +150,14 @@ func (s UserSetter) Overwrite(t *User) {
t.ID, _ = s.ID.Get()
}
if !s.Username.IsUnset() {
t.Username, _ = s.Username.GetNull()
t.Username, _ = s.Username.Get()
}
if !s.Password.IsUnset() {
t.Password, _ = s.Password.GetNull()
t.Password, _ = s.Password.Get()
}
if !s.ProfilePictureID.IsUnset() {
t.ProfilePictureID, _ = s.ProfilePictureID.GetNull()
}
if !s.Challenge.IsUnset() {
t.Challenge, _ = s.Challenge.GetNull()
}
}
func (s *UserSetter) Apply(q *dialect.InsertQuery) {
@ -182,7 +170,7 @@ func (s *UserSetter) Apply(q *dialect.InsertQuery) {
}
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
vals := make([]bob.Expression, 0, 5)
vals := make([]bob.Expression, 0, 4)
if !s.ID.IsUnset() {
vals = append(vals, sqlite.Arg(s.ID))
}
@ -199,10 +187,6 @@ func (s *UserSetter) Apply(q *dialect.InsertQuery) {
vals = append(vals, sqlite.Arg(s.ProfilePictureID))
}
if !s.Challenge.IsUnset() {
vals = append(vals, sqlite.Arg(s.Challenge))
}
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
@ -212,7 +196,7 @@ func (s UserSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
}
func (s UserSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 5)
exprs := make([]bob.Expression, 0, 4)
if !s.ID.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
@ -242,19 +226,12 @@ func (s UserSetter) Expressions(prefix ...string) []bob.Expression {
}})
}
if !s.Challenge.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "challenge")...),
sqlite.Arg(s.Challenge),
}})
}
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) {
func FindUser(ctx context.Context, exec bob.Executor, IDPK int64, cols ...string) (*User, error) {
if len(cols) == 0 {
return Users.Query(
SelectWhere.Users.ID.EQ(IDPK),
@ -268,7 +245,7 @@ func FindUser(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string
}
// UserExists checks the presence of a single record by primary key
func UserExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
func UserExists(ctx context.Context, exec bob.Executor, IDPK int64) (bool, error) {
return Users.Query(
SelectWhere.Users.ID.EQ(IDPK),
).Exists(ctx, exec)
@ -298,7 +275,7 @@ func (o *User) PrimaryKeyVals() bob.Expression {
}
func (o *User) pkEQ() dialect.Expression {
return sqlite.Quote("users", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
return sqlite.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)
}))
}
@ -359,7 +336,7 @@ func (o UserSlice) pkIN() dialect.Expression {
return sqlite.Raw("NULL")
}
return sqlite.Quote("users", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
return sqlite.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()
@ -476,9 +453,10 @@ func (o UserSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
}
type userJoins[Q dialect.Joinable] struct {
typ string
Files func(context.Context) modAs[Q, fileColumns]
Items func(context.Context) modAs[Q, itemColumns]
typ string
Files func(context.Context) modAs[Q, fileColumns]
Items func(context.Context) modAs[Q, itemColumns]
ProfilePictureFile func(context.Context) modAs[Q, fileColumns]
}
func (j userJoins[Q]) aliasedAs(alias string) userJoins[Q] {
@ -487,9 +465,10 @@ func (j userJoins[Q]) aliasedAs(alias string) userJoins[Q] {
func buildUserJoins[Q dialect.Joinable](cols userColumns, typ string) userJoins[Q] {
return userJoins[Q]{
typ: typ,
Files: usersJoinFiles[Q](cols, typ),
Items: usersJoinItems[Q](cols, typ),
typ: typ,
Files: usersJoinFiles[Q](cols, typ),
Items: usersJoinItems[Q](cols, typ),
ProfilePictureFile: usersJoinProfilePictureFile[Q](cols, typ),
}
}
@ -531,7 +510,26 @@ func usersJoinItems[Q dialect.Joinable](from userColumns, typ string) func(conte
}
}
// Files starts a query for related objects on files
func usersJoinProfilePictureFile[Q dialect.Joinable](from userColumns, typ string) func(context.Context) modAs[Q, fileColumns] {
return func(ctx context.Context) modAs[Q, fileColumns] {
return modAs[Q, fileColumns]{
c: FileColumns,
f: func(to fileColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, Files.Name().As(to.Alias())).On(
to.ID.EQ(from.ProfilePictureID),
))
}
return mods
},
}
}
}
// Files starts a query for related objects on file
func (o *User) Files(mods ...bob.Mod[*dialect.SelectQuery]) FilesQuery {
return Files.Query(append(mods,
sm.Where(FileColumns.UserID.EQ(sqlite.Arg(o.ID))),
@ -549,7 +547,7 @@ func (os UserSlice) Files(mods ...bob.Mod[*dialect.SelectQuery]) FilesQuery {
)...)
}
// Items starts a query for related objects on items
// Items starts a query for related objects on item
func (o *User) Items(mods ...bob.Mod[*dialect.SelectQuery]) ItemsQuery {
return Items.Query(append(mods,
sm.Where(ItemColumns.UserID.EQ(sqlite.Arg(o.ID))),
@ -567,6 +565,24 @@ func (os UserSlice) Items(mods ...bob.Mod[*dialect.SelectQuery]) ItemsQuery {
)...)
}
// ProfilePictureFile starts a query for related objects on file
func (o *User) ProfilePictureFile(mods ...bob.Mod[*dialect.SelectQuery]) FilesQuery {
return Files.Query(append(mods,
sm.Where(FileColumns.ID.EQ(sqlite.Arg(o.ProfilePictureID))),
)...)
}
func (os UserSlice) ProfilePictureFile(mods ...bob.Mod[*dialect.SelectQuery]) FilesQuery {
PKArgs := make([]bob.Expression, len(os))
for i, o := range os {
PKArgs[i] = sqlite.ArgGroup(o.ProfilePictureID)
}
return Files.Query(append(mods,
sm.Where(sqlite.Group(FileColumns.ID).In(PKArgs...)),
)...)
}
func (o *User) Preload(name string, retrieved any) error {
if o == nil {
return nil
@ -601,6 +617,18 @@ func (o *User) Preload(name string, retrieved any) error {
}
}
return nil
case "ProfilePictureFile":
rel, ok := retrieved.(*File)
if !ok {
return fmt.Errorf("user cannot load %T as %q", retrieved, name)
}
o.R.ProfilePictureFile = rel
if rel != nil {
rel.R.ProfilePictureUsers = UserSlice{o}
}
return nil
default:
return fmt.Errorf("user has no relationship %q", name)
}
@ -665,7 +693,7 @@ func (os UserSlice) LoadUserFiles(ctx context.Context, exec bob.Executor, mods .
for _, o := range os {
for _, rel := range files {
if o.ID != rel.UserID.GetOrZero() {
if o.ID != rel.UserID {
continue
}
@ -737,7 +765,7 @@ func (os UserSlice) LoadUserItems(ctx context.Context, exec bob.Executor, mods .
for _, o := range os {
for _, rel := range items {
if o.ID != rel.UserID.GetOrZero() {
if o.ID != rel.UserID {
continue
}
@ -750,9 +778,94 @@ func (os UserSlice) LoadUserItems(ctx context.Context, exec bob.Executor, mods .
return nil
}
func PreloadUserProfilePictureFile(opts ...sqlite.PreloadOption) sqlite.Preloader {
return sqlite.Preload[*File, FileSlice](orm.Relationship{
Name: "ProfilePictureFile",
Sides: []orm.RelSide{
{
From: TableNames.Users,
To: TableNames.Files,
FromColumns: []string{
ColumnNames.Users.ProfilePictureID,
},
ToColumns: []string{
ColumnNames.Files.ID,
},
},
},
}, Files.Columns().Names(), opts...)
}
func ThenLoadUserProfilePictureFile(queryMods ...bob.Mod[*dialect.SelectQuery]) sqlite.Loader {
return sqlite.Loader(func(ctx context.Context, exec bob.Executor, retrieved any) error {
loader, isLoader := retrieved.(interface {
LoadUserProfilePictureFile(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
})
if !isLoader {
return fmt.Errorf("object %T cannot load UserProfilePictureFile", retrieved)
}
err := loader.LoadUserProfilePictureFile(ctx, exec, queryMods...)
// Don't cause an issue due to missing relationships
if errors.Is(err, sql.ErrNoRows) {
return nil
}
return err
})
}
// LoadUserProfilePictureFile loads the user's ProfilePictureFile into the .R struct
func (o *User) LoadUserProfilePictureFile(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.ProfilePictureFile = nil
related, err := o.ProfilePictureFile(mods...).One(ctx, exec)
if err != nil {
return err
}
related.R.ProfilePictureUsers = UserSlice{o}
o.R.ProfilePictureFile = related
return nil
}
// LoadUserProfilePictureFile loads the user's ProfilePictureFile into the .R struct
func (os UserSlice) LoadUserProfilePictureFile(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
files, err := os.ProfilePictureFile(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
for _, rel := range files {
if o.ProfilePictureID.GetOrZero() != rel.ID {
continue
}
rel.R.ProfilePictureUsers = append(rel.R.ProfilePictureUsers, o)
o.R.ProfilePictureFile = rel
break
}
}
return nil
}
func insertUserFiles0(ctx context.Context, exec bob.Executor, files1 []*FileSetter, user0 *User) (FileSlice, error) {
for i := range files1 {
files1[i].UserID = omitnull.From(user0.ID)
files1[i].UserID = omit.From(user0.ID)
}
ret, err := Files.Insert(bob.ToMods(files1...)).All(ctx, exec)
@ -765,7 +878,7 @@ func insertUserFiles0(ctx context.Context, exec bob.Executor, files1 []*FileSett
func attachUserFiles0(ctx context.Context, exec bob.Executor, count int, files1 FileSlice, user0 *User) (FileSlice, error) {
setter := &FileSetter{
UserID: omitnull.From(user0.ID),
UserID: omit.From(user0.ID),
}
err := files1.UpdateAll(ctx, exec, *setter)
@ -820,7 +933,7 @@ func (user0 *User) AttachFiles(ctx context.Context, exec bob.Executor, related .
func insertUserItems0(ctx context.Context, exec bob.Executor, items1 []*ItemSetter, user0 *User) (ItemSlice, error) {
for i := range items1 {
items1[i].UserID = omitnull.From(user0.ID)
items1[i].UserID = omit.From(user0.ID)
}
ret, err := Items.Insert(bob.ToMods(items1...)).All(ctx, exec)
@ -833,7 +946,7 @@ func insertUserItems0(ctx context.Context, exec bob.Executor, items1 []*ItemSett
func attachUserItems0(ctx context.Context, exec bob.Executor, count int, items1 ItemSlice, user0 *User) (ItemSlice, error) {
setter := &ItemSetter{
UserID: omitnull.From(user0.ID),
UserID: omit.From(user0.ID),
}
err := items1.UpdateAll(ctx, exec, *setter)
@ -885,3 +998,49 @@ func (user0 *User) AttachItems(ctx context.Context, exec bob.Executor, related .
return nil
}
func attachUserProfilePictureFile0(ctx context.Context, exec bob.Executor, count int, user0 *User, file1 *File) (*User, error) {
setter := &UserSetter{
ProfilePictureID: omitnull.From(file1.ID),
}
err := user0.Update(ctx, exec, setter)
if err != nil {
return nil, fmt.Errorf("attachUserProfilePictureFile0: %w", err)
}
return user0, nil
}
func (user0 *User) InsertProfilePictureFile(ctx context.Context, exec bob.Executor, related *FileSetter) error {
file1, err := Files.Insert(related).One(ctx, exec)
if err != nil {
return fmt.Errorf("inserting related objects: %w", err)
}
_, err = attachUserProfilePictureFile0(ctx, exec, 1, user0, file1)
if err != nil {
return err
}
user0.R.ProfilePictureFile = file1
file1.R.ProfilePictureUsers = append(file1.R.ProfilePictureUsers, user0)
return nil
}
func (user0 *User) AttachProfilePictureFile(ctx context.Context, exec bob.Executor, file1 *File) error {
var err error
_, err = attachUserProfilePictureFile0(ctx, exec, 1, user0, file1)
if err != nil {
return err
}
user0.R.ProfilePictureFile = file1
file1.R.ProfilePictureUsers = append(file1.R.ProfilePictureUsers, user0)
return nil
}