Add callback.go
This commit is contained in:
		
							parent
							
								
									45edecd13b
								
							
						
					
					
						commit
						ff70578eeb
					
				
							
								
								
									
										98
									
								
								callback.go
									
									
									
									
									
								
							
							
						
						
									
										98
									
								
								callback.go
									
									
									
									
									
								
							| @ -1,55 +1,87 @@ | |||||||
| package gorm | package gorm | ||||||
| 
 | 
 | ||||||
| import ( |  | ||||||
| 	"fmt" |  | ||||||
| 	"time" |  | ||||||
| ) |  | ||||||
| 
 |  | ||||||
| type callback struct { | type callback struct { | ||||||
| 	Create []func() | 	create     []func() | ||||||
| 	Update []func() | 	update     []func() | ||||||
| 	Delete []func() | 	delete     []func() | ||||||
| 	Query  []func() | 	query      []func() | ||||||
|  | 	processors []*callback_processor | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (c *callback) RegisterCallback(typ string, fc func()) { | type callback_processor struct { | ||||||
|  | 	name      string | ||||||
|  | 	before    string | ||||||
|  | 	after     string | ||||||
|  | 	replace   bool | ||||||
|  | 	typ       string | ||||||
|  | 	processor func() | ||||||
|  | 	callback  *callback | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func query(db *DB) { | func (c *callback) addProcessor(typ string) *callback_processor { | ||||||
|  | 	cp := &callback_processor{typ: typ, callback: c} | ||||||
|  | 	c.processors = append(c.processors, cp) | ||||||
|  | 	return cp | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func save(db *DB) { | func (c *callback) Create() *callback_processor { | ||||||
|  | 	return c.addProcessor("create") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func create(db *DB) { | func (c *callback) Update() *callback_processor { | ||||||
|  | 	return c.addProcessor("update") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func update(db *DB) { | func (c *callback) Delete() *callback_processor { | ||||||
|  | 	return c.addProcessor("delete") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func Delete(scope *Scope) { | func (c *callback) Query() *callback_processor { | ||||||
| 	scope.CallMethod("BeforeDelete") | 	return c.addProcessor("query") | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| 	if !scope.HasError() { | func (c *callback) Sort() { | ||||||
| 		if !scope.Search.unscope && scope.HasColumn("DeletedAt") { | 	creates, updates, deletes, queries := []*callback_processor{}, []*callback_processor{}, []*callback_processor{}, []*callback_processor{} | ||||||
| 			scope.Raw(fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", scope.Table(), scope.AddToVars(time.Now()), scope.CombinedSql())) | 
 | ||||||
| 		} else { | 	for _, processor := range c.processors { | ||||||
| 			scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.Table(), scope.CombinedSql())) | 		switch processor.typ { | ||||||
|  | 		case "create": | ||||||
|  | 			creates = append(creates, processor) | ||||||
|  | 		case "update": | ||||||
|  | 			updates = append(updates, processor) | ||||||
|  | 		case "delete": | ||||||
|  | 			deletes = append(deletes, processor) | ||||||
|  | 		case "query": | ||||||
|  | 			queries = append(queries, processor) | ||||||
| 		} | 		} | ||||||
| 		scope.Exec() |  | ||||||
| 		scope.CallMethod("AfterDelete") |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var DefaultCallback = &callback{} | func (cp *callback_processor) Before(name string) *callback_processor { | ||||||
| 
 | 	cp.before = name | ||||||
| func init() { | 	return cp | ||||||
| 	DefaultCallback.Create().Before("Delete").After("Lalala").Register("delete", Delete) |  | ||||||
| 	DefaultCallback.Update().Before("Delete").After("Lalala").Remove("replace", Delete) |  | ||||||
| 	DefaultCallback.Delete().Before("Delete").After("Lalala").Replace("replace", Delete) |  | ||||||
| 	DefaultCallback.Query().Before("Delete").After("Lalala").Replace("replace", Delete) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Scope
 | func (cp *callback_processor) After(name string) *callback_processor { | ||||||
| // HasError(), HasColumn(), CallMethod(), Raw(), Exec()
 | 	cp.after = name | ||||||
| // TableName(), CombinedQuerySQL()
 | 	return cp | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (cp *callback_processor) Register(name string, fc func()) { | ||||||
|  | 	cp.name = name | ||||||
|  | 	cp.processor = fc | ||||||
|  | 	cp.callback.Sort() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (cp *callback_processor) Remove(name string) { | ||||||
|  | 	cp.Replace(name, func() {}) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (cp *callback_processor) Replace(name string, fc func()) { | ||||||
|  | 	cp.name = name | ||||||
|  | 	cp.processor = fc | ||||||
|  | 	cp.replace = true | ||||||
|  | 	cp.callback.Sort() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | var DefaultCallback = &callback{processors: []*callback_processor{}} | ||||||
|  | |||||||
							
								
								
									
										53
									
								
								callbacks/create.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								callbacks/create.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,53 @@ | |||||||
|  | package callback | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 
 | ||||||
|  | 	"github.com/jinzhu/gorm" | ||||||
|  | 
 | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func Create(scope *gorm.Scope) { | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func init() { | ||||||
|  | 	gorm.DefaultCallback.Create().Before().Register(Create) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func query(db *DB) { | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func save(db *DB) { | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func create(db *DB) { | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func update(db *DB) { | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func Delete(scope *Scope) { | ||||||
|  | 	scope.CallMethod("BeforeDelete") | ||||||
|  | 
 | ||||||
|  | 	if !scope.HasError() { | ||||||
|  | 		if !scope.Search.unscope && scope.HasColumn("DeletedAt") { | ||||||
|  | 			scope.Raw(fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", scope.Table(), scope.AddToVars(time.Now()), scope.CombinedSql())) | ||||||
|  | 		} else { | ||||||
|  | 			scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.Table(), scope.CombinedSql())) | ||||||
|  | 		} | ||||||
|  | 		scope.Exec() | ||||||
|  | 		scope.CallMethod("AfterDelete") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func init() { | ||||||
|  | 	DefaultCallback.Create().Before("Delete").After("Lalala").Register("delete", Delete) | ||||||
|  | 	DefaultCallback.Update().Before("Delete").After("Lalala").Remove("replace", Delete) | ||||||
|  | 	DefaultCallback.Delete().Before("Delete").After("Lalala").Replace("replace", Delete) | ||||||
|  | 	DefaultCallback.Query().Before("Delete").After("Lalala").Replace("replace", Delete) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Scope
 | ||||||
|  | // HasError(), HasColumn(), CallMethod(), Raw(), Exec()
 | ||||||
|  | // TableName(), CombinedQuerySQL()
 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu