Rewrite to_one template as a sub-template

- This allows us to use the sub-template in other templates.
This commit is contained in:
Aaron L 2016-07-16 23:57:08 -07:00
parent f4ce95891c
commit 1a53eab27f
2 changed files with 50 additions and 43 deletions

View file

@ -1,16 +1,12 @@
{{- if .Table.IsJoinTable -}} {{- define "relationship_to_one_helper"}}
{{- else -}} // {{.Function.Name}} pointed to by the foreign key.
{{- $dot := . -}} func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(selectCols ...string) (*{{.ForeignTable.NameGo}}, error) {
{{- range .Table.FKeys -}} return {{.Function.Receiver}}.{{.Function.Name}}X(boil.GetDB(), selectCols...)
{{- $rel := textsFromForeignKey $dot.Tables $dot.Table . -}}
// {{$rel.LocalTable.ColumnNameGo}} pointed to by the foreign key.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}(selectCols ...string) (*{{$rel.ForeignTable.NameGo}}, error) {
return {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}X(boil.GetDB(), selectCols...)
} }
// {{$rel.LocalTable.ColumnNameGo}} pointed to by the foreign key. Panics on error. // {{.Function.Name}}P pointed to by the foreign key. Panics on error.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}P(selectCols ...string) *{{$rel.ForeignTable.NameGo}} { func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}P(selectCols ...string) *{{.ForeignTable.NameGo}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}X(boil.GetDB(), selectCols...) o, err := {{.Function.Receiver}}.{{.Function.Name}}X(boil.GetDB(), selectCols...)
if err != nil { if err != nil {
panic(boil.WrapErr(err)) panic(boil.WrapErr(err))
} }
@ -18,32 +14,40 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.
return o return o
} }
// {{$rel.LocalTable.ColumnNameGo}} pointed to by the foreign key. // {{.Function.Name}}XP pointed to by the foreign key with exeuctor. Panics on error.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}X(exec boil.Executor, selectCols ...string) (*{{$rel.ForeignTable.NameGo}}, error) { func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}XP(exec boil.Executor, selectCols ...string) *{{.ForeignTable.NameGo}} {
{{$rel.Function.Varname}} := &{{$rel.ForeignTable.NameGo}}{} o, err := {{.Function.Receiver}}.{{.Function.Name}}X(exec, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
// {{.Function.Name}}X pointed to by the foreign key.
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}X(exec boil.Executor, selectCols ...string) (*{{.ForeignTable.NameGo}}, error) {
{{.Function.Varname}} := &{{.ForeignTable.NameGo}}{}
selectColumns := `*` selectColumns := `*`
if len(selectCols) != 0 { if len(selectCols) != 0 {
selectColumns = fmt.Sprintf(`"%s"`, strings.Join(selectCols, `","`)) selectColumns = fmt.Sprintf(`"%s"`, strings.Join(selectCols, `","`))
} }
query := fmt.Sprintf(`select %s from {{.ForeignTable}} where "{{.ForeignColumn}}" = $1`, selectColumns) query := fmt.Sprintf(`select %s from {{.ForeignTable.Name}} where "{{.ForeignTable.ColumnName}}" = $1`, selectColumns)
err := exec.QueryRow(query, {{$rel.Function.Receiver}}.{{titleCase .Column}}).Scan(boil.GetStructPointers({{$rel.Function.Varname}}, selectCols...)...) err := exec.QueryRow(query, {{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}).Scan(boil.GetStructPointers({{.Function.Varname}}, selectCols...)...)
if err != nil { if err != nil {
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err) return nil, fmt.Errorf(`{{.Function.PackageName}}: unable to select from {{.ForeignTable.Name}}: %v`, err)
} }
return {{$rel.Function.Varname}}, nil return {{.Function.Varname}}, nil
} }
// {{$rel.LocalTable.ColumnNameGo}} pointed to by the foreign key. Panics on error. {{end -}}
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}XP(exec boil.Executor, selectCols ...string) *{{$rel.ForeignTable.NameGo}} { {{- if .Table.IsJoinTable -}}
o, err := {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}X(exec, selectCols...) {{- else -}}
if err != nil { {{- $dot := . -}}
panic(boil.WrapErr(err)) {{- range .Table.FKeys -}}
} {{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
{{- template "relationship_to_one_helper" $rel -}}
return o
}
{{end -}} {{end -}}
{{- end -}} {{- end -}}

View file

@ -1,39 +1,42 @@
{{- if .Table.IsJoinTable -}} {{- define "relationship_to_one_test_helper"}}
{{- else -}} func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
{{- $dot := . -}}
{{- range .Table.FKeys -}}
{{- $rel := textsFromForeignKey $dot.Tables $dot.Table . -}}
func Test{{$rel.LocalTable.NameGo}}ToOne{{$rel.ForeignTable.NameGo}}_{{$rel.LocalTable.ColumnNameGo}}(t *testing.T) {
tx := MustTx(boil.Begin()) tx := MustTx(boil.Begin())
defer tx.Rollback() defer tx.Rollback()
var foreign {{$rel.ForeignTable.NameGo}} var foreign {{.ForeignTable.NameGo}}
var local {{$rel.LocalTable.NameGo}} var local {{.LocalTable.NameGo}}
{{if .Nullable -}} {{if .ForeignKey.Nullable -}}
local.{{.Column | titleCase}}.Valid = true local.{{.ForeignKey.Column | titleCase}}.Valid = true
{{end}} {{end}}
{{- if .ForeignColumnNullable -}} {{- if .ForeignKey.ForeignColumnNullable -}}
foreign.{{.ForeignColumn | titleCase}}.Valid = true foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true
{{end}} {{end}}
if err := foreign.InsertX(tx); err != nil { if err := foreign.InsertX(tx); err != nil {
t.Fatal(err) t.Fatal(err)
} }
local.{{$rel.Function.LocalAssignment}} = foreign.{{$rel.Function.ForeignAssignment}} local.{{.Function.LocalAssignment}} = foreign.{{.Function.ForeignAssignment}}
if err := local.InsertX(tx); err != nil { if err := local.InsertX(tx); err != nil {
t.Fatal(err) t.Fatal(err)
} }
checkForeign, err := local.{{$rel.LocalTable.ColumnNameGo}}X(tx) checkForeign, err := local.{{.Function.Name}}X(tx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if checkForeign.{{$rel.Function.ForeignAssignment}} != foreign.{{$rel.Function.ForeignAssignment}} { if checkForeign.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} {
t.Errorf("want: %v, got %v", foreign.{{$rel.Function.ForeignAssignment}}, checkForeign.{{$rel.Function.ForeignAssignment}}) t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, checkForeign.{{.Function.ForeignAssignment}})
} }
} }
{{end -}}
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.FKeys -}}
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
{{- template "relationship_to_one_test_helper" $rel -}}
{{end -}} {{end -}}
{{- end -}} {{- end -}}