-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhranaV2.go
609 lines (549 loc) · 16.9 KB
/
hranaV2.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package hranaV2
import (
"bytes"
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"github.com/tursodatabase/libsql-client-go/sqliteparserutils"
"io"
"net/http"
net_url "net/url"
"runtime/debug"
"strings"
"time"
"github.com/tursodatabase/libsql-client-go/libsql/internal/hrana"
"github.com/tursodatabase/libsql-client-go/libsql/internal/http/shared"
)
var commitHash string
func init() {
if info, ok := debug.ReadBuildInfo(); ok {
for _, module := range info.Deps {
if module.Path == "github.com/tursodatabase/libsql-client-go" {
parts := strings.Split(module.Version, "-")
if len(parts) == 3 {
commitHash = parts[2][:6]
return
}
}
}
}
commitHash = "unknown"
}
func Connect(url, jwt, host string, schemaDb bool) driver.Conn {
return &hranaV2Conn{url, jwt, host, schemaDb, "", false, 0}
}
type hranaV2Stmt struct {
conn *hranaV2Conn
numInput int
sql string
}
func (s *hranaV2Stmt) Close() error {
return nil
}
func (s *hranaV2Stmt) NumInput() int {
return s.numInput
}
func convertToNamed(args []driver.Value) []driver.NamedValue {
if len(args) == 0 {
return nil
}
var result []driver.NamedValue
for idx := range args {
result = append(result, driver.NamedValue{Ordinal: idx, Value: args[idx]})
}
return result
}
func (s *hranaV2Stmt) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), convertToNamed(args))
}
func (s *hranaV2Stmt) Query(args []driver.Value) (driver.Rows, error) {
return s.QueryContext(context.Background(), convertToNamed(args))
}
func (s *hranaV2Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
return s.conn.ExecContext(ctx, s.sql, args)
}
func (s *hranaV2Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return s.conn.QueryContext(ctx, s.sql, args)
}
type hranaV2Conn struct {
url string
jwt string
host string
schemaDb bool
baton string
streamClosed bool
replicationIndex uint64
}
func (h *hranaV2Conn) Ping() error {
return h.PingContext(context.Background())
}
func (h *hranaV2Conn) PingContext(ctx context.Context) error {
_, err := h.executeStmt(ctx, "SELECT 1", nil, false)
return err
}
func (h *hranaV2Conn) Prepare(query string) (driver.Stmt, error) {
return h.PrepareContext(context.Background(), query)
}
func (h *hranaV2Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
stmts, paramInfos, err := shared.ParseStatement(query)
if err != nil {
return nil, err
}
if len(stmts) != 1 {
return nil, fmt.Errorf("only one statement is supported got %d", len(stmts))
}
numInput := -1
if len(paramInfos[0].NamedParameters) == 0 {
numInput = paramInfos[0].PositionalParametersCount
}
return &hranaV2Stmt{h, numInput, query}, nil
}
func (h *hranaV2Conn) Close() error {
if h.baton != "" {
go func(baton, url, jwt, host string) {
msg := hrana.PipelineRequest{Baton: baton}
msg.Add(hrana.CloseStream())
_, _, _ = sendPipelineRequest(context.Background(), &msg, url, jwt, host)
}(h.baton, h.url, h.jwt, h.host)
}
return nil
}
func (h *hranaV2Conn) Begin() (driver.Tx, error) {
return h.BeginTx(context.Background(), driver.TxOptions{})
}
type hranaV2Tx struct {
conn *hranaV2Conn
}
func (h hranaV2Tx) Commit() error {
_, err := h.conn.ExecContext(context.Background(), "COMMIT", nil)
return err
}
func (h hranaV2Tx) Rollback() error {
_, err := h.conn.ExecContext(context.Background(), "ROLLBACK", nil)
return err
}
func (h *hranaV2Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if opts.ReadOnly {
return nil, fmt.Errorf("read only transactions are not supported")
}
if opts.Isolation != driver.IsolationLevel(sql.LevelDefault) {
return nil, fmt.Errorf("isolation level %d is not supported", opts.Isolation)
}
_, err := h.ExecContext(ctx, "BEGIN", nil)
if err != nil {
return nil, err
}
return &hranaV2Tx{h}, nil
}
func (h *hranaV2Conn) sendPipelineRequest(ctx context.Context, msg *hrana.PipelineRequest, streamClose bool) (*hrana.PipelineResponse, error) {
if h.streamClosed {
// If the stream is closed, we can't send any more requests using this connection.
return nil, fmt.Errorf("stream is closed: %w", driver.ErrBadConn)
}
if h.baton != "" {
msg.Baton = h.baton
}
if h.replicationIndex > 0 {
addReplicationIndex(msg, h.replicationIndex)
}
result, streamClosed, err := sendPipelineRequest(ctx, msg, h.url, h.jwt, h.host)
if streamClosed {
h.streamClosed = true
}
if err != nil {
return nil, err
}
h.baton = result.Baton
if result.Baton == "" && !streamClose {
// We need to remember that the stream is closed so we don't try to send any more requests using this connection.
h.streamClosed = true
}
if result.BaseUrl != "" {
h.url = result.BaseUrl
}
if idx := getReplicationIndex(&result); idx > h.replicationIndex {
h.replicationIndex = idx
}
return &result, nil
}
func addReplicationIndex(msg *hrana.PipelineRequest, replicationIndex uint64) {
for i := range msg.Requests {
if msg.Requests[i].Stmt != nil && msg.Requests[i].Stmt.ReplicationIndex == nil {
msg.Requests[i].Stmt.ReplicationIndex = &replicationIndex
} else if msg.Requests[i].Batch != nil && msg.Requests[i].Batch.ReplicationIndex == nil {
msg.Requests[i].Batch.ReplicationIndex = &replicationIndex
}
}
}
func getReplicationIndex(response *hrana.PipelineResponse) uint64 {
if response == nil || len(response.Results) == 0 {
return 0
}
var replicationIndex uint64
for _, result := range response.Results {
if result.Response == nil {
continue
}
if result.Response.Type == "execute" {
if res, err := result.Response.ExecuteResult(); err == nil && res.ReplicationIndex != nil {
if *res.ReplicationIndex > replicationIndex {
replicationIndex = *res.ReplicationIndex
}
}
} else if result.Response.Type == "batch" {
if res, err := result.Response.BatchResult(); err == nil && res.ReplicationIndex != nil {
if *res.ReplicationIndex > replicationIndex {
replicationIndex = *res.ReplicationIndex
}
}
}
}
return replicationIndex
}
func sendPipelineRequest(ctx context.Context, msg *hrana.PipelineRequest, url string, jwt string, host string) (result hrana.PipelineResponse, streamClosed bool, err error) {
reqBody, err := json.Marshal(msg)
if err != nil {
return hrana.PipelineResponse{}, false, err
}
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
pipelineURL, err := net_url.JoinPath(url, "/v2/pipeline")
if err != nil {
return hrana.PipelineResponse{}, false, err
}
req, err := http.NewRequestWithContext(ctx, "POST", pipelineURL, bytes.NewReader(reqBody))
if err != nil {
return hrana.PipelineResponse{}, false, err
}
if len(jwt) > 0 {
req.Header.Set("Authorization", "Bearer "+jwt)
}
req.Header.Set("x-libsql-client-version", "libsql-remote-go-"+commitHash)
req.Host = host
resp, err := http.DefaultClient.Do(req)
if err != nil {
return hrana.PipelineResponse{}, false, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return hrana.PipelineResponse{}, false, err
}
if resp.StatusCode != http.StatusOK {
// We need to remember that the stream is closed so we don't try to send any more requests using this connection.
var serverError struct {
Error string `json:"error"`
}
if err := json.Unmarshal(body, &serverError); err == nil {
return hrana.PipelineResponse{}, true, fmt.Errorf("error code %d: %s", resp.StatusCode, serverError.Error)
}
var errResponse hrana.Error
if err := json.Unmarshal(body, &errResponse); err == nil {
if errResponse.Code != nil {
if *errResponse.Code == "STREAM_EXPIRED" {
return hrana.PipelineResponse{}, true, fmt.Errorf("error code %s: %s\n%w", *errResponse.Code, errResponse.Message, driver.ErrBadConn)
} else {
return hrana.PipelineResponse{}, true, fmt.Errorf("error code %s: %s", *errResponse.Code, errResponse.Message)
}
}
return hrana.PipelineResponse{}, true, errors.New(errResponse.Message)
}
return hrana.PipelineResponse{}, true, fmt.Errorf("error code %d: %s", resp.StatusCode, string(body))
}
if err = json.Unmarshal(body, &result); err != nil {
return hrana.PipelineResponse{}, false, err
}
return result, false, nil
}
func (h *hranaV2Conn) executeMsg(ctx context.Context, msg *hrana.PipelineRequest) (*hrana.PipelineResponse, error) {
result, err := h.sendPipelineRequest(ctx, msg, false)
if err != nil {
return nil, err
}
for _, r := range result.Results {
if r.Error != nil {
return nil, errors.New(r.Error.Message)
}
if r.Response == nil {
return nil, errors.New("no response received")
}
}
return result, nil
}
type chunker struct {
chunk []string
iterator *sqliteparserutils.StatementIterator
limit int
}
func newChunker(iterator *sqliteparserutils.StatementIterator, limit int) *chunker {
return &chunker{iterator: iterator, chunk: make([]string, 0, limit), limit: limit}
}
func isTransactionStatement(stmt string) bool {
patterns := [][]byte{[]byte("begin"), []byte("commit"), []byte("end"), []byte("rollback")}
for _, p := range patterns {
if len(stmt) >= len(p) && bytes.Equal(bytes.ToLower([]byte(stmt[0:len(p)])), p) {
return true
}
}
return false
}
func (c *chunker) Next() (chunk []string, isEOF bool) {
c.chunk = c.chunk[:0]
var stmt string
for !isEOF && len(c.chunk) < c.limit {
stmt, _, isEOF = c.iterator.Next()
// We need to skip transaction statements. Chunks run in a transaction by default.
if stmt != "" && !isTransactionStatement(stmt) {
c.chunk = append(c.chunk, stmt)
}
}
return c.chunk, isEOF
}
func (h *hranaV2Conn) executeSingleStmt(ctx context.Context, stmt string, wantRows bool) (*hrana.PipelineResponse, error) {
msg := &hrana.PipelineRequest{}
executeStream, err := hrana.ExecuteStream(stmt, nil, wantRows)
if err != nil {
return nil, fmt.Errorf("failed to execute SQL: %s\n%w", stmt, err)
}
msg.Add(*executeStream)
res, err := h.executeMsg(ctx, msg)
if err != nil {
return nil, fmt.Errorf("failed to execute SQL: %s\n%w", stmt, err)
}
return res, nil
}
func (h *hranaV2Conn) executeInChunks(ctx context.Context, query string, wantRows bool) (*hrana.PipelineResponse, error) {
const chunkSize = 4096
iterator := sqliteparserutils.CreateStatementIterator(query)
chunker := newChunker(iterator, chunkSize)
chunk, isEOF := chunker.Next()
if isEOF && len(chunk) == 1 {
return h.executeSingleStmt(ctx, chunk[0], wantRows)
}
_, err := h.executeSingleStmt(ctx, "BEGIN", false)
if err != nil {
return nil, err
}
batch := &hrana.Batch{Steps: make([]hrana.BatchStep, chunkSize)}
msg := &hrana.PipelineRequest{}
msg.Add(hrana.StreamRequest{Type: "batch", Batch: batch})
for idx := range batch.Steps {
batch.Steps[idx].Stmt.WantRows = wantRows
}
result := &hrana.PipelineResponse{}
for {
for idx := range chunk {
batch.Steps[idx].Stmt.Sql = &chunk[idx]
}
if len(chunk) < chunkSize {
// We can trim batch.Steps because this is the last chunk anyway.
// isEOF has to be true at this point.
batch.Steps = batch.Steps[:len(chunk)]
}
res, err := h.executeMsg(ctx, msg)
if err != nil {
h.closeStream()
return nil, fmt.Errorf("failed to execute SQL:\n%w", err)
}
result.Baton = res.Baton
result.BaseUrl = res.BaseUrl
result.Results = append(result.Results, res.Results...)
if isEOF {
break
}
chunk, isEOF = chunker.Next()
}
_, err = h.executeSingleStmt(ctx, "COMMIT", false)
if err != nil {
h.closeStream()
return nil, err
}
return result, nil
}
func (h *hranaV2Conn) executeStmt(ctx context.Context, query string, args []driver.NamedValue, wantRows bool) (*hrana.PipelineResponse, error) {
const querySizeLimitForChunking = 20 * 1024 * 1024
if len(args) == 0 && len(query) > querySizeLimitForChunking && !h.schemaDb {
return h.executeInChunks(ctx, query, wantRows)
}
stmts, params, err := shared.ParseStatementAndArgs(query, args)
if err != nil {
return nil, fmt.Errorf("failed to execute SQL:\n%w", err)
}
msg := &hrana.PipelineRequest{}
if len(stmts) == 1 {
var p *shared.Params
if len(params) > 0 {
p = ¶ms[0]
}
executeStream, err := hrana.ExecuteStream(stmts[0], p, wantRows)
if err != nil {
return nil, fmt.Errorf("failed to execute SQL:\n%w", err)
}
msg.Add(*executeStream)
} else {
batchStream, err := hrana.BatchStream(stmts, params, wantRows, !h.schemaDb)
if err != nil {
return nil, fmt.Errorf("failed to execute SQL:\n%w", err)
}
msg.Add(*batchStream)
}
resp, err := h.executeMsg(ctx, msg)
if err != nil {
return nil, fmt.Errorf("failed to execute SQL:\n%w", err)
}
return resp, nil
}
func (h *hranaV2Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
result, err := h.executeStmt(ctx, query, args, false)
if err != nil {
return nil, err
}
switch result.Results[0].Response.Type {
case "execute":
res, err := result.Results[0].Response.ExecuteResult()
if err != nil {
return nil, err
}
return shared.NewResult(res.GetLastInsertRowId(), int64(res.AffectedRowCount)), nil
case "batch":
res, err := result.Results[0].Response.BatchResult()
if err != nil {
return nil, err
}
lastInsertRowId := int64(0)
affectedRowCount := int64(0)
upperBound := len(res.StepResults)
if !h.schemaDb {
upperBound -= 1
}
for idx := 0; idx < upperBound; idx++ {
r := res.StepResults[idx]
rowId := r.GetLastInsertRowId()
if rowId > 0 {
lastInsertRowId = rowId
}
affectedRowCount += int64(r.AffectedRowCount)
}
return shared.NewResult(lastInsertRowId, affectedRowCount), nil
default:
return nil, fmt.Errorf("failed to execute SQL: %s\n%s", query, "unknown response type")
}
}
type StmtResultRowsProvider struct {
r *hrana.StmtResult
}
func (p *StmtResultRowsProvider) SetsCount() int {
return 1
}
func (p *StmtResultRowsProvider) RowsCount(setIdx int) int {
if setIdx != 0 {
return 0
}
return len(p.r.Rows)
}
func (p *StmtResultRowsProvider) Columns(setIdx int) []string {
if setIdx != 0 {
return nil
}
res := make([]string, len(p.r.Cols))
for i, c := range p.r.Cols {
if c.Name != nil {
res[i] = *c.Name
}
}
return res
}
func (p *StmtResultRowsProvider) FieldValue(setIdx, rowIdx, colIdx int) driver.Value {
if setIdx != 0 {
return nil
}
return p.r.Rows[rowIdx][colIdx].ToValue(p.r.Cols[colIdx].Type)
}
func (p *StmtResultRowsProvider) Error(setIdx int) string {
return ""
}
func (p *StmtResultRowsProvider) HasResult(setIdx int) bool {
return setIdx == 0
}
type BatchResultRowsProvider struct {
r *hrana.BatchResult
}
func (p *BatchResultRowsProvider) SetsCount() int {
return len(p.r.StepResults)
}
func (p *BatchResultRowsProvider) RowsCount(setIdx int) int {
if setIdx >= len(p.r.StepResults) || p.r.StepResults[setIdx] == nil {
return 0
}
return len(p.r.StepResults[setIdx].Rows)
}
func (p *BatchResultRowsProvider) Columns(setIdx int) []string {
if setIdx >= len(p.r.StepResults) || p.r.StepResults[setIdx] == nil {
return nil
}
res := make([]string, len(p.r.StepResults[setIdx].Cols))
for i, c := range p.r.StepResults[setIdx].Cols {
if c.Name != nil {
res[i] = *c.Name
}
}
return res
}
func (p *BatchResultRowsProvider) FieldValue(setIdx, rowIdx, colIdx int) driver.Value {
if setIdx >= len(p.r.StepResults) || p.r.StepResults[setIdx] == nil {
return nil
}
return p.r.StepResults[setIdx].Rows[rowIdx][colIdx].ToValue(p.r.StepResults[setIdx].Cols[colIdx].Type)
}
func (p *BatchResultRowsProvider) Error(setIdx int) string {
if setIdx >= len(p.r.StepErrors) || p.r.StepErrors[setIdx] == nil {
return ""
}
return p.r.StepErrors[setIdx].Message
}
func (p *BatchResultRowsProvider) HasResult(setIdx int) bool {
return setIdx < len(p.r.StepResults) && p.r.StepResults[setIdx] != nil
}
func (h *hranaV2Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
result, err := h.executeStmt(ctx, query, args, true)
if err != nil {
return nil, err
}
switch result.Results[0].Response.Type {
case "execute":
res, err := result.Results[0].Response.ExecuteResult()
if err != nil {
return nil, err
}
return shared.NewRows(&StmtResultRowsProvider{res}), nil
case "batch":
res, err := result.Results[0].Response.BatchResult()
if err != nil {
return nil, err
}
if !h.schemaDb {
res.StepResults = res.StepResults[:len(res.StepResults)-1]
res.StepErrors = res.StepErrors[:len(res.StepErrors)-1]
}
return shared.NewRows(&BatchResultRowsProvider{res}), nil
default:
return nil, fmt.Errorf("failed to execute SQL: %s\n%s", query, "unknown response type")
}
}
func (h *hranaV2Conn) closeStream() {
if h.baton != "" {
go func(baton, url, jwt, host string) {
msg := hrana.PipelineRequest{Baton: baton}
msg.Add(hrana.CloseStream())
_, _, _ = sendPipelineRequest(context.Background(), &msg, url, jwt, host)
}(h.baton, h.url, h.jwt, h.host)
h.baton = ""
}
}
func (h *hranaV2Conn) ResetSession(ctx context.Context) error {
h.closeStream()
return nil
}