Add register plugin API
This commit is contained in:
		
							parent
							
								
									e77e7bb842
								
							
						
					
					
						commit
						f4bfc435cc
					
				| @ -27,4 +27,6 @@ var ( | ||||
| 	ErrorModelValueRequired = errors.New("model value required") | ||||
| 	// ErrUnsupportedDriver unsupported driver
 | ||||
| 	ErrUnsupportedDriver = errors.New("unsupported driver") | ||||
| 	// ErrRegistered registered
 | ||||
| 	ErrRegistered = errors.New("registered") | ||||
| ) | ||||
|  | ||||
							
								
								
									
										15
									
								
								gorm.go
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								gorm.go
									
									
									
									
									
								
							| @ -39,6 +39,8 @@ type Config struct { | ||||
| 	ConnPool ConnPool | ||||
| 	// Dialector database dialector
 | ||||
| 	Dialector | ||||
| 	// Plugins registered plugins
 | ||||
| 	Plugins map[string]Plugin | ||||
| 
 | ||||
| 	callbacks  *callbacks | ||||
| 	cacheStore *sync.Map | ||||
| @ -309,3 +311,16 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func (db *DB) Use(plugin Plugin) (err error) { | ||||
| 	name := plugin.Name() | ||||
| 	if _, ok := db.Plugins[name]; !ok { | ||||
| 		if err = plugin.Initialize(db); err == nil { | ||||
| 			db.Plugins[name] = plugin | ||||
| 		} | ||||
| 	} else { | ||||
| 		return ErrRegistered | ||||
| 	} | ||||
| 
 | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| @ -20,6 +20,12 @@ type Dialector interface { | ||||
| 	Explain(sql string, vars ...interface{}) string | ||||
| } | ||||
| 
 | ||||
| // Plugin GORM plugin interface
 | ||||
| type Plugin interface { | ||||
| 	Name() string | ||||
| 	Initialize(*DB) error | ||||
| } | ||||
| 
 | ||||
| // ConnPool db conns pool interface
 | ||||
| type ConnPool interface { | ||||
| 	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) | ||||
| @ -28,6 +34,7 @@ type ConnPool interface { | ||||
| 	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row | ||||
| } | ||||
| 
 | ||||
| // SavePointerDialectorInterface save pointer interface
 | ||||
| type SavePointerDialectorInterface interface { | ||||
| 	SavePoint(tx *DB, name string) error | ||||
| 	RollbackTo(tx *DB, name string) error | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu