forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multistatement.go
114 lines (108 loc) · 2.79 KB
/
multistatement.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"database/sql/driver"
"fmt"
"strconv"
"strings"
)
type childResult struct {
id string
typ string
}
func getChildResults(IDs string, types string) []childResult {
if IDs == "" {
return nil
}
queryIDs := strings.Split(IDs, ",")
resultTypes := strings.Split(types, ",")
res := make([]childResult, len(queryIDs))
for i, id := range queryIDs {
res[i] = childResult{id, resultTypes[i]}
}
return res
}
func (sc *snowflakeConn) handleMultiExec(
ctx context.Context,
data execResponseData) (
driver.Result, error) {
if data.ResultIDs == "" {
return nil, (&SnowflakeError{
Number: ErrNoResultIDs,
SQLState: data.SQLState,
Message: errMsgNoResultIDs,
QueryID: data.QueryID,
}).exceptionTelemetry(sc)
}
var updatedRows int64
childResults := getChildResults(data.ResultIDs, data.ResultTypes)
for _, child := range childResults {
resultPath := fmt.Sprintf(urlQueriesResultFmt, child.id)
childData, err := sc.getQueryResultResp(ctx, resultPath)
if err != nil {
logger.Errorf("error: %v", err)
code, err := strconv.Atoi(childData.Code)
if err != nil {
return nil, err
}
if childData != nil {
return nil, (&SnowflakeError{
Number: code,
SQLState: childData.Data.SQLState,
Message: err.Error(),
QueryID: childData.Data.QueryID,
}).exceptionTelemetry(sc)
}
return nil, err
}
if isDml(childData.Data.StatementTypeID) {
count, err := updateRows(childData.Data)
if err != nil {
logger.WithContext(ctx).Errorf("error: %v", err)
if childData != nil {
code, err := strconv.Atoi(childData.Code)
if err != nil {
return nil, err
}
return nil, (&SnowflakeError{
Number: code,
SQLState: childData.Data.SQLState,
Message: err.Error(),
QueryID: childData.Data.QueryID,
}).exceptionTelemetry(sc)
}
return nil, err
}
updatedRows += count
}
}
logger.WithContext(ctx).Infof("number of updated rows: %#v", updatedRows)
return &snowflakeResult{
affectedRows: updatedRows,
insertID: -1,
queryID: sc.QueryID,
}, nil
}
// Fill the corresponding rows and add chunk downloader into the rows when
// iterating across the childResults
func (sc *snowflakeConn) handleMultiQuery(
ctx context.Context,
data execResponseData,
rows *snowflakeRows) error {
if data.ResultIDs == "" {
return (&SnowflakeError{
Number: ErrNoResultIDs,
SQLState: data.SQLState,
Message: errMsgNoResultIDs,
QueryID: data.QueryID,
}).exceptionTelemetry(sc)
}
childResults := getChildResults(data.ResultIDs, data.ResultTypes)
for _, child := range childResults {
if err := sc.rowsForRunningQuery(ctx, child.id, rows); err != nil {
return err
}
}
return nil
}