-
Notifications
You must be signed in to change notification settings - Fork 720
/
db_provider.go
51 lines (43 loc) · 1.09 KB
/
db_provider.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
43
44
45
46
47
48
49
50
51
package db_provider
import (
"context"
"database/sql"
"github.com/golang/glog"
"github.com/prebid/prebid-server/v2/config"
)
type DbProvider interface {
Config() config.DatabaseConnection
ConnString() (string, error)
Open() error
Close() error
Ping() error
PrepareQuery(template string, params ...QueryParam) (query string, args []interface{})
QueryContext(ctx context.Context, template string, params ...QueryParam) (*sql.Rows, error)
}
func NewDbProvider(dataType config.DataType, cfg config.DatabaseConnection) DbProvider {
var provider DbProvider
switch cfg.Driver {
case "mysql":
provider = &MySqlDbProvider{
cfg: cfg,
}
case "postgres":
provider = &PostgresDbProvider{
cfg: cfg,
}
default:
glog.Fatalf("Unsupported database driver %s", cfg.Driver)
return nil
}
if err := provider.Open(); err != nil {
glog.Fatalf("Failed to open %s database connection: %v", dataType, err)
}
if err := provider.Ping(); err != nil {
glog.Fatalf("Failed to ping %s database: %v", dataType, err)
}
return provider
}
type QueryParam struct {
Name string
Value interface{}
}