40 lines
874 B
Go
40 lines
874 B
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
|
"github.com/go-flucky/flucky/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
|
|
}
|