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