-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
connect.go
42 lines (34 loc) · 881 Bytes
/
connect.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 (
"time"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/ory/x/logrusx"
"github.com/ory/x/sqlcon"
)
// Connect is a wrapper for connecting to different SQL drivers.
func Connect(db string, logger *logrusx.Logger, memf func() error, sqlf func(db *sqlx.DB) error) error {
if db == "memory" {
return memf()
} else if db == "" {
return errors.New("No database DSN provided")
}
scheme := sqlcon.GetDriverName(db)
switch scheme {
case "postgres":
fallthrough
case "cockroach":
fallthrough
case "mysql":
c, err := sqlcon.NewSQLConnection(db, logger)
if err != nil {
return errors.WithStack(err)
}
cdb, err := c.GetDatabaseRetry(time.Second*15, time.Minute*5)
if err != nil {
return errors.WithStack(err)
}
return sqlf(cdb)
}
return errors.Errorf("The provided database DSN %s can not be handled", db)
}