Fix last few bugs with find replacing

This commit is contained in:
Aaron L 2016-09-18 11:21:26 -07:00
parent b39a5f0dad
commit 1eb1ed10cf
2 changed files with 3 additions and 23 deletions

View file

@ -17,17 +17,6 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function
} }
} }
{{if$txt.Function.OneToOne -}}
oldVal := related.{{$txt.Function.ForeignAssignment}}
related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
{{if .ForeignKey.ForeignColumnNullable -}}
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
{{end -}}
if err = related.Update(exec, "{{.ForeignKey.ForeignColumn}}"); err != nil {
related.{{$txt.Function.ForeignAssignment}} = oldVal
return errors.Wrap(err, "failed to update local table")
}
{{else -}}
oldVal := {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} oldVal := {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
{{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}} {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}}
@ -35,7 +24,6 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function
{{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = oldVal {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = oldVal
return errors.Wrap(err, "failed to update local table") return errors.Wrap(err, "failed to update local table")
} }
{{end -}}
if {{$txt.Function.Receiver}}.R == nil { if {{$txt.Function.Receiver}}.R == nil {
{{$txt.Function.Receiver}}.R = &{{$localNameSingular}}R{ {{$txt.Function.Receiver}}.R = &{{$localNameSingular}}R{
@ -45,7 +33,7 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function
{{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = related {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = related
} }
{{if (or .ForeignKey.Unique$txt.Function.OneToOne) -}} {{if .ForeignKey.Unique -}}
if related.R == nil { if related.R == nil {
related.R = &{{$varNameSingular}}R{ related.R = &{{$varNameSingular}}R{
{{$txt.Function.ForeignName}}: {{$txt.Function.Receiver}}, {{$txt.Function.ForeignName}}: {{$txt.Function.Receiver}},
@ -69,26 +57,18 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function
return nil return nil
} }
{{- if or (.ForeignKey.Nullable) (and$txt.Function.OneToOne .ForeignKey.ForeignColumnNullable)}} {{- if .ForeignKey.Nullable}}
// Remove{{$txt.Function.Name}} relationship. // Remove{{$txt.Function.Name}} relationship.
// Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to nil. // Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to nil.
// Removes {{$txt.Function.Receiver}} from all passed in related items' relationships struct (Optional). // Removes {{$txt.Function.Receiver}} from all passed in related items' relationships struct (Optional).
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Executor, related *{{$txt.ForeignTable.NameGo}}) error { func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Executor, related *{{$txt.ForeignTable.NameGo}}) error {
var err error var err error
{{if$txt.Function.OneToOne -}}
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false
if err = related.Update(exec, "{{.ForeignKey.ForeignColumn}}"); err != nil {
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
return errors.Wrap(err, "failed to update local table")
}
{{else -}}
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = false {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = false
if err = {{$txt.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil { if err = {{$txt.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
return errors.Wrap(err, "failed to update local table") return errors.Wrap(err, "failed to update local table")
} }
{{end -}}
{{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = nil {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = nil
if related == nil || related.R == nil { if related == nil || related.R == nil {

View file

@ -217,7 +217,7 @@ func TestOneToOneSet(t *testing.T) {
{{- range $table.ToOneRelationships -}} {{- range $table.ToOneRelationships -}}
{{- if not .ForeignColumnNullable -}} {{- if not .ForeignColumnNullable -}}
{{- else -}} {{- else -}}
{{- $txt := textsFrom $dot.Tables $table . -}} {{- $txt := txtsFromOneToOne $dot.Tables $table . -}}
t.Run("{{$txt.LocalTable.NameGo}}To{{$txt.Function.Name}}", test{{$txt.LocalTable.NameGo}}OneToOneSetOp{{$txt.Function.Name}}) t.Run("{{$txt.LocalTable.NameGo}}To{{$txt.Function.Name}}", test{{$txt.LocalTable.NameGo}}OneToOneSetOp{{$txt.Function.Name}})
{{- end -}}{{- /* if foreign column nullable */ -}} {{- end -}}{{- /* if foreign column nullable */ -}}
{{- end -}}{{- /* range */ -}} {{- end -}}{{- /* range */ -}}