Skip to content

Commit

Permalink
Add relationship query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Dec 17, 2022
1 parent c07bf59 commit 36f610d
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 146 deletions.
2 changes: 2 additions & 0 deletions orm/gen/templates/02_globals.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s/model" $.Dialect)}}
{{if not $table.PKey -}}
var {{$tAlias.UpPlural}}View = model.NewView[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice]({{quoteAndJoin .Schema $table.Name}})
type {{$tAlias.UpPlural}}Query = *model.ViewQuery[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice]
{{- else -}}
var {{$tAlias.UpPlural}}Table = model.NewTable[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice, *Optional{{$tAlias.UpSingular}}]({{quoteAndJoin .Schema $table.Name}})
type {{$tAlias.UpPlural}}Query = *model.TableQuery[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice, *Optional{{$tAlias.UpSingular}}]
{{- end}}

{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s" $.Dialect)}}
Expand Down
15 changes: 5 additions & 10 deletions orm/gen/templates/04_query.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
{{$tAlias := .Aliases.Table .Table.Name -}}
{{$.Importer.Import "github.com/stephenafamo/bob"}}


{{if not .Table.PKey -}}

// {{$tAlias.UpPlural}} begins a query on {{.Table.Name}}
func {{$tAlias.UpPlural}}(mods ...bob.Mod[*{{$.Dialect}}.SelectQuery]) *model.ViewQuery[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice] {
func {{$tAlias.UpPlural}}(mods ...bob.Mod[*{{$.Dialect}}.SelectQuery]) {{$tAlias.UpPlural}}Query {
{{if not .Table.PKey -}}
return {{$tAlias.UpPlural}}View.Query(mods...)
}

{{- else -}}

// {{$tAlias.UpPlural}} begins a query on {{.Table.Name}}
func {{$tAlias.UpPlural}}(mods ...bob.Mod[*{{$.Dialect}}.SelectQuery]) *model.TableQuery[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice, *Optional{{$tAlias.UpSingular}}] {
{{else -}}
return {{$tAlias.UpPlural}}Table.Query(mods...)
{{end -}}
}

{{if .Table.PKey -}}
{{$pkArgs := ""}}
{{range $colName := $table.PKey.Columns -}}
{{- $column := $table.GetColumn $colName -}}
Expand Down
142 changes: 142 additions & 0 deletions orm/gen/templates/09_rel_query.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Name -}}
{{$.Importer.Import "github.com/stephenafamo/bob"}}

{{range $rel := $table.Relationships -}}
{{- $fAlias := $.Aliases.Table $rel.Foreign -}}
{{- $relAlias := $tAlias.Relationship $rel.Name -}}
// {{$relAlias}} starts a query for related objects on {{$rel.Foreign}}
func (o *{{$tAlias.UpSingular}}) {{$relAlias}}(mods ...bob.Mod[*{{$.Dialect}}.SelectQuery]) {{$fAlias.UpPlural}}Query {
q := {{$fAlias.UpPlural}}(mods...)
q.Apply(
{{- range $index := until (len $rel.Sides) | reverse -}}
{{/* Index counts down */}}
{{/* This also flips the meaning of $from and $to */}}
{{- $side := index $rel.Sides $index -}}
{{- $from := $.Aliases.Table $side.From -}}
{{- $to := $.Aliases.Table $side.To -}}
{{- if gt $index 0 -}}
qm.InnerJoin({{$from.UpPlural}}Table.Name()).On(
{{end -}}
{{range $i, $local := $side.FromColumns -}}
{{- $fromCol := index $from.Columns $local -}}
{{- $toCol := index $to.Columns (index $side.ToColumns $i) -}}
{{- if gt $index 0 -}}
{{$to.UpSingular}}Columns.{{$toCol}}.EQ({{$from.UpSingular}}Columns.{{$fromCol}}),
{{- else -}}
qm.Where({{$to.UpSingular}}Columns.{{$toCol}}.EQ({{$.Dialect}}.Arg(o.{{$fromCol}}))),
{{- end -}}
{{- end}}
{{- range $where := $side.FromWhere}}
{{- $fromCol := index $from.Columns $where.Column}}
{{if eq $index 0 -}}qm.Where({{end -}}
{{$.Dialect}}.X({{$from.UpSingular}}Columns.{{$fromCol}}, "=", {{$.Dialect}}.Arg({{$where.Value}})),
{{- if eq $index 0 -}}),{{- end -}}
{{- end}}
{{- range $where := $side.ToWhere}}
{{- $toCol := index $to.Columns $where.Column}}
{{if eq $index 0 -}}qm.Where({{end -}}
{{$.Dialect}}.X({{$to.UpSingular}}Columns.{{$toCol}}, "=", {{$.Dialect}}.Arg({{$where.Value}}),
{{- if eq $index 0 -}}),{{- end -}}
{{- end}}
{{- if gt $index 0 -}}
),
{{- end -}}
{{- end}}
)

return q
}

{{if le (len $rel.Sides) 1 -}}
{{$side := (index $rel.Sides 0) -}}
{{$fromAlias := $.Aliases.Table $side.From -}}
{{$toAlias := $.Aliases.Table $side.To -}}

func (os {{$tAlias.UpSingular}}Slice) {{$relAlias}}(mods ...bob.Mod[*{{$.Dialect}}.SelectQuery]) {{$fAlias.UpPlural}}Query {
{{range $index, $local := $side.FromColumns -}}
{{- $fromCol := index $fromAlias.Columns $local -}}
{{$fromCol}}Args := make([]any, 0, len(os))
for _, o := range os {
{{$fromCol}}Args = append({{$fromCol}}Args, {{$.Dialect}}.Arg(o.{{$fromCol}}))
}
{{- end}}

q := {{$fAlias.UpPlural}}(mods...)
q.Apply(
{{range $index, $local := $side.FromColumns -}}
{{- $fromCol := index $fromAlias.Columns $local -}}
{{- $toCol := index $toAlias.Columns (index $side.ToColumns $index) -}}
qm.Where({{$fAlias.UpSingular}}Columns.{{$toCol}}.In({{$fromCol}}Args...)),
{{- end}}
{{- range $where := $side.FromWhere}}
{{- $fromCol := index $fromAlias.Columns $where.Column}}
qm.Where({{$.Dialect}}.X({{$fromAlias.UpSingular}}Columns.{{$fromCol}}, "=", {{$.Dialect}}.Arg({{$where.Value}}))),
{{- end}}
{{- range $where := $side.ToWhere}}
{{- $toCol := index $toAlias.Columns $where.Column}}
qm.Where({{$.Dialect}}.X({{$toAlias.UpSingular}}Columns.{{$toCol}}, "=", {{$.Dialect}}.Arg({{$where.Value}}))),
{{- end}}
)

return q
}
{{else -}}
{{$firstSide := (index $rel.Sides 0) -}}
{{$firstFrom := $.Aliases.Table $firstSide.From -}}
{{$firstTo := $.Aliases.Table $firstSide.To -}}

{{$lastSide := (index $rel.Sides (sub (len $rel.Sides) 1)) -}}
{{$lastFrom := $.Aliases.Table $lastSide.From -}}
{{$lastTo := $.Aliases.Table $lastSide.To -}}

func (os {{$tAlias.UpSingular}}Slice) {{$relAlias}}(mods ...bob.Mod[*{{$.Dialect}}.SelectQuery]) {{$fAlias.UpPlural}}Query {
{{range $index, $local := $firstSide.FromColumns -}}
{{- $fromCol := index $firstFrom.Columns $local -}}
{{$fromCol}}Args := make([]any, 0, len(os))
for _, o := range os {
{{$fromCol}}Args = append({{$fromCol}}Args, {{$.Dialect}}.Arg(o.{{$fromCol}}))
}
{{- end}}

q := {{$fAlias.UpPlural}}(mods...)
q.Apply(
{{- range $index := until (len $rel.Sides) | reverse -}}
{{/* Index counts down */}}
{{/* This also flips the meaning of $from and $to */}}
{{- $side := index $rel.Sides $index -}}
{{- $from := $.Aliases.Table $side.From -}}
{{- $to := $.Aliases.Table $side.To -}}
{{- if gt $index 0 -}}
qm.InnerJoin({{$from.UpPlural}}Table.Name()).On(
{{end -}}
{{range $i, $local := $side.FromColumns -}}
{{- $foreign := index $side.ToColumns $i -}}
{{- $fromCol := index $from.Columns $local -}}
{{- $toCol := index $to.Columns $foreign -}}
{{- if gt $index 0 -}}
{{$to.UpSingular}}Columns.{{$toCol}}.EQ({{$from.UpSingular}}Columns.{{$fromCol}}),
{{- else -}}
qm.Where({{$to.UpSingular}}Columns.{{$toCol}}.In({{$fromCol}}Args...)),
{{- end}}
{{- end}}
{{- range $where := $side.FromWhere}}
{{- $fromCol := index $from.Columns $where.Column}}
qm.Where({{$.Dialect}}.X({{$from.UpSingular}}Columns.{{$fromCol}}, "=", {{$.Dialect}}.Arg({{$where.Value}}))),
{{- end}}
{{- range $where := $side.ToWhere}}
{{- $toCol := index $to.Columns $where.Column}}
qm.Where({{$.Dialect}}.X({{$to.UpSingular}}Columns.{{$toCol}}, "=", {{$.Dialect}}.Arg({{$where.Value}}))),
{{- end}}
{{- if gt $index 0}}
),
{{- end -}}
{{- end}}
)

return q
}
{{end -}}


{{end -}}

0 comments on commit 36f610d

Please sign in to comment.