Make a rough sketch of what this would look like
Needs cleanup: Do not merge
This commit is contained in:
parent
a426f09a75
commit
c624943f53
10 changed files with 107 additions and 19 deletions
|
@ -143,10 +143,11 @@ func removeDuplicates(dedup []string) []string {
|
|||
|
||||
var defaultTemplateImports = imports{
|
||||
standard: importList{
|
||||
`"fmt"`,
|
||||
`"strings"`,
|
||||
`"bytes"`,
|
||||
`"database/sql"`,
|
||||
`"fmt"`,
|
||||
`"reflect"`,
|
||||
`"strings"`,
|
||||
`"sync"`,
|
||||
`"time"`,
|
||||
},
|
||||
|
@ -177,8 +178,9 @@ var defaultSingletonTemplateImports = map[string]imports{
|
|||
|
||||
var defaultTestTemplateImports = imports{
|
||||
standard: importList{
|
||||
`"testing"`,
|
||||
`"bytes"`,
|
||||
`"reflect"`,
|
||||
`"testing"`,
|
||||
},
|
||||
thirdParty: importList{
|
||||
`"github.com/vattle/sqlboiler/boil"`,
|
||||
|
|
|
@ -36,6 +36,10 @@ var (
|
|||
{{$varNameSingular}}UpsertCache = make(map[string]insertCache)
|
||||
)
|
||||
|
||||
// Force time package dependency for automated UpdatedAt/CreatedAt.
|
||||
var _ = time.Second
|
||||
var (
|
||||
// Force time package dependency for automated UpdatedAt/CreatedAt.
|
||||
_ = time.Second
|
||||
// Force bytes in case of primary key column that uses []byte (for relationship compares)
|
||||
_ = bytes.MinRead
|
||||
)
|
||||
{{end -}}
|
||||
|
|
|
@ -67,7 +67,15 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo
|
|||
|
||||
for _, foreign := range resultSlice {
|
||||
for _, local := range slice {
|
||||
{{/* Use the foreign table (the one without the fkey) to avoid null types in the id type */}}
|
||||
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
|
||||
{{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
|
||||
{{- $type := $column.Type -}}
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 == bytes.Compare(local.{{.Function.LocalAssignment}}, foreign.{{.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
if local.R == nil {
|
||||
local.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
|
|
|
@ -106,11 +106,17 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
|
|||
return nil
|
||||
}
|
||||
|
||||
{{$column := $dot.Table.GetColumn .Column -}}
|
||||
{{- $type := $column.Type -}}
|
||||
{{if .ToJoinTable -}}
|
||||
for i, foreign := range resultSlice {
|
||||
localJoinCol := localJoinCols[i]
|
||||
for _, local := range slice {
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, localJoinCol) {
|
||||
{{else -}}
|
||||
if local.{{$txt.Function.LocalAssignment}} == localJoinCol {
|
||||
{{end -}}
|
||||
if local.R == nil {
|
||||
local.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
|
@ -122,7 +128,11 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
|
|||
{{else -}}
|
||||
for _, foreign := range resultSlice {
|
||||
for _, local := range slice {
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
if local.R == nil {
|
||||
local.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
{{- define "relationship_to_one_setops_helper" -}}
|
||||
{{- $tmplData := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}}
|
||||
{{- $dot := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}}
|
||||
{{- with .Rel -}}
|
||||
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
|
||||
{{- $localNameSingular := .ForeignKey.Table | singular | camelCase}}
|
||||
|
@ -75,7 +75,14 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(e
|
|||
related.R.{{.Function.ForeignName}} = nil
|
||||
{{else -}}
|
||||
for i, ri := range related.R.{{.Function.ForeignName}} {
|
||||
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
|
||||
{{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
|
||||
{{- $type := $column.Type}}
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 != bytes.Compare({{.Function.Receiver}}.{{.Function.LocalAssignment}}, ri.{{.Function.LocalAssignment}}) {
|
||||
{{else -}}
|
||||
if {{.Function.Receiver}}.{{.Function.LocalAssignment}} != ri.{{.Function.LocalAssignment}} {
|
||||
{{end -}}
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -90,8 +97,8 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(e
|
|||
|
||||
return nil
|
||||
}
|
||||
{{- end -}}{{/* if foreignkey nullable */}}
|
||||
{{end -}}{{/* end with */}}
|
||||
{{end -}}{{/* if foreignkey nullable */}}
|
||||
{{- end -}}{{/* end with */}}
|
||||
{{- end -}}{{/* end define */}}
|
||||
|
||||
{{- /* Begin execution of template for one-to-one setops */ -}}
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
{{- $table := .Table }}
|
||||
{{- range .Table.ToManyRelationships -}}
|
||||
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
|
||||
{{- template "relationship_to_one_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
|
||||
{{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table . -}}
|
||||
{{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}}
|
||||
{{- else -}}
|
||||
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
|
||||
func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
|
||||
|
@ -59,12 +60,23 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
|
|||
|
||||
bFound, cFound := false, false
|
||||
for _, v := range {{$varname}} {
|
||||
{{- $column := $dot.Table.GetColumn .Column -}}
|
||||
{{- $type := $column.Type}}
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, b.{{$rel.Function.ForeignAssignment}}) {
|
||||
bFound = true
|
||||
}
|
||||
if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, c.{{$rel.Function.ForeignAssignment}}) {
|
||||
cFound = true
|
||||
}
|
||||
{{else -}}
|
||||
if v.{{$rel.Function.ForeignAssignment}} == b.{{$rel.Function.ForeignAssignment}} {
|
||||
bFound = true
|
||||
}
|
||||
if v.{{$rel.Function.ForeignAssignment}} == c.{{$rel.Function.ForeignAssignment}} {
|
||||
cFound = true
|
||||
}
|
||||
{{end -}}
|
||||
}
|
||||
|
||||
if !bFound {
|
||||
|
|
|
@ -8,8 +8,9 @@
|
|||
{{- else -}}
|
||||
{{- $varNameSingular := .Table | singular | camelCase -}}
|
||||
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
|
||||
{{- $rel := textsFromRelationship $dot.Tables $table .}}
|
||||
|
||||
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
|
||||
{{- $column := $dot.Table.GetColumn .Column -}}
|
||||
{{- $type := $column.Type}}
|
||||
func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) {
|
||||
var err error
|
||||
|
||||
|
@ -63,12 +64,21 @@ func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.
|
|||
}
|
||||
{{- else}}
|
||||
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}}) {
|
||||
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}})
|
||||
}
|
||||
if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, second.{{$rel.Function.ForeignAssignment}}) {
|
||||
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, second.{{$rel.Function.ForeignAssignment}})
|
||||
}
|
||||
{{else -}}
|
||||
if a.{{$rel.Function.LocalAssignment}} != first.{{$rel.Function.ForeignAssignment}} {
|
||||
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}})
|
||||
}
|
||||
if a.{{$rel.Function.LocalAssignment}} != second.{{$rel.Function.ForeignAssignment}} {
|
||||
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, second.{{$rel.Function.ForeignAssignment}})
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
if first.R.{{$rel.Function.ForeignName}} != &a {
|
||||
t.Error("relationship was not added properly to the foreign slice")
|
||||
|
@ -174,12 +184,21 @@ func test{{$rel.LocalTable.NameGo}}ToManySetOp{{$rel.Function.Name}}(t *testing.
|
|||
if c.{{$rel.ForeignTable.ColumnNameGo}}.Valid {
|
||||
t.Error("want c's foreign key value to be nil")
|
||||
}
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}}) {
|
||||
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}})
|
||||
}
|
||||
if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, e.{{$rel.Function.ForeignAssignment}}) {
|
||||
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, e.{{$rel.Function.ForeignAssignment}})
|
||||
}
|
||||
{{else -}}
|
||||
if a.{{$rel.Function.LocalAssignment}} != d.{{$rel.Function.ForeignAssignment}} {
|
||||
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}})
|
||||
}
|
||||
if a.{{$rel.Function.LocalAssignment}} != e.{{$rel.Function.ForeignAssignment}} {
|
||||
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, e.{{$rel.Function.ForeignAssignment}})
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
if b.R.{{$rel.Function.ForeignName}} != nil {
|
||||
t.Error("relationship was not removed properly from the foreign struct")
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
{{- define "relationship_to_one_test_helper"}}
|
||||
{{- $dot := .Dot -}}
|
||||
{{- with .Rel -}}
|
||||
func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
||||
tx := MustTx(boil.Begin())
|
||||
defer tx.Rollback()
|
||||
|
@ -37,7 +39,14 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
{{$foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
|
||||
{{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
|
||||
{{- $type := $column.Type -}}
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 == bytes.Compare(check.{{.Function.ForeignAssignment}}, foreign.{{.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}})
|
||||
}
|
||||
|
||||
|
@ -59,11 +68,12 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
|
|||
}
|
||||
|
||||
{{end -}}
|
||||
{{- end -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_test_helper" $rel -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}}
|
||||
{{end -}}
|
||||
{{- end -}}
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
{{- define "relationship_to_one_setops_test_helper" -}}
|
||||
{{- $dot := .Dot -}}
|
||||
{{- with .Rel -}}
|
||||
{{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}}
|
||||
{{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase}}
|
||||
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
|
||||
{{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
|
||||
{{- $type := $column.Type -}}
|
||||
func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
||||
var err error
|
||||
|
||||
|
@ -34,7 +39,11 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}})
|
||||
}
|
||||
if a.R.{{.Function.Name}} != x {
|
||||
|
@ -48,7 +57,11 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
|||
t.Fatal("failed to reload", err)
|
||||
}
|
||||
|
||||
{{if eq $type "[]byte" -}}
|
||||
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}})
|
||||
}
|
||||
|
||||
|
@ -120,14 +133,14 @@ func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function
|
|||
}
|
||||
{{end -}}
|
||||
}
|
||||
{{end -}}
|
||||
{{- end -}}
|
||||
{{end -}}{{/* end if foreign key nullable */}}
|
||||
{{- end -}}{{/* with rel */}}
|
||||
{{- end -}}{{/* define */}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}}
|
||||
|
||||
{{template "relationship_to_one_setops_test_helper" $rel -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}}
|
||||
{{template "relationship_to_one_setops_test_helper" (preserveDot $dot $txt) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
||||
var {{$varNameSingular}}DBTypes = map[string]string{{"{"}}{{.Table.Columns | columnDBTypes | makeStringMap}}{{"}"}}
|
||||
var (
|
||||
{{$varNameSingular}}DBTypes = map[string]string{{"{"}}{{.Table.Columns | columnDBTypes | makeStringMap}}{{"}"}}
|
||||
_ = bytes.MinRead
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue