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-24 23:34:31 +02:00
|
|
|
{{- $varNameSingular := .Table | singular | camelCase -}}
|
|
|
|
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
|
|
|
|
{{- $foreignPKeyCols := (getTable $dot.Tables .ForeignTable).PKey.Columns -}}
|
|
|
|
{{- $foreignSchemaTable := .ForeignTable | $dot.SchemaTable}}
|
2017-01-10 06:06:47 +01:00
|
|
|
// Set{{$txt.Function.Name}}G of the {{.Table | singular}} to the related item.
|
|
|
|
// Sets o.R.{{$txt.Function.Name}} to related.
|
|
|
|
// Adds o to related.R.{{$txt.Function.ForeignName}}.
|
|
|
|
// Uses the global database handle.
|
|
|
|
func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}G(insert bool, related *{{$txt.ForeignTable.NameGo}}) error {
|
|
|
|
return o.Set{{$txt.Function.Name}}(boil.GetDB(), insert, related)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set{{$txt.Function.Name}}P of the {{.Table | singular}} to the related item.
|
|
|
|
// Sets o.R.{{$txt.Function.Name}} to related.
|
|
|
|
// Adds o to related.R.{{$txt.Function.ForeignName}}.
|
|
|
|
// Panics on error.
|
|
|
|
func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}P(exec boil.Executor, insert bool, related *{{$txt.ForeignTable.NameGo}}) {
|
|
|
|
if err := o.Set{{$txt.Function.Name}}(exec, insert, related); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set{{$txt.Function.Name}}GP of the {{.Table | singular}} to the related item.
|
|
|
|
// Sets o.R.{{$txt.Function.Name}} to related.
|
|
|
|
// Adds o to related.R.{{$txt.Function.ForeignName}}.
|
|
|
|
// Uses the global database handle and panics on error.
|
|
|
|
func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}GP(insert bool, related *{{$txt.ForeignTable.NameGo}}) {
|
|
|
|
if err := o.Set{{$txt.Function.Name}}(boil.GetDB(), insert, related); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 01:02:08 +02:00
|
|
|
// 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 {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
2016-09-24 09:23:17 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if insert {
|
2016-09-24 23:34:31 +02:00
|
|
|
related.{{$txt.Function.ForeignAssignment}} = o.{{$txt.Function.LocalAssignment}}
|
|
|
|
{{if .ForeignColumnNullable -}}
|
|
|
|
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
|
|
|
|
{{- end}}
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if err = related.Insert(exec); err != nil {
|
2016-09-24 09:23:17 +02:00
|
|
|
return errors.Wrap(err, "failed to insert into foreign table")
|
|
|
|
}
|
|
|
|
} else {
|
2016-09-24 23:34:31 +02:00
|
|
|
updateQuery := fmt.Sprintf(
|
|
|
|
"UPDATE {{$foreignSchemaTable}} SET %s WHERE %s",
|
|
|
|
strmangle.SetParamNames("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.ForeignColumn}}"{{"}"}}),
|
|
|
|
strmangle.WhereClause("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}2{{else}}0{{end}}, {{$foreignVarNameSingular}}PrimaryKeyColumns),
|
|
|
|
)
|
|
|
|
values := []interface{}{o.{{$txt.LocalTable.ColumnNameGo}}, related.{{$foreignPKeyCols | stringMap $dot.StringFuncs.titleCase | join ", related."}}{{"}"}}
|
|
|
|
|
|
|
|
if boil.DebugMode {
|
2017-08-23 22:37:33 +02:00
|
|
|
qStr, err := InterpolateParams(updateQuery, values...)
|
2017-07-06 21:15:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(boil.DebugWriter, qStr)
|
2016-09-24 23:34:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = exec.Exec(updateQuery, values...); err != nil {
|
2016-09-24 09:23:17 +02:00
|
|
|
return errors.Wrap(err, "failed to update foreign table")
|
2016-09-19 02:43:44 +02:00
|
|
|
}
|
2016-09-24 23:34:31 +02:00
|
|
|
|
|
|
|
related.{{$txt.Function.ForeignAssignment}} = o.{{$txt.Function.LocalAssignment}}
|
|
|
|
{{if .ForeignColumnNullable -}}
|
|
|
|
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
|
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-27 07:43:50 +02:00
|
|
|
|
2016-09-24 23:34:31 +02:00
|
|
|
|
2016-09-21 05:37:28 +02:00
|
|
|
if o.R == nil {
|
2016-09-24 23:34:31 +02:00
|
|
|
o.R = &{{$varNameSingular}}R{
|
2016-09-18 20:13:11 +02:00
|
|
|
{{$txt.Function.Name}}: related,
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-21 05:37:28 +02:00
|
|
|
o.R.{{$txt.Function.Name}} = related
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-27 07:43:50 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if related.R == nil {
|
2016-09-24 23:34:31 +02:00
|
|
|
related.R = &{{$foreignVarNameSingular}}R{
|
2016-09-21 05:37:28 +02:00
|
|
|
{{$txt.Function.ForeignName}}: o,
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-21 05:37:28 +02:00
|
|
|
related.R.{{$txt.Function.ForeignName}} = o
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
return nil
|
2016-08-26 08:50:45 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 02:43:44 +02:00
|
|
|
{{- if .ForeignColumnNullable}}
|
2017-01-10 06:06:47 +01:00
|
|
|
// Remove{{$txt.Function.Name}}G relationship.
|
|
|
|
// Sets o.R.{{$txt.Function.Name}} to nil.
|
|
|
|
// Removes o from all passed in related items' relationships struct (Optional).
|
|
|
|
// Uses the global database handle.
|
|
|
|
func (o *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}G(related *{{$txt.ForeignTable.NameGo}}) error {
|
|
|
|
return o.Remove{{$txt.Function.Name}}(boil.GetDB(), related)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove{{$txt.Function.Name}}P relationship.
|
|
|
|
// Sets o.R.{{$txt.Function.Name}} to nil.
|
|
|
|
// Removes o from all passed in related items' relationships struct (Optional).
|
|
|
|
// Panics on error.
|
|
|
|
func (o *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}P(exec boil.Executor, related *{{$txt.ForeignTable.NameGo}}) {
|
|
|
|
if err := o.Remove{{$txt.Function.Name}}(exec, related); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove{{$txt.Function.Name}}GP relationship.
|
|
|
|
// Sets o.R.{{$txt.Function.Name}} to nil.
|
|
|
|
// Removes o from all passed in related items' relationships struct (Optional).
|
|
|
|
// Uses the global database handle and panics on error.
|
|
|
|
func (o *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}GP(related *{{$txt.ForeignTable.NameGo}}) {
|
|
|
|
if err := o.Remove{{$txt.Function.Name}}(boil.GetDB(), related); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
2016-08-27 08:36:57 +02:00
|
|
|
|
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
|
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-21 05:37:28 +02:00
|
|
|
o.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-18 20:13:11 +02:00
|
|
|
related.R.{{$txt.Function.ForeignName}} = nil
|
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 */}}
|