-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsn.go
69 lines (67 loc) · 1.87 KB
/
dsn.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package immusql
import (
"fmt"
"net/url"
"path/filepath"
"strconv"
"strings"
)
// parseDSN parses a dsn specifying a immudb database
// into a configuration for connecting to it.
func parseDSN(dsn string) (dsnConfig, error) {
conf := dsnConfig{}
url, err := url.Parse(dsn)
if err != nil {
return conf, fmt.Errorf("parsing dsn %s failed: %v", dsn, err)
}
if url.Scheme != "immudb" && url.Scheme != "immudbe" {
return conf, fmt.Errorf("scheme of dsn is %s but has to be immudb or immudbe", url.Scheme)
}
// The immudbe scheme indicates that an embedded engine should be created.
if url.Scheme == "immudbe" {
conf.Embedded = true
}
conf.User = url.User.Username()
conf.Pass, _ = url.User.Password()
// If no hostname is specified, use localhost as default.
host := url.Hostname()
if host == "" {
conf.Host = "localhost"
} else {
conf.Host = host
}
// If no port is specified, use the default port 3322.
port := url.Port()
if port == "" {
conf.Port = 3322
} else {
portInt, err := strconv.Atoi(port)
if err != nil {
return conf, fmt.Errorf("parsing port number '%s' as integer failed: %v", port, err)
}
conf.Port = portInt
}
// Parse the datebase name and the path to it,
// if the embedded engine is used.
path := url.Path
if conf.Embedded {
// Set the path to the embedded database.
conf.Path = filepath.Dir(filepath.FromSlash(path))
// Use the last part of the path as database name.
conf.Name = filepath.Base(path)
if conf.Name == "/" || conf.Name == "." {
// "/" and "." are not valid database names.
// An empty name is set and replaced with the
// name of the default database below.
conf.Name = ""
}
} else {
// Use the full path as database name.
conf.Name = strings.TrimPrefix(path, "/")
}
// If no database is given, use the database named defaultdb.
if conf.Name == "" {
conf.Name = "defaultdb"
}
return conf, nil
}