From 5f394a4f69363918133f64d30201e32c193177cc Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sat, 24 Sep 2016 14:34:31 -0700 Subject: [PATCH] Fix one-to-one setops --- .../11_relationship_one_to_one_setops.tpl | 49 ++++++++++++------- .../relationship_one_to_one_setops.tpl | 11 ++++- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/templates/11_relationship_one_to_one_setops.tpl b/templates/11_relationship_one_to_one_setops.tpl index 64f5a4b..7466d7a 100644 --- a/templates/11_relationship_one_to_one_setops.tpl +++ b/templates/11_relationship_one_to_one_setops.tpl @@ -3,40 +3,51 @@ {{- $dot := . -}} {{- range .Table.ToOneRelationships -}} {{- $txt := txtsFromOneToOne $dot.Tables $dot.Table . -}} - {{- $varNameSingular := .ForeignTable | singular | camelCase -}} - {{- $localNameSingular := .Table | singular | camelCase}} + {{- $varNameSingular := .Table | singular | camelCase -}} + {{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}} + {{- $foreignPKeyCols := (getTable $dot.Tables .ForeignTable).PKey.Columns -}} + {{- $foreignSchemaTable := .ForeignTable | $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}}. 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 { + related.{{$txt.Function.ForeignAssignment}} = o.{{$txt.Function.LocalAssignment}} + {{if .ForeignColumnNullable -}} + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true + {{- end}} + if err = related.Insert(exec); err != nil { - 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}} + 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 { + 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 foreign table") } + + related.{{$txt.Function.ForeignAssignment}} = o.{{$txt.Function.LocalAssignment}} + {{if .ForeignColumnNullable -}} + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true + {{- end}} } + if o.R == nil { - o.R = &{{$localNameSingular}}R{ + o.R = &{{$varNameSingular}}R{ {{$txt.Function.Name}}: related, } } else { @@ -44,7 +55,7 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo } if related.R == nil { - related.R = &{{$varNameSingular}}R{ + related.R = &{{$foreignVarNameSingular}}R{ {{$txt.Function.ForeignName}}: o, } } else { diff --git a/templates_test/relationship_one_to_one_setops.tpl b/templates_test/relationship_one_to_one_setops.tpl index d8e5c21..e5213fe 100644 --- a/templates_test/relationship_one_to_one_setops.tpl +++ b/templates_test/relationship_one_to_one_setops.tpl @@ -4,7 +4,8 @@ {{- range .Table.ToOneRelationships -}} {{- $txt := txtsFromOneToOne $dot.Tables $dot.Table .}} {{- $varNameSingular := .Table | singular | camelCase -}} -{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase}} +{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}} +{{- $foreignPKeyCols := (getTable $dot.Tables .ForeignTable).PKey.Columns}} func test{{$txt.LocalTable.NameGo}}OneToOneSetOp{{$txt.ForeignTable.NameGo}}Using{{$txt.Function.Name}}(t *testing.T) { var err error @@ -53,12 +54,20 @@ func test{{$txt.LocalTable.NameGo}}OneToOneSetOp{{$txt.ForeignTable.NameGo}}Usin t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}) } + {{if setInclude .ForeignColumn $foreignPKeyCols -}} + if exists, err := {{$txt.ForeignTable.NameGo}}Exists(tx, x.{{$foreignPKeyCols | stringMap $dot.StringFuncs.titleCase | join ", x."}}); err != nil { + t.Fatal(err) + } else if !exists { + t.Error("want 'x' to exist") + } + {{else -}} zero := reflect.Zero(reflect.TypeOf(x.{{$txt.Function.ForeignAssignment}})) reflect.Indirect(reflect.ValueOf(&x.{{$txt.Function.ForeignAssignment}})).Set(zero) if err = x.Reload(tx); err != nil { t.Fatal("failed to reload", err) } + {{- end}} {{if $txt.Function.UsesBytes -}} if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) {