gorm/callback.go

247 lines
7.7 KiB
Go
Raw Normal View History

2014-01-23 12:43:39 +04:00
package gorm
2014-01-26 08:41:37 +04:00
import (
"fmt"
)
2014-01-25 16:04:01 +04:00
2016-01-16 16:55:00 +03:00
// defaultCallback hold default callbacks defined by gorm
var defaultCallback = &Callback{}
2016-01-16 14:20:52 +03:00
2016-01-16 16:55:00 +03:00
// Callback contains callbacks that used when CURD objects
2016-01-16 14:20:52 +03:00
// Field `creates` hold callbacks will be call when creating object
// Field `updates` hold callbacks will be call when updating object
// Field `deletes` hold callbacks will be call when deleting object
// Field `queries` hold callbacks will be call when querying object with query methods like Find, First, Related, Association...
// Field `rowQueries` hold callbacks will be call when querying object with Row, Rows...
// Field `processors` hold all callback processors, will be used to generate above callbacks in order
2016-01-16 16:55:00 +03:00
type Callback struct {
2014-01-26 08:41:37 +04:00
creates []*func(scope *Scope)
updates []*func(scope *Scope)
deletes []*func(scope *Scope)
queries []*func(scope *Scope)
2015-04-17 13:27:20 +03:00
rowQueries []*func(scope *Scope)
2016-01-16 14:20:52 +03:00
processors []*CallbackProcessor
2014-01-24 17:52:26 +04:00
}
2016-01-17 10:30:42 +03:00
// CallbackProcessor contains all informations for a callback
2016-01-16 14:20:52 +03:00
type CallbackProcessor struct {
name string // current callback's name
before string // register current callback before a callback
after string // register current callback after a callback
replace bool // replace callbacks with same name
remove bool // delete callbacks with same name
kind string // callback type: create, update, delete, query, row_query
processor *func(scope *Scope) // callback handler
2016-01-16 16:55:00 +03:00
parent *Callback
2014-01-23 12:43:39 +04:00
}
2016-01-16 16:55:00 +03:00
func (c *Callback) addProcessor(kind string) *CallbackProcessor {
2016-01-16 14:20:52 +03:00
cp := &CallbackProcessor{kind: kind, parent: c}
2014-01-24 17:52:26 +04:00
c.processors = append(c.processors, cp)
return cp
2014-01-23 12:43:39 +04:00
}
2016-01-16 16:55:00 +03:00
func (c *Callback) clone() *Callback {
return &Callback{
2015-02-26 09:47:56 +03:00
creates: c.creates,
updates: c.updates,
deletes: c.deletes,
queries: c.queries,
processors: c.processors,
}
2014-01-29 06:25:58 +04:00
}
2016-01-16 14:20:52 +03:00
// Create could be used to register callbacks for creating object
// db.Callback().Create().After("gorm:create").Register("plugin:run_after_create", func(*Scope) {
// // business logic
// ...
//
// // set error if some thing wrong happened, will rollback the creating
// scope.Err(errors.New("error"))
// })
2016-01-16 16:55:00 +03:00
func (c *Callback) Create() *CallbackProcessor {
2014-01-24 17:52:26 +04:00
return c.addProcessor("create")
2014-01-23 12:43:39 +04:00
}
2016-01-16 14:20:52 +03:00
// Update could be used to register callbacks for updating object, refer `Create` for usage
2016-01-16 16:55:00 +03:00
func (c *Callback) Update() *CallbackProcessor {
2014-01-24 17:52:26 +04:00
return c.addProcessor("update")
2014-01-23 12:43:39 +04:00
}
2016-01-16 14:20:52 +03:00
// Delete could be used to register callbacks for deleting object, refer `Create` for usage
2016-01-16 16:55:00 +03:00
func (c *Callback) Delete() *CallbackProcessor {
2014-01-24 17:52:26 +04:00
return c.addProcessor("delete")
2014-01-23 12:43:39 +04:00
}
2016-01-16 14:20:52 +03:00
// Query could be used to register callbacks for querying objects with query methods like `Find`, `First`, `Related`, `Association`...
// refer `Create` for usage
2016-01-16 16:55:00 +03:00
func (c *Callback) Query() *CallbackProcessor {
2014-01-24 17:52:26 +04:00
return c.addProcessor("query")
2014-01-23 12:43:39 +04:00
}
2016-01-17 10:30:42 +03:00
// RowQuery could be used to register callbacks for querying objects with `Row`, `Rows`, refer `Create` for usage
2016-01-16 16:55:00 +03:00
func (c *Callback) RowQuery() *CallbackProcessor {
2015-04-17 13:27:20 +03:00
return c.addProcessor("row_query")
}
2016-01-16 14:20:52 +03:00
// After insert a new callback after callback `callbackName`, refer `Callbacks.Create`
func (cp *CallbackProcessor) After(callbackName string) *CallbackProcessor {
cp.after = callbackName
2014-01-24 17:52:26 +04:00
return cp
}
2016-01-16 14:20:52 +03:00
// Before insert a new callback before callback `callbackName`, refer `Callbacks.Create`
func (cp *CallbackProcessor) Before(callbackName string) *CallbackProcessor {
cp.before = callbackName
2014-01-24 17:52:26 +04:00
return cp
}
2016-01-16 14:20:52 +03:00
// Register a new callback, refer `Callbacks.Create`
func (cp *CallbackProcessor) Register(callbackName string, callback func(scope *Scope)) {
cp.name = callbackName
cp.processor = &callback
cp.parent.reorder()
2014-01-24 17:52:26 +04:00
}
2016-01-16 14:20:52 +03:00
// Remove a registered callback
// db.Callback().Create().Remove("gorm:update_time_stamp_when_create")
func (cp *CallbackProcessor) Remove(callbackName string) {
fmt.Printf("[info] removing callback `%v` from %v\n", callbackName, fileWithLineNum())
cp.name = callbackName
2014-01-25 16:04:01 +04:00
cp.remove = true
2016-01-16 14:20:52 +03:00
cp.parent.reorder()
}
// Replace a registered callback with new callback
// db.Callback().Create().Replace("gorm:update_time_stamp_when_create", func(*Scope) {
// scope.SetColumn("Created", now)
// scope.SetColumn("Updated", now)
// })
func (cp *CallbackProcessor) Replace(callbackName string, callback func(scope *Scope)) {
fmt.Printf("[info] replacing callback `%v` from %v\n", callbackName, fileWithLineNum())
cp.name = callbackName
cp.processor = &callback
2014-01-24 17:52:26 +04:00
cp.replace = true
2016-01-16 14:20:52 +03:00
cp.parent.reorder()
2014-01-25 15:35:21 +04:00
}
2016-01-17 10:30:42 +03:00
// Get registered callback
// db.Callback().Create().Get("gorm:create")
func (cp *CallbackProcessor) Get(callbackName string) (callback func(scope *Scope)) {
for _, processor := range cp.parent.processors {
if processor.name == callbackName && processor.kind == cp.kind && !cp.remove {
return *cp.processor
}
}
return nil
}
2016-01-16 14:20:52 +03:00
// getRIndex get right index from string slice
2014-01-25 16:04:01 +04:00
func getRIndex(strs []string, str string) int {
for i := len(strs) - 1; i >= 0; i-- {
if strs[i] == str {
return i
2014-01-25 15:35:21 +04:00
}
}
return -1
}
2016-01-16 14:20:52 +03:00
// sortProcessors sort callback processors based on its before, after, remove, replace
func sortProcessors(cps []*CallbackProcessor) []*func(scope *Scope) {
var sortCallbackProcessor func(c *CallbackProcessor)
2014-01-25 15:35:21 +04:00
var names, sortedNames = []string{}, []string{}
for _, cp := range cps {
2014-01-25 16:04:01 +04:00
if index := getRIndex(names, cp.name); index > -1 {
if !cp.replace && !cp.remove {
2014-07-15 07:48:30 +04:00
fmt.Printf("[warning] duplicated callback `%v` from %v\n", cp.name, fileWithLineNum())
2014-01-25 16:04:01 +04:00
}
}
2014-01-25 15:35:21 +04:00
names = append(names, cp.name)
}
2016-01-16 14:20:52 +03:00
sortCallbackProcessor = func(c *CallbackProcessor) {
2014-01-25 16:04:01 +04:00
if getRIndex(sortedNames, c.name) > -1 {
2014-01-25 15:35:21 +04:00
return
}
if len(c.before) > 0 {
2014-01-25 16:04:01 +04:00
if index := getRIndex(sortedNames, c.before); index > -1 {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames[:index], append([]string{c.name}, sortedNames[index:]...)...)
2014-01-25 16:04:01 +04:00
} else if index := getRIndex(names, c.before); index > -1 {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames, c.name)
2014-08-27 12:10:33 +04:00
sortCallbackProcessor(cps[index])
2014-01-25 15:35:21 +04:00
} else {
sortedNames = append(sortedNames, c.name)
}
}
if len(c.after) > 0 {
2014-01-25 16:04:01 +04:00
if index := getRIndex(sortedNames, c.after); index > -1 {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames[:index+1], append([]string{c.name}, sortedNames[index+1:]...)...)
2014-01-25 16:04:01 +04:00
} else if index := getRIndex(names, c.after); index > -1 {
2014-01-25 15:35:21 +04:00
cp := cps[index]
if len(cp.before) == 0 {
cp.before = c.name
}
2014-08-27 12:10:33 +04:00
sortCallbackProcessor(cp)
2014-01-25 15:35:21 +04:00
} else {
sortedNames = append(sortedNames, c.name)
}
}
2014-08-27 12:10:33 +04:00
if getRIndex(sortedNames, c.name) == -1 {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames, c.name)
}
}
for _, cp := range cps {
2014-08-27 12:10:33 +04:00
sortCallbackProcessor(cp)
2014-01-25 15:35:21 +04:00
}
2014-01-26 08:41:37 +04:00
var funcs = []*func(scope *Scope){}
var sortedFuncs = []*func(scope *Scope){}
2014-01-25 15:35:21 +04:00
for _, name := range sortedNames {
2014-01-25 16:04:01 +04:00
index := getRIndex(names, name)
if !cps[index].remove {
sortedFuncs = append(sortedFuncs, cps[index].processor)
}
2014-01-25 15:35:21 +04:00
}
for _, cp := range cps {
2014-01-25 16:04:01 +04:00
if sindex := getRIndex(sortedNames, cp.name); sindex == -1 {
if !cp.remove {
funcs = append(funcs, cp.processor)
}
2014-01-25 15:35:21 +04:00
}
}
return append(sortedFuncs, funcs...)
}
2016-01-16 14:20:52 +03:00
// reorder all registered processors, and reset CURD callbacks
2016-01-16 16:55:00 +03:00
func (c *Callback) reorder() {
2016-01-16 14:20:52 +03:00
var creates, updates, deletes, queries, rowQueries []*CallbackProcessor
2014-01-25 15:35:21 +04:00
for _, processor := range c.processors {
2016-01-16 14:20:52 +03:00
switch processor.kind {
2014-01-25 15:35:21 +04:00
case "create":
creates = append(creates, processor)
case "update":
updates = append(updates, processor)
case "delete":
deletes = append(deletes, processor)
case "query":
queries = append(queries, processor)
2015-04-17 13:27:20 +03:00
case "row_query":
rowQueries = append(rowQueries, processor)
2014-01-25 15:35:21 +04:00
}
}
c.creates = sortProcessors(creates)
c.updates = sortProcessors(updates)
c.deletes = sortProcessors(deletes)
c.queries = sortProcessors(queries)
2015-04-17 13:27:20 +03:00
c.rowQueries = sortProcessors(rowQueries)
2014-01-23 12:43:39 +04:00
}