-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
85 lines (79 loc) · 2.12 KB
/
connector.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package polypheny
import (
bytes "bytes"
driver "database/sql/driver"
net "net"
"sync/atomic"
context "golang.org/x/net/context"
prism "github.com/polypheny/Polypheny-Go-Driver/prism"
)
// Connector implements the driver.Connector interface
type Connector struct {
// TODO: add ConnectionProperties support
address string
username string
password string
}
// Connect connects to a Polypheny server and returns PolyphenyConn which implements driver.Conn
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
// Step 1, dial to the server
// TODO: does polypheny also use other networks?
var d net.Dialer
netConn, err := d.DialContext(ctx, "tcp", c.address)
if err != nil {
return nil, err
}
conn := PolyphenyConn{
address: c.address,
username: c.username,
netConn: netConn,
isConnected: atomic.Int32{},
}
conn.isConnected.Store(statusServerConnected)
// Step 2, exchange transport version
recvVersion, err := conn.recv(1)
if err != nil {
return nil, err
}
sendVersion := []byte(transportVersion)
err = conn.send(sendVersion, 1)
if err != nil {
return nil, err
}
if !bytes.Equal(recvVersion, sendVersion) {
return nil, &ClientError{
message: "The transport version is incompatible with server",
}
}
// Step 3, send prism connection request
// // auto commit is disabled
isAutoCommit := false
request := prism.Request{
Type: &prism.Request_ConnectionRequest{
ConnectionRequest: &prism.ConnectionRequest{
MajorApiVersion: majorApiVersion,
MinorApiVersion: minorApiVersion,
Username: &conn.username,
Password: &c.password,
ConnectionProperties: &prism.ConnectionProperties{
IsAutoCommit: &isAutoCommit,
},
},
},
}
response, err := conn.helperSendAndRecv(&request)
if !response.GetConnectionResponse().GetIsCompatible() {
return nil, &ClientError{
message: "The API version is incompatible with server",
}
}
if err != nil {
return nil, err
}
conn.isConnected.Store(statusPolyphenyConnected)
return &conn, nil
}
// Driver retuens a PolyphenyDriver
func (c *Connector) Driver() driver.Driver {
return PolyphenyDriver{}
}