Skip to content

Commit

Permalink
Add register plugin API
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 23, 2020
1 parent e77e7bb commit f4bfc43
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 15 additions & 0 deletions gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit f4bfc43

Please sign in to comment.