2019-02-13 21:54:59 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package dbutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-12-02 21:05:21 +00:00
|
|
|
// SplitConnStr returns the driver and DSN portions of a URL, along with the db implementation.
|
|
|
|
func SplitConnStr(s string) (driver string, source string, implementation Implementation, err error) {
|
2019-02-13 21:54:59 +00:00
|
|
|
// consider https://github.com/xo/dburl if this ends up lacking
|
|
|
|
parts := strings.SplitN(s, "://", 2)
|
|
|
|
if len(parts) != 2 {
|
2019-12-02 21:05:21 +00:00
|
|
|
return "", "", Unknown, fmt.Errorf("could not parse DB URL %s", s)
|
2019-02-13 21:54:59 +00:00
|
|
|
}
|
2019-12-02 21:05:21 +00:00
|
|
|
driver = parts[0]
|
|
|
|
source = parts[1]
|
|
|
|
implementation = setImplementation(parts[0])
|
|
|
|
|
|
|
|
if driver == "postgres" {
|
|
|
|
source = s // postgres wants full URLS for its DSN
|
|
|
|
}
|
|
|
|
if driver == "cockroach" {
|
|
|
|
driver = "postgres" // cockroach's driver is actually postgres
|
|
|
|
source = "postgres://" + source
|
2019-02-13 21:54:59 +00:00
|
|
|
}
|
2019-12-02 21:05:21 +00:00
|
|
|
return driver, source, implementation, nil
|
2019-02-13 21:54:59 +00:00
|
|
|
}
|