Fix one-to-one setops
This commit is contained in:
parent
09ed5709e5
commit
5f394a4f69
2 changed files with 40 additions and 20 deletions
|
@ -3,40 +3,51 @@
|
||||||
{{- $dot := . -}}
|
{{- $dot := . -}}
|
||||||
{{- range .Table.ToOneRelationships -}}
|
{{- range .Table.ToOneRelationships -}}
|
||||||
{{- $txt := txtsFromOneToOne $dot.Tables $dot.Table . -}}
|
{{- $txt := txtsFromOneToOne $dot.Tables $dot.Table . -}}
|
||||||
{{- $varNameSingular := .ForeignTable | singular | camelCase -}}
|
{{- $varNameSingular := .Table | singular | camelCase -}}
|
||||||
{{- $localNameSingular := .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.
|
// Set{{$txt.Function.Name}} of the {{.Table | singular}} to the related item.
|
||||||
// Sets o.R.{{$txt.Function.Name}} to related.
|
// Sets o.R.{{$txt.Function.Name}} to related.
|
||||||
// Adds o to related.R.{{$txt.Function.ForeignName}}.
|
// 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 {
|
func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executor, insert bool, related *{{$txt.ForeignTable.NameGo}}) error {
|
||||||
var err 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 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 {
|
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")
|
return errors.Wrap(err, "failed to insert into foreign table")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err = related.Update(exec, "{{.ForeignColumn}}"); err != nil {
|
updateQuery := fmt.Sprintf(
|
||||||
related.{{$txt.Function.ForeignAssignment}} = oldVal
|
"UPDATE {{$foreignSchemaTable}} SET %s WHERE %s",
|
||||||
{{if .ForeignColumnNullable -}}
|
strmangle.SetParamNames("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.ForeignColumn}}"{{"}"}}),
|
||||||
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false
|
strmangle.WhereClause("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}2{{else}}0{{end}}, {{$foreignVarNameSingular}}PrimaryKeyColumns),
|
||||||
{{- end}}
|
)
|
||||||
|
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")
|
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 {
|
if o.R == nil {
|
||||||
o.R = &{{$localNameSingular}}R{
|
o.R = &{{$varNameSingular}}R{
|
||||||
{{$txt.Function.Name}}: related,
|
{{$txt.Function.Name}}: related,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -44,7 +55,7 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo
|
||||||
}
|
}
|
||||||
|
|
||||||
if related.R == nil {
|
if related.R == nil {
|
||||||
related.R = &{{$varNameSingular}}R{
|
related.R = &{{$foreignVarNameSingular}}R{
|
||||||
{{$txt.Function.ForeignName}}: o,
|
{{$txt.Function.ForeignName}}: o,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
{{- range .Table.ToOneRelationships -}}
|
{{- range .Table.ToOneRelationships -}}
|
||||||
{{- $txt := txtsFromOneToOne $dot.Tables $dot.Table .}}
|
{{- $txt := txtsFromOneToOne $dot.Tables $dot.Table .}}
|
||||||
{{- $varNameSingular := .Table | singular | camelCase -}}
|
{{- $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) {
|
func test{{$txt.LocalTable.NameGo}}OneToOneSetOp{{$txt.ForeignTable.NameGo}}Using{{$txt.Function.Name}}(t *testing.T) {
|
||||||
var err error
|
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}})
|
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}}))
|
zero := reflect.Zero(reflect.TypeOf(x.{{$txt.Function.ForeignAssignment}}))
|
||||||
reflect.Indirect(reflect.ValueOf(&x.{{$txt.Function.ForeignAssignment}})).Set(zero)
|
reflect.Indirect(reflect.ValueOf(&x.{{$txt.Function.ForeignAssignment}})).Set(zero)
|
||||||
|
|
||||||
if err = x.Reload(tx); err != nil {
|
if err = x.Reload(tx); err != nil {
|
||||||
t.Fatal("failed to reload", err)
|
t.Fatal("failed to reload", err)
|
||||||
}
|
}
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
{{if $txt.Function.UsesBytes -}}
|
{{if $txt.Function.UsesBytes -}}
|
||||||
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) {
|
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) {
|
||||||
|
|
Loading…
Reference in a new issue