sqlboiler/templates/11_relationship_one_to_one_setops.tpl

80 lines
2.6 KiB
Smarty
Raw Normal View History

2016-09-18 20:13:11 +02:00
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
2016-09-19 01:49:18 +02:00
{{- range .Table.ToOneRelationships -}}
{{- $txt := txtsFromOneToOne $dot.Tables $dot.Table . -}}
2016-09-19 01:02:08 +02:00
{{- $varNameSingular := .ForeignTable | singular | camelCase -}}
{{- $localNameSingular := .Table | singular | camelCase}}
// Set{{$txt.Function.Name}} of the {{.Table | singular}} to the related item.
2016-09-21 05:37:28 +02:00
// Sets o.R.{{$txt.Function.Name}} to related.
// Adds o to related.R.{{$txt.Function.ForeignName}}.
func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executor, insert bool, related *{{$txt.ForeignTable.NameGo}}) error {
var err error
2016-09-19 02:43:44 +02:00
oldVal := related.{{$txt.Function.ForeignAssignment}}
2016-09-21 05:37:28 +02:00
related.{{$txt.Function.ForeignAssignment}} = o.{{$txt.Function.LocalAssignment}}
2016-09-19 02:43:44 +02:00
{{if .ForeignColumnNullable -}}
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
{{- end}}
if insert {
if err = related.Insert(exec); err != nil {
2016-09-19 02:43:44 +02:00
related.{{$txt.Function.ForeignAssignment}} = oldVal
{{if .ForeignColumnNullable -}}
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false
{{- end}}
return errors.Wrap(err, "failed to insert into foreign table")
}
2016-09-19 02:43:44 +02:00
} else {
if err = related.Update(exec, "{{.ForeignColumn}}"); err != nil {
related.{{$txt.Function.ForeignAssignment}} = oldVal
{{if .ForeignColumnNullable -}}
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false
{{- end}}
return errors.Wrap(err, "failed to update foreign table")
}
}
2016-08-27 07:43:50 +02:00
2016-09-21 05:37:28 +02:00
if o.R == nil {
o.R = &{{$localNameSingular}}R{
2016-09-18 20:13:11 +02:00
{{$txt.Function.Name}}: related,
}
} else {
2016-09-21 05:37:28 +02:00
o.R.{{$txt.Function.Name}} = related
}
2016-08-27 07:43:50 +02:00
if related.R == nil {
related.R = &{{$varNameSingular}}R{
2016-09-21 05:37:28 +02:00
{{$txt.Function.ForeignName}}: o,
}
} else {
2016-09-21 05:37:28 +02:00
related.R.{{$txt.Function.ForeignName}} = o
}
return nil
2016-08-26 08:50:45 +02:00
}
2016-09-19 02:43:44 +02:00
{{- if .ForeignColumnNullable}}
2016-09-18 20:13:11 +02:00
// Remove{{$txt.Function.Name}} relationship.
2016-09-21 05:37:28 +02:00
// Sets o.R.{{$txt.Function.Name}} to nil.
// Removes o from all passed in related items' relationships struct (Optional).
func (o *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Executor, related *{{$txt.ForeignTable.NameGo}}) error {
var err error
2016-09-19 02:43:44 +02:00
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false
if err = related.Update(exec, "{{.ForeignColumn}}"); err != nil {
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
return errors.Wrap(err, "failed to update local table")
}
2016-09-21 05:37:28 +02:00
o.R.{{$txt.Function.Name}} = nil
if related == nil || related.R == nil {
return nil
}
2016-09-01 06:38:25 +02:00
2016-09-18 20:13:11 +02:00
related.R.{{$txt.Function.ForeignName}} = nil
return nil
2016-08-26 08:50:45 +02:00
}
{{end -}}{{/* if foreignkey nullable */}}
2016-09-18 20:13:11 +02:00
{{- end -}}{{/* range */}}
{{- end -}}{{/* join table */}}