forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstmt.go
166 lines (140 loc) · 3.6 KB
/
stmt.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
package athena
import (
"context"
"database/sql/driver"
"fmt"
"strconv"
"strings"
"github.com/prestodb/presto-go-client/presto"
)
type stmtAthena struct {
prepareKey string
numInput int
ctasTable string
afterDownload func() error
conn *conn
resultMode ResultMode
}
func (s *stmtAthena) Close() error {
query := fmt.Sprintf("DEALLOCATE PREPARE %s", s.prepareKey)
_, err := s.conn.startQuery(query)
return err
}
func (s *stmtAthena) NumInput() int {
return s.numInput
}
func (s *stmtAthena) Exec(args []driver.Value) (driver.Result, error) {
values := make([]interface{}, 0, len(args))
for _, val := range args {
values = append(values, val)
}
ctx := context.Background()
query, err := s.makeQuery(ctx, values)
if err != nil {
return nil, err
}
_, err = s.runQuery(ctx, query)
return nil, err
}
func (s *stmtAthena) Query(args []driver.Value) (driver.Rows, error) {
values := make([]interface{}, 0, len(args))
for _, val := range args {
values = append(values, val)
}
ctx := context.Background()
query, err := s.makeQuery(ctx, values)
if err != nil {
return nil, err
}
return s.runQuery(ctx, query)
}
func (s *stmtAthena) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
values := make([]interface{}, 0, len(args))
for _, val := range args {
values = append(values, val.Value)
}
query, err := s.makeQuery(ctx, values)
if err != nil {
return nil, err
}
_, err = s.runQuery(ctx, query)
return nil, err
}
func (s *stmtAthena) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
values := make([]interface{}, 0, len(args))
for _, val := range args {
values = append(values, val.Value)
}
query, err := s.makeQuery(ctx, values)
if err != nil {
return nil, err
}
return s.runQuery(ctx, query)
}
func (s *stmtAthena) makeQuery(ctx context.Context, args []interface{}) (string, error) {
params := make([]string, 0, len(args))
for _, arg := range args {
var param string
param, err := serial(ctx, arg)
if err != nil {
return "", err
}
params = append(params, param)
}
var query string
if len(params) > 0 {
query = fmt.Sprintf("EXECUTE %s USING %s", s.prepareKey, strings.Join(params, ","))
} else {
query = fmt.Sprintf("EXECUTE %s", s.prepareKey)
}
return query, nil
}
func (s *stmtAthena) runQuery(ctx context.Context, query string) (driver.Rows, error) {
// timeout
timeout := s.conn.timeout
if to, ok := getTimeout(ctx); ok {
timeout = to
}
// catalog
catalog := s.conn.catalog
if cat, ok := getCatalog(ctx); ok {
catalog = cat
}
// output location (with empty value)
if checkOutputLocation(s.resultMode, s.conn.OutputLocation) {
var err error
s.conn.OutputLocation, err = getOutputLocation(s.conn.athena, s.conn.workgroup)
if err != nil {
return nil, err
}
}
queryID, err := s.conn.startQuery(query)
if err != nil {
return nil, err
}
if err := s.conn.waitOnQuery(ctx, queryID); err != nil {
return nil, err
}
return newRows(rowsConfig{
Athena: s.conn.athena,
QueryID: queryID,
SkipHeader: !isDDLQuery(query),
ResultMode: s.resultMode,
Session: s.conn.session,
OutputLocation: s.conn.OutputLocation,
Timeout: timeout,
AfterDownload: s.afterDownload,
CTASTable: s.ctasTable,
DB: s.conn.db,
Catalog: catalog,
})
}
func serial(ctx context.Context, v interface{}) (string, error) {
switch x := v.(type) {
case float32:
return strconv.FormatFloat(float64(x), 'g', -1, 32), nil
case float64:
return strconv.FormatFloat(x, 'g', -1, 64), nil
}
return presto.Serial(v)
}