diff --git a/boil/query.go b/boil/query.go index f179abd..da97245 100644 --- a/boil/query.go +++ b/boil/query.go @@ -115,6 +115,11 @@ func SetExecutor(q *Query, exec Executor) { q.executor = exec } +// GetExecutor on the query. +func GetExecutor(q *Query) Executor { + return q.executor +} + // SetSQL on the query. func SetSQL(q *Query, sql string, args ...interface{}) { q.plainSQL = plainSQL{sql: sql, args: args} diff --git a/templates/01_types.tpl b/templates/01_types.tpl index eb62947..c8eb4a2 100644 --- a/templates/01_types.tpl +++ b/templates/01_types.tpl @@ -15,7 +15,7 @@ var ( type ( {{$tableNameSingular}}Slice []*{{$tableNameSingular}} {{if eq .NoHooks false -}} - {{$tableNameSingular}}Hook func(*{{$tableNameSingular}}) error + {{$tableNameSingular}}Hook func(boil.Executor, *{{$tableNameSingular}}) error {{- end}} {{$varNameSingular}}Query struct { diff --git a/templates/02_hooks.tpl b/templates/02_hooks.tpl index 7503314..c4b23c3 100644 --- a/templates/02_hooks.tpl +++ b/templates/02_hooks.tpl @@ -13,9 +13,9 @@ var {{$varNameSingular}}AfterDeleteHooks []{{$tableNameSingular}}Hook var {{$varNameSingular}}AfterUpsertHooks []{{$tableNameSingular}}Hook // doBeforeInsertHooks executes all "before insert" hooks. -func (o *{{$tableNameSingular}}) doBeforeInsertHooks() (err error) { +func (o *{{$tableNameSingular}}) doBeforeInsertHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}BeforeInsertHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } @@ -24,9 +24,9 @@ func (o *{{$tableNameSingular}}) doBeforeInsertHooks() (err error) { } // doBeforeUpdateHooks executes all "before Update" hooks. -func (o *{{$tableNameSingular}}) doBeforeUpdateHooks() (err error) { +func (o *{{$tableNameSingular}}) doBeforeUpdateHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}BeforeUpdateHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } @@ -35,9 +35,9 @@ func (o *{{$tableNameSingular}}) doBeforeUpdateHooks() (err error) { } // doBeforeDeleteHooks executes all "before Delete" hooks. -func (o *{{$tableNameSingular}}) doBeforeDeleteHooks() (err error) { +func (o *{{$tableNameSingular}}) doBeforeDeleteHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}BeforeDeleteHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } @@ -46,9 +46,9 @@ func (o *{{$tableNameSingular}}) doBeforeDeleteHooks() (err error) { } // doBeforeUpsertHooks executes all "before Upsert" hooks. -func (o *{{$tableNameSingular}}) doBeforeUpsertHooks() (err error) { +func (o *{{$tableNameSingular}}) doBeforeUpsertHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}BeforeUpsertHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } @@ -57,9 +57,9 @@ func (o *{{$tableNameSingular}}) doBeforeUpsertHooks() (err error) { } // doAfterInsertHooks executes all "after Insert" hooks. -func (o *{{$tableNameSingular}}) doAfterInsertHooks() (err error) { +func (o *{{$tableNameSingular}}) doAfterInsertHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}AfterInsertHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } @@ -68,9 +68,9 @@ func (o *{{$tableNameSingular}}) doAfterInsertHooks() (err error) { } // doAfterSelectHooks executes all "after Select" hooks. -func (o *{{$tableNameSingular}}) doAfterSelectHooks() (err error) { +func (o *{{$tableNameSingular}}) doAfterSelectHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}AfterSelectHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } @@ -79,9 +79,9 @@ func (o *{{$tableNameSingular}}) doAfterSelectHooks() (err error) { } // doAfterUpdateHooks executes all "after Update" hooks. -func (o *{{$tableNameSingular}}) doAfterUpdateHooks() (err error) { +func (o *{{$tableNameSingular}}) doAfterUpdateHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}AfterUpdateHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } @@ -90,9 +90,9 @@ func (o *{{$tableNameSingular}}) doAfterUpdateHooks() (err error) { } // doAfterDeleteHooks executes all "after Delete" hooks. -func (o *{{$tableNameSingular}}) doAfterDeleteHooks() (err error) { +func (o *{{$tableNameSingular}}) doAfterDeleteHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}AfterDeleteHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } @@ -101,9 +101,9 @@ func (o *{{$tableNameSingular}}) doAfterDeleteHooks() (err error) { } // doAfterUpsertHooks executes all "after Upsert" hooks. -func (o *{{$tableNameSingular}}) doAfterUpsertHooks() (err error) { +func (o *{{$tableNameSingular}}) doAfterUpsertHooks(exec boil.Executor) (err error) { for _, hook := range {{$varNameSingular}}AfterUpsertHooks { - if err := hook(o); err != nil { + if err := hook(exec, o); err != nil { return err } } diff --git a/templates/03_finishers.tpl b/templates/03_finishers.tpl index b82f87a..28b6a23 100644 --- a/templates/03_finishers.tpl +++ b/templates/03_finishers.tpl @@ -24,8 +24,8 @@ func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) { return nil, errors.Wrap(err, "{{.PkgName}}: failed to execute a one query for {{.Table.Name}}") } - {{if eq .NoHooks false -}} - if err := o.doAfterSelectHooks(); err != nil { + {{if not .NoHooks -}} + if err := o.doAfterSelectHooks(boil.GetExecutor(q.Query)); err != nil { return o, err } {{- end}} @@ -52,10 +52,10 @@ func (q {{$varNameSingular}}Query) All() ({{$tableNameSingular}}Slice, error) { return nil, errors.Wrap(err, "{{.PkgName}}: failed to assign all query results to {{$tableNameSingular}} slice") } - {{if eq .NoHooks false -}} + {{if not .NoHooks -}} if len({{$varNameSingular}}AfterSelectHooks) != 0 { for _, obj := range o { - if err := obj.doAfterSelectHooks(); err != nil { + if err := obj.doAfterSelectHooks(boil.GetExecutor(q.Query)); err != nil { return o, err } } diff --git a/templates/10_insert.tpl b/templates/10_insert.tpl index a486b26..0ced780 100644 --- a/templates/10_insert.tpl +++ b/templates/10_insert.tpl @@ -34,8 +34,8 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string var err error {{- template "timestamp_insert_helper" . }} - {{if eq .NoHooks false -}} - if err := o.doBeforeInsertHooks(); err != nil { + {{if not .NoHooks -}} + if err := o.doBeforeInsertHooks(exec); err != nil { return err } {{- end}} @@ -61,9 +61,13 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}") } - {{if eq .NoHooks false -}} + {{if not .NoHooks -}} if len(returnColumns) == 0 { - return o.doAfterInsertHooks() + return o.doAfterInsertHooks(exec) + } + {{- else -}} + if len(returnColumns) == 0 { + return nil } {{- end}} @@ -95,8 +99,8 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string } {{end}} - {{if eq .NoHooks false -}} - return o.doAfterInsertHooks() + {{if not .NoHooks -}} + return o.doAfterInsertHooks(exec) {{- else -}} return nil {{- end}} diff --git a/templates/11_update.tpl b/templates/11_update.tpl index 4152335..275a6a4 100644 --- a/templates/11_update.tpl +++ b/templates/11_update.tpl @@ -37,8 +37,8 @@ func (o *{{$tableNameSingular}}) UpdateP(exec boil.Executor, whitelist ... strin func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string) error { {{- template "timestamp_update_helper" . -}} - {{if eq .NoHooks false -}} - if err := o.doBeforeUpdateHooks(); err != nil { + {{if not .NoHooks -}} + if err := o.doBeforeUpdateHooks(exec); err != nil { return err } {{- end}} @@ -70,8 +70,8 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string return errors.Errorf("failed to update single row, updated %d rows", r) } - {{if eq .NoHooks false -}} - return o.doAfterUpdateHooks() + {{if not .NoHooks -}} + return o.doAfterUpdateHooks(exec) {{- else -}} return nil {{- end}} diff --git a/templates/12_upsert.tpl b/templates/12_upsert.tpl index 837386e..50efd50 100644 --- a/templates/12_upsert.tpl +++ b/templates/12_upsert.tpl @@ -26,8 +26,8 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, updateOnConflict boo return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for upsert") } - {{if eq .NoHooks false -}} - if err := o.doBeforeUpsertHooks(); err != nil { + {{if not .NoHooks -}} + if err := o.doBeforeUpsertHooks(exec); err != nil { return err } {{- end}} @@ -73,8 +73,8 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, updateOnConflict boo return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}") } - {{if eq .NoHooks false -}} - if err := o.doAfterUpsertHooks(); err != nil { + {{if not .NoHooks -}} + if err := o.doAfterUpsertHooks(exec); err != nil { return err } {{- end}} diff --git a/templates/13_delete.tpl b/templates/13_delete.tpl index 3f07aef..a3c8b51 100644 --- a/templates/13_delete.tpl +++ b/templates/13_delete.tpl @@ -35,8 +35,8 @@ func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error { return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for delete") } - {{if eq .NoHooks false -}} - if err := o.doBeforeDeleteHooks(); err != nil { + {{if not .NoHooks -}} + if err := o.doBeforeDeleteHooks(exec); err != nil { return err } {{- end}} @@ -55,8 +55,8 @@ func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error { return errors.Wrap(err, "{{.PkgName}}: unable to delete from {{.Table.Name}}") } - {{if eq .NoHooks false -}} - if err := o.doAfterDeleteHooks(); err != nil { + {{if not .NoHooks -}} + if err := o.doAfterDeleteHooks(exec); err != nil { return err } {{- end}} @@ -119,10 +119,10 @@ func (o {{$tableNameSingular}}Slice) DeleteAll(exec boil.Executor) error { return nil } - {{if eq .NoHooks false -}} + {{if not .NoHooks -}} if len({{$varNameSingular}}BeforeDeleteHooks) != 0 { for _, obj := range o { - if err := obj.doBeforeDeleteHooks(); err != nil { + if err := obj.doBeforeDeleteHooks(exec); err != nil { return err } } @@ -147,10 +147,10 @@ func (o {{$tableNameSingular}}Slice) DeleteAll(exec boil.Executor) error { return errors.Wrap(err, "{{.PkgName}}: unable to delete all from {{$varNameSingular}} slice") } - {{if eq .NoHooks false -}} + {{if not .NoHooks -}} if len({{$varNameSingular}}AfterDeleteHooks) != 0 { for _, obj := range o { - if err := obj.doAfterDeleteHooks(); err != nil { + if err := obj.doAfterDeleteHooks(exec); err != nil { return err } } diff --git a/templates/17_auto_timestamps.tpl b/templates/17_auto_timestamps.tpl index 4adae76..0c5bf5d 100644 --- a/templates/17_auto_timestamps.tpl +++ b/templates/17_auto_timestamps.tpl @@ -1,5 +1,5 @@ {{- define "timestamp_insert_helper" -}} - {{- if eq .NoAutoTimestamps false -}} + {{- if not .NoAutoTimestamps -}} {{- $colNames := .Table.Columns | columnNames -}} {{if containsAny $colNames "created_at" "updated_at"}} loc := boil.GetLocation() @@ -31,7 +31,7 @@ {{- end}} {{- end -}} {{- define "timestamp_update_helper" -}} - {{- if eq .NoAutoTimestamps false -}} + {{- if not .NoAutoTimestamps -}} {{- $colNames := .Table.Columns | columnNames -}} {{if containsAny $colNames "updated_at"}} loc := boil.GetLocation() diff --git a/templates_test/hooks.tpl b/templates_test/hooks.tpl index 46d8fd8..f7b365a 100644 --- a/templates_test/hooks.tpl +++ b/templates_test/hooks.tpl @@ -3,47 +3,47 @@ {{- $tableNamePlural := .Table.Name | plural | titleCase -}} {{- $varNamePlural := .Table.Name | plural | camelCase -}} {{- $varNameSingular := .Table.Name | singular | camelCase -}} -func {{$varNameSingular}}BeforeInsertHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}BeforeInsertHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } -func {{$varNameSingular}}AfterInsertHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}AfterInsertHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } -func {{$varNameSingular}}AfterSelectHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}AfterSelectHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } -func {{$varNameSingular}}BeforeUpdateHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}BeforeUpdateHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } -func {{$varNameSingular}}AfterUpdateHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}AfterUpdateHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } -func {{$varNameSingular}}BeforeDeleteHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}BeforeDeleteHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } -func {{$varNameSingular}}AfterDeleteHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}AfterDeleteHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } -func {{$varNameSingular}}BeforeUpsertHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}BeforeUpsertHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } -func {{$varNameSingular}}AfterUpsertHook(o *{{$tableNameSingular}}) error { +func {{$varNameSingular}}AfterUpsertHook(e boil.Executor, o *{{$tableNameSingular}}) error { *o = {{$tableNameSingular}}{} return nil } @@ -62,7 +62,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { } {{$tableNameSingular}}AddHook(boil.HookBeforeInsert, {{$varNameSingular}}BeforeInsertHook) - if err = o.doBeforeInsertHooks(); err != nil { + if err = o.doBeforeInsertHooks(nil); err != nil { t.Errorf("Unable to execute doBeforeInsertHooks: %s", err) } if !reflect.DeepEqual(o, empty) { @@ -71,7 +71,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { {{$varNameSingular}}BeforeInsertHooks = []{{$tableNameSingular}}Hook{} {{$tableNameSingular}}AddHook(boil.HookAfterInsert, {{$varNameSingular}}AfterInsertHook) - if err = o.doAfterInsertHooks(); err != nil { + if err = o.doAfterInsertHooks(nil); err != nil { t.Errorf("Unable to execute doAfterInsertHooks: %s", err) } if !reflect.DeepEqual(o, empty) { @@ -80,7 +80,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { {{$varNameSingular}}AfterInsertHooks = []{{$tableNameSingular}}Hook{} {{$tableNameSingular}}AddHook(boil.HookAfterSelect, {{$varNameSingular}}AfterSelectHook) - if err = o.doAfterSelectHooks(); err != nil { + if err = o.doAfterSelectHooks(nil); err != nil { t.Errorf("Unable to execute doAfterSelectHooks: %s", err) } if !reflect.DeepEqual(o, empty) { @@ -89,7 +89,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { {{$varNameSingular}}AfterSelectHooks = []{{$tableNameSingular}}Hook{} {{$tableNameSingular}}AddHook(boil.HookBeforeUpdate, {{$varNameSingular}}BeforeUpdateHook) - if err = o.doBeforeUpdateHooks(); err != nil { + if err = o.doBeforeUpdateHooks(nil); err != nil { t.Errorf("Unable to execute doBeforeUpdateHooks: %s", err) } if !reflect.DeepEqual(o, empty) { @@ -98,7 +98,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { {{$varNameSingular}}BeforeUpdateHooks = []{{$tableNameSingular}}Hook{} {{$tableNameSingular}}AddHook(boil.HookAfterUpdate, {{$varNameSingular}}AfterUpdateHook) - if err = o.doAfterUpdateHooks(); err != nil { + if err = o.doAfterUpdateHooks(nil); err != nil { t.Errorf("Unable to execute doAfterUpdateHooks: %s", err) } if !reflect.DeepEqual(o, empty) { @@ -107,7 +107,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { {{$varNameSingular}}AfterUpdateHooks = []{{$tableNameSingular}}Hook{} {{$tableNameSingular}}AddHook(boil.HookBeforeDelete, {{$varNameSingular}}BeforeDeleteHook) - if err = o.doBeforeDeleteHooks(); err != nil { + if err = o.doBeforeDeleteHooks(nil); err != nil { t.Errorf("Unable to execute doBeforeDeleteHooks: %s", err) } if !reflect.DeepEqual(o, empty) { @@ -116,7 +116,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { {{$varNameSingular}}BeforeDeleteHooks = []{{$tableNameSingular}}Hook{} {{$tableNameSingular}}AddHook(boil.HookAfterDelete, {{$varNameSingular}}AfterDeleteHook) - if err = o.doAfterDeleteHooks(); err != nil { + if err = o.doAfterDeleteHooks(nil); err != nil { t.Errorf("Unable to execute doAfterDeleteHooks: %s", err) } if !reflect.DeepEqual(o, empty) { @@ -125,7 +125,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { {{$varNameSingular}}AfterDeleteHooks = []{{$tableNameSingular}}Hook{} {{$tableNameSingular}}AddHook(boil.HookBeforeUpsert, {{$varNameSingular}}BeforeUpsertHook) - if err = o.doBeforeUpsertHooks(); err != nil { + if err = o.doBeforeUpsertHooks(nil); err != nil { t.Errorf("Unable to execute doBeforeUpsertHooks: %s", err) } if !reflect.DeepEqual(o, empty) { @@ -134,7 +134,7 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) { {{$varNameSingular}}BeforeUpsertHooks = []{{$tableNameSingular}}Hook{} {{$tableNameSingular}}AddHook(boil.HookAfterUpsert, {{$varNameSingular}}AfterUpsertHook) - if err = o.doAfterUpsertHooks(); err != nil { + if err = o.doAfterUpsertHooks(nil); err != nil { t.Errorf("Unable to execute doAfterUpsertHooks: %s", err) } if !reflect.DeepEqual(o, empty) {