-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.go
77 lines (64 loc) · 1.65 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
package hive
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/emersion/go-sasl"
"github.com/taozle/go-hive-driver/thriftlib"
"net/url"
)
var (
ErrNoPassword = errors.New("hive: passwd is required")
)
func init() {
sql.Register("hive", &Driver{})
}
type Driver struct {
}
//
// connString format:
// hive://user@host:port?batch=100
func (*Driver) Open(connString string) (driver.Conn, error) {
info, err := url.Parse(connString)
if err != nil {
return nil, err
}
var transport thrift.TTransport
transport, err = thrift.NewTSocket(info.Host)
if err != nil {
return nil, err
}
config := parseConfigFromQuery(info.Query())
switch config.auth {
case "sasl":
passwd, ok := info.User.Password()
if !ok {
return nil, ErrNoPassword
}
transport = NewTSaslClientTransport(transport, func() sasl.Client {
return sasl.NewPlainClient("", info.User.Username(), passwd)
})
default:
}
if err := transport.Open(); err != nil {
return nil, err
}
protocol := thrift.NewTBinaryProtocolTransport(transport)
tclient := thrift.NewTStandardClient(protocol, protocol)
service := thriftlib.NewTCLIServiceClient(tclient)
req := thriftlib.NewTOpenSessionReq()
req.ClientProtocol = thriftlib.TProtocolVersion_HIVE_CLI_SERVICE_PROTOCOL_V6
if name := info.User.Username(); name != "" {
req.Username = &name
}
if password, ok := info.User.Password(); ok {
req.Password = &password
}
session, err := service.OpenSession(context.Background(), req)
if err != nil {
return nil, err
}
return &Connection{client: service, session: session.SessionHandle, config: config}, nil
}