Skip to content

Commit 35ae3e3

Browse files
committed
Fixed up comments
Signed-off-by: nithinkdb <[email protected]>
1 parent e3cd62e commit 35ae3e3

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

connection.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,28 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
117117
exStmtResp, opStatusResp, err := c.runQuery(ctx, query, args)
118118

119119
if exStmtResp != nil && exStmtResp.OperationHandle != nil {
120-
req := cli_service.TGetResultSetMetadataReq{
121-
OperationHandle: exStmtResp.OperationHandle,
122-
}
123-
resp, err2 := c.client.GetResultSetMetadata(ctx, &req)
124-
if err2 != nil {
125-
return nil, errors.New("Error performing staging operation")
120+
var isStagingOperation bool
121+
if exStmtResp.DirectResults != nil && exStmtResp.DirectResults.ResultSetMetadata != nil && exStmtResp.DirectResults.ResultSetMetadata.IsStagingOperation != nil {
122+
isStagingOperation = *exStmtResp.DirectResults.ResultSetMetadata.IsStagingOperation
123+
} else {
124+
req := cli_service.TGetResultSetMetadataReq{
125+
OperationHandle: exStmtResp.OperationHandle,
126+
}
127+
resp, err := c.client.GetResultSetMetadata(ctx, &req)
128+
if err != nil {
129+
return nil, dbsqlerrint.NewDriverError(ctx, "Error performing staging operation", err)
130+
}
131+
isStagingOperation = *resp.IsStagingOperation
126132
}
127-
if *resp.IsStagingOperation {
133+
if isStagingOperation {
128134
if len(driverctx.StagingPathsFromContext(ctx)) != 0 {
129135
row, err := rows.NewRows(c.id, corrId, exStmtResp.OperationHandle, c.client, c.cfg, exStmtResp.DirectResults)
130-
if err == nil {
131-
return nil, dbsqlerrint.NewDriverError(ctx, "Error reading row.", errors.New("Error reading row."))
136+
if err != nil {
137+
return nil, dbsqlerrint.NewDriverError(ctx, "Error reading row.", err)
132138
}
133139
return c.ExecStagingOperation(ctx, row)
134140
} else {
135-
return nil, dbsqlerrint.NewDriverError(ctx, "Staging ctx must be provided.", errors.New("Staging ctx must be provided."))
141+
return nil, dbsqlerrint.NewDriverError(ctx, "Staging ctx must be provided.", nil)
136142
}
137143
}
138144

@@ -168,9 +174,9 @@ func Succeeded(response *http.Response) bool {
168174
return false
169175
}
170176

171-
func (c *conn) HandleStagingPut(presignedUrl string, headers map[string]string, localFile string) (driver.Result, error) {
177+
func (c *conn) HandleStagingPut(ctx context.Context, presignedUrl string, headers map[string]string, localFile string) (driver.Result, error) {
172178
if localFile == "" {
173-
return nil, fmt.Errorf("cannot perform PUT without specifying a local_file")
179+
return nil, dbsqlerrint.NewDriverError(ctx, "cannot perform PUT without specifying a local_file", nil)
174180
}
175181
client := &http.Client{}
176182

@@ -192,13 +198,13 @@ func (c *conn) HandleStagingPut(presignedUrl string, headers map[string]string,
192198
content, err := io.ReadAll(res.Body)
193199

194200
if err != nil || !Succeeded(res) {
195-
return nil, fmt.Errorf("staging operation over HTTP was unsuccessful: %d-%s", res.StatusCode, content)
201+
return nil, dbsqlerrint.NewDriverError(ctx, fmt.Sprintf("staging operation over HTTP was unsuccessful: %d-%s, nil", res.StatusCode, content), nil)
196202
}
197203
return driver.ResultNoRows, nil
198204

199205
}
200206

201-
func (c *conn) HandleStagingGet(presignedUrl string, headers map[string]string, localFile string) (driver.Result, error) {
207+
func (c *conn) HandleStagingGet(ctx context.Context, presignedUrl string, headers map[string]string, localFile string) (driver.Result, error) {
202208
if localFile == "" {
203209
return nil, fmt.Errorf("cannot perform GET without specifying a local_file")
204210
}
@@ -216,18 +222,18 @@ func (c *conn) HandleStagingGet(presignedUrl string, headers map[string]string,
216222
content, err := io.ReadAll(res.Body)
217223

218224
if err != nil || !Succeeded(res) {
219-
return nil, fmt.Errorf("staging operation over HTTP was unsuccessful: %d-%s", res.StatusCode, content)
225+
return nil, dbsqlerrint.NewDriverError(ctx, fmt.Sprintf("staging operation over HTTP was unsuccessful: %d-%s, nil", res.StatusCode, content), nil)
220226
}
221227

222-
err = os.WriteFile(localFile, content, 0644)
228+
err = os.WriteFile(localFile, content, 0644) //nolint:gosec
223229
if err != nil {
224230
return nil, err
225231
}
226232
return driver.ResultNoRows, nil
227233

228234
}
229235

230-
func (c *conn) HandleStagingDelete(presignedUrl string, headers map[string]string) (driver.Result, error) {
236+
func (c *conn) HandleStagingDelete(ctx context.Context, presignedUrl string, headers map[string]string) (driver.Result, error) {
231237
client := &http.Client{}
232238
req, _ := http.NewRequest("DELETE", presignedUrl, nil)
233239
for k, v := range headers {
@@ -241,7 +247,7 @@ func (c *conn) HandleStagingDelete(presignedUrl string, headers map[string]strin
241247
content, err := io.ReadAll(res.Body)
242248

243249
if err != nil || !Succeeded(res) {
244-
return nil, fmt.Errorf("staging operation over HTTP was unsuccessful: %d-%s", res.StatusCode, content)
250+
return nil, dbsqlerrint.NewDriverError(ctx, fmt.Sprintf("staging operation over HTTP was unsuccessful: %d-%s, nil", res.StatusCode, content), nil)
245251
}
246252

247253
return driver.ResultNoRows, nil
@@ -289,23 +295,21 @@ func (c *conn) ExecStagingOperation(
289295
switch operation {
290296
case "PUT":
291297
if localPathIsAllowed(ctx, localFile) {
292-
c.HandleStagingPut(presignedUrl, headers, localFile)
298+
return c.HandleStagingPut(ctx, presignedUrl, headers, localFile)
293299
} else {
294300
return nil, fmt.Errorf("local file operations are restricted to paths within the configured staging_allowed_local_path")
295301
}
296302
case "GET":
297303
if localPathIsAllowed(ctx, localFile) {
298-
c.HandleStagingGet(presignedUrl, headers, localFile)
304+
return c.HandleStagingGet(ctx, presignedUrl, headers, localFile)
299305
} else {
300-
return nil, fmt.Errorf("local file operations are restricted to paths within the configured staging_allowed_local_path")
306+
return nil, dbsqlerrint.NewDriverError(ctx, "local file operations are restricted to paths within the configured staging_allowed_local_path", nil)
301307
}
302308
case "DELETE":
303-
c.HandleStagingDelete(presignedUrl, headers)
309+
return c.HandleStagingDelete(ctx, presignedUrl, headers)
304310
default:
305-
return nil, fmt.Errorf("operation %s is not supported. Supported operations are GET, PUT, and REMOVE", operation)
311+
return nil, dbsqlerrint.NewDriverError(ctx, fmt.Sprintf("operation %s is not supported. Supported operations are GET, PUT, and REMOVE", operation), nil)
306312
}
307-
308-
return driver.ResultNoRows, nil
309313
}
310314

311315
// QueryContext executes a query that may return rows, such as a

0 commit comments

Comments
 (0)