...
1 package dbal
2
3 import (
4 "strings"
5 "sync"
6
7 "github.com/pkg/errors"
8 )
9
10 var (
11 drivers = make([]func() Driver, 0)
12 dmtx sync.Mutex
13
14
15 ErrNoResponsibleDriverFound = errors.New("dsn value requested an unknown driver")
16 ErrSQLiteSupportMissing = errors.New(`the DSN connection string looks like a SQLite connection, but SQLite support was not built into the binary. Please check if you have downloaded the correct binary or are using the correct Docker Image. Binary archives and Docker Images indicate SQLite support by appending the -sqlite suffix`)
17 )
18
19
20 type Driver interface {
21
22 CanHandle(dsn string) bool
23
24
25 Ping() error
26 }
27
28
29 func RegisterDriver(d func() Driver) {
30 dmtx.Lock()
31 drivers = append(drivers, d)
32 dmtx.Unlock()
33 }
34
35
36 func GetDriverFor(dsn string) (Driver, error) {
37 for _, f := range drivers {
38 driver := f()
39 if driver.CanHandle(dsn) {
40 return driver, nil
41 }
42 }
43
44 if IsSQLite(dsn) {
45 return nil, ErrSQLiteSupportMissing
46 }
47
48 return nil, ErrNoResponsibleDriverFound
49 }
50
51
52 func IsSQLite(dsn string) bool {
53 scheme := strings.Split(dsn, "://")[0]
54 return scheme == "sqlite" || scheme == "sqlite3"
55 }
56
View as plain text