forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
306 lines (284 loc) · 8.1 KB
/
connection.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright (c) 2017-2018 Snowflake Computing Inc. All right reserved.
package gosnowflake
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"net/url"
"strconv"
"strings"
"sync/atomic"
)
const (
statementTypeIDDml = int64(0x3000)
statementTypeIDInsert = statementTypeIDDml + int64(0x100)
statementTypeIDUpdate = statementTypeIDDml + int64(0x200)
statementTypeIDDelete = statementTypeIDDml + int64(0x300)
statementTypeIDMerge = statementTypeIDDml + int64(0x400)
statementTypeIDMultiTableInsert = statementTypeIDDml + int64(0x500)
)
type snowflakeConn struct {
cfg *Config
rest *snowflakeRestful
SequeceCounter uint64
QueryID string
SQLState string
}
// isDml returns true if the statement type code is in the range of DML.
func (sc *snowflakeConn) isDml(v int64) bool {
switch v {
case statementTypeIDDml, statementTypeIDInsert,
statementTypeIDUpdate, statementTypeIDDelete,
statementTypeIDMerge, statementTypeIDMultiTableInsert:
return true
}
return false
}
func (sc *snowflakeConn) exec(
ctx context.Context,
query string, noResult bool, isInternal bool, parameters []driver.NamedValue) (*execResponse, error) {
var err error
counter := atomic.AddUint64(&sc.SequeceCounter, 1) // query sequence counter
req := execRequest{
SQLText: query,
AsyncExec: noResult,
SequenceID: counter,
}
req.IsInternal = isInternal
tsmode := "TIMESTAMP_NTZ"
idx := 1
if len(parameters) > 0 {
req.Bindings = make(map[string]execBindParameter, len(parameters))
for i, n := 0, len(parameters); i < n; i++ {
t := goTypeToSnowflake(parameters[i].Value, tsmode)
glog.V(2).Infof("tmode: %v\n", t)
if t == "CHANGE_TYPE" {
tsmode, err = dataTypeMode(parameters[i].Value)
if err != nil {
return nil, err
}
} else {
v1, err := valueToString(parameters[i].Value, tsmode)
if err != nil {
return nil, err
}
req.Bindings[strconv.Itoa(idx)] = execBindParameter{
Type: t,
Value: v1,
}
idx++
}
}
}
glog.V(2).Infof("bindings: %v", req.Bindings)
headers := make(map[string]string)
headers["Content-Type"] = headerContentTypeApplicationJSON
headers["accept"] = headerAcceptTypeApplicationSnowflake // TODO v1.1: change to JSON in case of PUT/GET
headers["User-Agent"] = userAgent
jsonBody, err := json.Marshal(req)
if err != nil {
return nil, err
}
var data *execResponse
data, err = sc.rest.FuncPostQuery(ctx, sc.rest, &url.Values{}, headers, jsonBody, sc.rest.RequestTimeout)
if err != nil {
return nil, err
}
var code int
if data.Code != "" {
code, err = strconv.Atoi(data.Code)
if err != nil {
code = -1
return nil, err
}
} else {
code = -1
}
glog.V(2).Infof("Success: %v, Code: %v", data.Success, code)
if !data.Success {
return nil, &SnowflakeError{
Number: code,
SQLState: data.Data.SQLState,
Message: data.Message,
QueryID: data.Data.QueryID,
}
}
glog.V(2).Info("Exec/Query SUCCESS")
sc.cfg.Database = data.Data.FinalDatabaseName
sc.cfg.Schema = data.Data.FinalSchemaName
sc.cfg.Role = data.Data.FinalRoleName
sc.cfg.Warehouse = data.Data.FinalWarehouseName
sc.QueryID = data.Data.QueryID
sc.SQLState = data.Data.SQLState
sc.populateSessionParameters(data.Data.Parameters)
return data, err
}
func (sc *snowflakeConn) Begin() (driver.Tx, error) {
return sc.BeginTx(context.TODO(), driver.TxOptions{})
}
func (sc *snowflakeConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
glog.V(2).Info("BeginTx")
if opts.ReadOnly {
return nil, &SnowflakeError{
Number: ErrNoReadOnlyTransaction,
SQLState: SQLStateFeatureNotSupported,
Message: errMsgNoReadOnlyTransaction,
}
}
if int(opts.Isolation) != int(sql.LevelDefault) {
return nil, &SnowflakeError{
Number: ErrNoDefaultTransactionIsolationLevel,
SQLState: SQLStateFeatureNotSupported,
Message: errMsgNoDefaultTransactionIsolationLevel,
}
}
if sc.rest == nil {
return nil, driver.ErrBadConn
}
_, err := sc.exec(ctx, "BEGIN", false, false, nil)
if err != nil {
return nil, err
}
return &snowflakeTx{sc}, err
}
func (sc *snowflakeConn) cleanup() {
glog.Flush() // must flush log buffer while the process is running.
sc.rest = nil
sc.cfg = nil
}
func (sc *snowflakeConn) Close() (err error) {
glog.V(2).Infoln("Close")
// ensure transaction is rollbacked
_, err = sc.exec(context.Background(), "ROLLBACK", false, false, nil)
if err != nil {
glog.V(2).Info(err)
}
err = sc.rest.FuncCloseSession(sc.rest)
if err != nil {
glog.V(2).Info(err)
}
sc.cleanup()
return nil
}
func (sc *snowflakeConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
glog.V(2).Infoln("Prepare")
if sc.rest == nil {
return nil, driver.ErrBadConn
}
stmt := &snowflakeStmt{
sc: sc,
query: query,
}
return stmt, nil
}
func (sc *snowflakeConn) Prepare(query string) (driver.Stmt, error) {
return sc.PrepareContext(context.TODO(), query)
}
func (sc *snowflakeConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
glog.V(2).Infof("Exec: %#v, %v", query, args)
if sc.rest == nil {
return nil, driver.ErrBadConn
}
// TODO: handle noResult and isInternal
data, err := sc.exec(ctx, query, false, false, args)
if err != nil {
return nil, err
}
var updatedRows int64
if sc.isDml(data.Data.StatementTypeID) {
// collects all values from the returned row sets
updatedRows = 0
for i, n := 0, len(data.Data.RowType); i < n; i++ {
v, err := strconv.ParseInt(*data.Data.RowSet[0][i], 10, 64)
if err != nil {
return nil, err
}
updatedRows += v
}
glog.V(2).Infof("number of updated rows: %#v", updatedRows)
return &snowflakeResult{
affectedRows: updatedRows,
insertID: -1}, nil // last insert id is not supported by Snowflake
}
glog.V(2).Info("DDL")
return driver.ResultNoRows, nil
}
func (sc *snowflakeConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
glog.V(2).Infoln("Query")
if sc.rest == nil {
return nil, driver.ErrBadConn
}
// TODO: handle noResult and isInternal
data, err := sc.exec(ctx, query, false, false, args)
if err != nil {
glog.V(2).Infof("error: %v", err)
return nil, err
}
rows := new(snowflakeRows)
rows.sc = sc
rows.RowType = data.Data.RowType
rows.ChunkDownloader = &snowflakeChunkDownloader{
sc: sc,
ctx: ctx,
CurrentChunk: data.Data.RowSet,
ChunkMetas: data.Data.Chunks,
Total: int64(data.Data.Total),
TotalRowIndex: int64(-1),
Qrmk: data.Data.Qrmk,
ChunkHeader: data.Data.ChunkHeaders,
FuncDownload: downloadChunk,
FuncDownloadHelper: downloadChunkHelper,
FuncGet: getChunk,
}
rows.ChunkDownloader.start()
return rows, err
}
func (sc *snowflakeConn) Exec(
query string,
args []driver.Value) (
driver.Result, error) {
return sc.ExecContext(context.TODO(), query, toNamedValues(args))
}
func (sc *snowflakeConn) Query(
query string,
args []driver.Value) (
driver.Rows, error) {
return sc.QueryContext(context.TODO(), query, toNamedValues(args))
}
func (sc *snowflakeConn) Ping(ctx context.Context) error {
glog.V(2).Infoln("Ping")
if sc.rest == nil {
return driver.ErrBadConn
}
// TODO: handle noResult and isInternal
_, err := sc.exec(ctx, "SELECT 1", false, false, []driver.NamedValue{})
return err
}
func (sc *snowflakeConn) populateSessionParameters(parameters []nameValueParameter) {
// other session parameters (not all)
glog.V(2).Infof("params: %#v", parameters)
for _, param := range parameters {
v := ""
switch param.Value.(type) {
case int64:
if vv, ok := param.Value.(int64); ok {
v = strconv.FormatInt(vv, 10)
}
case float64:
if vv, ok := param.Value.(float64); ok {
v = strconv.FormatFloat(vv, 'g', -1, 64)
}
case bool:
if vv, ok := param.Value.(bool); ok {
v = strconv.FormatBool(vv)
}
default:
if vv, ok := param.Value.(string); ok {
v = vv
}
}
glog.V(3).Infof("parameter. name: %v, value: %v", param.Name, v)
sc.cfg.Params[strings.ToLower(param.Name)] = &v
}
}