PKGBUILD/pkg/storage/db/db.go

40 lines
882 B
Go
Raw Normal View History

package db
import (
"database/sql"
"fmt"
"github.com/go-flucky/flucky/pkg/config"
"github.com/volker-raschek/go-logger/pkg/logger"
_ "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
}