-
Notifications
You must be signed in to change notification settings - Fork 64
/
conn.go
124 lines (103 loc) · 3.09 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
package athena
import (
"context"
"database/sql/driver"
"errors"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/athena"
"github.com/aws/aws-sdk-go-v2/service/athena/types"
)
type conn struct {
athena 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("The go-athena driver doesn't support prepared statements yet. 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("The go-athena driver doesn't support prepared statements yet. 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(ctx, query)
if err != nil {
return nil, err
}
if err := c.waitOnQuery(ctx, queryID); err != nil {
return nil, err
}
return newRows(ctx, 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(ctx context.Context, query string) (string, error) {
resp, err := c.athena.StartQueryExecution(ctx, &athena.StartQueryExecutionInput{
QueryString: aws.String(query),
QueryExecutionContext: &types.QueryExecutionContext{
Database: aws.String(c.db),
},
ResultConfiguration: &types.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.GetQueryExecution(ctx, &athena.GetQueryExecutionInput{
QueryExecutionId: aws.String(queryID),
})
if err != nil {
return err
}
switch statusResp.QueryExecution.Status.State {
case types.QueryExecutionStateCancelled:
return context.Canceled
case types.QueryExecutionStateFailed:
reason := *statusResp.QueryExecution.Status.StateChangeReason
return errors.New(reason)
case types.QueryExecutionStateSucceeded:
return nil
case types.QueryExecutionStateQueued:
case types.QueryExecutionStateRunning:
}
select {
case <-ctx.Done():
c.athena.StopQueryExecution(ctx, &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("The go-athena driver doesn't support prepared statements yet")
}
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)