-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdsn.go
More file actions
50 lines (45 loc) · 997 Bytes
/
dsn.go
File metadata and controls
50 lines (45 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package sql
import (
"context"
"fmt"
"github.com/viant/afs"
"net/url"
"os"
"path"
"strings"
)
// Config represent Connection config
type Config struct {
BaseURL string
url.Values
}
// ParseDSN parses the DSN string to a Config
func ParseDSN(dsn string) (*Config, error) {
URL, err := url.Parse(dsn)
if err != nil {
return nil, fmt.Errorf("invalid dsn: %v", err)
}
cfg := &Config{
Values: URL.Query(),
}
if URL.Scheme == "file" && URL.Path != "" {
fs := afs.New()
cwd, _ := os.Getwd()
candidate := path.Join(cwd, URL.Path[1:])
if ok, _ := fs.Exists(context.Background(), candidate); ok {
URL.Path = candidate
}
}
cfg.BaseURL = URL.Scheme + "://" + URL.Host + URL.Path
if idx := strings.Index(dsn, "?"); idx != -1 {
cfg.BaseURL = dsn[:idx]
}
if len(cfg.Values) > 0 {
var unsupported []string
for k := range cfg.Values {
unsupported = append(unsupported, k)
}
return nil, fmt.Errorf("unsupported options: %v", unsupported)
}
return cfg, nil
}