-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.go
97 lines (77 loc) · 2.16 KB
/
conn.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
package hive
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"github.com/taozle/go-hive-driver/thriftlib"
)
var (
ErrUnsupportedPreparedStmt = errors.New("hive: hive doesn't support prepared stmt")
)
type Connection struct {
client *thriftlib.TCLIServiceClient
session *thriftlib.TSessionHandle
config *config
}
func (*Connection) Prepare(query string) (driver.Stmt, error) {
panic("hive: doesn't support prepared statements")
}
func (*Connection) Begin() (driver.Tx, error) {
panic("hive: doesn't support transaction")
}
func (c *Connection) Close() error {
req := thriftlib.NewTCloseSessionReq()
req.SessionHandle = c.session
resp, err := c.client.CloseSession(context.Background(), req)
if err != nil {
return err
}
if err := formatResponseErr(resp.GetStatus()); err != nil {
return err
}
return nil
}
func (c *Connection) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
if len(args) > 0 {
return nil, ErrUnsupportedPreparedStmt
}
req := thriftlib.NewTExecuteStatementReq()
req.SessionHandle = c.session
req.Statement = query
ret, err := c.client.ExecuteStatement(ctx, req)
if err != nil {
return nil, err
}
if err := formatResponseErr(ret.GetStatus()); err != nil {
return nil, err
}
return nil, nil
}
func (c *Connection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
if len(args) > 0 {
return nil, ErrUnsupportedPreparedStmt
}
req := thriftlib.NewTExecuteStatementReq()
req.SessionHandle = c.session
req.Statement = query
ret, err := c.client.ExecuteStatement(ctx, req)
if err != nil {
return nil, err
}
if err := formatResponseErr(ret.GetStatus()); err != nil {
return nil, err
}
rs := newRowSet(c.client, ret.GetOperationHandle(), c.config)
if err := rs.Bootstrap(); err != nil {
return nil, err
}
return rs, nil
}
func formatResponseErr(status *thriftlib.TStatus) error {
code := status.GetStatusCode()
if code == thriftlib.TStatusCode_SUCCESS_STATUS || code == thriftlib.TStatusCode_SUCCESS_WITH_INFO_STATUS {
return nil
}
return fmt.Errorf("hive: query failed. errmsg=%s", status.GetErrorMessage())
}