37 lines
720 B
Go
37 lines
720 B
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
type DBOType string
|
|
|
|
func (dboType DBOType) String() string {
|
|
return string(dboType)
|
|
}
|
|
|
|
const (
|
|
DBOTypePostgres DBOType = "postgres"
|
|
DBOTypeOracle = "oracle"
|
|
)
|
|
|
|
func New(dboType DBOType, host string, port string, database string, user string, password string) (Database, error) {
|
|
connStr := fmt.Sprintf("%v://%v:%v@%v:%v/%v?sslmode=disable", dboType.String(), user, password, host, port, database)
|
|
newDBO, err := sql.Open(dboType.String(), connStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch dboType {
|
|
case "postgres":
|
|
return &Postgres{
|
|
dbo: newDBO,
|
|
}, nil
|
|
default:
|
|
return nil, fmt.Errorf("Unknown Database Type")
|
|
}
|
|
}
|