-
Notifications
You must be signed in to change notification settings - Fork 2
/
rows.go
195 lines (177 loc) · 4.17 KB
/
rows.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package trino
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"time"
)
// driverRows implements driver.Rows
type driverRows struct {
ctx context.Context
stmt *driverStmt
nextURI string
err error
rowindex int
columns []string
coltype []*typeConverter
data []queryData
}
var _ driver.Rows = &driverRows{}
func (qr *driverRows) Close() error {
if qr.nextURI != "" {
hs := make(http.Header)
hs.Add(vhs[v]["user"], qr.stmt.user)
req, err := qr.stmt.conn.newRequest("DELETE", qr.nextURI, nil, hs)
if err != nil {
return err
}
ctx, cancel := context.WithDeadline(
context.Background(),
time.Now().Add(DefaultCancelQueryTimeout),
)
defer cancel()
resp, err := qr.stmt.conn.roundTrip(ctx, req)
if err != nil {
qferr, ok := err.(*ErrQueryFailed)
if ok && qferr.StatusCode == http.StatusNoContent {
qr.nextURI = ""
return nil
}
return err
}
resp.Body.Close()
}
return qr.err
}
func (qr *driverRows) Columns() []string {
if qr.err != nil {
return []string{}
}
if qr.columns == nil {
if err := qr.fetch(false); err != nil {
qr.err = err
return []string{}
}
}
return qr.columns
}
var _coltypeLengthSuffix = regexp.MustCompile(`\(\d+\)$`)
func (qr *driverRows) ColumnTypeDatabaseTypeName(index int) string {
name := qr.coltype[index].typeName
if m := _coltypeLengthSuffix.FindStringSubmatch(name); m != nil {
name = name[0 : len(name)-len(m[0])]
}
return name
}
func (qr *driverRows) Next(dest []driver.Value) error {
if qr.err != nil {
return qr.err
}
if qr.columns == nil || qr.rowindex >= len(qr.data) {
if qr.nextURI == "" {
qr.err = io.EOF
return qr.err
}
if err := qr.fetch(true); err != nil {
qr.err = err
return err
}
}
if len(qr.coltype) == 0 {
qr.err = sql.ErrNoRows
return qr.err
}
for i, v := range qr.coltype {
vv, err := v.ConvertValue(qr.data[qr.rowindex][i])
if err != nil {
qr.err = err
return err
}
dest[i] = vv
}
qr.rowindex++
return nil
}
type queryResponse struct {
ID string `json:"id"`
InfoURI string `json:"infoUri"`
PartialCancelURI string `json:"partialCancelUri"`
NextURI string `json:"nextUri"`
Columns []queryColumn `json:"columns"`
Data []queryData `json:"data"`
Stats stmtStats `json:"stats"`
Error stmtError `json:"error"`
}
type queryColumn struct {
Name string `json:"name"`
Type string `json:"type"`
TypeSignature typeSignature `json:"typeSignature"`
}
type queryData []interface{}
type typeSignature struct {
RawType string `json:"rawType"`
TypeArguments []interface{} `json:"typeArguments"`
LiteralArguments []interface{} `json:"literalArguments"`
}
func (qr *driverRows) fetch(allowEOF bool) error {
hs := make(http.Header)
hs.Add(vhs[v]["user"], qr.stmt.user)
req, err := qr.stmt.conn.newRequest("GET", qr.nextURI, nil, hs)
if err != nil {
return err
}
resp, err := qr.stmt.conn.roundTrip(qr.ctx, req)
if err != nil {
return err
}
defer resp.Body.Close()
var qresp queryResponse
d := json.NewDecoder(resp.Body)
d.UseNumber()
err = d.Decode(&qresp)
if err != nil {
return fmt.Errorf("trino: %v", err)
}
err = handleResponseError(resp.StatusCode, qresp.Error)
if err != nil {
return err
}
qr.rowindex = 0
qr.data = qresp.Data
qr.nextURI = qresp.NextURI
if qr.stmt.callback != nil {
qr.stmt.callback.OnUpdated(QueryInfo{
Id: qresp.ID,
QueryStats: qresp.Stats,
Cancel: qr.Close,
})
}
if len(qr.data) == 0 {
if qr.nextURI != "" {
return qr.fetch(allowEOF)
}
if allowEOF {
return io.EOF
}
} else {
// 有数据之后不能忽略 next uri,数据可能是分段传输的
// qr.nextURI = ""
}
if qr.columns == nil && len(qresp.Columns) > 0 {
qr.initColumns(&qresp)
}
return nil
}
func (qr *driverRows) initColumns(qresp *queryResponse) {
qr.columns = make([]string, len(qresp.Columns))
qr.coltype = make([]*typeConverter, len(qresp.Columns))
for i, col := range qresp.Columns {
qr.columns[i] = col.Name
qr.coltype[i] = newTypeConverter(col.Type)
}
}