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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package storage
import (
"errors"
"fmt"
"github.com/eugenetriguba/bolt/internal/configloader"
"github.com/upper/db/v4"
"github.com/upper/db/v4/adapter/mysql"
"github.com/upper/db/v4/adapter/postgresql"
)
var postgresqlDriverName = "postgresql"
var mysqlDriverName = "mysql"
var supportedDrivers = []string{postgresqlDriverName, mysqlDriverName}
var (
ErrMalformedConnectionString = errors.New(
"malformed database connection parameters provided",
)
ErrUnableToConnect = errors.New("unable to open connection to database")
ErrUnsupportedDriver = fmt.Errorf("unsupported driver, supported drivers are %s", supportedDrivers)
)
type DB struct {
Session db.Session
}
// DBConnect establishes a connection to the database using the
// connection configuration.
//
// The following errors may be returned:
// - ErrUnableToConnect: Unable to make a connection to the database with
// the provided connection parameters.
// - ErrUnsupportedDriver: The provided driver is not supported.
func DBConnect(cfg configloader.ConnectionConfig) (DB, error) {
var db db.Session
var err error = nil
switch cfg.Driver {
case postgresqlDriverName:
db, err = postgresql.Open(postgresql.ConnectionURL{
User: cfg.User,
Password: cfg.Password,
Host: cfg.Host,
Database: cfg.DBName,
})
case mysqlDriverName:
db, err = mysql.Open(mysql.ConnectionURL{
User: cfg.User,
Password: cfg.Password,
Host: cfg.Host,
Database: cfg.DBName,
})
default:
return DB{}, ErrUnsupportedDriver
}
if err != nil {
return DB{}, fmt.Errorf("%w: %v", ErrUnableToConnect, err)
}
return DB{Session: db}, nil
}
|