Use query generation for relationship_to_one

- This allows the use of query mods
This commit is contained in:
Aaron L 2016-08-01 20:37:58 -07:00
parent fb8540af1a
commit 9a5742eeea
3 changed files with 16 additions and 19 deletions

View file

@ -1,12 +1,12 @@
{{- define "relationship_to_one_helper"}}
// {{.Function.Name}}G pointed to by the foreign key.
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}G(selectCols ...string) (*{{.ForeignTable.NameGo}}, error) {
return {{.Function.Receiver}}.{{.Function.Name}}(boil.GetDB(), selectCols...)
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}G(mods ...qm.QueryMod) (*{{.ForeignTable.NameGo}}, error) {
return {{.Function.Receiver}}.{{.Function.Name}}(boil.GetDB(), mods...)
}
// {{.Function.Name}}GP pointed to by the foreign key. Panics on error.
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}GP(selectCols ...string) *{{.ForeignTable.NameGo}} {
o, err := {{.Function.Receiver}}.{{.Function.Name}}(boil.GetDB(), selectCols...)
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}GP(mods ...qm.QueryMod) *{{.ForeignTable.NameGo}} {
o, err := {{.Function.Receiver}}.{{.Function.Name}}(boil.GetDB(), mods...)
if err != nil {
panic(boil.WrapErr(err))
}
@ -15,8 +15,8 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}GP(selec
}
// {{.Function.Name}}P pointed to by the foreign key with exeuctor. Panics on error.
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}P(exec boil.Executor, selectCols ...string) *{{.ForeignTable.NameGo}} {
o, err := {{.Function.Receiver}}.{{.Function.Name}}(exec, selectCols...)
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}P(exec boil.Executor, mods ...qm.QueryMod) *{{.ForeignTable.NameGo}} {
o, err := {{.Function.Receiver}}.{{.Function.Name}}(exec, mods...)
if err != nil {
panic(boil.WrapErr(err))
}
@ -25,21 +25,14 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}P(exec b
}
// {{.Function.Name}} pointed to by the foreign key.
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(exec boil.Executor, selectCols ...string) (*{{.ForeignTable.NameGo}}, error) {
{{.Function.Varname}} := &{{.ForeignTable.NameGo}}{}
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) (*{{.ForeignTable.NameGo}}, error) {
queryMods := make([]qm.QueryMod, 2, len(mods)+2)
queryMods[0] = qm.From("{{.ForeignTable.Name}}")
queryMods[1] = qm.Where("{{.ForeignTable.ColumnName}}=$1", {{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}})
selectColumns := `*`
if len(selectCols) != 0 {
selectColumns = fmt.Sprintf(`"%s"`, strings.Join(selectCols, `","`))
}
queryMods = append(queryMods, mods...)
query := fmt.Sprintf(`select %s from {{.ForeignTable.Name}} where "{{.ForeignTable.ColumnName}}" = $1`, selectColumns)
err := exec.QueryRow(query, {{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}).Scan(boil.GetStructPointers({{.Function.Varname}}, selectCols...)...)
if err != nil {
return nil, fmt.Errorf(`{{.Function.PackageName}}: unable to select from {{.ForeignTable.Name}}: %v`, err)
}
return {{.Function.Varname}}, nil
return {{.ForeignTable.NamePluralGo}}(exec, queryMods...).One()
}
{{end -}}

View file

@ -19,6 +19,7 @@ type RelationshipToOneTexts struct {
ForeignTable struct {
NameGo string
NamePluralGo string
Name string
ColumnNameGo string
ColumnName string
@ -47,6 +48,7 @@ func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table
r.ForeignTable.Name = fkey.ForeignTable
r.ForeignTable.NameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignTable))
r.ForeignTable.NamePluralGo = strmangle.TitleCase(fkey.ForeignTable)
r.ForeignTable.ColumnName = fkey.ForeignColumn
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignColumn))

View file

@ -85,6 +85,7 @@ func TestTextsFromForeignKey(t *testing.T) {
expect.ForeignTable.Name = "users"
expect.ForeignTable.NameGo = "User"
expect.ForeignTable.NamePluralGo = "Users"
expect.ForeignTable.ColumnName = "id"
expect.ForeignTable.ColumnNameGo = "ID"
@ -131,6 +132,7 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
expect.ForeignTable.Name = "videos"
expect.ForeignTable.NameGo = "Video"
expect.ForeignTable.NamePluralGo = "Videos"
expect.ForeignTable.ColumnName = "user_id"
expect.ForeignTable.ColumnNameGo = "UserID"