Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hooks to templates for relationships. #1348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions templates/main/10_relationship_to_one_setops.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (o *{{$ltable.UpSingular}}) Set{{$rel.Foreign}}({{if $.NoContext}}exec boil
}
}

{{if not $.NoHooks -}}
if err = o.doBeforeUpdateHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return errors.Wrap(err, "failed to perform before update hooks")
}
{{end -}}

updateQuery := fmt.Sprintf(
"UPDATE {{$schemaTable}} SET %s WHERE %s",
strmangle.SetParamNames("{{$.LQ}}", "{{$.RQ}}", {{if $.Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.Column}}"{{"}"}}),
Expand Down Expand Up @@ -94,6 +100,12 @@ func (o *{{$ltable.UpSingular}}) Set{{$rel.Foreign}}({{if $.NoContext}}exec boil
return errors.Wrap(err, "failed to update local table")
}
{{- end}}

{{if not $.NoHooks -}}
if err := o.doAfterUpdateHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return errors.Wrap(err, "failed to perform after update hooks")
}
{{- end}}

{{if $usesPrimitives -}}
o.{{$col}} = related.{{$fcol}}
Expand Down
12 changes: 12 additions & 0 deletions templates/main/11_relationship_one_to_one_setops.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}GP({{if not $.NoContext}}
// Adds o to related.R.{{$relAlias.Foreign}}.
{{- end}}
func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, insert bool, related *{{$ftable.UpSingular}}) error {

var err error

if insert {
Expand All @@ -71,6 +72,11 @@ func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}({{if $.NoContext}}exec b
return errors.Wrap(err, "failed to insert into foreign table")
}
} else {
{{if not $.NoHooks -}}
if err = o.doBeforeUpdateHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return errors.Wrap(err, "failed to perform before update hooks")
}
{{end -}}
updateQuery := fmt.Sprintf(
"UPDATE {{$schemaForeignTable}} SET %s WHERE %s",
strmangle.SetParamNames("{{$.LQ}}", "{{$.RQ}}", {{if $.Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.ForeignColumn}}"{{"}"}}),
Expand Down Expand Up @@ -104,6 +110,12 @@ func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}({{if $.NoContext}}exec b
{{- else -}}
queries.Assign(&related.{{$fcol}}, o.{{$col}})
{{- end}}

{{if not $.NoHooks -}}
if err := o.doAfterUpdateHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return errors.Wrap(err, "failed to perform after update hooks")
}
{{- end}}
}


Expand Down
35 changes: 34 additions & 1 deletion templates/main/12_relationship_to_many_setops.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func (o *{{$ltable.UpSingular}}) Add{{$relAlias.Local}}GP({{if not $.NoContext}}
{{- end}}
func (o *{{$ltable.UpSingular}}) Add{{$relAlias.Local}}({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, insert bool, related ...*{{$ftable.UpSingular}}) error {
var err error

{{if not $.NoHooks -}}
if err := o.doBeforeInsertHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return err
}
{{- end}}

for _, rel := range related {
if insert {
{{if not .ToJoinTable -}}
Expand Down Expand Up @@ -175,7 +182,11 @@ func (o *{{$ltable.UpSingular}}) Add{{$relAlias.Local}}({{if $.NoContext}}exec b
{{end -}}
{{end -}}

{{if not $.NoHooks -}}
return o.doAfterInsertHooks({{if not $.NoContext}}ctx, {{end -}} exec)
{{- else -}}
return nil
{{- end}}
}

{{- if (or .ForeignColumnNullable .ToJoinTable)}}
Expand Down Expand Up @@ -240,6 +251,12 @@ func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}GP({{if not $.NoContext}}
// Sets related.R.{{$relAlias.Foreign}}'s {{$relAlias.Local}} accordingly.
{{- end}}
func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, insert bool, related ...*{{$ftable.UpSingular}}) error {
{{if not $.NoHooks -}}
if err := o.doBeforeDeleteHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return errors.Wrap(err, "failed to perform before delete hooks")
}
{{- end}}

{{if .ToJoinTable -}}
query := "delete from {{.JoinTable | $.SchemaTable}} where {{.JoinLocalColumn | $.Quotes}} = {{if $.Dialect.UseIndexPlaceholders}}$1{{else}}?{{end}}"
values := []interface{}{{"{"}}o.{{$col}}}
Expand Down Expand Up @@ -269,6 +286,12 @@ func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}({{if $.NoContext}}exec b
return errors.Wrap(err, "failed to remove relationships before set")
}

{{if not $.NoHooks -}}
if err := o.doAfterDeleteHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return errors.Wrap(err, "failed to perform after delete hooks")
}
{{- end}}

{{if and .ToJoinTable (not $.NoBackReferencing) -}}
remove{{$relAlias.Local}}From{{$relAlias.Foreign}}Slice(o, related)
if o.R != nil {
Expand Down Expand Up @@ -346,7 +369,11 @@ func (o *{{$ltable.UpSingular}}) Remove{{$relAlias.Local}}({{if $.NoContext}}exe
if len(related) == 0 {
return nil
}

{{if not $.NoHooks -}}
if err := o.doBeforeDeleteHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return errors.Wrap(err, "failed to perform before delete hooks")
}
{{- end}}
var err error
{{if .ToJoinTable -}}
query := fmt.Sprintf(
Expand Down Expand Up @@ -393,6 +420,12 @@ func (o *{{$ltable.UpSingular}}) Remove{{$relAlias.Local}}({{if $.NoContext}}exe
}
{{end -}}

{{if not $.NoHooks -}}
if err := o.doAfterDeleteHooks({{if not $.NoContext}}ctx, {{end -}} exec); err != nil {
return errors.Wrap(err, "failed to perform after delete hooks")
}
{{- end}}

{{if and .ToJoinTable (not $.NoBackReferencing) -}}
remove{{$relAlias.Local}}From{{$relAlias.Foreign}}Slice(o, related)
{{end -}}
Expand Down