updown/repository/connector.go

42 lines
815 B
Go

package repository
import (
"database/sql"
"errors"
_ "github.com/lib/pq"
)
const driver = "postgres"
// ErrNotConnected is returned when the database connection has never been attempted
var ErrNotConnected = errors.New("repository: Database connection was never established")
var connector *DatabaseConnector
// DatabaseConnector holds a connection to the database
type DatabaseConnector struct {
*sql.DB
}
// GetConnector gets the stored database connection
func GetConnector() *DatabaseConnector {
if connector == nil {
panic(ErrNotConnected)
}
return connector
}
// Connect connects to the database
func Connect() (*DatabaseConnector, error) {
connectorCandidate, err := initDatabaseConnector()
if err != nil {
return nil, err
}
connector = connectorCandidate
return connector, nil
}