2019-09-04 11:37:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-01-18 13:42:15 +00:00
|
|
|
"errors"
|
2019-12-07 15:53:49 +00:00
|
|
|
"net/url"
|
2019-09-04 11:37:50 +00:00
|
|
|
|
|
|
|
_ "github.com/lib/pq"
|
2019-09-29 18:08:54 +00:00
|
|
|
"github.com/volker-raschek/go-logger/pkg/logger"
|
2019-09-04 11:37:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-01-18 13:42:15 +00:00
|
|
|
errorUnsupportedDatabase = errors.New("Unsupported database scheme")
|
|
|
|
flogger = logger.NewSilentLogger()
|
2019-09-29 18:08:54 +00:00
|
|
|
)
|
2019-09-04 11:37:50 +00:00
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
func New(storageEndpoint *url.URL) (Database, error) {
|
|
|
|
switch storageEndpoint.Scheme {
|
|
|
|
case "postgres":
|
2020-01-18 13:42:15 +00:00
|
|
|
newDBO, err := sql.Open(storageEndpoint.Scheme, storageEndpoint.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-04 11:37:50 +00:00
|
|
|
return &Postgres{
|
|
|
|
dbo: newDBO,
|
|
|
|
}, nil
|
|
|
|
default:
|
2020-01-18 13:42:15 +00:00
|
|
|
return nil, errorUnsupportedDatabase
|
2019-09-04 11:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetLogger(logger logger.Logger) {
|
|
|
|
flogger = logger
|
|
|
|
}
|