@@ -66,10 +66,18 @@ func (c *Connection) Close() error {
66
66
return nil
67
67
}
68
68
69
- func (c * Connection ) QueryContext (ctx context.Context , query string , args []driver.NamedValue ) (driver.Rows , error ) {
69
+ func removeLastSemicolon (s string ) string {
70
+ n := len (s )
71
+ if n > 0 && s [n - 1 ] == ';' {
72
+ return s [0 : n - 1 ]
73
+ }
74
+ return s
75
+ }
76
+
77
+ func (c * Connection ) execute (ctx context.Context , query string , args []driver.NamedValue ) (* hiveserver2.TExecuteStatementResp , error ) {
70
78
executeReq := hiveserver2 .NewTExecuteStatementReq ()
71
79
executeReq .SessionHandle = c .session
72
- executeReq .Statement = query
80
+ executeReq .Statement = removeLastSemicolon ( query )
73
81
74
82
resp , err := c .thrift .ExecuteStatement (executeReq )
75
83
if err != nil {
@@ -79,22 +87,21 @@ func (c *Connection) QueryContext(ctx context.Context, query string, args []driv
79
87
if ! isSuccessStatus (resp .Status ) {
80
88
return nil , fmt .Errorf ("Error from server: %s" , resp .Status .String ())
81
89
}
90
+ return resp , nil
91
+ }
82
92
93
+ func (c * Connection ) QueryContext (ctx context.Context , query string , args []driver.NamedValue ) (driver.Rows , error ) {
94
+ resp , err := c .execute (ctx , query , args )
95
+ if err != nil {
96
+ return nil , err
97
+ }
83
98
return newRows (c .thrift , resp .OperationHandle , c .options ), nil
84
99
}
85
100
86
101
func (c * Connection ) ExecContext (ctx context.Context , query string , args []driver.NamedValue ) (driver.Result , error ) {
87
- executeReq := hiveserver2 .NewTExecuteStatementReq ()
88
- executeReq .SessionHandle = c .session
89
- executeReq .Statement = query
90
-
91
- resp , err := c .thrift .ExecuteStatement (executeReq )
102
+ resp , err := c .execute (ctx , query , args )
92
103
if err != nil {
93
- return nil , fmt .Errorf ("Error in ExecuteStatement: %+v, %v" , resp , err )
94
- }
95
-
96
- if ! isSuccessStatus (resp .Status ) {
97
- return nil , fmt .Errorf ("Error from server: %s" , resp .Status .String ())
104
+ return nil , err
98
105
}
99
106
return newHiveResult (resp .OperationHandle ), nil
100
107
}
0 commit comments