Use query mods for to_many

This commit is contained in:
Aaron L 2016-08-03 22:36:08 -07:00
parent a1b14f4eae
commit 535c86bca0

View file

@ -9,14 +9,14 @@
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
// {{$rel.Function.Name}}G retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}G(selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) {
return {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(boil.GetDB(), selectCols...)
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}G(mods ...qm.QueryMod) ({{$rel.ForeignTable.Slice}}, error) {
return {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(boil.GetDB(), mods...)
}
// {{$rel.Function.Name}}GP panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}GP(selectCols ...string) {{$rel.ForeignTable.Slice}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(boil.GetDB(), selectCols...)
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}GP(mods ...qm.QueryMod) {{$rel.ForeignTable.Slice}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(boil.GetDB(), mods...)
if err != nil {
panic(boil.WrapErr(err))
}
@ -26,8 +26,8 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
// {{$rel.Function.Name}}P panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}P(exec boil.Executor, selectCols ...string) {{$rel.ForeignTable.Slice}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(exec, selectCols...)
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}P(exec boil.Executor, mods ...qm.QueryMod) {{$rel.ForeignTable.Slice}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(exec, mods...)
if err != nil {
panic(boil.WrapErr(err))
}
@ -37,40 +37,29 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
// {{$rel.Function.Name}} retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(exec boil.Executor, selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) {
var ret {{$rel.ForeignTable.Slice}}
selectColumns := `"{{id 0}}".*`
if len(selectCols) != 0 {
selectColumns = `"{{id 0}}".` + strings.Join(selectCols, `","{{id 0}}"."`)
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) ({{$rel.ForeignTable.Slice}}, error) {
queryMods := []qm.QueryMod{
qm.Select(`"{{id 0}}".*`),
}
if len(mods) != 0 {
queryMods = append(queryMods, mods...)
}
{{if .ToJoinTable -}}
query := fmt.Sprintf(`select %s from {{.ForeignTable}} "{{id 0}}" inner join {{.JoinTable}} as "{{id 1}}" on "{{id 1}}"."{{.JoinForeignColumn}}" = "{{id 0}}"."{{.ForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}"=$1`, selectColumns)
queryMods = append(queryMods,
qm.InnerJoin(`"{{.JoinTable}}" as "{{id 1}}" on "{{id 1}}"."{{.JoinForeignColumn}}" = "{{id 0}}"."{{.ForeignColumn}}"`),
qm.Where(`"{{id 1}}"."{{.JoinLocalColumn}}"=$1`, {{.Column | titleCase | printf "%s.%s" $rel.Function.Receiver }}),
)
{{else -}}
query := fmt.Sprintf(`select %s from {{.ForeignTable}} "{{id 0}}" where "{{id 0}}"."{{.ForeignColumn}}"=$1`, selectColumns)
queryMods = append(queryMods,
qm.Where(`"{{id 0}}"."{{.ForeignColumn}}"=$1`, {{.Column | titleCase | printf "%s.%s" $rel.Function.Receiver }}),
)
{{end}}
rows, err := exec.Query(query, {{.Column | titleCase | printf "%s.%s" $rel.Function.Receiver }})
if err != nil {
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err)
}
defer rows.Close()
for rows.Next() {
next := new({{$rel.ForeignTable.NameGo}})
err = rows.Scan(boil.GetStructPointers(next, selectCols...)...)
if err != nil {
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to scan into {{$rel.ForeignTable.NameGo}}: %v`, err)
}
ret = append(ret, next)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{$rel.ForeignTable.NameGo}}: %v`, err)
}
return ret, nil
query := {{$rel.ForeignTable.NamePluralGo}}(exec, queryMods...)
boil.SetFrom(query.Query, `"{{.ForeignTable}}" as "{{id 0}}"`)
return query.All()
}
{{end -}}{{- /* if unique foreign key */ -}}