Rewrite to_one template as a sub-template
- This allows us to use the sub-template in other templates.
This commit is contained in:
parent
f4ce95891c
commit
1a53eab27f
2 changed files with 50 additions and 43 deletions
|
@ -1,16 +1,12 @@
|
|||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $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...)
|
||||
{{- define "relationship_to_one_helper"}}
|
||||
// {{.Function.Name}} pointed to by the foreign key.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(selectCols ...string) (*{{.ForeignTable.NameGo}}, error) {
|
||||
return {{.Function.Receiver}}.{{.Function.Name}}X(boil.GetDB(), selectCols...)
|
||||
}
|
||||
|
||||
// {{$rel.LocalTable.ColumnNameGo}} 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}} {
|
||||
o, err := {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}X(boil.GetDB(), selectCols...)
|
||||
// {{.Function.Name}}P pointed to by the foreign key. Panics on error.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}P(selectCols ...string) *{{.ForeignTable.NameGo}} {
|
||||
o, err := {{.Function.Receiver}}.{{.Function.Name}}X(boil.GetDB(), selectCols...)
|
||||
if err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
|
@ -18,32 +14,40 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.
|
|||
return o
|
||||
}
|
||||
|
||||
// {{$rel.LocalTable.ColumnNameGo}} pointed to by the foreign key.
|
||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}X(exec boil.Executor, selectCols ...string) (*{{$rel.ForeignTable.NameGo}}, error) {
|
||||
{{$rel.Function.Varname}} := &{{$rel.ForeignTable.NameGo}}{}
|
||||
// {{.Function.Name}}XP pointed to by the foreign key with exeuctor. Panics on error.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}XP(exec boil.Executor, selectCols ...string) *{{.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 := `*`
|
||||
if len(selectCols) != 0 {
|
||||
selectColumns = fmt.Sprintf(`"%s"`, strings.Join(selectCols, `","`))
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`select %s from {{.ForeignTable}} where "{{.ForeignColumn}}" = $1`, selectColumns)
|
||||
err := exec.QueryRow(query, {{$rel.Function.Receiver}}.{{titleCase .Column}}).Scan(boil.GetStructPointers({{$rel.Function.Varname}}, selectCols...)...)
|
||||
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(`{{$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.
|
||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.LocalTable.ColumnNameGo}}XP(exec boil.Executor, selectCols ...string) *{{$rel.ForeignTable.NameGo}} {
|
||||
o, err := {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}X(exec, selectCols...)
|
||||
if err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
{{end -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_helper" $rel -}}
|
||||
{{end -}}
|
||||
{{- end -}}
|
||||
|
|
|
@ -1,39 +1,42 @@
|
|||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $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) {
|
||||
{{- define "relationship_to_one_test_helper"}}
|
||||
func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
||||
tx := MustTx(boil.Begin())
|
||||
defer tx.Rollback()
|
||||
|
||||
var foreign {{$rel.ForeignTable.NameGo}}
|
||||
var local {{$rel.LocalTable.NameGo}}
|
||||
{{if .Nullable -}}
|
||||
local.{{.Column | titleCase}}.Valid = true
|
||||
var foreign {{.ForeignTable.NameGo}}
|
||||
var local {{.LocalTable.NameGo}}
|
||||
{{if .ForeignKey.Nullable -}}
|
||||
local.{{.ForeignKey.Column | titleCase}}.Valid = true
|
||||
{{end}}
|
||||
{{- if .ForeignColumnNullable -}}
|
||||
foreign.{{.ForeignColumn | titleCase}}.Valid = true
|
||||
{{- if .ForeignKey.ForeignColumnNullable -}}
|
||||
foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true
|
||||
{{end}}
|
||||
|
||||
if err := foreign.InsertX(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
local.{{$rel.Function.LocalAssignment}} = foreign.{{$rel.Function.ForeignAssignment}}
|
||||
local.{{.Function.LocalAssignment}} = foreign.{{.Function.ForeignAssignment}}
|
||||
if err := local.InsertX(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
checkForeign, err := local.{{$rel.LocalTable.ColumnNameGo}}X(tx)
|
||||
checkForeign, err := local.{{.Function.Name}}X(tx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if checkForeign.{{$rel.Function.ForeignAssignment}} != foreign.{{$rel.Function.ForeignAssignment}} {
|
||||
t.Errorf("want: %v, got %v", foreign.{{$rel.Function.ForeignAssignment}}, checkForeign.{{$rel.Function.ForeignAssignment}})
|
||||
if checkForeign.{{.Function.ForeignAssignment}} != foreign.{{.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 -}}
|
||||
|
|
Loading…
Reference in a new issue