123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- package gorm
- import "log"
- var DefaultCallback = &Callback{}
- 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
- }
- type CallbackProcessor struct {
- name string
- before string
- after string
- replace bool
- remove bool
- kind string
- processor *func(scope *Scope) // callback handler
- parent *Callback
- }
- func (c *Callback) clone() *Callback {
- return &Callback{
- creates: c.creates,
- updates: c.updates,
- deletes: c.deletes,
- queries: c.queries,
- rowQueries: c.rowQueries,
- processors: c.processors,
- }
- }
- func (c *Callback) Create() *CallbackProcessor {
- return &CallbackProcessor{kind: "create", parent: c}
- }
- func (c *Callback) Update() *CallbackProcessor {
- return &CallbackProcessor{kind: "update", parent: c}
- }
- func (c *Callback) Delete() *CallbackProcessor {
- return &CallbackProcessor{kind: "delete", parent: c}
- }
- func (c *Callback) Query() *CallbackProcessor {
- return &CallbackProcessor{kind: "query", parent: c}
- }
- func (c *Callback) RowQuery() *CallbackProcessor {
- return &CallbackProcessor{kind: "row_query", parent: c}
- }
- func (cp *CallbackProcessor) After(callbackName string) *CallbackProcessor {
- cp.after = callbackName
- return cp
- }
- func (cp *CallbackProcessor) Before(callbackName string) *CallbackProcessor {
- cp.before = callbackName
- return cp
- }
- func (cp *CallbackProcessor) Register(callbackName string, callback func(scope *Scope)) {
- if cp.kind == "row_query" {
- if cp.before == "" && cp.after == "" && callbackName != "gorm:row_query" {
- log.Printf("Registing RowQuery callback %v without specify order with Before(), After(), applying Before('gorm:row_query') by default for compatibility...\n", callbackName)
- cp.before = "gorm:row_query"
- }
- }
- cp.name = callbackName
- cp.processor = &callback
- cp.parent.processors = append(cp.parent.processors, cp)
- cp.parent.reorder()
- }
- func (cp *CallbackProcessor) Remove(callbackName string) {
- log.Printf("[info] removing callback `%v` from %v\n", callbackName, fileWithLineNum())
- cp.name = callbackName
- cp.remove = true
- cp.parent.processors = append(cp.parent.processors, cp)
- cp.parent.reorder()
- }
- func (cp *CallbackProcessor) Replace(callbackName string, callback func(scope *Scope)) {
- log.Printf("[info] replacing callback `%v` from %v\n", callbackName, fileWithLineNum())
- cp.name = callbackName
- cp.processor = &callback
- cp.replace = true
- cp.parent.processors = append(cp.parent.processors, cp)
- cp.parent.reorder()
- }
- func (cp *CallbackProcessor) Get(callbackName string) (callback func(scope *Scope)) {
- for _, p := range cp.parent.processors {
- if p.name == callbackName && p.kind == cp.kind && !cp.remove {
- return *p.processor
- }
- }
- return nil
- }
- func getRIndex(strs []string, str string) int {
- for i := len(strs) - 1; i >= 0; i-- {
- if strs[i] == str {
- return i
- }
- }
- return -1
- }
- func sortProcessors(cps []*CallbackProcessor) []*func(scope *Scope) {
- var (
- allNames, sortedNames []string
- sortCallbackProcessor func(c *CallbackProcessor)
- )
- for _, cp := range cps {
-
- if index := getRIndex(allNames, cp.name); index > -1 && !cp.replace && !cp.remove {
- log.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 c.before != "" {
- if index := getRIndex(sortedNames, c.before); index != -1 {
-
- sortedNames = append(sortedNames[:index], append([]string{c.name}, sortedNames[index:]...)...)
- } else if index := getRIndex(allNames, c.before); index != -1 {
-
- sortedNames = append(sortedNames, c.name)
- sortCallbackProcessor(cps[index])
- }
- }
- if c.after != "" {
- if index := getRIndex(sortedNames, c.after); index != -1 {
-
- sortedNames = append(sortedNames[:index+1], append([]string{c.name}, sortedNames[index+1:]...)...)
- } else if index := getRIndex(allNames, c.after); index != -1 {
-
- cp := cps[index]
-
- if cp.before == "" {
- cp.before = c.name
- }
- sortCallbackProcessor(cp)
- }
- }
-
- 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
- }
- func (c *Callback) reorder() {
- var creates, updates, deletes, queries, rowQueries []*CallbackProcessor
- for _, processor := range c.processors {
- if processor.name != "" {
- 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)
- }
|