diff --git a/templates/04_relationship_to_one.tpl b/templates/04_relationship_to_one.tpl index b5b5b5d..900fed3 100644 --- a/templates/04_relationship_to_one.tpl +++ b/templates/04_relationship_to_one.tpl @@ -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 -}} diff --git a/text_helpers.go b/text_helpers.go index bef1b11..a04d289 100644 --- a/text_helpers.go +++ b/text_helpers.go @@ -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)) diff --git a/text_helpers_test.go b/text_helpers_test.go index 2e2153a..ee695e5 100644 --- a/text_helpers_test.go +++ b/text_helpers_test.go @@ -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"