Unexport Relationship struct

This commit is contained in:
Patrick O'brien 2016-09-01 13:33:05 +10:00
parent 2ecff95e42
commit 67ae024439
5 changed files with 23 additions and 17 deletions

View file

@ -4,6 +4,7 @@
{{- $tableNameSingular := .Table.Name | singular -}} {{- $tableNameSingular := .Table.Name | singular -}}
{{- $modelName := $tableNameSingular | titleCase -}} {{- $modelName := $tableNameSingular | titleCase -}}
{{- $modelNameCamel := $tableNameSingular | camelCase -}}
// {{$modelName}} is an object representing the database table. // {{$modelName}} is an object representing the database table.
type {{$modelName}} struct { type {{$modelName}} struct {
{{range $column := .Table.Columns -}} {{range $column := .Table.Columns -}}
@ -11,15 +12,15 @@ type {{$modelName}} struct {
{{end -}} {{end -}}
{{- if .Table.IsJoinTable -}} {{- if .Table.IsJoinTable -}}
{{- else}} {{- else}}
R *{{$modelName}}R `boil:"-" json:"-" toml:"-" yaml:"-"` R *{{$modelNameCamel}}R `boil:"-" json:"-" toml:"-" yaml:"-"`
{{end -}} {{end -}}
} }
{{- $dot := . -}} {{- $dot := . -}}
{{- if .Table.IsJoinTable -}} {{- if .Table.IsJoinTable -}}
{{- else}} {{- else}}
// {{$modelName}}R is where relationships are stored. // {{$modelNameCamel}}R is where relationships are stored.
type {{$modelName}}R struct { type {{$modelNameCamel}}R struct {
{{range .Table.FKeys -}} {{range .Table.FKeys -}}
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}} {{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
{{- template "relationship_to_one_struct_helper" $rel}} {{- template "relationship_to_one_struct_helper" $rel}}

View file

@ -1,11 +1,12 @@
{{- define "relationship_to_one_eager_helper" -}} {{- define "relationship_to_one_eager_helper" -}}
{{- $varNameSingular := .Dot.Table.Name | singular | camelCase -}}
{{- $noHooks := .Dot.NoHooks -}} {{- $noHooks := .Dot.NoHooks -}}
{{- with .Rel -}} {{- with .Rel -}}
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}} {{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}} {{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
// Load{{.Function.Name}} allows an eager lookup of values, cached into the // Load{{.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects. // loaded structs of the objects.
func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { func (r *{{$varNameSingular}}R) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
var slice []*{{.LocalTable.NameGo}} var slice []*{{.LocalTable.NameGo}}
var object *{{.LocalTable.NameGo}} var object *{{.LocalTable.NameGo}}
@ -58,7 +59,7 @@ func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singul
if singular && len(resultSlice) != 0 { if singular && len(resultSlice) != 0 {
if object.R == nil { if object.R == nil {
object.R = &{{.LocalTable.NameGo}}R{} object.R = &{{$varNameSingular}}R{}
} }
object.R.{{.Function.Name}} = resultSlice[0] object.R.{{.Function.Name}} = resultSlice[0]
return nil return nil
@ -68,7 +69,7 @@ func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singul
for _, local := range slice { for _, local := range slice {
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} { if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {
if local.R == nil { if local.R == nil {
local.R = &{{.LocalTable.NameGo}}R{} local.R = &{{$varNameSingular}}R{}
} }
local.R.{{.Function.Name}} = foreign local.R.{{.Function.Name}} = foreign
break break

View file

@ -6,12 +6,13 @@
{{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table . -}} {{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table . -}}
{{- template "relationship_to_one_eager_helper" (preserveDot $dot $txt) -}} {{- template "relationship_to_one_eager_helper" (preserveDot $dot $txt) -}}
{{- else -}} {{- else -}}
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
{{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}} {{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}}
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}} {{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}} {{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}}
// Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the // Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects. // loaded structs of the objects.
func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { func (r *{{$varNameSingular}}R) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
var slice []*{{$txt.LocalTable.NameGo}} var slice []*{{$txt.LocalTable.NameGo}}
var object *{{$txt.LocalTable.NameGo}} var object *{{$txt.LocalTable.NameGo}}
@ -94,7 +95,7 @@ func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor
{{- end}} {{- end}}
if singular { if singular {
if object.R == nil { if object.R == nil {
object.R = &{{$txt.LocalTable.NameGo}}R{} object.R = &{{$varNameSingular}}R{}
} }
object.R.{{$txt.Function.Name}} = resultSlice object.R.{{$txt.Function.Name}} = resultSlice
return nil return nil
@ -106,7 +107,7 @@ func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor
for _, local := range slice { for _, local := range slice {
if local.{{$txt.Function.LocalAssignment}} == localJoinCol { if local.{{$txt.Function.LocalAssignment}} == localJoinCol {
if local.R == nil { if local.R == nil {
local.R = &{{$txt.LocalTable.NameGo}}R{} local.R = &{{$varNameSingular}}R{}
} }
local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign) local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign)
break break
@ -118,7 +119,7 @@ func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor
for _, local := range slice { for _, local := range slice {
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} { if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} {
if local.R == nil { if local.R == nil {
local.R = &{{$txt.LocalTable.NameGo}}R{} local.R = &{{$varNameSingular}}R{}
} }
local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign) local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign)
break break

View file

@ -1,5 +1,6 @@
{{- define "relationship_to_one_setops_helper" -}} {{- define "relationship_to_one_setops_helper" -}}
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase}} {{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
{{- $localNameSingular := .ForeignKey.Table | singular | camelCase -}}
// Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item. // Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
// Sets {{.Function.Receiver}}.R.{{.Function.Name}} to related. // Sets {{.Function.Receiver}}.R.{{.Function.Name}} to related.
@ -20,7 +21,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
} }
if {{.Function.Receiver}}.R == nil { if {{.Function.Receiver}}.R == nil {
{{.Function.Receiver}}.R = &{{.LocalTable.NameGo}}R{ {{.Function.Receiver}}.R = &{{$localNameSingular}}R{
{{.Function.Name}}: related, {{.Function.Name}}: related,
} }
} else { } else {
@ -29,7 +30,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
{{if (or .ForeignKey.Unique .Function.OneToOne) -}} {{if (or .ForeignKey.Unique .Function.OneToOne) -}}
if related.R == nil { if related.R == nil {
related.R = &{{.ForeignTable.NameGo}}R{ related.R = &{{$varNameSingular}}R{
{{.Function.ForeignName}}: {{.Function.Receiver}}, {{.Function.ForeignName}}: {{.Function.Receiver}},
} }
} else { } else {
@ -37,7 +38,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
} }
{{else -}} {{else -}}
if related.R == nil { if related.R == nil {
related.R = &{{.ForeignTable.NameGo}}R{ related.R = &{{$varNameSingular}}R{
{{.Function.ForeignName}}: {{.LocalTable.NameGo}}Slice{{"{"}}{{.Function.Receiver}}{{"}"}}, {{.Function.ForeignName}}: {{.LocalTable.NameGo}}Slice{{"{"}}{{.Function.Receiver}}{{"}"}},
} }
} else { } else {

View file

@ -8,6 +8,8 @@
{{- template "relationship_to_one_setops_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}} {{- template "relationship_to_one_setops_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
{{- else -}} {{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table .}} {{- $rel := textsFromRelationship $dot.Tables $table .}}
{{- $localNameSingular := .Table | singular | camelCase -}}
{{- $foreignNameSingular := .ForeignTable | singular | camelCase -}}
// Add{{$rel.Function.Name}} adds the given related objects to the existing relationships // Add{{$rel.Function.Name}} adds the given related objects to the existing relationships
// of the {{$table.Name | singular}}, optionally inserting them as new records. // of the {{$table.Name | singular}}, optionally inserting them as new records.
@ -51,7 +53,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
{{end -}} {{end -}}
if {{$rel.Function.Receiver}}.R == nil { if {{$rel.Function.Receiver}}.R == nil {
{{$rel.Function.Receiver}}.R = &{{$rel.LocalTable.NameGo}}R{ {{$rel.Function.Receiver}}.R = &{{$localNameSingular}}R{
{{$rel.Function.Name}}: related, {{$rel.Function.Name}}: related,
} }
} else { } else {
@ -61,7 +63,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
for _, rel := range related { for _, rel := range related {
if rel.R == nil { if rel.R == nil {
rel.R = &{{$rel.ForeignTable.NameGo}}R{ rel.R = &{{$foreignNameSingular}}R{
{{$rel.Function.ForeignName}}: {{$rel.LocalTable.NameGo}}Slice{{"{"}}{{$rel.Function.Receiver}}{{"}"}}, {{$rel.Function.ForeignName}}: {{$rel.LocalTable.NameGo}}Slice{{"{"}}{{$rel.Function.Receiver}}{{"}"}},
} }
} else { } else {
@ -71,7 +73,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
{{else -}} {{else -}}
for _, rel := range related { for _, rel := range related {
if rel.R == nil { if rel.R == nil {
rel.R = &{{$rel.ForeignTable.NameGo}}R{ rel.R = &{{$foreignNameSingular}}R{
{{$rel.Function.ForeignName}}: {{$rel.Function.Receiver}}, {{$rel.Function.ForeignName}}: {{$rel.Function.Receiver}},
} }
} else { } else {