forked from sql-machine-learning/gohive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
70 lines (56 loc) · 1.6 KB
/
connection.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
package gohive
import (
"context"
"database/sql/driver"
"fmt"
"sqlflow.org/gohive/hiveserver2"
)
// Options for opened Hive sessions.
type Options struct {
PollIntervalSeconds int64
BatchSize int64
}
type Connection struct {
thrift *hiveserver2.TCLIServiceClient
session *hiveserver2.TSessionHandle
options Options
}
func (c *Connection) Begin() (driver.Tx, error) {
return nil, nil
}
func (c *Connection) Prepare(query string) (driver.Stmt, error) {
return nil, nil
}
func (c *Connection) isOpen() bool {
return c.session != nil
}
func (c *Connection) Close() error {
if c.isOpen() {
closeReq := hiveserver2.NewTCloseSessionReq()
closeReq.SessionHandle = c.session
resp, err := c.thrift.CloseSession(closeReq)
if err != nil {
return fmt.Errorf("Error closing session %s %s", resp, err)
}
c.session = nil
}
return nil
}
func (c *Connection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
executeReq := hiveserver2.NewTExecuteStatementReq()
executeReq.SessionHandle = c.session
executeReq.Statement = query
resp, err := c.thrift.ExecuteStatement(executeReq)
if err != nil {
return nil, fmt.Errorf("Error in ExecuteStatement: %+v, %v", resp, err)
}
if !isSuccessStatus(resp.Status) {
return nil, fmt.Errorf("Error from server: %s", resp.Status.String())
}
return newRows(c.thrift, resp.OperationHandle, c.options), nil
}
func isSuccessStatus(p *hiveserver2.TStatus) bool {
status := p.GetStatusCode()
return status == hiveserver2.TStatusCode_SUCCESS_STATUS ||
status == hiveserver2.TStatusCode_SUCCESS_WITH_INFO_STATUS
}