-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
62 lines (56 loc) · 1.64 KB
/
driver.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
package polypheny
import (
sql "database/sql"
driver "database/sql/driver"
context "golang.org/x/net/context"
strings "strings"
)
// PolyphenyDriver implements driver.Driver and DriverContext interface
type PolyphenyDriver struct{}
func init() {
sql.Register("polypheny", PolyphenyDriver{})
}
func parseDSN(name string) (string, string, string, error) {
connectionStrings := strings.Split(name, ",")
if len(connectionStrings) != 2 {
return "", "", "", &ClientError{
message: "Connection string (DSN) should have a format like addr:port,user:password",
}
}
addr := connectionStrings[0]
user := strings.Split(connectionStrings[1], ":")
if len(user) != 2 {
return "", "", "", &ClientError{
message: "Connection string (DSN) should have a format like addr:port,user:password",
}
}
username := user[0]
password := user[1]
return addr, username, password, nil
}
// Open will return a PolyphenyConn which implements driver.Conn and represents a Connection to Polypheny server
// The name, also called DSN, has the following format: addr:port,user:password
func (d PolyphenyDriver) Open(name string) (driver.Conn, error) {
addr, username, password, err := parseDSN(name)
if err != nil {
return nil, err
}
connector := Connector{
address: addr,
username: username,
password: password,
}
return connector.Connect(context.Background())
}
// OpenConnector will return a Connector
func (d PolyphenyDriver) OpenConnector(name string) (driver.Connector, error) {
addr, username, password, err := parseDSN(name)
if err != nil {
return nil, err
}
return &Connector{
address: addr,
username: username,
password: password,
}, nil
}