2016-09-18 20:13:11 +02:00
|
|
|
{{- /* Begin execution of template for one-to-one setops */ -}}
|
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- range .Table.FKeys -}}
|
2016-09-18 20:18:43 +02:00
|
|
|
{{- $txt := txtsFromFKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
|
2016-09-17 09:02:03 +02:00
|
|
|
{{- $localNameSingular := .ForeignKey.Table | singular | camelCase}}
|
2016-09-18 20:13:11 +02:00
|
|
|
// Set{{$txt.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
|
|
|
|
// Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to related.
|
|
|
|
// Adds {{$txt.Function.Receiver}} to related.R.{{$txt.Function.ForeignName}}.
|
|
|
|
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executor, insert bool, related *{{$txt.ForeignTable.NameGo}}) error {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
if insert {
|
|
|
|
if err = related.Insert(exec); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to insert into foreign table")
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
oldVal := {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
|
|
|
|
related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
|
|
|
|
{{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}}
|
|
|
|
if err = {{$txt.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
|
|
|
|
{{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = oldVal
|
2016-09-14 10:08:30 +02:00
|
|
|
return errors.Wrap(err, "failed to update local table")
|
|
|
|
}
|
2016-08-27 07:43:50 +02:00
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
if {{$txt.Function.Receiver}}.R == nil {
|
|
|
|
{{$txt.Function.Receiver}}.R = &{{$localNameSingular}}R{
|
|
|
|
{{$txt.Function.Name}}: related,
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-18 20:13:11 +02:00
|
|
|
{{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = related
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-27 07:43:50 +02:00
|
|
|
|
2016-09-18 20:21:26 +02:00
|
|
|
{{if .ForeignKey.Unique -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
if related.R == nil {
|
|
|
|
related.R = &{{$varNameSingular}}R{
|
2016-09-18 20:13:11 +02:00
|
|
|
{{$txt.Function.ForeignName}}: {{$txt.Function.Receiver}},
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-18 20:13:11 +02:00
|
|
|
related.R.{{$txt.Function.ForeignName}} = {{$txt.Function.Receiver}}
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
{{else -}}
|
|
|
|
if related.R == nil {
|
|
|
|
related.R = &{{$varNameSingular}}R{
|
2016-09-18 20:13:11 +02:00
|
|
|
{{$txt.Function.ForeignName}}: {{$txt.LocalTable.NameGo}}Slice{{"{"}}{{$txt.Function.Receiver}}{{"}"}},
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-18 20:13:11 +02:00
|
|
|
related.R.{{$txt.Function.ForeignName}} = append(related.R.{{$txt.Function.ForeignName}}, {{$txt.Function.Receiver}})
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
{{end -}}
|
2016-08-28 09:06:33 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if .ForeignKey.Nullable}}
|
2016-09-18 20:13:11 +02:00
|
|
|
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
|
2016-09-14 10:08:30 +02:00
|
|
|
{{end -}}
|
|
|
|
return nil
|
2016-08-26 08:50:45 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 20:21:26 +02:00
|
|
|
{{- if .ForeignKey.Nullable}}
|
2016-09-18 20:13:11 +02:00
|
|
|
// Remove{{$txt.Function.Name}} relationship.
|
|
|
|
// Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to nil.
|
|
|
|
// Removes {{$txt.Function.Receiver}} from all passed in related items' relationships struct (Optional).
|
|
|
|
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Executor, related *{{$txt.ForeignTable.NameGo}}) error {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
2016-08-27 08:36:57 +02:00
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = false
|
|
|
|
if err = {{$txt.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
|
|
|
|
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
|
2016-09-14 10:08:30 +02:00
|
|
|
return errors.Wrap(err, "failed to update local table")
|
|
|
|
}
|
2016-08-27 08:36:57 +02:00
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
{{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = nil
|
2016-09-14 10:08:30 +02:00
|
|
|
if related == nil || related.R == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 06:38:25 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if .ForeignKey.Unique -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
related.R.{{$txt.Function.ForeignName}} = nil
|
2016-09-14 10:08:30 +02:00
|
|
|
{{else -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
for i, ri := range related.R.{{$txt.Function.ForeignName}} {
|
|
|
|
{{if $txt.Function.UsesBytes -}}
|
|
|
|
if 0 != bytes.Compare({{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}, ri.{{$txt.Function.LocalAssignment}}) {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{else -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} != ri.{{$txt.Function.LocalAssignment}} {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-08-29 00:01:37 +02:00
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
ln := len(related.R.{{$txt.Function.ForeignName}})
|
2016-09-14 10:08:30 +02:00
|
|
|
if ln > 1 && i < ln-1 {
|
2016-09-18 20:13:11 +02:00
|
|
|
related.R.{{$txt.Function.ForeignName}}[i] = related.R.{{$txt.Function.ForeignName}}[ln-1]
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-09-18 20:13:11 +02:00
|
|
|
related.R.{{$txt.Function.ForeignName}} = related.R.{{$txt.Function.ForeignName}}[:ln-1]
|
2016-09-14 10:08:30 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
{{end -}}
|
2016-08-29 00:01:37 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return nil
|
2016-08-26 08:50:45 +02:00
|
|
|
}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}{{/* if foreignkey nullable */}}
|
2016-09-18 20:13:11 +02:00
|
|
|
{{- end -}}{{/* range */}}
|
|
|
|
{{- end -}}{{/* join table */}}
|