From fa2202abb2dfcf24147f434970c342c7cd2330cd Mon Sep 17 00:00:00 2001 From: Aaron L Date: Thu, 22 Sep 2016 23:17:54 -0700 Subject: [PATCH] Fix SetOp for half join tables --- templates/10_relationship_to_one_setops.tpl | 35 ++++++++++++------- templates_test/relationship_to_one_setops.tpl | 8 +++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/templates/10_relationship_to_one_setops.tpl b/templates/10_relationship_to_one_setops.tpl index 7653361..d0f7a01 100644 --- a/templates/10_relationship_to_one_setops.tpl +++ b/templates/10_relationship_to_one_setops.tpl @@ -3,8 +3,9 @@ {{- $dot := . -}} {{- range .Table.FKeys -}} {{- $txt := txtsFromFKey $dot.Tables $dot.Table . -}} - {{- $varNameSingular := .ForeignTable | singular | camelCase -}} - {{- $localNameSingular := .Table | singular | camelCase}} + {{- $foreignNameSingular := .ForeignTable | singular | camelCase -}} + {{- $varNameSingular := .Table | singular | camelCase}} + {{- $schemaTable := .Table | $dot.SchemaTable -}} // 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}}. @@ -16,21 +17,29 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo } } - oldVal := o.{{$txt.Function.LocalAssignment}} + 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") + } + o.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}} {{if .Nullable -}} o.{{$txt.LocalTable.ColumnNameGo}}.Valid = true {{- end}} - if err = o.Update(exec, "{{.Column}}"); err != nil { - o.{{$txt.Function.LocalAssignment}} = oldVal - {{if .Nullable -}} - o.{{$txt.LocalTable.ColumnNameGo}}.Valid = false - {{- end}} - return errors.Wrap(err, "failed to update local table") - } if o.R == nil { - o.R = &{{$localNameSingular}}R{ + o.R = &{{$varNameSingular}}R{ {{$txt.Function.Name}}: related, } } else { @@ -39,7 +48,7 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo {{if .Unique -}} if related.R == nil { - related.R = &{{$varNameSingular}}R{ + related.R = &{{$foreignNameSingular}}R{ {{$txt.Function.ForeignName}}: o, } } else { @@ -47,7 +56,7 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo } {{else -}} if related.R == nil { - related.R = &{{$varNameSingular}}R{ + related.R = &{{$foreignNameSingular}}R{ {{$txt.Function.ForeignName}}: {{$txt.LocalTable.NameGo}}Slice{{"{"}}o{{"}"}}, } } else { diff --git a/templates_test/relationship_to_one_setops.tpl b/templates_test/relationship_to_one_setops.tpl index 6abe24b..443e469 100644 --- a/templates_test/relationship_to_one_setops.tpl +++ b/templates_test/relationship_to_one_setops.tpl @@ -60,6 +60,13 @@ func test{{$txt.LocalTable.NameGo}}ToOneSetOp{{$txt.ForeignTable.NameGo}}Using{{ t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}) } + {{if setInclude .Column $dot.Table.PKey.Columns -}} + if exists, err := {{$txt.LocalTable.NameGo}}Exists(tx, a.{{$dot.Table.PKey.Columns | stringMap $dot.StringFuncs.titleCase | join ", a."}}); err != nil { + t.Fatal(err) + } else if !exists { + t.Error("want 'a' to exist") + } + {{else -}} zero := reflect.Zero(reflect.TypeOf(a.{{$txt.Function.LocalAssignment}})) reflect.Indirect(reflect.ValueOf(&a.{{$txt.Function.LocalAssignment}})).Set(zero) @@ -74,6 +81,7 @@ func test{{$txt.LocalTable.NameGo}}ToOneSetOp{{$txt.ForeignTable.NameGo}}Using{{ {{end -}} t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) } + {{- end}} } } {{- if .Nullable}}