diff --git a/callbacks.go b/callbacks.go index 02e741e7..88b295a7 100644 --- a/callbacks.go +++ b/callbacks.go @@ -126,8 +126,10 @@ func (p *processor) Execute(db *DB) *DB { } } - for _, f := range p.fns { - f(db) + for _, c := range p.callbacks { + if stmt.ShouldSkipHook(c) { + c.handler(db) + } } db.Logger.Trace(stmt.Context, curTime, func() (string, int64) { diff --git a/finisher_api.go b/finisher_api.go index 537c955a..e3678683 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -635,3 +635,17 @@ func (db *DB) Exec(sql string, values ...interface{}) (tx *DB) { return tx.callbacks.Raw().Execute(tx) } + +// add SkipHook name +func (db *DB) SkipHookByName(name ...string) (tx *DB) { + tx = db.getInstance() + tx.Statement.SkipHooksNames = append(tx.Statement.SkipHooksNames, name...) + return tx +} + +// add SkipHook name +func (db *DB) SkipHookByFunc(fns ...func(*DB)) (tx *DB) { + tx = db.getInstance() + tx.Statement.SkipHooksFunc = append(tx.Statement.SkipHooksFunc, fns...) + return tx +} diff --git a/statement.go b/statement.go index 8b682c84..53447281 100644 --- a/statement.go +++ b/statement.go @@ -39,6 +39,8 @@ type Statement struct { Context context.Context RaiseErrorOnNotFound bool SkipHooks bool + SkipHooksNames []string + SkipHooksFunc []func(*DB) SQL strings.Builder Vars []interface{} CurDestIndex int @@ -671,3 +673,31 @@ func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) ( return results, !notRestricted && len(stmt.Selects) > 0 } + +// determine +func (stmt *Statement) ShouldSkipHook(c *callback) (skip bool) { + if stmt.SkipHooks { + // skip all + skip = true + } else { + // skip by name + if len(stmt.SkipHooksNames) > 0 { + for _, hookName := range stmt.SkipHooksNames { + if hookName == c.name { + skip = true + break + } + } + } + // skip by func + if skip || len(stmt.SkipHooksFunc) > 0 { + for _, hookFunc := range stmt.SkipHooksFunc { + // compare with ptr + if &hookFunc == &c.handler { + skip = true + break + } + } + } + } +}