github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/02_hooks.go.tpl (about)

     1  {{- if not .NoHooks -}}
     2  {{- $alias := .Aliases.Table .Table.Name}}
     3  
     4  var {{$alias.DownSingular}}AfterSelectHooks []{{$alias.UpSingular}}Hook
     5  
     6  {{if or (not .Table.IsView) (.Table.ViewCapabilities.CanInsert) -}}
     7  var {{$alias.DownSingular}}BeforeInsertHooks []{{$alias.UpSingular}}Hook
     8  var {{$alias.DownSingular}}AfterInsertHooks []{{$alias.UpSingular}}Hook
     9  {{- end}}
    10  
    11  {{if not .Table.IsView -}}
    12  var {{$alias.DownSingular}}BeforeUpdateHooks []{{$alias.UpSingular}}Hook
    13  var {{$alias.DownSingular}}AfterUpdateHooks []{{$alias.UpSingular}}Hook
    14  
    15  var {{$alias.DownSingular}}BeforeDeleteHooks []{{$alias.UpSingular}}Hook
    16  var {{$alias.DownSingular}}AfterDeleteHooks []{{$alias.UpSingular}}Hook
    17  {{- end}}
    18  
    19  {{if or (not .Table.IsView) (.Table.ViewCapabilities.CanUpsert) -}}
    20  var {{$alias.DownSingular}}BeforeUpsertHooks []{{$alias.UpSingular}}Hook
    21  var {{$alias.DownSingular}}AfterUpsertHooks []{{$alias.UpSingular}}Hook
    22  {{- end}}
    23  
    24  // doAfterSelectHooks executes all "after Select" hooks.
    25  func (o *{{$alias.UpSingular}}) doAfterSelectHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
    26  	{{if not .NoContext -}}
    27  	if boil.HooksAreSkipped(ctx) {
    28  		return nil
    29  	}
    30  
    31  	{{end -}}
    32  	for _, hook := range {{$alias.DownSingular}}AfterSelectHooks {
    33  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
    34  			return err
    35  		}
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  {{if or (not .Table.IsView) (.Table.ViewCapabilities.CanInsert) -}}
    42  // doBeforeInsertHooks executes all "before insert" hooks.
    43  func (o *{{$alias.UpSingular}}) doBeforeInsertHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
    44  	{{if not .NoContext -}}
    45  	if boil.HooksAreSkipped(ctx) {
    46  		return nil
    47  	}
    48  
    49  	{{end -}}
    50  	for _, hook := range {{$alias.DownSingular}}BeforeInsertHooks {
    51  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  // doAfterInsertHooks executes all "after Insert" hooks.
    60  func (o *{{$alias.UpSingular}}) doAfterInsertHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
    61  	{{if not .NoContext -}}
    62  	if boil.HooksAreSkipped(ctx) {
    63  		return nil
    64  	}
    65  
    66  	{{end -}}
    67  	for _, hook := range {{$alias.DownSingular}}AfterInsertHooks {
    68  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
    69  			return err
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  {{- end}}
    76  
    77  {{if not .Table.IsView -}}
    78  // doBeforeUpdateHooks executes all "before Update" hooks.
    79  func (o *{{$alias.UpSingular}}) doBeforeUpdateHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
    80  	{{if not .NoContext -}}
    81  	if boil.HooksAreSkipped(ctx) {
    82  		return nil
    83  	}
    84  
    85  	{{end -}}
    86  	for _, hook := range {{$alias.DownSingular}}BeforeUpdateHooks {
    87  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
    88  			return err
    89  		}
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  // doAfterUpdateHooks executes all "after Update" hooks.
    96  func (o *{{$alias.UpSingular}}) doAfterUpdateHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
    97  	{{if not .NoContext -}}
    98  	if boil.HooksAreSkipped(ctx) {
    99  		return nil
   100  	}
   101  
   102  	{{end -}}
   103  	for _, hook := range {{$alias.DownSingular}}AfterUpdateHooks {
   104  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
   105  			return err
   106  		}
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  // doBeforeDeleteHooks executes all "before Delete" hooks.
   113  func (o *{{$alias.UpSingular}}) doBeforeDeleteHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
   114  	{{if not .NoContext -}}
   115  	if boil.HooksAreSkipped(ctx) {
   116  		return nil
   117  	}
   118  
   119  	{{end -}}
   120  	for _, hook := range {{$alias.DownSingular}}BeforeDeleteHooks {
   121  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
   122  			return err
   123  		}
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  // doAfterDeleteHooks executes all "after Delete" hooks.
   130  func (o *{{$alias.UpSingular}}) doAfterDeleteHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
   131  	{{if not .NoContext -}}
   132  	if boil.HooksAreSkipped(ctx) {
   133  		return nil
   134  	}
   135  
   136  	{{end -}}
   137  	for _, hook := range {{$alias.DownSingular}}AfterDeleteHooks {
   138  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
   139  			return err
   140  		}
   141  	}
   142  
   143  	return nil
   144  }
   145  {{- end}}
   146  
   147  {{if or (not .Table.IsView) (.Table.ViewCapabilities.CanUpsert) -}}
   148  // doBeforeUpsertHooks executes all "before Upsert" hooks.
   149  func (o *{{$alias.UpSingular}}) doBeforeUpsertHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
   150  	{{if not .NoContext -}}
   151  	if boil.HooksAreSkipped(ctx) {
   152  		return nil
   153  	}
   154  
   155  	{{end -}}
   156  	for _, hook := range {{$alias.DownSingular}}BeforeUpsertHooks {
   157  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
   158  			return err
   159  		}
   160  	}
   161  
   162  	return nil
   163  }
   164  
   165  // doAfterUpsertHooks executes all "after Upsert" hooks.
   166  func (o *{{$alias.UpSingular}}) doAfterUpsertHooks({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (err error) {
   167  	{{if not .NoContext -}}
   168  	if boil.HooksAreSkipped(ctx) {
   169  		return nil
   170  	}
   171  
   172  	{{end -}}
   173  	for _, hook := range {{$alias.DownSingular}}AfterUpsertHooks {
   174  		if err := hook({{if not .NoContext}}ctx, {{end -}} exec, o); err != nil {
   175  			return err
   176  		}
   177  	}
   178  
   179  	return nil
   180  }
   181  {{- end}}
   182  
   183  // Add{{$alias.UpSingular}}Hook registers your hook function for all future operations.
   184  func Add{{$alias.UpSingular}}Hook(hookPoint boil.HookPoint, {{$alias.DownSingular}}Hook {{$alias.UpSingular}}Hook) {
   185  	switch hookPoint {
   186  		case boil.AfterSelectHook:
   187  			{{$alias.DownSingular}}AfterSelectHooks = append({{$alias.DownSingular}}AfterSelectHooks, {{$alias.DownSingular}}Hook)
   188  		{{- if or (not .Table.IsView) (.Table.ViewCapabilities.CanInsert)}}
   189  		case boil.BeforeInsertHook:
   190  			{{$alias.DownSingular}}BeforeInsertHooks = append({{$alias.DownSingular}}BeforeInsertHooks, {{$alias.DownSingular}}Hook)
   191  		case boil.AfterInsertHook:
   192  			{{$alias.DownSingular}}AfterInsertHooks = append({{$alias.DownSingular}}AfterInsertHooks, {{$alias.DownSingular}}Hook)
   193  		{{- end}}
   194  		{{- if not .Table.IsView}}
   195  		case boil.BeforeUpdateHook:
   196  			{{$alias.DownSingular}}BeforeUpdateHooks = append({{$alias.DownSingular}}BeforeUpdateHooks, {{$alias.DownSingular}}Hook)
   197  		case boil.AfterUpdateHook:
   198  			{{$alias.DownSingular}}AfterUpdateHooks = append({{$alias.DownSingular}}AfterUpdateHooks, {{$alias.DownSingular}}Hook)
   199  		case boil.BeforeDeleteHook:
   200  			{{$alias.DownSingular}}BeforeDeleteHooks = append({{$alias.DownSingular}}BeforeDeleteHooks, {{$alias.DownSingular}}Hook)
   201  		case boil.AfterDeleteHook:
   202  			{{$alias.DownSingular}}AfterDeleteHooks = append({{$alias.DownSingular}}AfterDeleteHooks, {{$alias.DownSingular}}Hook)
   203  		{{- end}}
   204  		{{- if or (not .Table.IsView) (.Table.ViewCapabilities.CanInsert)}}
   205  		case boil.BeforeUpsertHook:
   206  			{{$alias.DownSingular}}BeforeUpsertHooks = append({{$alias.DownSingular}}BeforeUpsertHooks, {{$alias.DownSingular}}Hook)
   207  		case boil.AfterUpsertHook:
   208  			{{$alias.DownSingular}}AfterUpsertHooks = append({{$alias.DownSingular}}AfterUpsertHooks, {{$alias.DownSingular}}Hook)
   209  		{{- end}}
   210  	}
   211  }
   212  {{- end}}