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]
|
2019-12-05 20:42:12 +00:00
|
|
|
implementation = ImplementationForScheme(parts[0])
|
2019-12-02 21:05:21 +00:00
|
|
|
|
2019-12-11 19:04:09 +00:00
|
|
|
if implementation == Postgres || implementation == Cockroach {
|
|
|
|
source = s // postgres and cockroach want full URLS for their DSNs
|
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
|
|
|
}
|