-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
37 lines (32 loc) · 881 Bytes
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package dbx
import (
"errors"
"github.com/jucardi/go-beans/beans"
"github.com/jucardi/go-strings/stringx"
)
type IDbProvider interface {
Dial(cfg *DbConfig) (IDatabase, error)
}
func Register(dbType string, provider IDbProvider, setPrimary ...bool) error {
if err := beans.Register((*IDbProvider)(nil), dbType, provider); err != nil {
return err
}
if len(setPrimary) > 0 && setPrimary[0] {
return beans.SetPrimary((*IDbProvider)(nil), dbType)
}
return nil
}
func Dial(cfg *DbConfig, dbType ...string) (IDatabase, error) {
provider := GetProvider(dbType...)
if provider == nil {
return nil, errors.New("database provider was not found.")
}
return provider.Dial(cfg)
}
func GetProvider(dbType ...string) IDbProvider {
ret := beans.Resolve((*IDbProvider)(nil), stringx.GetOrDefault("", dbType...))
if ret != nil {
return ret.(IDbProvider)
}
return nil
}