1
0
Fork 0
gobelt/sqldb/callback.go

41 lines
910 B
Go
Raw Normal View History

2018-06-24 22:22:27 +00:00
package sqldb
import (
"context"
"time"
)
type (
// BeforeCallback is a kind of function that can be called before a query is
// executed.
BeforeCallback func(ctx context.Context, query string)
// AfterCallback is a kind of function that can be called after a query was
// executed.
AfterCallback func(ctx context.Context, query string, took time.Duration, err error)
)
type callbacks struct {
before []BeforeCallback
after []AfterCallback
}
func (c *callbacks) addBefore(cb BeforeCallback) {
c.before = append(c.before, cb)
}
func (c *callbacks) addAfter(cb AfterCallback) {
c.after = append(c.after, cb)
}
func (c *callbacks) callBefore(ctx context.Context, query string) {
for _, cb := range c.before {
cb(ctx, query)
}
}
func (c *callbacks) callAfter(ctx context.Context, query string, took time.Duration, err error) {
for _, cb := range c.after {
cb(ctx, query, took, err)
}
}