Skip to content

Commit

Permalink
Merge pull request #220 from daddz/preload-alias
Browse files Browse the repository at this point in the history
Add PreloadAs PreloadOption to override the join alias
  • Loading branch information
stephenafamo committed May 28, 2024
2 parents 96d466b + 7b8f359 commit 047f9b3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dialect/mysql/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func PreloadWhere(f ...func(from, to string) []bob.Expression) PreloadOption {
return internal.PreloadWhere[*dialect.SelectQuery](f)
}

func PreloadAs(alias string) PreloadOption {
return internal.PreloadAs[*dialect.SelectQuery](alias)
}

func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...PreloadOption) Preloader {
settings := internal.NewPreloadSettings[T, Ts, *dialect.SelectQuery](cols)
for _, o := range opts {
Expand All @@ -64,6 +68,9 @@ func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...Preloa

for i, side := range rel.Sides {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
if settings.Alias != "" {
alias = settings.Alias
}
on := make([]bob.Expression, 0, len(side.FromColumns)+len(side.FromWhere)+len(side.ToWhere))
for i, fromCol := range side.FromColumns {
toCol := side.ToColumns[i]
Expand Down
7 changes: 7 additions & 0 deletions dialect/psql/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func PreloadWhere(f ...func(from, to string) []bob.Expression) PreloadOption {
return internal.PreloadWhere[*dialect.SelectQuery](f)
}

func PreloadAs(alias string) PreloadOption {
return internal.PreloadAs[*dialect.SelectQuery](alias)
}

func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...PreloadOption) Preloader {
settings := internal.NewPreloadSettings[T, Ts, *dialect.SelectQuery](cols)
for _, o := range opts {
Expand All @@ -64,6 +68,9 @@ func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...Preloa

for i, side := range rel.Sides {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
if settings.Alias != "" {
alias = settings.Alias
}
on := make([]bob.Expression, 0, len(side.FromColumns)+len(side.FromWhere)+len(side.ToWhere))
for i, fromCol := range side.FromColumns {
toCol := side.ToColumns[i]
Expand Down
7 changes: 7 additions & 0 deletions dialect/sqlite/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func PreloadWhere(f ...func(from, to string) []bob.Expression) PreloadOption {
return internal.PreloadWhere[*dialect.SelectQuery](f)
}

func PreloadAs(alias string) PreloadOption {
return internal.PreloadAs[*dialect.SelectQuery](alias)
}

func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...PreloadOption) Preloader {
settings := internal.NewPreloadSettings[T, Ts, *dialect.SelectQuery](cols)
for _, o := range opts {
Expand All @@ -64,6 +68,9 @@ func Preload[T any, Ts ~[]T](rel orm.Relationship, cols []string, opts ...Preloa

for i, side := range rel.Sides {
alias = fmt.Sprintf("%s_%d", side.To, internal.RandInt())
if settings.Alias != "" {
alias = settings.Alias
}
on := make([]bob.Expression, 0, len(side.FromColumns)+len(side.FromWhere)+len(side.ToWhere))
for i, fromCol := range side.FromColumns {
toCol := side.ToColumns[i]
Expand Down
7 changes: 7 additions & 0 deletions internal/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ type PreloadSettings[Q loadable] struct {
SubLoaders []Preloader[Q]
ExtraLoader *AfterPreloader
Mods [][]preloadfilter
Alias string
}

type PreloadOption[Q loadable] interface {
ModifyPreloadSettings(*PreloadSettings[Q])
}

type PreloadAs[Q loadable] string

func (o PreloadAs[Q]) ModifyPreloadSettings(el *PreloadSettings[Q]) {
el.Alias = string(o)
}

type PreloadOnly[Q loadable] []string

func (o PreloadOnly[Q]) ModifyPreloadSettings(el *PreloadSettings[Q]) {
Expand Down
9 changes: 9 additions & 0 deletions website/docs/code-generation/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The mod function accepts options:

1. `OnlyColumns`: In the related model, load only these columns.
1. `ExceptColumns`: In the related model, do not load these columns.
1. `PreloadAs`: Explicitly sets the table alias for the related model to allow using columns of the related model for the query.
1. `Loaders`: Other loaders mods can be given as an option to the preloader to load nested relationships. This works for both other preloaders and then-loaders.

```go
Expand All @@ -100,6 +101,14 @@ jet, err := models.Jets(ctx, db,
).One()
```

```go
jets, err := models.Jets(ctx, db,
models.PreloadJetPilot(psql.PreloadAs("pilot")), // "LEFT JOIN "pilots" AS "pilot" ON ("jet"."pilot_id" = "pilot"."id")
models.PreloadJetCoPilot(psql.PreloadAs("copilot")), // "LEFT JOIN "pilots" AS "copilot" ON ("jet"."copilot_id" = "copilot"."id")
sm.OrderBy(psql.Quote("pilot", models.ColumnNames.Pilot.LastName)) // ORDER BY "pilot"."last_name" DESC
).All()
```

### ThenLoad

```go
Expand Down

0 comments on commit 047f9b3

Please sign in to comment.