forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rows_api.go
127 lines (105 loc) · 2.56 KB
/
rows_api.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
package athena
import (
"database/sql/driver"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/aws/aws-sdk-go/service/athena/athenaiface"
)
type rowsAPI struct {
athena athenaiface.AthenaAPI
queryID string
resultMode ResultMode
// use only api mode
done bool
skipHeaderRow bool
out *athena.GetQueryResultsOutput
}
func newRowsAPI(cfg rowsConfig) (*rowsAPI, error) {
r := &rowsAPI{
athena: cfg.Athena,
queryID: cfg.QueryID,
skipHeaderRow: cfg.SkipHeader,
resultMode: cfg.ResultMode,
}
err := r.init(cfg)
return r, err
}
func (r *rowsAPI) init(cfg rowsConfig) error {
shouldContinue, err := r.fetchNextPage(nil)
if err != nil {
return err
}
r.done = !shouldContinue
return nil
}
func (r *rowsAPI) fetchNextPage(token *string) (bool, error) {
var err error
r.out, err = r.athena.GetQueryResults(&athena.GetQueryResultsInput{
QueryExecutionId: aws.String(r.queryID),
NextToken: token,
})
if err != nil {
return false, err
}
var rowOffset = 0
// First row of the first page contains header if the query is not DDL.
// These are also available in *athena.Row.ResultSetMetadata.
if r.skipHeaderRow {
rowOffset = 1
r.skipHeaderRow = false
}
if len(r.out.ResultSet.Rows) < rowOffset+1 {
return false, nil
}
r.out.ResultSet.Rows = r.out.ResultSet.Rows[rowOffset:]
return true, nil
}
func (r *rowsAPI) nextAPI(dest []driver.Value) error {
if r.done {
return io.EOF
}
// If nothing left to iterate...
if len(r.out.ResultSet.Rows) == 0 {
// And if nothing more to paginate...
if r.out.NextToken == nil || *r.out.NextToken == "" {
return io.EOF
}
cont, err := r.fetchNextPage(r.out.NextToken)
if err != nil {
return err
}
if !cont {
return io.EOF
}
}
// Shift to next row
cur := r.out.ResultSet.Rows[0]
columns := r.out.ResultSet.ResultSetMetadata.ColumnInfo
if err := convertRow(columns, cur.Data, dest); err != nil {
return err
}
r.out.ResultSet.Rows = r.out.ResultSet.Rows[1:]
return nil
}
func (r *rowsAPI) Columns() []string {
var columns []string
for _, colInfo := range r.out.ResultSet.ResultSetMetadata.ColumnInfo {
columns = append(columns, *colInfo.Name)
}
return columns
}
func (r *rowsAPI) ColumnTypeDatabaseTypeName(index int) string {
colInfo := r.out.ResultSet.ResultSetMetadata.ColumnInfo[index]
if colInfo.Type != nil {
return *colInfo.Type
}
return ""
}
func (r *rowsAPI) Next(dest []driver.Value) error {
return r.nextAPI(dest)
}
func (r *rowsAPI) Close() error {
r.done = true
return nil
}