forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
138 lines (114 loc) · 3.52 KB
/
conn.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
package athena
import (
"context"
"database/sql/driver"
"errors"
"time"
"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 conn struct {
athena athenaiface.AthenaAPI
db string
OutputLocation string
pollFrequency time.Duration
}
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
if len(args) > 0 {
panic("Athena doesn't support prepared statements. Format your own arguments.")
}
rows, err := c.runQuery(ctx, query)
return rows, err
}
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
if len(args) > 0 {
panic("Athena doesn't support prepared statements. Format your own arguments.")
}
_, err := c.runQuery(ctx, query)
return nil, err
}
func (c *conn) runQuery(ctx context.Context, query string) (driver.Rows, error) {
queryID, err := c.startQuery(query)
if err != nil {
return nil, err
}
if err := c.waitOnQuery(ctx, queryID); err != nil {
return nil, err
}
return newRows(rowsConfig{
Athena: c.athena,
QueryID: queryID,
// todo add check for ddl queries to not skip header(#10)
SkipHeader: true,
})
}
// startQuery starts an Athena query and returns its ID.
func (c *conn) startQuery(query string) (string, error) {
resp, err := c.athena.StartQueryExecution(&athena.StartQueryExecutionInput{
QueryString: aws.String(query),
QueryExecutionContext: &athena.QueryExecutionContext{
Database: aws.String(c.db),
},
ResultConfiguration: &athena.ResultConfiguration{
OutputLocation: aws.String(c.OutputLocation),
},
})
if err != nil {
return "", err
}
return *resp.QueryExecutionId, nil
}
// waitOnQuery blocks until a query finishes, returning an error if it failed.
func (c *conn) waitOnQuery(ctx context.Context, queryID string) error {
for {
statusResp, err := c.athena.GetQueryExecutionWithContext(ctx, &athena.GetQueryExecutionInput{
QueryExecutionId: aws.String(queryID),
})
if err != nil {
return err
}
switch *statusResp.QueryExecution.Status.State {
case athena.QueryExecutionStateCancelled:
return context.Canceled
case athena.QueryExecutionStateFailed:
reason := *statusResp.QueryExecution.Status.StateChangeReason
return errors.New(reason)
case athena.QueryExecutionStateSucceeded:
return nil
case athena.QueryExecutionStateQueued:
case athena.QueryExecutionStateRunning:
}
select {
case <-ctx.Done():
c.athena.StopQueryExecution(&athena.StopQueryExecutionInput{
QueryExecutionId: aws.String(queryID),
})
return ctx.Err()
case <-time.After(c.pollFrequency):
continue
}
}
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
panic("Athena doesn't support prepared statements")
}
func (c *conn) Begin() (driver.Tx, error) {
panic("Athena doesn't support transactions")
}
func (c *conn) Close() error {
return nil
}
var _ driver.QueryerContext = (*conn)(nil)
var _ driver.ExecerContext = (*conn)(nil)
// HACK(tejasmanohar): database/sql calls Prepare() if your driver doesn't implement
// Queryer. Regardless, db.Query/Exec* calls Query/Exec-Context so I've filed a bug--
// https://github.com/golang/go/issues/22980.
func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
panic("Query() is noop")
}
func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
panic("Exec() is noop")
}
var _ driver.Queryer = (*conn)(nil)
var _ driver.Execer = (*conn)(nil)