forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
122 lines (100 loc) · 2.39 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
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 rows struct {
athena athenaiface.AthenaAPI
queryID string
done bool
skipHeaderRow bool
out *athena.GetQueryResultsOutput
}
type rowsConfig struct {
Athena athenaiface.AthenaAPI
QueryID string
SkipHeader bool
}
func newRows(cfg rowsConfig) (*rows, error) {
r := rows{
athena: cfg.Athena,
queryID: cfg.QueryID,
skipHeaderRow: cfg.SkipHeader,
}
shouldContinue, err := r.fetchNextPage(nil)
if err != nil {
return nil, err
}
r.done = !shouldContinue
return &r, nil
}
func (r *rows) Columns() []string {
var columns []string
for _, colInfo := range r.out.ResultSet.ResultSetMetadata.ColumnInfo {
columns = append(columns, *colInfo.Name)
}
return columns
}
func (r *rows) ColumnTypeDatabaseTypeName(index int) string {
colInfo := r.out.ResultSet.ResultSetMetadata.ColumnInfo[index]
if colInfo.Type != nil {
return *colInfo.Type
}
return ""
}
func (r *rows) Next(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 *rows) 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 *rows) Close() error {
r.done = true
return nil
}