sqlboiler/templates/02_hooks.tpl

138 lines
4.9 KiB
Smarty
Raw Permalink Normal View History

{{- if not .NoHooks -}}
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
var {{$varNameSingular}}BeforeInsertHooks []{{$tableNameSingular}}Hook
var {{$varNameSingular}}BeforeUpdateHooks []{{$tableNameSingular}}Hook
var {{$varNameSingular}}BeforeDeleteHooks []{{$tableNameSingular}}Hook
var {{$varNameSingular}}BeforeUpsertHooks []{{$tableNameSingular}}Hook
var {{$varNameSingular}}AfterInsertHooks []{{$tableNameSingular}}Hook
var {{$varNameSingular}}AfterSelectHooks []{{$tableNameSingular}}Hook
var {{$varNameSingular}}AfterUpdateHooks []{{$tableNameSingular}}Hook
var {{$varNameSingular}}AfterDeleteHooks []{{$tableNameSingular}}Hook
var {{$varNameSingular}}AfterUpsertHooks []{{$tableNameSingular}}Hook
// doBeforeInsertHooks executes all "before insert" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doBeforeInsertHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}BeforeInsertHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
return nil
}
// doBeforeUpdateHooks executes all "before Update" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doBeforeUpdateHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}BeforeUpdateHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
return nil
}
// doBeforeDeleteHooks executes all "before Delete" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doBeforeDeleteHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}BeforeDeleteHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
return nil
}
2016-08-04 05:11:45 +02:00
// doBeforeUpsertHooks executes all "before Upsert" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doBeforeUpsertHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}BeforeUpsertHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
2016-08-04 05:11:45 +02:00
return nil
2016-08-04 05:11:45 +02:00
}
// doAfterInsertHooks executes all "after Insert" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doAfterInsertHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}AfterInsertHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
return nil
}
// doAfterSelectHooks executes all "after Select" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doAfterSelectHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}AfterSelectHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
return nil
}
// doAfterUpdateHooks executes all "after Update" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doAfterUpdateHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}AfterUpdateHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
return nil
}
// doAfterDeleteHooks executes all "after Delete" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doAfterDeleteHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}AfterDeleteHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
return nil
}
2016-08-04 05:11:45 +02:00
// doAfterUpsertHooks executes all "after Upsert" hooks.
2016-08-29 14:38:19 +02:00
func (o *{{$tableNameSingular}}) doAfterUpsertHooks(exec boil.Executor) (err error) {
for _, hook := range {{$varNameSingular}}AfterUpsertHooks {
if err := hook(exec, o); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
2016-08-04 05:11:45 +02:00
return nil
2016-08-04 05:11:45 +02:00
}
2016-09-15 06:20:35 +02:00
// Add{{$tableNameSingular}}Hook registers your hook function for all future operations.
2016-09-01 03:12:46 +02:00
func Add{{$tableNameSingular}}Hook(hookPoint boil.HookPoint, {{$varNameSingular}}Hook {{$tableNameSingular}}Hook) {
switch hookPoint {
case boil.BeforeInsertHook:
{{$varNameSingular}}BeforeInsertHooks = append({{$varNameSingular}}BeforeInsertHooks, {{$varNameSingular}}Hook)
case boil.BeforeUpdateHook:
{{$varNameSingular}}BeforeUpdateHooks = append({{$varNameSingular}}BeforeUpdateHooks, {{$varNameSingular}}Hook)
case boil.BeforeDeleteHook:
{{$varNameSingular}}BeforeDeleteHooks = append({{$varNameSingular}}BeforeDeleteHooks, {{$varNameSingular}}Hook)
case boil.BeforeUpsertHook:
{{$varNameSingular}}BeforeUpsertHooks = append({{$varNameSingular}}BeforeUpsertHooks, {{$varNameSingular}}Hook)
case boil.AfterInsertHook:
{{$varNameSingular}}AfterInsertHooks = append({{$varNameSingular}}AfterInsertHooks, {{$varNameSingular}}Hook)
case boil.AfterSelectHook:
{{$varNameSingular}}AfterSelectHooks = append({{$varNameSingular}}AfterSelectHooks, {{$varNameSingular}}Hook)
case boil.AfterUpdateHook:
{{$varNameSingular}}AfterUpdateHooks = append({{$varNameSingular}}AfterUpdateHooks, {{$varNameSingular}}Hook)
case boil.AfterDeleteHook:
{{$varNameSingular}}AfterDeleteHooks = append({{$varNameSingular}}AfterDeleteHooks, {{$varNameSingular}}Hook)
case boil.AfterUpsertHook:
{{$varNameSingular}}AfterUpsertHooks = append({{$varNameSingular}}AfterUpsertHooks, {{$varNameSingular}}Hook)
}
}
2016-08-28 12:48:50 +02:00
{{- end}}