-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver.go
85 lines (71 loc) · 2.38 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
package fireboltgosdk
import (
"context"
"database/sql"
"database/sql/driver"
)
type FireboltDriver struct {
engineUrl string
cachedParams map[string]string
client Client
lastUsedDsn string
}
// Open parses the dsn string, and if correct tries to establish a connection
func (d *FireboltDriver) Open(dsn string) (driver.Conn, error) {
conn, err := d.OpenConnector(dsn)
if err != nil {
return nil, err
}
return conn.Connect(context.Background())
}
func copyMap(original map[string]string) map[string]string {
newMap := make(map[string]string)
for k, v := range original {
newMap[k] = v
}
return newMap
}
func (d *FireboltDriver) OpenConnector(dsn string) (driver.Connector, error) {
infolog.Println("Opening firebolt connector")
if d.lastUsedDsn != dsn || d.lastUsedDsn == "" {
d.lastUsedDsn = "" //nolint
infolog.Println("constructing new client")
// parsing dsn string to get configuration settings
settings, err := ParseDSNString(dsn)
if err != nil {
return nil, ConstructNestedError("error during parsing a dsn", err)
}
// authenticating and getting access token
infolog.Println("dsn parsed correctly, trying to authenticate")
d.client, err = Authenticate(settings, GetHostNameURL())
if err != nil {
return nil, ConstructNestedError("error during authentication", err)
}
d.engineUrl, d.cachedParams, err = d.client.GetConnectionParameters(context.TODO(), settings.engineName, settings.database)
if err != nil {
return nil, ConstructNestedError("error during getting engine url", err)
}
d.lastUsedDsn = dsn //nolint
}
return &FireboltConnector{d.engineUrl, d.client, copyMap(d.cachedParams), d}, nil
}
// FireboltConnector is an intermediate type between a Connection and a Driver which stores session data
type FireboltConnector struct {
engineUrl string
client Client
cachedParameters map[string]string
driver *FireboltDriver
}
// Connect returns a connection to the database
func (c *FireboltConnector) Connect(ctx context.Context) (driver.Conn, error) {
infolog.Printf("firebolt connection is created")
return &fireboltConnection{c.client, c.engineUrl, c.cachedParameters, c}, nil
}
// Driver returns the underlying driver of the Connector
func (c *FireboltConnector) Driver() driver.Driver {
return c.driver
}
// init registers a firebolt connector
func init() {
sql.Register("firebolt", &FireboltDriver{})
}