2019-09-04 11:37:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
2019-09-24 13:34:14 +00:00
|
|
|
"github.com/volker-raschek/go-logger/pkg/logger"
|
2019-09-04 11:37:50 +00:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
flogger logger.Logger
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flogger = logger.NewSilentLogger()
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(databaseSettings *config.DatabaseSettings) (Database, error) {
|
|
|
|
connStr := fmt.Sprintf("%v://%v:%v@%v:%v/%v?sslmode=disable", databaseSettings.Vendor.String(), databaseSettings.User, databaseSettings.Password, databaseSettings.Host, databaseSettings.Port, databaseSettings.Database)
|
|
|
|
newDBO, err := sql.Open(databaseSettings.Vendor.String(), connStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch databaseSettings.Vendor {
|
|
|
|
case config.VendorPostgreSQL:
|
|
|
|
return &Postgres{
|
|
|
|
dbo: newDBO,
|
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown Database Type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetLogger(logger logger.Logger) {
|
|
|
|
flogger = logger
|
|
|
|
}
|