forked from Boostport/avatica
/
driver.go
88 lines (66 loc) · 1.82 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Package avatica provides an Apache Phoenix Query Server/Avatica driver for Go's database/sql package.
Quickstart
Import the database/sql package along with the avatica driver.
import "database/sql"
import _ "github.com/Boostport/avatica"
db, err := sql.Open("avatica", "http://phoenix-query-server:8765")
See https://github.com/Boostport/avatica#usage for more details
*/
package avatica
import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/Boostport/avatica/message"
"github.com/satori/go.uuid"
"golang.org/x/net/context"
)
// Driver is exported to allow it to be used directly.
type Driver struct{}
// Open a Connection to the server.
// See https://github.com/Boostport/avatica#dsn for more information
// on how the DSN is formatted.
func (a *Driver) Open(dsn string) (driver.Conn, error) {
config, err := ParseDSN(dsn)
if err != nil {
return nil, fmt.Errorf("Unable to open connection: %s", err)
}
httpClient := NewHTTPClient(config.endpoint, httpClientAuthConfig{
username: config.avaticaUser,
password: config.avaticaPassword,
authenticationType: config.authentication,
})
connectionId := uuid.NewV4().String()
info := map[string]string{
"AutoCommit": "true",
"Consistency": "8",
}
if config.user != "" {
info["user"] = config.user
}
if config.password != "" {
info["password"] = config.password
}
// Open a connection to the server
req := &message.OpenConnectionRequest{
ConnectionId: connectionId,
Info: info,
}
if config.schema != "" {
req.Info["schema"] = config.schema
}
_, err = httpClient.post(context.Background(), req)
if err != nil {
return nil, err
}
conn := &conn{
connectionId: connectionId,
httpClient: httpClient,
config: config,
}
return conn, nil
}
func init() {
sql.Register("avatica", &Driver{})
}