mirror of https://github.com/go-gorm/gorm.git
238 lines
7.9 KiB
Go
238 lines
7.9 KiB
Go
package gorm
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// defaultCallback hold default callbacks defined by gorm
|
|
var defaultCallback = &Callback{}
|
|
|
|
// Callback contains callbacks that used when CURD objects
|
|
// 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
|
|
type Callback struct {
|
|
creates []*func(scope *Scope)
|
|
updates []*func(scope *Scope)
|
|
deletes []*func(scope *Scope)
|
|
queries []*func(scope *Scope)
|
|
rowQueries []*func(scope *Scope)
|
|
processors []*CallbackProcessor
|
|
}
|
|
|
|
// CallbackProcessor contains all informations for a callback
|
|
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
|
|
parent *Callback
|
|
}
|
|
|
|
func (c *Callback) addProcessor(kind string) *CallbackProcessor {
|
|
cp := &CallbackProcessor{kind: kind, parent: c}
|
|
c.processors = append(c.processors, cp)
|
|
return cp
|
|
}
|
|
|
|
func (c *Callback) clone() *Callback {
|
|
return &Callback{
|
|
creates: c.creates,
|
|
updates: c.updates,
|
|
deletes: c.deletes,
|
|
queries: c.queries,
|
|
processors: c.processors,
|
|
}
|
|
}
|
|
|
|
// 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"))
|
|
// })
|
|
func (c *Callback) Create() *CallbackProcessor {
|
|
return c.addProcessor("create")
|
|
}
|
|
|
|
// Update could be used to register callbacks for updating object, refer `Create` for usage
|
|
func (c *Callback) Update() *CallbackProcessor {
|
|
return c.addProcessor("update")
|
|
}
|
|
|
|
// Delete could be used to register callbacks for deleting object, refer `Create` for usage
|
|
func (c *Callback) Delete() *CallbackProcessor {
|
|
return c.addProcessor("delete")
|
|
}
|
|
|
|
// Query could be used to register callbacks for querying objects with query methods like `Find`, `First`, `Related`, `Association`...
|
|
// refer `Create` for usage
|
|
func (c *Callback) Query() *CallbackProcessor {
|
|
return c.addProcessor("query")
|
|
}
|
|
|
|
// RowQuery could be used to register callbacks for querying objects with `Row`, `Rows`, refer `Create` for usage
|
|
func (c *Callback) RowQuery() *CallbackProcessor {
|
|
return c.addProcessor("row_query")
|
|
}
|
|
|
|
// After insert a new callback after callback `callbackName`, refer `Callbacks.Create`
|
|
func (cp *CallbackProcessor) After(callbackName string) *CallbackProcessor {
|
|
cp.after = callbackName
|
|
return cp
|
|
}
|
|
|
|
// Before insert a new callback before callback `callbackName`, refer `Callbacks.Create`
|
|
func (cp *CallbackProcessor) Before(callbackName string) *CallbackProcessor {
|
|
cp.before = callbackName
|
|
return cp
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
|
|
// 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
|
|
cp.remove = true
|
|
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
|
|
cp.replace = true
|
|
cp.parent.reorder()
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// getRIndex get right index from string slice
|
|
func getRIndex(strs []string, str string) int {
|
|
for i := len(strs) - 1; i >= 0; i-- {
|
|
if strs[i] == str {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// sortProcessors sort callback processors based on its before, after, remove, replace
|
|
func sortProcessors(cps []*CallbackProcessor) []*func(scope *Scope) {
|
|
var (
|
|
allNames, sortedNames []string
|
|
sortCallbackProcessor func(c *CallbackProcessor)
|
|
)
|
|
|
|
for _, cp := range cps {
|
|
// show warning message the callback name already exists
|
|
if index := getRIndex(allNames, cp.name); index > -1 && !cp.replace && !cp.remove {
|
|
fmt.Printf("[warning] duplicated callback `%v` from %v\n", cp.name, fileWithLineNum())
|
|
}
|
|
allNames = append(allNames, cp.name)
|
|
}
|
|
|
|
sortCallbackProcessor = func(c *CallbackProcessor) {
|
|
if getRIndex(sortedNames, c.name) == -1 { // if not sorted
|
|
if c.before != "" { // if defined before callback
|
|
if index := getRIndex(sortedNames, c.before); index != -1 {
|
|
// if before callback already sorted, append current callback just after it
|
|
sortedNames = append(sortedNames[:index], append([]string{c.name}, sortedNames[index:]...)...)
|
|
} else if index := getRIndex(allNames, c.before); index != -1 {
|
|
// if before callback exists but haven't sorted, append current callback to last
|
|
sortedNames = append(sortedNames, c.name)
|
|
sortCallbackProcessor(cps[index])
|
|
}
|
|
}
|
|
|
|
if c.after != "" { // if defined after callback
|
|
if index := getRIndex(sortedNames, c.after); index != -1 {
|
|
// if after callback already sorted, append current callback just before it
|
|
sortedNames = append(sortedNames[:index+1], append([]string{c.name}, sortedNames[index+1:]...)...)
|
|
} else if index := getRIndex(allNames, c.after); index != -1 {
|
|
// if after callback exists but haven't sorted
|
|
cp := cps[index]
|
|
// set after callback's before callback to current callback
|
|
if cp.before == "" {
|
|
cp.before = c.name
|
|
}
|
|
sortCallbackProcessor(cp)
|
|
}
|
|
}
|
|
|
|
// if current callback haven't been sorted, append it to last
|
|
if getRIndex(sortedNames, c.name) == -1 {
|
|
sortedNames = append(sortedNames, c.name)
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, cp := range cps {
|
|
sortCallbackProcessor(cp)
|
|
}
|
|
|
|
var sortedFuncs []*func(scope *Scope)
|
|
for _, name := range sortedNames {
|
|
if index := getRIndex(allNames, name); !cps[index].remove {
|
|
sortedFuncs = append(sortedFuncs, cps[index].processor)
|
|
}
|
|
}
|
|
|
|
return sortedFuncs
|
|
}
|
|
|
|
// reorder all registered processors, and reset CURD callbacks
|
|
func (c *Callback) reorder() {
|
|
var creates, updates, deletes, queries, rowQueries []*CallbackProcessor
|
|
|
|
for _, processor := range c.processors {
|
|
switch processor.kind {
|
|
case "create":
|
|
creates = append(creates, processor)
|
|
case "update":
|
|
updates = append(updates, processor)
|
|
case "delete":
|
|
deletes = append(deletes, processor)
|
|
case "query":
|
|
queries = append(queries, processor)
|
|
case "row_query":
|
|
rowQueries = append(rowQueries, processor)
|
|
}
|
|
}
|
|
|
|
c.creates = sortProcessors(creates)
|
|
c.updates = sortProcessors(updates)
|
|
c.deletes = sortProcessors(deletes)
|
|
c.queries = sortProcessors(queries)
|
|
c.rowQueries = sortProcessors(rowQueries)
|
|
}
|