-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
driver.go
42 lines (34 loc) · 1.01 KB
/
driver.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
38
39
40
41
42
package dbal
import (
"sync"
"github.com/pkg/errors"
)
var (
drivers = make([]func() Driver, 0)
dmtx sync.Mutex
// ErrNoResponsibleDriverFound is returned when no driver was found for the provided DSN.
ErrNoResponsibleDriverFound = errors.New("no driver is capable of handling the given DSN")
)
// Driver represents a driver
type Driver interface {
// CanHandle returns true if the driver is capable of handling the given DSN or false otherwise.
CanHandle(dsn string) bool
// Ping returns nil if the driver has connectivity and is healthy or an error otherwise.
Ping() error
}
// RegisterDriver registers a driver
func RegisterDriver(d func() Driver) {
dmtx.Lock()
drivers = append(drivers, d)
dmtx.Unlock()
}
// GetDriverFor returns a driver for the given DSN or ErrNoResponsibleDriverFound if no driver was found.
func GetDriverFor(dsn string) (Driver, error) {
for _, f := range drivers {
driver := f()
if driver.CanHandle(dsn) {
return driver, nil
}
}
return nil, ErrNoResponsibleDriverFound
}