-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlibsql.go
780 lines (693 loc) · 20.5 KB
/
libsql.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//go:build cgo
// +build cgo
package libsql
/*
#cgo CFLAGS: -I${SRCDIR}/lib/include
#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/darwin_amd64
#cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/darwin_arm64
#cgo linux,amd64 LDFLAGS: -L${SRCDIR}/lib/linux_amd64
#cgo linux,arm64 LDFLAGS: -L${SRCDIR}/lib/linux_arm64
#cgo LDFLAGS: -lsql_experimental
#cgo LDFLAGS: -lm
#cgo darwin LDFLAGS: -framework Security
#cgo darwin LDFLAGS: -framework CoreFoundation
#include <libsql.h>
#include <stdlib.h>
*/
import "C"
import (
"context"
"database/sql"
sqldriver "database/sql/driver"
"errors"
"fmt"
"io"
"net/url"
"regexp"
"strings"
"time"
"unsafe"
"github.com/antlr4-go/antlr/v4"
"github.com/libsql/sqlite-antlr4-parser/sqliteparser"
"github.com/libsql/sqlite-antlr4-parser/sqliteparserutils"
)
func init() {
sql.Register("libsql", driver{})
}
type config struct {
authToken *string
readYourWrites *bool
encryptionKey *string
syncInterval *time.Duration
}
type Option interface {
apply(*config) error
}
type option func(*config) error
type Replicated struct {
FrameNo int
FramesSynced int
}
func (o option) apply(c *config) error {
return o(c)
}
func WithAuthToken(authToken string) Option {
return option(func(o *config) error {
if o.authToken != nil {
return fmt.Errorf("authToken already set")
}
if authToken == "" {
return fmt.Errorf("authToken must not be empty")
}
o.authToken = &authToken
return nil
})
}
func WithReadYourWrites(readYourWrites bool) Option {
return option(func(o *config) error {
if o.readYourWrites != nil {
return fmt.Errorf("read your writes already set")
}
o.readYourWrites = &readYourWrites
return nil
})
}
func WithEncryption(key string) Option {
return option(func(o *config) error {
if o.encryptionKey != nil {
return fmt.Errorf("encryption key already set")
}
if key == "" {
return fmt.Errorf("encryption key must not be empty")
}
o.encryptionKey = &key
return nil
})
}
func WithSyncInterval(interval time.Duration) Option {
return option(func(o *config) error {
if o.syncInterval != nil {
return fmt.Errorf("sync interval already set")
}
o.syncInterval = &interval
return nil
})
}
func NewEmbeddedReplicaConnector(dbPath string, primaryUrl string, opts ...Option) (*Connector, error) {
var config config
errs := make([]error, 0, len(opts))
for _, opt := range opts {
if err := opt.apply(&config); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return nil, errors.Join(errs...)
}
authToken := ""
if config.authToken != nil {
authToken = *config.authToken
}
readYourWrites := true
if config.readYourWrites != nil {
readYourWrites = *config.readYourWrites
}
encryptionKey := ""
if config.encryptionKey != nil {
encryptionKey = *config.encryptionKey
}
syncInterval := time.Duration(0)
if config.syncInterval != nil {
syncInterval = *config.syncInterval
}
return openEmbeddedReplicaConnector(dbPath, primaryUrl, authToken, readYourWrites, encryptionKey, syncInterval)
}
type driver struct{}
func (d driver) Open(dbAddress string) (sqldriver.Conn, error) {
connector, err := d.OpenConnector(dbAddress)
if err != nil {
return nil, err
}
return connector.Connect(context.Background())
}
func (d driver) OpenConnector(dbAddress string) (sqldriver.Connector, error) {
if strings.HasPrefix(dbAddress, ":memory:") {
return openLocalConnector(dbAddress)
}
u, err := url.Parse(dbAddress)
if err != nil {
return nil, err
}
switch u.Scheme {
case "file":
return openLocalConnector(dbAddress)
case "http":
fallthrough
case "https":
fallthrough
case "libsql":
authToken := u.Query().Get("authToken")
u.RawQuery = ""
return openRemoteConnector(u.String(), authToken)
}
return nil, fmt.Errorf("unsupported URL scheme: %s\nThis driver supports only URLs that start with libsql://, file:, https:// or http://", u.Scheme)
}
func libsqlSync(nativeDbPtr C.libsql_database_t) (Replicated, error) {
var errMsg *C.char
var rep C.replicated;
statusCode := C.libsql_sync2(nativeDbPtr, &rep, &errMsg)
if statusCode != 0 {
return Replicated{0, 0}, libsqlError("failed to sync database ", statusCode, errMsg)
}
return Replicated{FrameNo: int(rep.frame_no), FramesSynced: int(rep.frames_synced)}, nil
}
func openLocalConnector(dbPath string) (*Connector, error) {
nativeDbPtr, err := libsqlOpenLocal(dbPath)
if err != nil {
return nil, err
}
return &Connector{nativeDbPtr: nativeDbPtr}, nil
}
func openRemoteConnector(primaryUrl, authToken string) (*Connector, error) {
nativeDbPtr, err := libsqlOpenRemote(primaryUrl, authToken)
if err != nil {
return nil, err
}
return &Connector{nativeDbPtr: nativeDbPtr}, nil
}
func openEmbeddedReplicaConnector(dbPath, primaryUrl, authToken string, readYourWrites bool, encryptionKey string, syncInterval time.Duration) (*Connector, error) {
var closeCh chan struct{}
var closeAckCh chan struct{}
nativeDbPtr, err := libsqlOpenWithSync(dbPath, primaryUrl, authToken, readYourWrites, encryptionKey)
if err != nil {
return nil, err
}
if _, err := libsqlSync(nativeDbPtr); err != nil {
C.libsql_close(nativeDbPtr)
return nil, err
}
if syncInterval != 0 {
closeCh = make(chan struct{}, 1)
closeAckCh = make(chan struct{}, 1)
go func() {
for {
timerCh := make(chan struct{}, 1)
go func() {
time.Sleep(syncInterval)
timerCh <- struct{}{}
}()
select {
case <-closeCh:
closeAckCh <- struct{}{}
return
case <-timerCh:
if _, err := libsqlSync(nativeDbPtr); err != nil {
fmt.Println(err)
}
}
}
}()
}
return &Connector{nativeDbPtr: nativeDbPtr, closeCh: closeCh, closeAckCh: closeAckCh}, nil
}
type Connector struct {
nativeDbPtr C.libsql_database_t
closeCh chan<- struct{}
closeAckCh <-chan struct{}
}
func (c *Connector) Sync() (Replicated, error) {
return libsqlSync(c.nativeDbPtr)
}
func (c *Connector) Close() error {
if c.closeCh != nil {
c.closeCh <- struct{}{}
<-c.closeAckCh
c.closeCh = nil
c.closeAckCh = nil
}
if c.nativeDbPtr != nil {
C.libsql_close(c.nativeDbPtr)
}
c.nativeDbPtr = nil
return nil
}
func (c *Connector) Connect(ctx context.Context) (sqldriver.Conn, error) {
nativeConnPtr, err := libsqlConnect(c.nativeDbPtr)
if err != nil {
return nil, err
}
return &conn{nativePtr: nativeConnPtr}, nil
}
func (c *Connector) Driver() sqldriver.Driver {
return driver{}
}
func libsqlError(message string, statusCode C.int, errMsg *C.char) error {
code := int(statusCode)
if errMsg != nil {
msg := C.GoString(errMsg)
C.libsql_free_string(errMsg)
return fmt.Errorf("%s\nerror code = %d: %v", message, code, msg)
} else {
return fmt.Errorf("%s\nerror code = %d", message, code)
}
}
func libsqlOpenLocal(dataSourceName string) (C.libsql_database_t, error) {
connectionString := C.CString(dataSourceName)
defer C.free(unsafe.Pointer(connectionString))
var db C.libsql_database_t
var errMsg *C.char
statusCode := C.libsql_open_file(connectionString, &db, &errMsg)
if statusCode != 0 {
return nil, libsqlError(fmt.Sprint("failed to open local database ", dataSourceName), statusCode, errMsg)
}
return db, nil
}
func libsqlOpenRemote(url, authToken string) (C.libsql_database_t, error) {
connectionString := C.CString(url)
defer C.free(unsafe.Pointer(connectionString))
authTokenNativeString := C.CString(authToken)
defer C.free(unsafe.Pointer(authTokenNativeString))
var db C.libsql_database_t
var errMsg *C.char
statusCode := C.libsql_open_remote(connectionString, authTokenNativeString, &db, &errMsg)
if statusCode != 0 {
return nil, libsqlError(fmt.Sprint("failed to open remote database ", url), statusCode, errMsg)
}
return db, nil
}
func libsqlOpenWithSync(dbPath, primaryUrl, authToken string, readYourWrites bool, encryptionKey string) (C.libsql_database_t, error) {
dbPathNativeString := C.CString(dbPath)
defer C.free(unsafe.Pointer(dbPathNativeString))
primaryUrlNativeString := C.CString(primaryUrl)
defer C.free(unsafe.Pointer(primaryUrlNativeString))
authTokenNativeString := C.CString(authToken)
defer C.free(unsafe.Pointer(authTokenNativeString))
var readYourWritesNative C.char = 0
if readYourWrites {
readYourWritesNative = 1
}
var encrytionKeyNativeString *C.char
if encryptionKey != "" {
encrytionKeyNativeString = C.CString(encryptionKey)
defer C.free(unsafe.Pointer(encrytionKeyNativeString))
}
var db C.libsql_database_t
var errMsg *C.char
statusCode := C.libsql_open_sync(dbPathNativeString, primaryUrlNativeString, authTokenNativeString, readYourWritesNative, encrytionKeyNativeString, &db, &errMsg)
if statusCode != 0 {
return nil, libsqlError(fmt.Sprintf("failed to open database %s %s", dbPath, primaryUrl), statusCode, errMsg)
}
return db, nil
}
func libsqlConnect(db C.libsql_database_t) (C.libsql_connection_t, error) {
var conn C.libsql_connection_t
var errMsg *C.char
statusCode := C.libsql_connect(db, &conn, &errMsg)
if statusCode != 0 {
return nil, libsqlError("failed to connect to database", statusCode, errMsg)
}
return conn, nil
}
type conn struct {
nativePtr C.libsql_connection_t
}
func (c *conn) Prepare(query string) (sqldriver.Stmt, error) {
return c.PrepareContext(context.Background(), query)
}
func (c *conn) Begin() (sqldriver.Tx, error) {
return c.BeginTx(context.Background(), sqldriver.TxOptions{})
}
func (c *conn) Close() error {
C.libsql_disconnect(c.nativePtr)
return nil
}
type ParamsInfo struct {
NamedParameters []string
PositionalParametersCount int
}
func isPositionalParameter(param string) (ok bool, err error) {
re := regexp.MustCompile(`\?([0-9]*).*`)
match := re.FindSubmatch([]byte(param))
if match == nil {
return false, nil
}
posS := string(match[1])
if posS == "" {
return true, nil
}
return true, fmt.Errorf("unsuppoted positional parameter. This driver does not accept positional parameters with indexes (like ?<number>)")
}
func removeParamPrefix(paramName string) (string, error) {
if paramName[0] == ':' || paramName[0] == '@' || paramName[0] == '$' {
return paramName[1:], nil
}
return "", fmt.Errorf("all named parameters must start with ':', or '@' or '$'")
}
func extractParameters(stmt string) (nameParams []string, positionalParamsCount int, err error) {
statementStream := antlr.NewInputStream(stmt)
sqliteparser.NewSQLiteLexer(statementStream)
lexer := sqliteparser.NewSQLiteLexer(statementStream)
allTokens := lexer.GetAllTokens()
nameParamsSet := make(map[string]bool)
for _, token := range allTokens {
tokenType := token.GetTokenType()
if tokenType == sqliteparser.SQLiteLexerBIND_PARAMETER {
parameter := token.GetText()
isPositionalParameter, err := isPositionalParameter(parameter)
if err != nil {
return []string{}, 0, err
}
if isPositionalParameter {
positionalParamsCount++
} else {
paramWithoutPrefix, err := removeParamPrefix(parameter)
if err != nil {
return []string{}, 0, err
} else {
nameParamsSet[paramWithoutPrefix] = true
}
}
}
}
nameParams = make([]string, 0, len(nameParamsSet))
for k := range nameParamsSet {
nameParams = append(nameParams, k)
}
return nameParams, positionalParamsCount, nil
}
func parseStatement(sql string) ([]string, []ParamsInfo, error) {
stmts, _ := sqliteparserutils.SplitStatement(sql)
stmtsParams := make([]ParamsInfo, len(stmts))
for idx, stmt := range stmts {
nameParams, positionalParamsCount, err := extractParameters(stmt)
if err != nil {
return nil, nil, err
}
stmtsParams[idx] = ParamsInfo{nameParams, positionalParamsCount}
}
return stmts, stmtsParams, nil
}
func (c *conn) PrepareContext(ctx context.Context, query string) (sqldriver.Stmt, error) {
stmts, paramInfos, err := 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 &stmt{c, query, numInput}, nil
}
func (c *conn) BeginTx(ctx context.Context, opts sqldriver.TxOptions) (sqldriver.Tx, error) {
if opts.ReadOnly {
return nil, fmt.Errorf("read only transactions are not supported")
}
if opts.Isolation != sqldriver.IsolationLevel(sql.LevelDefault) {
return nil, fmt.Errorf("isolation level %d is not supported", opts.Isolation)
}
_, err := c.ExecContext(ctx, "BEGIN", nil)
if err != nil {
return nil, err
}
return &tx{c}, nil
}
func (c *conn) executeNoArgs(query string, exec bool) (C.libsql_rows_t, error) {
queryCString := C.CString(query)
defer C.free(unsafe.Pointer(queryCString))
var rows C.libsql_rows_t
var errMsg *C.char
var statusCode C.int
if exec {
statusCode = C.libsql_execute(c.nativePtr, queryCString, &errMsg)
} else {
statusCode = C.libsql_query(c.nativePtr, queryCString, &rows, &errMsg)
}
if statusCode != 0 {
return nil, libsqlError(fmt.Sprint("failed to execute query ", query), statusCode, errMsg)
}
return rows, nil
}
func (c *conn) execute(query string, args []sqldriver.NamedValue, exec bool) (C.libsql_rows_t, error) {
if len(args) == 0 {
return c.executeNoArgs(query, exec)
}
queryCString := C.CString(query)
defer C.free(unsafe.Pointer(queryCString))
var stmt C.libsql_stmt_t
var errMsg *C.char
statusCode := C.libsql_prepare(c.nativePtr, queryCString, &stmt, &errMsg)
if statusCode != 0 {
return nil, libsqlError(fmt.Sprint("failed to prepare query ", query), statusCode, errMsg)
}
defer C.libsql_free_stmt(stmt)
for _, arg := range args {
var errMsg *C.char
var statusCode C.int
idx := arg.Ordinal
switch arg.Value.(type) {
case int64:
statusCode = C.libsql_bind_int(stmt, C.int(idx), C.longlong(arg.Value.(int64)), &errMsg)
case float64:
statusCode = C.libsql_bind_float(stmt, C.int(idx), C.double(arg.Value.(float64)), &errMsg)
case []byte:
blob := arg.Value.([]byte)
nativeBlob := C.CBytes(blob)
statusCode = C.libsql_bind_blob(stmt, C.int(idx), (*C.uchar)(nativeBlob), C.int(len(blob)), &errMsg)
C.free(nativeBlob)
case string:
valueStr := C.CString(arg.Value.(string))
statusCode = C.libsql_bind_string(stmt, C.int(idx), valueStr, &errMsg)
C.free(unsafe.Pointer(valueStr))
case nil:
statusCode = C.libsql_bind_null(stmt, C.int(idx), &errMsg)
case bool:
var valueInt int
if arg.Value.(bool) {
valueInt = 1
} else {
valueInt = 0
}
statusCode = C.libsql_bind_int(stmt, C.int(idx), C.longlong(valueInt), &errMsg)
case time.Time:
valueStr := C.CString(arg.Value.(time.Time).Format(time.RFC3339Nano))
statusCode = C.libsql_bind_string(stmt, C.int(idx), valueStr, &errMsg)
C.free(unsafe.Pointer(valueStr))
default:
return nil, fmt.Errorf("unsupported type %T", arg.Value)
}
if statusCode != 0 {
return nil, libsqlError(fmt.Sprintf("failed to bind argument no. %d with value %v and type %T", idx, arg.Value, arg.Value), statusCode, errMsg)
}
}
var rows C.libsql_rows_t
if exec {
statusCode = C.libsql_execute_stmt(stmt, &errMsg)
} else {
statusCode = C.libsql_query_stmt(stmt, &rows, &errMsg)
}
if statusCode != 0 {
return nil, libsqlError(fmt.Sprint("failed to execute query ", query), statusCode, errMsg)
}
return rows, nil
}
type execResult struct {
id int64
changes int64
}
func (r execResult) LastInsertId() (int64, error) {
return r.id, nil
}
func (r execResult) RowsAffected() (int64, error) {
return r.changes, nil
}
func (c *conn) ExecContext(ctx context.Context, query string, args []sqldriver.NamedValue) (sqldriver.Result, error) {
rows, err := c.execute(query, args, true)
if err != nil {
return nil, err
}
id := int64(C.libsql_last_insert_rowid(c.nativePtr))
changes := int64(C.libsql_changes(c.nativePtr))
if rows != nil {
C.libsql_free_rows(rows)
}
return execResult{id, changes}, nil
}
type stmt struct {
conn *conn
sql string
numInput int
}
func (s *stmt) Close() error {
return nil
}
func (s *stmt) NumInput() int {
return s.numInput
}
func convertToNamed(args []sqldriver.Value) []sqldriver.NamedValue {
if len(args) == 0 {
return nil
}
result := make([]sqldriver.NamedValue, 0, len(args))
for idx := range args {
result = append(result, sqldriver.NamedValue{Ordinal: idx, Value: args[idx]})
}
return result
}
func (s *stmt) Exec(args []sqldriver.Value) (sqldriver.Result, error) {
return s.ExecContext(context.Background(), convertToNamed(args))
}
func (s *stmt) Query(args []sqldriver.Value) (sqldriver.Rows, error) {
return s.QueryContext(context.Background(), convertToNamed(args))
}
func (s *stmt) ExecContext(ctx context.Context, args []sqldriver.NamedValue) (sqldriver.Result, error) {
return s.conn.ExecContext(ctx, s.sql, args)
}
func (s *stmt) QueryContext(ctx context.Context, args []sqldriver.NamedValue) (sqldriver.Rows, error) {
return s.conn.QueryContext(ctx, s.sql, args)
}
type tx struct {
conn *conn
}
func (t tx) Commit() error {
_, err := t.conn.ExecContext(context.Background(), "COMMIT", nil)
return err
}
func (t tx) Rollback() error {
_, err := t.conn.ExecContext(context.Background(), "ROLLBACK", nil)
return err
}
const (
TYPE_INT int = iota + 1
TYPE_FLOAT
TYPE_TEXT
TYPE_BLOB
TYPE_NULL
)
func newRows(nativePtr C.libsql_rows_t) (*rows, error) {
if nativePtr == nil {
return &rows{nil, nil}, nil
}
columnCount := int(C.libsql_column_count(nativePtr))
columns := make([]string, columnCount)
for i := 0; i < columnCount; i++ {
var ptr *C.char
var errMsg *C.char
statusCode := C.libsql_column_name(nativePtr, C.int(i), &ptr, &errMsg)
if statusCode != 0 {
return nil, libsqlError(fmt.Sprint("failed to get column name for index ", i), statusCode, errMsg)
}
columns[i] = C.GoString(ptr)
C.libsql_free_string(ptr)
}
return &rows{nativePtr, columns}, nil
}
type rows struct {
nativePtr C.libsql_rows_t
columnNames []string
}
func (r *rows) Columns() []string {
return r.columnNames
}
func (r *rows) Close() error {
if r.nativePtr != nil {
C.libsql_free_rows(r.nativePtr)
r.nativePtr = nil
}
return nil
}
func (r *rows) Next(dest []sqldriver.Value) error {
if r.nativePtr == nil {
return io.EOF
}
var row C.libsql_row_t
var errMsg *C.char
statusCode := C.libsql_next_row(r.nativePtr, &row, &errMsg)
if statusCode != 0 {
return libsqlError("failed to get next row", statusCode, errMsg)
}
if row == nil {
r.Close()
return io.EOF
}
defer C.libsql_free_row(row)
count := len(dest)
if count > len(r.columnNames) {
count = len(r.columnNames)
}
Outerloop:
for i := 0; i < count; i++ {
var columnType C.int
var errMsg *C.char
statusCode := C.libsql_column_type(r.nativePtr, row, C.int(i), &columnType, &errMsg)
if statusCode != 0 {
return libsqlError(fmt.Sprint("failed to get column type for index ", i), statusCode, errMsg)
}
switch int(columnType) {
case TYPE_NULL:
dest[i] = nil
case TYPE_INT:
var value C.longlong
var errMsg *C.char
statusCode := C.libsql_get_int(row, C.int(i), &value, &errMsg)
if statusCode != 0 {
return libsqlError(fmt.Sprint("failed to get integer for column ", i), statusCode, errMsg)
}
dest[i] = int64(value)
case TYPE_FLOAT:
var value C.double
var errMsg *C.char
statusCode := C.libsql_get_float(row, C.int(i), &value, &errMsg)
if statusCode != 0 {
return libsqlError(fmt.Sprint("failed to get float for column ", i), statusCode, errMsg)
}
dest[i] = float64(value)
case TYPE_BLOB:
var nativeBlob C.blob
var errMsg *C.char
statusCode := C.libsql_get_blob(row, C.int(i), &nativeBlob, &errMsg)
if statusCode != 0 {
return libsqlError(fmt.Sprint("failed to get blob for column ", i), statusCode, errMsg)
}
dest[i] = C.GoBytes(unsafe.Pointer(nativeBlob.ptr), C.int(nativeBlob.len))
C.libsql_free_blob(nativeBlob)
case TYPE_TEXT:
var ptr *C.char
var errMsg *C.char
statusCode := C.libsql_get_string(row, C.int(i), &ptr, &errMsg)
if statusCode != 0 {
return libsqlError(fmt.Sprint("failed to get string for column ", i), statusCode, errMsg)
}
str := C.GoString(ptr)
C.libsql_free_string(ptr)
for _, format := range []string{
time.RFC3339Nano,
"2006-01-02 15:04:05.999999999-07:00",
"2006-01-02T15:04:05.999999999-07:00",
"2006-01-02 15:04:05.999999999",
"2006-01-02T15:04:05.999999999",
"2006-01-02 15:04:05",
"2006-01-02T15:04:05",
"2006-01-02 15:04",
"2006-01-02T15:04",
"2006-01-02",
} {
if t, err := time.ParseInLocation(format, str, time.UTC); err == nil {
dest[i] = t
continue Outerloop
}
}
dest[i] = str
}
}
return nil
}
func (c *conn) QueryContext(ctx context.Context, query string, args []sqldriver.NamedValue) (sqldriver.Rows, error) {
rowsNativePtr, err := c.execute(query, args, false)
if err != nil {
return nil, err
}
return newRows(rowsNativePtr)
}