Skip to content

Commit

Permalink
Merge pull request #221 from stephenafamo/alias-where
Browse files Browse the repository at this point in the history
Add `AliasedAs()` method to `tableColumns` and `tableWhere`
  • Loading branch information
stephenafamo committed May 28, 2024
2 parents 047f9b3 + d4fc157 commit a365b5a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Add PreloadAs PreloadOption to override the join alias when preloading a relationship with a left join. (thanks @daddz)
- Add `AliasedAs()` method to `tableColumns` and `tableWhere` types to use a custom alias.

### Removed

- Remove `TableWhere` function from the generated code. It was not used by the rest of the generated code and offered no clear benefit.

## [v0.26.1] - 2024-05-26

### Fixed
Expand Down
32 changes: 23 additions & 9 deletions gen/templates/models/01_types.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,28 @@ func {{$tAlias.DownPlural}}Join[Q dialect.Joinable](ctx context.Context) joinSet
{{- end}}

{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s" $.Dialect)}}
var {{$tAlias.UpSingular}}Columns = struct {
var {{$tAlias.UpSingular}}Columns = build{{$tAlias.UpSingular}}Columns({{quote $table.Key}})
type {{$tAlias.DownSingular}}Columns struct {
{{range $column := $table.Columns -}}
{{- $colAlias := $tAlias.Column $column.Name -}}
{{$colAlias}} {{$.Dialect}}.Expression
{{end -}}
}{
{{range $column := $table.Columns -}}
{{- $colAlias := $tAlias.Column $column.Name -}}
{{$colAlias}}: {{$.Dialect}}.Quote({{quote $table.Key}}, {{quote $column.Name}}),
{{end -}}
}

func ({{$tAlias.DownSingular}}Columns) AliasedAs(alias string) {{$tAlias.DownSingular}}Columns {
return build{{$tAlias.UpSingular}}Columns(alias)
}

func build{{$tAlias.UpSingular}}Columns(alias string) {{$tAlias.DownSingular}}Columns {
return {{$tAlias.DownSingular}}Columns{
{{range $column := $table.Columns -}}
{{- $colAlias := $tAlias.Column $column.Name -}}
{{$colAlias}}: {{$.Dialect}}.Quote(alias, {{quote $column.Name}}),
{{end -}}
}
}


type {{$tAlias.DownSingular}}Where[Q {{$.Dialect}}.Filterable] struct {
{{range $column := $table.Columns -}}
{{- $colAlias := $tAlias.Column $column.Name -}}
Expand All @@ -229,14 +239,18 @@ type {{$tAlias.DownSingular}}Where[Q {{$.Dialect}}.Filterable] struct {
{{end -}}
}

func {{$tAlias.UpSingular}}Where[Q {{$.Dialect}}.Filterable]() {{$tAlias.DownSingular}}Where[Q] {
func ({{$tAlias.DownSingular}}Where[Q]) AliasedAs(alias string) {{$tAlias.DownSingular}}Where[Q] {
return build{{$tAlias.UpSingular}}Where[Q](build{{$tAlias.UpSingular}}Columns(alias))
}

func build{{$tAlias.UpSingular}}Where[Q {{$.Dialect}}.Filterable](cols {{$tAlias.DownSingular}}Columns) {{$tAlias.DownSingular}}Where[Q] {
return {{$tAlias.DownSingular}}Where[Q]{
{{range $column := $table.Columns -}}
{{- $colAlias := $tAlias.Column $column.Name -}}
{{- if $column.Nullable -}}
{{$colAlias}}: {{$.Dialect}}.WhereNull[Q, {{$column.Type}}]({{$tAlias.UpSingular}}Columns.{{$colAlias}}),
{{$colAlias}}: {{$.Dialect}}.WhereNull[Q, {{$column.Type}}](cols.{{$colAlias}}),
{{- else -}}
{{$colAlias}}: {{$.Dialect}}.Where[Q, {{$column.Type}}]({{$tAlias.UpSingular}}Columns.{{$colAlias}}),
{{$colAlias}}: {{$.Dialect}}.Where[Q, {{$column.Type}}](cols.{{$colAlias}}),
{{- end}}
{{end -}}
}
Expand Down
2 changes: 1 addition & 1 deletion gen/templates/models/singleton/bob_main.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Where[Q {{$.Dialect}}.Filterable]() struct {
}{
{{range $table := .Tables -}}
{{$tAlias := $.Aliases.Table $table.Key -}}
{{$tAlias.UpPlural}}: {{$tAlias.UpSingular}}Where[Q](),
{{$tAlias.UpPlural}}: build{{$tAlias.UpSingular}}Where[Q]({{$tAlias.UpSingular}}Columns),
{{end -}}
}
}
Expand Down
32 changes: 25 additions & 7 deletions website/docs/code-generation/usage.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---

sidebar_position: 3
description: How to use the Generated Models

---

# Usage
Expand Down Expand Up @@ -74,9 +72,9 @@ var JetsTable = psql.NewTablex[*Jet, JetSlice, *JetSetter]("", "jets")

**JetsTable** gives the full range of capabilites of a Bob model, including

* Flexible queries: One, All, Cursor, Count, Exists
* Expressions for names and column lists
* Hooks
- Flexible queries: One, All, Cursor, Count, Exists
- Expressions for names and column lists
- Hooks

[Read the documentation to see how to use](../models/table)

Expand Down Expand Up @@ -207,7 +205,17 @@ users, err := models.Users(
).All()
```

Since each query type has its own mods, `SelectWhere`, `InsertWhere`, `UpdateWhere` and `DeleteWhere` are all generated.
Since each query type has its own mods, `SelectWhere`, `InsertWhere`, `UpdateWhere` and `DeleteWhere` are all generated.

:::tip

To use these filters with an aliased table name, use the `AliasedAs` method.

```go
models.SelectWhere.Users.AliasedAs("u").Name.IsNull()
```

:::

### Join Helpers

Expand All @@ -224,7 +232,7 @@ models.Jets(
).All()
```

Since each query type has its own mods, `SelectJoins`, `InsertJoins`, `UpdateJoins` and `DeleteJoins` are all generated.
Since each query type has its own mods, `SelectJoins`, `InsertJoins`, `UpdateJoins` and `DeleteJoins` are all generated.

### Column Expressions

Expand All @@ -244,4 +252,14 @@ psql.Select(

```

:::tip

To use these expressions with an aliased table name, use the `AliasedAs` method.

```go
models.JetColumns.AliasedAs("j").Name.IsNull()
```

:::

[^1]: Some are technically just global variables. But they are never mutated by Bob, or expected to be mutated by the user.

0 comments on commit a365b5a

Please sign in to comment.