sqlboiler/templates/10_relationship_to_one_setops.tpl

170 lines
6.2 KiB
Smarty
Raw Normal View History

2016-09-18 20:13:11 +02:00
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.FKeys -}}
2016-09-19 01:02:08 +02:00
{{- $txt := txtsFromFKey $dot.Tables $dot.Table . -}}
2016-09-23 08:17:54 +02:00
{{- $foreignNameSingular := .ForeignTable | singular | camelCase -}}
{{- $varNameSingular := .Table | singular | camelCase}}
{{- $schemaTable := .Table | $dot.SchemaTable}}
// 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 {
2018-02-07 15:35:46 +01:00
panic(errors.Err(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 {
2018-02-07 15:35:46 +01:00
panic(errors.Err(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 {
var err error
if insert {
if err = related.Insert(exec); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Prefix("failed to insert into foreign table", err)
}
}
2016-09-23 08:17:54 +02:00
updateQuery := fmt.Sprintf(
"UPDATE {{$schemaTable}} SET %s WHERE %s",
strmangle.SetParamNames("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.Column}}"{{"}"}}),
strmangle.WhereClause("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}2{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns),
)
values := []interface{}{related.{{$txt.ForeignTable.ColumnNameGo}}, o.{{$dot.Table.PKey.Columns | stringMap $dot.StringFuncs.titleCase | join ", o."}}{{"}"}}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, updateQuery)
fmt.Fprintln(boil.DebugWriter, values)
2016-09-23 08:17:54 +02:00
}
if _, err = exec.Exec(updateQuery, values...); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Prefix("failed to update local table", err)
2016-09-23 08:17:54 +02:00
}
2016-09-21 05:37:28 +02:00
o.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}}
2016-09-19 02:43:44 +02:00
{{if .Nullable -}}
2016-09-21 05:37:28 +02:00
o.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
2016-09-19 02:43:44 +02:00
{{- end}}
2016-08-27 07:43:50 +02:00
2016-09-21 05:37:28 +02:00
if o.R == nil {
2016-09-23 08:17:54 +02:00
o.R = &{{$varNameSingular}}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
2016-09-19 01:02:08 +02:00
{{if .Unique -}}
if related.R == nil {
2016-09-23 08:17:54 +02:00
related.R = &{{$foreignNameSingular}}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
}
{{else -}}
if related.R == nil {
2016-09-23 08:17:54 +02:00
related.R = &{{$foreignNameSingular}}R{
2016-09-21 05:37:28 +02:00
{{$txt.Function.ForeignName}}: {{$txt.LocalTable.NameGo}}Slice{{"{"}}o{{"}"}},
}
} else {
2016-09-21 05:37:28 +02:00
related.R.{{$txt.Function.ForeignName}} = append(related.R.{{$txt.Function.ForeignName}}, o)
}
2016-09-19 02:43:44 +02:00
{{- end}}
return nil
2016-08-26 08:50:45 +02:00
}
2016-09-19 01:02:08 +02:00
{{- if .Nullable}}
// 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 {
2018-02-07 15:35:46 +01:00
panic(errors.Err(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 {
2018-02-07 15:35:46 +01:00
panic(errors.Err(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 {
var err error
2016-09-21 05:37:28 +02:00
o.{{$txt.LocalTable.ColumnNameGo}}.Valid = false
if err = o.Update(exec, "{{.Column}}"); err != nil {
o.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
2018-02-07 15:35:46 +01:00
return errors.Prefix("failed to update local table", err)
}
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-19 01:02:08 +02:00
{{if .Unique -}}
2016-09-18 20:13:11 +02:00
related.R.{{$txt.Function.ForeignName}} = nil
{{else -}}
2016-09-18 20:13:11 +02:00
for i, ri := range related.R.{{$txt.Function.ForeignName}} {
{{if $txt.Function.UsesBytes -}}
2016-09-21 05:37:28 +02:00
if 0 != bytes.Compare(o.{{$txt.Function.LocalAssignment}}, ri.{{$txt.Function.LocalAssignment}}) {
{{else -}}
2016-09-21 05:37:28 +02:00
if o.{{$txt.Function.LocalAssignment}} != ri.{{$txt.Function.LocalAssignment}} {
{{end -}}
continue
}
2016-08-29 00:01:37 +02:00
2016-09-18 20:13:11 +02:00
ln := len(related.R.{{$txt.Function.ForeignName}})
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-18 20:13:11 +02:00
related.R.{{$txt.Function.ForeignName}} = related.R.{{$txt.Function.ForeignName}}[:ln-1]
break
}
{{end -}}
2016-08-29 00:01:37 +02:00
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 */}}