From e9eda8fa1bc511bf1719f4240627aa67fd1ad743 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sat, 24 Sep 2016 00:23:17 -0700 Subject: [PATCH] Revert "Copy to_one_setops into one_to_one_setops" This reverts commit e157a59714a64c802c8ebf66481401e70a6db3fd. --- templates/10_relationship_to_one_setops.tpl | 4 +- .../11_relationship_one_to_one_setops.tpl | 62 +++++++------------ 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/templates/10_relationship_to_one_setops.tpl b/templates/10_relationship_to_one_setops.tpl index 9b56574..d0f7a01 100644 --- a/templates/10_relationship_to_one_setops.tpl +++ b/templates/10_relationship_to_one_setops.tpl @@ -13,7 +13,7 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo var err error if insert { if err = related.Insert(exec); err != nil { - return errors.Wrap(err, "failed to insert into foreign table {{$txt.ForeignTable}}") + return errors.Wrap(err, "failed to insert into foreign table") } } @@ -30,7 +30,7 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo } if _, err = exec.Exec(updateQuery, values...); err != nil { - return errors.Wrap(err, "failed to update local table {{.Table}}") + return errors.Wrap(err, "failed to update local table") } o.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}} diff --git a/templates/11_relationship_one_to_one_setops.tpl b/templates/11_relationship_one_to_one_setops.tpl index cfac999..64f5a4b 100644 --- a/templates/11_relationship_one_to_one_setops.tpl +++ b/templates/11_relationship_one_to_one_setops.tpl @@ -3,67 +3,53 @@ {{- $dot := . -}} {{- range .Table.ToOneRelationships -}} {{- $txt := txtsFromOneToOne $dot.Tables $dot.Table . -}} - {{- $foreignNameSingular := .ForeignTable | singular | camelCase -}} - {{- $varNameSingular := .Table | singular | camelCase}} - {{- $schemaTable := .Table | $dot.SchemaTable -}} + {{- $varNameSingular := .ForeignTable | singular | camelCase -}} + {{- $localNameSingular := .Table | singular | camelCase}} // Set{{$txt.Function.Name}} of the {{.Table | singular}} to the related item. // 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 + + oldVal := related.{{$txt.Function.ForeignAssignment}} + related.{{$txt.Function.ForeignAssignment}} = o.{{$txt.Function.LocalAssignment}} + {{if .ForeignColumnNullable -}} + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true + {{- end}} + if insert { if err = related.Insert(exec); err != nil { - return errors.Wrap(err, "failed to insert into foreign table {{$txt.ForeignTable}}") + related.{{$txt.Function.ForeignAssignment}} = oldVal + {{if .ForeignColumnNullable -}} + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false + {{- end}} + return errors.Wrap(err, "failed to insert into foreign table") + } + } 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") } } - 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) - } - - if _, err = exec.Exec(updateQuery, values...); err != nil { - return errors.Wrap(err, "failed to update local table {{.Table}}") - } - - o.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}} - {{if .Nullable -}} - o.{{$txt.LocalTable.ColumnNameGo}}.Valid = true - {{- end}} - if o.R == nil { - o.R = &{{$varNameSingular}}R{ + o.R = &{{$localNameSingular}}R{ {{$txt.Function.Name}}: related, } } else { o.R.{{$txt.Function.Name}} = related } - {{if .Unique -}} if related.R == nil { - related.R = &{{$foreignNameSingular}}R{ + related.R = &{{$varNameSingular}}R{ {{$txt.Function.ForeignName}}: o, } } else { related.R.{{$txt.Function.ForeignName}} = o } - {{else -}} - if related.R == nil { - related.R = &{{$foreignNameSingular}}R{ - {{$txt.Function.ForeignName}}: {{$txt.LocalTable.NameGo}}Slice{{"{"}}o{{"}"}}, - } - } else { - related.R.{{$txt.Function.ForeignName}} = append(related.R.{{$txt.Function.ForeignName}}, o) - } - {{- end}} - return nil }