-
Notifications
You must be signed in to change notification settings - Fork 3
/
driver.go
109 lines (94 loc) · 2.57 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2022 Exasol AG. All rights reserved.
//
// This Source Code Form is subject to the terms of the MIT License,
// see https://github.com/exasol/exasol-driver-go/blob/main/LICENSE
// Package exasol implements a driver for the Exasol database.
package exasol
import (
"context"
"database/sql"
"database/sql/driver"
"github.com/exasol/exasol-driver-go/internal/config"
"github.com/exasol/exasol-driver-go/pkg/connection"
"github.com/exasol/exasol-driver-go/pkg/dsn"
)
func init() {
sql.Register("exasol", &ExasolDriver{})
}
// ExasolDriver is an implementation of the [database/sql/driver.Driver] interface.
type ExasolDriver struct{}
// Open implements the driver.Driver interface.
func (e ExasolDriver) Open(input string) (driver.Conn, error) {
dsnConfig, err := dsn.ParseDSN(input)
if err != nil {
return nil, err
}
c := &Connector{
Config: dsn.ToInternalConfig(dsnConfig),
}
return c.Connect(context.Background())
}
// OpenConnector implements the driver.DriverContext interface.
func (e ExasolDriver) OpenConnector(input string) (driver.Connector, error) {
dsnConfig, err := dsn.ParseDSN(input)
if err != nil {
return nil, err
}
return &Connector{
Config: dsn.ToInternalConfig(dsnConfig),
}, nil
}
// Connector implements the [database/sql/driver.Connector] interface.
type Connector struct {
Config *config.Config
}
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
conn := &connection.Connection{
Config: c.Config,
Ctx: ctx,
IsClosed: true,
}
err := conn.Connect()
if err != nil {
return nil, err
}
err = conn.Login(ctx)
if err != nil {
return nil, err
}
return conn, err
}
func (c Connector) Driver() driver.Driver {
return &ExasolDriver{}
}
// NewConfig creates a new builder with username/password authentication.
func NewConfig(user, password string) *dsn.DSNConfigBuilder {
return &dsn.DSNConfigBuilder{
Config: &dsn.DSNConfig{
Host: "localhost",
Port: 8563,
User: user,
Password: password,
},
}
}
// NewConfigWithAccessToken creates a new builder with access token authentication.
func NewConfigWithAccessToken(token string) *dsn.DSNConfigBuilder {
return &dsn.DSNConfigBuilder{
Config: &dsn.DSNConfig{
Host: "localhost",
Port: 8563,
AccessToken: token,
},
}
}
// NewConfigWithRefreshToken creates a new builder with refresh token authentication.
func NewConfigWithRefreshToken(token string) *dsn.DSNConfigBuilder {
return &dsn.DSNConfigBuilder{
Config: &dsn.DSNConfig{
Host: "localhost",
Port: 8563,
RefreshToken: token,
},
}
}