Skip to content

Commit 58edb8f

Browse files
authored
Impl stmt (#36)
* add Prepare function for connection * reduce exposure * stmt not implemented
1 parent f2dade3 commit 58edb8f

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

connection.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,36 @@ import (
88
"sqlflow.org/gohive/hiveserver2"
99
)
1010

11-
// Options for opened Hive sessions.
12-
type Options struct {
11+
// hiveOptions for opened Hive sessions.
12+
type hiveOptions struct {
1313
PollIntervalSeconds int64
1414
BatchSize int64
1515
}
1616

17-
type Connection struct {
17+
type hiveConnection struct {
1818
thrift *hiveserver2.TCLIServiceClient
1919
session *hiveserver2.TSessionHandle
20-
options Options
20+
options hiveOptions
2121
}
2222

23-
func (c *Connection) Begin() (driver.Tx, error) {
23+
func (c *hiveConnection) Begin() (driver.Tx, error) {
2424
return nil, nil
2525
}
2626

27-
func (c *Connection) Prepare(query string) (driver.Stmt, error) {
28-
return nil, nil
27+
func (c *hiveConnection) Prepare(qry string) (driver.Stmt, error) {
28+
if !c.isOpen() {
29+
return nil, fmt.Errorf("driver: bad connection")
30+
}
31+
return &hiveStmt{hc: c, query: qry}, nil
2932
}
3033

31-
func (c *Connection) isOpen() bool {
34+
func (c *hiveConnection) isOpen() bool {
3235
return c.session != nil
3336
}
3437

3538
// As hiveserver2 thrift api does not provide Ping method,
3639
// we use GetInfo instead to check the health of hiveserver2.
37-
func (c *Connection) Ping(ctx context.Context) (err error) {
40+
func (c *hiveConnection) Ping(ctx context.Context) (err error) {
3841
getInfoReq := hiveserver2.NewTGetInfoReq()
3942
getInfoReq.SessionHandle = c.session
4043
getInfoReq.InfoType = hiveserver2.TGetInfoType_CLI_SERVER_NAME
@@ -52,7 +55,7 @@ func (c *Connection) Ping(ctx context.Context) (err error) {
5255
return nil
5356
}
5457

55-
func (c *Connection) Close() error {
58+
func (c *hiveConnection) Close() error {
5659
if c.isOpen() {
5760
closeReq := hiveserver2.NewTCloseSessionReq()
5861
closeReq.SessionHandle = c.session
@@ -74,7 +77,7 @@ func removeLastSemicolon(s string) string {
7477
return s
7578
}
7679

77-
func (c *Connection) execute(ctx context.Context, query string, args []driver.NamedValue) (*hiveserver2.TExecuteStatementResp, error) {
80+
func (c *hiveConnection) execute(ctx context.Context, query string, args []driver.NamedValue) (*hiveserver2.TExecuteStatementResp, error) {
7881
executeReq := hiveserver2.NewTExecuteStatementReq()
7982
executeReq.SessionHandle = c.session
8083
executeReq.Statement = removeLastSemicolon(query)
@@ -90,15 +93,15 @@ func (c *Connection) execute(ctx context.Context, query string, args []driver.Na
9093
return resp, nil
9194
}
9295

93-
func (c *Connection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
96+
func (c *hiveConnection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
9497
resp, err := c.execute(ctx, query, args)
9598
if err != nil {
9699
return nil, err
97100
}
98101
return newRows(c.thrift, resp.OperationHandle, c.options), nil
99102
}
100103

101-
func (c *Connection) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
104+
func (c *hiveConnection) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
102105
resp, err := c.execute(ctx, query, args)
103106
if err != nil {
104107
return nil, err

driver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func (d drv) Open(dsn string) (driver.Conn, error) {
4949
return nil, err
5050
}
5151

52-
options := Options{PollIntervalSeconds: 5, BatchSize: 100000}
53-
conn := &Connection{client, session.SessionHandle, options}
52+
options := hiveOptions{PollIntervalSeconds: 5, BatchSize: 100000}
53+
conn := &hiveConnection{client, session.SessionHandle, options}
5454
return conn, nil
5555
}
5656

rows.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
type rowSet struct {
1616
thrift *hiveserver2.TCLIServiceClient
1717
operation *hiveserver2.TOperationHandle
18-
options Options
18+
options hiveOptions
1919

2020
columns []*hiveserver2.TColumnDesc
2121
columnStrs []string
@@ -25,10 +25,10 @@ type rowSet struct {
2525

2626
// resultSet is column-oriented storage format
2727
resultSet [][]interface{}
28-
status *Status
28+
status *hiveStatus
2929
}
3030

31-
type Status struct {
31+
type hiveStatus struct {
3232
state *hiveserver2.TOperationState
3333
}
3434

@@ -151,7 +151,7 @@ func (r *rowSet) poll() error {
151151
if resp.OperationState == nil {
152152
return errors.New("No error from GetStatus, but nil status!")
153153
}
154-
r.status = &Status{resp.OperationState}
154+
r.status = &hiveStatus{resp.OperationState}
155155
return nil
156156
}
157157

@@ -235,7 +235,7 @@ func convertColumn(col *hiveserver2.TColumn) (colValues interface{}, length int)
235235
}
236236
}
237237

238-
func (s Status) isStopped() bool {
238+
func (s hiveStatus) isStopped() bool {
239239
if s.state == nil {
240240
return false
241241
}
@@ -249,13 +249,13 @@ func (s Status) isStopped() bool {
249249
return false
250250
}
251251

252-
func (s Status) isFinished() bool {
252+
func (s hiveStatus) isFinished() bool {
253253
return s.state != nil && *s.state == hiveserver2.TOperationState_FINISHED_STATE
254254
}
255255

256256
func newRows(thrift *hiveserver2.TCLIServiceClient,
257257
operation *hiveserver2.TOperationHandle,
258-
options Options) driver.Rows {
258+
options hiveOptions) driver.Rows {
259259
return &rowSet{thrift, operation, options, nil, nil,
260260
0, nil, nil, nil}
261261
}

statement.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package gohive
2+
3+
import (
4+
"database/sql/driver"
5+
)
6+
7+
type hiveStmt struct {
8+
hc *hiveConnection
9+
query string
10+
}
11+
12+
func (stmt *hiveStmt) Close() error {
13+
panic("not implemented")
14+
}
15+
16+
func (stmt *hiveStmt) NumInput() int {
17+
panic("not implemented")
18+
}
19+
20+
// Exec accepts stmt like: "INSERT INTO `TABLE` (f1, f2) VALUES(1.3, false)"
21+
func (stmt *hiveStmt) Exec(args []driver.Value) (driver.Result, error) {
22+
panic("not implemented")
23+
}
24+
25+
func (stmt *hiveStmt) Query(args []driver.Value) (driver.Rows, error) {
26+
panic("not implemented")
27+
}

0 commit comments

Comments
 (0)