forked from tursodatabase/go-libsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsql.go
More file actions
699 lines (576 loc) · 15 KB
/
Copy pathlibsql.go
File metadata and controls
699 lines (576 loc) · 15 KB
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
//go:build cgo
// +build cgo
package libsql
/*
#cgo CFLAGS: -I${SRCDIR}/lib/
#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-apple-darwin
#cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-apple-darwin
#cgo linux,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-unknown-linux-gnu
#cgo linux,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-unknown-linux-gnu
#cgo windows,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-unknown-linux-gnu
#cgo LDFLAGS: -llibsql
#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"
"database/sql/driver"
"errors"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
"unsafe"
)
type Batchable interface {
Batch(query string) error
}
type config struct {
authToken *string
readYourWrites *bool
encryptionKey *string
syncInterval *time.Duration
}
type Option interface {
apply(*config) error
}
type option func(*config) error
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 NewConnector(ConnectorOptions{
Url: primaryUrl,
Path: dbPath,
AuthToken: authToken,
EncryptionKey: encryptionKey,
DisableReadYourWrites: !readYourWrites,
SyncInterval: uint64(syncInterval.Milliseconds()),
})
}
// goErr takes ownership of err, any references to err after calling goErr are invalid
func goErr(err *C.libsql_error_t) error {
defer C.libsql_error_deinit(err)
return errors.New(C.GoString(C.libsql_error_message(err)))
}
func cString(s string) (*C.char, func()) {
if s == "" {
return nil, func() {}
}
cs := C.CString(s)
return cs, func() {
C.free(unsafe.Pointer(cs))
}
}
func init() {
sql.Register("libsql", Driver{})
}
type Replicated struct {
FrameNo int
FramesSynced int
}
type Driver struct{}
func (d Driver) Open(dbAddress string) (driver.Conn, error) {
connector, err := d.OpenConnector(dbAddress)
if err != nil {
return nil, err
}
return connector.Connect(context.Background())
}
func (d Driver) OpenConnector(dbAddress string) (driver.Connector, error) {
if strings.TrimSpace(dbAddress) == ":memory:" {
return NewConnector(ConnectorOptions{})
}
u, err := url.Parse(dbAddress)
if err != nil {
return nil, err
}
path := u.Query().Get("path")
authToken := u.Query().Get("authToken")
encryptionKey := u.Query().Get("encryptionKey")
var withWebpki bool
{
s := u.Query().Get("withWebpki")
switch s {
case "true":
withWebpki = true
case "false":
withWebpki = false
case "":
withWebpki = false
default:
return nil, errors.New("withWebpki must be either `true` or `false`")
}
}
var readYourWrites bool
{
s := u.Query().Get("readYourWrites")
switch s {
case "true":
readYourWrites = true
case "false":
readYourWrites = false
case "":
readYourWrites = false
default:
return nil, errors.New("readYourWrites must be either `true` or `false`")
}
}
syncInterval := 0
{
s := u.Query().Get("syncInterval")
if s != "" {
syncInterval, err = strconv.Atoi(s)
if err != nil {
return nil, err
}
}
}
switch u.Scheme {
case "file":
return NewConnector(ConnectorOptions{
Path: u.Opaque,
EncryptionKey: encryptionKey,
})
case "http", "https", "libsql":
return NewConnector(ConnectorOptions{
Path: path,
Url: "libsql://" + u.Hostname(),
AuthToken: authToken,
EncryptionKey: encryptionKey,
SyncInterval: uint64(syncInterval),
WithWebpki: withWebpki,
DisableReadYourWrites: !readYourWrites,
})
}
return nil, fmt.Errorf("Unsupported URL scheme: %s\nThis driver supports only URLs that start with libsql://, file:, https:// or http://", u.Scheme)
}
type ConnectorOptions struct {
Url string
Path string
AuthToken string
EncryptionKey string
SyncInterval uint64
WithWebpki bool
DisableReadYourWrites bool
}
type Connector struct {
inner C.libsql_database_t
}
func NewConnector(opt ConnectorOptions) (*Connector, error) {
path, free := cString(opt.Path)
defer free()
url, free := cString(opt.Url)
defer free()
authToken, free := cString(opt.AuthToken)
defer free()
println(opt.EncryptionKey)
encryptionKey, free := cString(opt.EncryptionKey)
defer free()
db := C.libsql_database_init(C.libsql_database_desc_t{
path: path,
url: url,
auth_token: authToken,
encryption_key: encryptionKey,
disable_read_your_writes: C.bool(opt.DisableReadYourWrites),
webpki: C.bool(opt.WithWebpki),
sync_interval: C.uint64_t(opt.SyncInterval),
})
if db.err != nil {
return nil, goErr(db.err)
}
return &Connector{inner: db}, nil
}
func (c *Connector) Sync() (Replicated, error) {
sync := C.libsql_database_sync(c.inner)
if sync.err != nil {
return Replicated{}, goErr(sync.err)
}
return Replicated{
FrameNo: int(sync.frame_no),
FramesSynced: int(sync.frames_synced),
}, nil
}
func (c *Connector) Close() error {
if c.inner.inner == nil {
return nil
}
C.libsql_database_deinit(c.inner)
c.inner.inner = nil
return nil
}
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
conn := C.libsql_database_connect(c.inner)
if conn.err != nil {
return nil, goErr(conn.err)
}
return &connection{inner: conn}, nil
}
func (c *Connector) Driver() driver.Driver {
return Driver{}
}
type connection struct {
inner C.libsql_connection_t
tx C.libsql_transaction_t
in_transaction bool
}
func (conn *connection) Batch(query string) error {
cQuery, free := cString(query)
defer free()
var batch C.libsql_batch_t
if conn.in_transaction {
batch = C.libsql_transaction_batch(conn.tx, cQuery)
} else {
batch = C.libsql_connection_batch(conn.inner, cQuery)
}
if batch.err != nil {
return goErr(batch.err)
}
return nil
}
func (conn *connection) Prepare(query string) (driver.Stmt, error) {
return conn.PrepareContext(context.Background(), query)
}
func (conn *connection) Begin() (driver.Tx, error) {
return conn.BeginTx(context.Background(), driver.TxOptions{})
}
func (conn *connection) 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)
}
conn.in_transaction = true
conn.tx = C.libsql_connection_transaction(conn.inner)
if conn.tx.err != nil {
return nil, goErr(conn.tx.err)
}
return &transaction{conn: conn}, nil
}
func (conn *connection) Close() error {
C.libsql_connection_deinit(conn.inner)
return nil
}
func (conn *connection) PrepareContext(ctx context.Context, queryString string) (driver.Stmt, error) {
query := C.CString(queryString)
defer C.free(unsafe.Pointer(query))
stmt := C.libsql_connection_prepare(conn.inner, query)
if stmt.err != nil {
return nil, goErr(stmt.err)
}
return &statement{inner: stmt}, nil
}
type statement struct {
inner C.libsql_statement_t
}
func (stmt *statement) Close() error {
if stmt.inner.inner == nil {
return nil
}
C.libsql_statement_deinit(stmt.inner)
stmt.inner.inner = nil
return nil
}
func (stmt *statement) NumInput() int {
return -1
}
func (stmt *statement) Exec(args []driver.Value) (driver.Result, error) {
named := make([]driver.NamedValue, len(args))
for i := range named {
named[i] = driver.NamedValue{Ordinal: i, Value: args[i]}
}
return stmt.ExecContext(context.Background(), named)
}
func (stmt *statement) Query(args []driver.Value) (driver.Rows, error) {
named := make([]driver.NamedValue, len(args))
for i := range named {
named[i] = driver.NamedValue{Ordinal: i, Value: args[i]}
}
return stmt.QueryContext(context.Background(), named)
}
func (stmt *statement) bindSingle(arg driver.NamedValue, toValue func(any) C.libsql_value_t) error {
if arg.Name == "" {
bind := C.libsql_statement_bind_value(stmt.inner, toValue(arg.Value))
if bind.err != nil {
return goErr(bind.err)
}
} else {
name := C.CString(arg.Name)
defer C.free(unsafe.Pointer(name))
bind := C.libsql_statement_bind_named(stmt.inner, name, toValue(arg.Value))
if bind.err != nil {
return goErr(bind.err)
}
}
return nil
}
func (stmt *statement) Bind(args []driver.NamedValue) error {
// TODO: Be more resilient to unordered positional arguments.
for _, arg := range args {
switch arg.Value.(type) {
case bool:
err := stmt.bindSingle(arg, func(a any) C.libsql_value_t {
v := 0
if a.(bool) {
v = 1
}
return C.libsql_integer(C.int64_t(v))
})
if err != nil {
return err
}
case int64:
err := stmt.bindSingle(arg, func(a any) C.libsql_value_t {
return C.libsql_integer(C.int64_t(a.(int64)))
})
if err != nil {
return err
}
case float64:
err := stmt.bindSingle(arg, func(a any) C.libsql_value_t {
return C.libsql_real(C.double(a.(float64)))
})
if err != nil {
return err
}
case time.Time:
valueString := arg.Value.(time.Time).Format(time.RFC3339Nano)
value := C.CString(valueString)
defer C.free(unsafe.Pointer(value))
err := stmt.bindSingle(arg, func(a any) C.libsql_value_t {
return C.libsql_text(value, C.ulong(len(valueString)))
})
if err != nil {
return err
}
case string:
valueString := arg.Value.(string)
value := C.CString(valueString)
defer C.free(unsafe.Pointer(value))
err := stmt.bindSingle(arg, func(a any) C.libsql_value_t {
return C.libsql_text(value, C.size_t(len(valueString)))
})
if err != nil {
return err
}
case []byte:
valueBytes := arg.Value.([]byte)
value := C.CBytes(valueBytes)
defer C.free(unsafe.Pointer(value))
err := stmt.bindSingle(arg, func(a any) C.libsql_value_t {
return C.libsql_blob((*C.uchar)(value), C.size_t(len(valueBytes)))
})
if err != nil {
return err
}
case nil:
err := stmt.bindSingle(arg, func(a any) C.libsql_value_t {
return C.libsql_null()
})
if err != nil {
return err
}
}
}
return nil
}
func (stmt *statement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
err := stmt.Bind(args)
if err != nil {
return nil, err
}
exec := C.libsql_statement_execute(stmt.inner)
if exec.err != nil {
return nil, goErr(exec.err)
}
return &result{rowsAffected: int64(exec.rows_changed)}, nil
}
func (stmt *statement) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
err := stmt.Bind(args)
if err != nil {
return nil, err
}
rows := C.libsql_statement_query(stmt.inner)
if rows.err != nil {
return nil, goErr(rows.err)
}
return &Rows{inner: rows}, nil
}
type Rows struct {
inner C.libsql_rows_t
}
func fromValue(v C.libsql_value_t) driver.Value {
switch v._type {
case C.LIBSQL_TYPE_INTEGER:
return int64(*(*C.int64_t)(unsafe.Pointer(&v.value[0])))
case C.LIBSQL_TYPE_REAL:
return float64(*(*C.double)(unsafe.Pointer(&v.value[0])))
case C.LIBSQL_TYPE_TEXT:
slice := *(*C.libsql_slice_t)(unsafe.Pointer(&v.value[0]))
defer C.libsql_slice_deinit(slice)
str := C.GoString((*C.char)(slice.ptr))
{
str := strings.TrimSuffix(str, "Z")
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 {
return t
}
}
}
return str
case C.LIBSQL_TYPE_BLOB:
slice := *(*C.libsql_slice_t)(unsafe.Pointer(&v.value[0]))
defer C.libsql_slice_deinit(slice)
return C.GoBytes(slice.ptr, C.int(slice.len))
case C.LIBSQL_TYPE_NULL:
return nil
}
panic("unreachable")
}
func (rows *Rows) Close() error {
C.libsql_rows_deinit(rows.inner)
return nil
}
func (rows *Rows) Columns() []string {
columns := make([]string, C.libsql_rows_column_count(rows.inner))
for i := range columns {
name := C.libsql_rows_column_name(rows.inner, C.int(i))
defer C.libsql_slice_deinit(name)
columns[i] = C.GoString((*C.char)(name.ptr))
}
return columns
}
func (rows *Rows) Next(dest []driver.Value) error {
row := C.libsql_rows_next(rows.inner)
if row.err != nil {
return goErr(row.err)
}
if C.libsql_row_empty(row) {
return io.EOF
}
for i := range dest {
result := C.libsql_row_value(row, C.int(i))
if result.err != nil {
return goErr(result.err)
}
dest[i] = fromValue(result.ok)
}
return nil
}
type result struct {
rowsAffected int64
}
func (res *result) LastInsertId() (int64, error) {
return -1, nil
}
func (res *result) RowsAffected() (int64, error) {
return res.rowsAffected, nil
}
type transaction struct {
conn *connection
}
func (t *transaction) Commit() error {
if !t.conn.in_transaction {
return errors.New("Not inside a transaction")
}
C.libsql_transaction_commit(t.conn.tx)
t.conn.tx = C.libsql_transaction_t{}
t.conn.in_transaction = false
return nil
}
func (t *transaction) Rollback() error {
if !t.conn.in_transaction {
return errors.New("Not inside a transaction")
}
C.libsql_transaction_rollback(t.conn.tx)
t.conn.tx = C.libsql_transaction_t{}
t.conn.in_transaction = false
return nil
}