-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvtab.go
More file actions
915 lines (849 loc) · 25.8 KB
/
vtab.go
File metadata and controls
915 lines (849 loc) · 25.8 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
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
// Copyright 2025 The Sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite // import "modernc.org/sqlite"
import (
"fmt"
"math"
"sync"
"unsafe"
"modernc.org/libc"
"modernc.org/libc/sys/types"
sqlite3 "modernc.org/sqlite/lib"
"modernc.org/sqlite/vtab"
)
func init() {
vtab.SetRegisterFunc(registerModule)
}
var (
// vtabModules tracks Go virtual table modules registered via the vtab
// package. Each module is identified by an integer ID used as pAux when
// calling sqlite3_create_module_v2, so that trampolines can recover the
// Go Module implementation.
vtabModules = struct {
mu sync.RWMutex
m map[uintptr]*goModule
ids idGen
// name2id keeps stable IDs per module name to avoid unbounded growth
// across connections.
name2id map[string]uintptr
}{
m: make(map[uintptr]*goModule),
name2id: make(map[string]uintptr),
}
// nativeModules holds sqlite3_module instances for registered modules,
// allocated via libc so that transpiled C code can access them without
// tripping Go's checkptr instrumentation.
nativeModules = struct {
mu sync.RWMutex
m map[string]uintptr
}{
m: make(map[string]uintptr),
}
// vtabTables maps sqlite3_vtab* (pVtab) to the corresponding Go Table.
vtabTables = struct {
mu sync.RWMutex
m map[uintptr]*goTable
}{
m: make(map[uintptr]*goTable),
}
// vtabCursors maps sqlite3_vtab_cursor* (pCursor) to the corresponding Go
// Cursor.
vtabCursors = struct {
mu sync.RWMutex
m map[uintptr]*goCursor
}{
m: make(map[uintptr]*goCursor),
}
)
// goModule wraps a vtab.Module implementation with its name.
type goModule struct {
name string
impl vtab.Module
}
// goTable wraps a vtab.Table implementation and remembers its module.
type goTable struct {
mod *goModule
impl vtab.Table
}
// goCursor wraps a vtab.Cursor implementation and remembers its table.
type goCursor struct {
table *goTable
impl vtab.Cursor
}
// Use aliases of the underlying lib types so field layouts remain correct.
type cIndexConstraint = sqlite3.Tsqlite3_index_constraint
type cIndexOrderBy = sqlite3.Tsqlite3_index_orderby
type cConstraintUsage = sqlite3.Tsqlite3_index_constraint_usage
// registerModule is installed as the hook for vtab.RegisterModule.
func registerModule(name string, m vtab.Module) error {
if _, exists := d.modules[name]; exists {
return fmt.Errorf("sqlite: module %q already registered", name)
}
d.modules[name] = m
return nil
}
// registerModules installs all globally registered vtab modules on this
// connection by calling sqlite3_create_module_v2 for each one.
func (c *conn) registerModules() error {
for name, mod := range d.modules {
if err := c.registerSingleModule(name, mod); err != nil {
return err
}
}
return nil
}
func (c *conn) registerSingleModule(name string, m vtab.Module) error {
// Allocate or reuse a stable ID for this module name and remember the Go implementation.
vtabModules.mu.Lock()
modID, ok := vtabModules.name2id[name]
if !ok {
modID = vtabModules.ids.next()
vtabModules.name2id[name] = modID
}
vtabModules.m[modID] = &goModule{name: name, impl: m}
vtabModules.mu.Unlock()
nativeModules.mu.Lock()
defer nativeModules.mu.Unlock()
var modPtr uintptr
if existing, exists := nativeModules.m[name]; exists {
modPtr = existing
} else {
// Allocate with the C allocator so the transpiled SQLite code can
// access the struct without tripping Go's checkptr.
modPtr = libc.Xcalloc(c.tls, 1, types.Size_t(unsafe.Sizeof(sqlite3.Sqlite3_module{})))
if modPtr == 0 {
if !ok {
vtabModules.mu.Lock()
delete(vtabModules.name2id, name)
delete(vtabModules.m, modID)
vtabModules.mu.Unlock()
}
return fmt.Errorf("sqlite: failed to allocate module %q", name)
}
mod := (*sqlite3.Sqlite3_module)(unsafe.Pointer(modPtr))
mod.FiVersion = 2
mod.FxCreate = cFuncPointer(vtabCreateTrampoline)
mod.FxConnect = cFuncPointer(vtabConnectTrampoline)
mod.FxBestIndex = cFuncPointer(vtabBestIndexTrampoline)
mod.FxDisconnect = cFuncPointer(vtabDisconnectTrampoline)
mod.FxDestroy = cFuncPointer(vtabDestroyTrampoline)
mod.FxOpen = cFuncPointer(vtabOpenTrampoline)
mod.FxClose = cFuncPointer(vtabCloseTrampoline)
mod.FxFilter = cFuncPointer(vtabFilterTrampoline)
mod.FxNext = cFuncPointer(vtabNextTrampoline)
mod.FxEof = cFuncPointer(vtabEofTrampoline)
mod.FxColumn = cFuncPointer(vtabColumnTrampoline)
mod.FxRowid = cFuncPointer(vtabRowidTrampoline)
mod.FxFindFunction = cFuncPointer(vtabFindFunctionTrampoline)
mod.FxRename = cFuncPointer(vtabRenameTrampoline)
mod.FxUpdate = cFuncPointer(vtabUpdateTrampoline)
mod.FxBegin = cFuncPointer(vtabBeginTrampoline)
mod.FxSync = cFuncPointer(vtabSyncTrampoline)
mod.FxCommit = cFuncPointer(vtabCommitTrampoline)
mod.FxRollback = cFuncPointer(vtabRollbackTrampoline)
mod.FxSavepoint = cFuncPointer(vtabSavepointTrampoline)
mod.FxRelease = cFuncPointer(vtabReleaseTrampoline)
mod.FxRollbackTo = cFuncPointer(vtabRollbackToTrampoline)
nativeModules.m[name] = modPtr
}
// Prepare C string for module name.
zName, err := libc.CString(name)
if err != nil {
return err
}
defer libc.Xfree(c.tls, zName)
// Register the module with this connection.
if rc := sqlite3.Xsqlite3_create_module_v2(c.tls, c.db, zName, modPtr, modID, 0); rc != sqlite3.SQLITE_OK {
return fmt.Errorf("create_module %q: %w", name, c.errstr(rc))
}
return nil
}
func vtabConfig(tls *libc.TLS, db uintptr, op int32, args ...int32) error {
var va uintptr
if len(args) > 1 {
return fmt.Errorf("vtab_config: too many args (%d)", len(args))
}
if len(args) == 1 {
const vaSize = 8
p := sqlite3.Xsqlite3_malloc(tls, vaSize)
if p == 0 {
return fmt.Errorf("vtab: out of memory")
}
defer sqlite3.Xsqlite3_free(tls, p)
libc.VaList(p, args[0])
va = p
}
if rc := sqlite3.Xsqlite3_vtab_config(tls, db, op, va); rc != sqlite3.SQLITE_OK {
return fmt.Errorf("vtab_config op=%d: rc=%d", op, rc)
}
return nil
}
// vtabCreateTrampoline is the xCreate callback. It invokes the corresponding
// Go vtab.Module.Create method, declares a default schema based on argv, and
// allocates a sqlite3_vtab.
func vtabCreateTrampoline(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) int32 {
gm := lookupGoModule(pAux)
if gm == nil {
setVtabError(tls, pzErr, fmt.Sprintf("vtab: unknown module id %d", pAux))
return sqlite3.SQLITE_ERROR
}
args := extractVtabArgs(tls, argc, argv)
ctx := vtab.NewContextWithConfig(func(schema string) error {
zSchema, err := libc.CString(schema)
if err != nil {
return err
}
defer libc.Xfree(tls, zSchema)
if rc := sqlite3.Xsqlite3_declare_vtab(tls, db, zSchema); rc != sqlite3.SQLITE_OK {
return fmt.Errorf("declare_vtab failed: rc=%d", rc)
}
return nil
}, func() error {
return vtabConfig(tls, db, sqlite3.SQLITE_VTAB_CONSTRAINT_SUPPORT, 1)
}, func(op int32, args ...int32) error {
return vtabConfig(tls, db, op, args...)
})
tbl, err := gm.impl.Create(ctx, args)
if err != nil {
setVtabError(tls, pzErr, err.Error())
return sqlite3.SQLITE_ERROR
}
sz := unsafe.Sizeof(sqlite3.Sqlite3_vtab{})
p := sqlite3.Xsqlite3_malloc(tls, int32(sz))
if p == 0 {
setVtabError(tls, pzErr, "vtab: out of memory")
return sqlite3.SQLITE_NOMEM
}
mem := (*libc.RawMem)(unsafe.Pointer(p))[:sz:sz]
for i := range mem {
mem[i] = 0
}
*(*uintptr)(unsafe.Pointer(ppVtab)) = p
gt := &goTable{mod: gm, impl: tbl}
vtabTables.mu.Lock()
vtabTables.m[p] = gt
vtabTables.mu.Unlock()
return sqlite3.SQLITE_OK
}
// vtabConnectTrampoline is the xConnect callback. It mirrors
// vtabCreateTrampoline but calls Module.Connect.
func vtabConnectTrampoline(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) int32 {
gm := lookupGoModule(pAux)
if gm == nil {
setVtabError(tls, pzErr, fmt.Sprintf("vtab: unknown module id %d", pAux))
return sqlite3.SQLITE_ERROR
}
args := extractVtabArgs(tls, argc, argv)
ctx := vtab.NewContextWithConfig(func(schema string) error {
zSchema, err := libc.CString(schema)
if err != nil {
return err
}
defer libc.Xfree(tls, zSchema)
if rc := sqlite3.Xsqlite3_declare_vtab(tls, db, zSchema); rc != sqlite3.SQLITE_OK {
return fmt.Errorf("declare_vtab failed: rc=%d", rc)
}
return nil
}, func() error {
return vtabConfig(tls, db, sqlite3.SQLITE_VTAB_CONSTRAINT_SUPPORT, 1)
}, func(op int32, args ...int32) error {
return vtabConfig(tls, db, op, args...)
})
tbl, err := gm.impl.Connect(ctx, args)
if err != nil {
setVtabError(tls, pzErr, err.Error())
return sqlite3.SQLITE_ERROR
}
sz := unsafe.Sizeof(sqlite3.Sqlite3_vtab{})
p := sqlite3.Xsqlite3_malloc(tls, int32(sz))
if p == 0 {
setVtabError(tls, pzErr, "vtab: out of memory")
return sqlite3.SQLITE_NOMEM
}
mem := (*libc.RawMem)(unsafe.Pointer(p))[:sz:sz]
for i := range mem {
mem[i] = 0
}
*(*uintptr)(unsafe.Pointer(ppVtab)) = p
gt := &goTable{mod: gm, impl: tbl}
vtabTables.mu.Lock()
vtabTables.m[p] = gt
vtabTables.mu.Unlock()
return sqlite3.SQLITE_OK
}
// vtabBestIndexTrampoline maps sqlite3_index_info to vtab.IndexInfo and
// delegates to Table.BestIndex. It also mirrors constraint and ORDER BY
// information into the Go structure.
func vtabBestIndexTrampoline(tls *libc.TLS, pVtab uintptr, pInfo uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
idx := (*sqlite3.Sqlite3_index_info)(unsafe.Pointer(pInfo))
info := &vtab.IndexInfo{}
// Populate Constraints from sqlite3_index_info.aConstraint.
if idx.FnConstraint > 0 && idx.FaConstraint != 0 {
n := int(idx.FnConstraint)
cs := make([]vtab.Constraint, 0, n)
base := idx.FaConstraint
sz := unsafe.Sizeof(cIndexConstraint{})
for i := 0; i < n; i++ {
c := (*cIndexConstraint)(unsafe.Pointer(base + uintptr(i)*sz))
op := vtab.OpUnknown
switch int32(c.Fop) {
case sqlite3.SQLITE_INDEX_CONSTRAINT_EQ:
op = vtab.OpEQ
case sqlite3.SQLITE_INDEX_CONSTRAINT_GT:
op = vtab.OpGT
case sqlite3.SQLITE_INDEX_CONSTRAINT_LE:
op = vtab.OpLE
case sqlite3.SQLITE_INDEX_CONSTRAINT_LT:
op = vtab.OpLT
case sqlite3.SQLITE_INDEX_CONSTRAINT_GE:
op = vtab.OpGE
case sqlite3.SQLITE_INDEX_CONSTRAINT_MATCH:
op = vtab.OpMATCH
case sqlite3.SQLITE_INDEX_CONSTRAINT_NE:
op = vtab.OpNE
case sqlite3.SQLITE_INDEX_CONSTRAINT_IS:
op = vtab.OpIS
case sqlite3.SQLITE_INDEX_CONSTRAINT_ISNOT:
op = vtab.OpISNOT
case sqlite3.SQLITE_INDEX_CONSTRAINT_ISNULL:
op = vtab.OpISNULL
case sqlite3.SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
op = vtab.OpISNOTNULL
case sqlite3.SQLITE_INDEX_CONSTRAINT_LIKE:
op = vtab.OpLIKE
case sqlite3.SQLITE_INDEX_CONSTRAINT_GLOB:
op = vtab.OpGLOB
case sqlite3.SQLITE_INDEX_CONSTRAINT_REGEXP:
op = vtab.OpREGEXP
case sqlite3.SQLITE_INDEX_CONSTRAINT_FUNCTION:
op = vtab.OpFUNCTION
case sqlite3.SQLITE_INDEX_CONSTRAINT_LIMIT:
op = vtab.OpLIMIT
case sqlite3.SQLITE_INDEX_CONSTRAINT_OFFSET:
op = vtab.OpOFFSET
}
cs = append(cs, vtab.Constraint{
Column: int(c.FiColumn),
Op: op,
Usable: c.Fusable != 0,
ArgIndex: -1, // 0-based; -1 means ignore
Omit: false,
})
}
info.Constraints = cs
}
// Populate OrderBy from sqlite3_index_info.aOrderBy.
if idx.FnOrderBy > 0 && idx.FaOrderBy != 0 {
n := int(idx.FnOrderBy)
obs := make([]vtab.OrderBy, 0, n)
base := idx.FaOrderBy
sz := unsafe.Sizeof(cIndexOrderBy{})
for i := 0; i < n; i++ {
ob := (*cIndexOrderBy)(unsafe.Pointer(base + uintptr(i)*sz))
obs = append(obs, vtab.OrderBy{
Column: int(ob.FiColumn),
Desc: ob.Fdesc != 0,
})
}
info.OrderBy = obs
}
// Populate ColUsed and idxFlags for module visibility.
if idx.FcolUsed != 0 {
info.ColUsed = uint64(idx.FcolUsed)
}
if idx.FidxFlags != 0 {
info.IdxFlags = int(idx.FidxFlags)
}
if err := gt.impl.BestIndex(info); err != nil {
// Report error via zErrMsg on pVtab.
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
// Propagate any ArgIndex assignments back into aConstraintUsage so that
// SQLite will populate xFilter's argv[] accordingly.
if idx.FnConstraint > 0 && idx.FaConstraintUsage != 0 && len(info.Constraints) > 0 {
n := int(idx.FnConstraint)
base := idx.FaConstraintUsage
sz := unsafe.Sizeof(cConstraintUsage{})
for i := 0; i < n && i < len(info.Constraints); i++ {
cu := (*cConstraintUsage)(unsafe.Pointer(base + uintptr(i)*sz))
argIndex := info.Constraints[i].ArgIndex
if argIndex >= 0 {
// Go ArgIndex is 0-based; SQLite wants 1-based.
cu.FargvIndex = int32(argIndex + 1)
}
if info.Constraints[i].Omit {
cu.Fomit = 1
}
}
}
// Guard against int32 overflow: SQLite expects idxNum as int32.
if info.IdxNum < math.MinInt32 || info.IdxNum > math.MaxInt32 {
setVtabZErrMsg(tls, pVtab, fmt.Sprintf("vtab: IdxNum %d out of int32 range", info.IdxNum))
return sqlite3.SQLITE_ERROR
}
idx.FidxNum = int32(info.IdxNum)
if info.IdxStr != "" {
// Allocate using SQLite allocator because needToFreeIdxStr=1 instructs
// SQLite to free the string with sqlite3_free.
z := sqlite3AllocCString(tls, info.IdxStr)
if z != 0 {
idx.FidxStr = z
idx.FneedToFreeIdxStr = 1
}
}
if info.OrderByConsumed {
idx.ForderByConsumed = 1
}
if info.IdxFlags != 0 {
idx.FidxFlags = int32(info.IdxFlags)
}
if info.EstimatedCost != 0 {
idx.FestimatedCost = info.EstimatedCost
}
if info.EstimatedRows != 0 {
idx.FestimatedRows = sqlite3.Sqlite3_int64(info.EstimatedRows)
}
return sqlite3.SQLITE_OK
}
// vtabDisconnectTrampoline is xDisconnect. It frees the sqlite3_vtab and
// calls Table.Disconnect.
func vtabDisconnectTrampoline(tls *libc.TLS, pVtab uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt != nil {
_ = gt.impl.Disconnect()
vtabTables.mu.Lock()
delete(vtabTables.m, pVtab)
vtabTables.mu.Unlock()
}
sqlite3.Xsqlite3_free(tls, pVtab)
return sqlite3.SQLITE_OK
}
// vtabDestroyTrampoline is xDestroy. Currently identical to Disconnect.
func vtabDestroyTrampoline(tls *libc.TLS, pVtab uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt != nil {
_ = gt.impl.Destroy()
vtabTables.mu.Lock()
delete(vtabTables.m, pVtab)
vtabTables.mu.Unlock()
}
sqlite3.Xsqlite3_free(tls, pVtab)
return sqlite3.SQLITE_OK
}
// vtabOpenTrampoline is xOpen. It allocates an empty sqlite3_vtab_cursor and
// creates a Go Cursor via Table.Open.
func vtabOpenTrampoline(tls *libc.TLS, pVtab uintptr, ppCursor uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
curImpl, err := gt.impl.Open()
if err != nil {
return sqlite3.SQLITE_ERROR
}
sz := unsafe.Sizeof(sqlite3.Sqlite3_vtab_cursor{})
p := sqlite3.Xsqlite3_malloc(tls, int32(sz))
if p == 0 {
return sqlite3.SQLITE_NOMEM
}
mem := (*libc.RawMem)(unsafe.Pointer(p))[:sz:sz]
for i := range mem {
mem[i] = 0
}
*(*uintptr)(unsafe.Pointer(ppCursor)) = p
// Link cursor back to its vtab so error reporting can set zErrMsg.
cur := (*sqlite3.Sqlite3_vtab_cursor)(unsafe.Pointer(p))
cur.FpVtab = pVtab
gc := &goCursor{table: gt, impl: curImpl}
vtabCursors.mu.Lock()
vtabCursors.m[p] = gc
vtabCursors.mu.Unlock()
return sqlite3.SQLITE_OK
}
// vtabCloseTrampoline is xClose. It frees the sqlite3_vtab_cursor and calls
// Cursor.Close.
func vtabCloseTrampoline(tls *libc.TLS, pCursor uintptr) int32 {
vtabCursors.mu.RLock()
gc := vtabCursors.m[pCursor]
vtabCursors.mu.RUnlock()
if gc != nil {
_ = gc.impl.Close()
vtabCursors.mu.Lock()
delete(vtabCursors.m, pCursor)
vtabCursors.mu.Unlock()
}
sqlite3.Xsqlite3_free(tls, pCursor)
return sqlite3.SQLITE_OK
}
// vtabFilterTrampoline is xFilter.
func vtabFilterTrampoline(tls *libc.TLS, pCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) int32 {
vtabCursors.mu.RLock()
gc := vtabCursors.m[pCursor]
vtabCursors.mu.RUnlock()
if gc == nil {
return sqlite3.SQLITE_ERROR
}
var idxStrGo string
if idxStr != 0 {
idxStrGo = libc.GoString(idxStr)
}
vals := functionArgs(tls, argc, argv)
if err := gc.impl.Filter(int(idxNum), idxStrGo, vals); err != nil {
// Set zErrMsg on the associated vtab for better diagnostics.
if pCursor != 0 {
cur := (*sqlite3.Sqlite3_vtab_cursor)(unsafe.Pointer(pCursor))
if cur.FpVtab != 0 {
setVtabZErrMsg(tls, cur.FpVtab, err.Error())
}
}
return sqlite3.SQLITE_ERROR
}
return sqlite3.SQLITE_OK
}
// vtabNextTrampoline is xNext.
func vtabNextTrampoline(tls *libc.TLS, pCursor uintptr) int32 {
_ = tls
vtabCursors.mu.RLock()
gc := vtabCursors.m[pCursor]
vtabCursors.mu.RUnlock()
if gc == nil {
return sqlite3.SQLITE_ERROR
}
if err := gc.impl.Next(); err != nil {
return sqlite3.SQLITE_ERROR
}
return sqlite3.SQLITE_OK
}
// vtabEofTrampoline is xEof.
func vtabEofTrampoline(tls *libc.TLS, pCursor uintptr) int32 {
_ = tls
vtabCursors.mu.RLock()
gc := vtabCursors.m[pCursor]
vtabCursors.mu.RUnlock()
if gc == nil || gc.impl.Eof() {
return 1
}
return 0
}
// vtabColumnTrampoline is xColumn.
func vtabColumnTrampoline(tls *libc.TLS, pCursor uintptr, ctx uintptr, iCol int32) int32 {
vtabCursors.mu.RLock()
gc := vtabCursors.m[pCursor]
vtabCursors.mu.RUnlock()
if gc == nil {
sqlite3.Xsqlite3_result_null(tls, ctx)
return sqlite3.SQLITE_OK
}
val, err := gc.impl.Column(int(iCol))
if err != nil {
// Report via result error on the context.
z, cerr := libc.CString(err.Error())
if cerr == nil {
defer libc.Xfree(tls, z)
sqlite3.Xsqlite3_result_error(tls, ctx, z, -1)
sqlite3.Xsqlite3_result_error_code(tls, ctx, sqlite3.SQLITE_ERROR)
} else {
sqlite3.Xsqlite3_result_error_code(tls, ctx, sqlite3.SQLITE_ERROR)
}
return sqlite3.SQLITE_ERROR
}
if err := functionReturnValue(tls, ctx, val); err != nil {
// Include a descriptive error message for easier debugging
// (e.g., unsupported type conversions).
if err != nil {
z, cerr := libc.CString(err.Error())
if cerr == nil {
defer libc.Xfree(tls, z)
sqlite3.Xsqlite3_result_error(tls, ctx, z, -1)
}
}
sqlite3.Xsqlite3_result_error_code(tls, ctx, sqlite3.SQLITE_ERROR)
return sqlite3.SQLITE_ERROR
}
return sqlite3.SQLITE_OK
}
// vtabRowidTrampoline is xRowid.
func vtabRowidTrampoline(tls *libc.TLS, pCursor uintptr, pRowid uintptr) int32 {
_ = tls
vtabCursors.mu.RLock()
gc := vtabCursors.m[pCursor]
vtabCursors.mu.RUnlock()
if gc == nil {
*(*int64)(unsafe.Pointer(pRowid)) = 0
return sqlite3.SQLITE_OK
}
rowid, err := gc.impl.Rowid()
if err != nil {
*(*int64)(unsafe.Pointer(pRowid)) = 0
return sqlite3.SQLITE_ERROR
}
*(*int64)(unsafe.Pointer(pRowid)) = rowid
return sqlite3.SQLITE_OK
}
func lookupGoModule(id uintptr) *goModule {
vtabModules.mu.RLock()
defer vtabModules.mu.RUnlock()
return vtabModules.m[id]
}
func extractVtabArgs(tls *libc.TLS, argc int32, argv uintptr) []string {
args := make([]string, argc)
for i := int32(0); i < argc; i++ {
cstr := *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*unsafe.Sizeof(uintptr(0))))
args[i] = libc.GoString(cstr)
}
return args
}
func setVtabError(tls *libc.TLS, pzErr uintptr, msg string) {
if pzErr == 0 {
return
}
z := sqlite3AllocCString(tls, msg)
if z == 0 {
return
}
*(*uintptr)(unsafe.Pointer(pzErr)) = z
}
// setVtabZErrMsg sets pVtab->zErrMsg to a newly allocated C string containing
// msg. SQLite will free this pointer.
func setVtabZErrMsg(tls *libc.TLS, pVtab uintptr, msg string) {
if pVtab == 0 {
return
}
vt := (*sqlite3.Sqlite3_vtab)(unsafe.Pointer(pVtab))
if vt.FzErrMsg != 0 {
sqlite3.Xsqlite3_free(tls, vt.FzErrMsg)
vt.FzErrMsg = 0
}
z := sqlite3AllocCString(tls, msg)
if z == 0 {
return
}
vt.FzErrMsg = z
}
// Optional vtab callbacks
// vtabFindFunctionTrampoline is xFindFunction. We currently do not expose a
// Go surface for per-table SQL functions; report not found (return 0).
func vtabFindFunctionTrampoline(tls *libc.TLS, pVtab uintptr, nArg int32, zName uintptr, pxFunc uintptr, ppArg uintptr) int32 {
_ = tls
_ = pVtab
_ = nArg
_ = zName
if pxFunc != 0 {
*(*uintptr)(unsafe.Pointer(pxFunc)) = 0
}
if ppArg != 0 {
*(*uintptr)(unsafe.Pointer(ppArg)) = 0
}
return 0 // not found
}
// vtabRenameTrampoline is xRename. Calls Table.Rename if implemented.
func vtabRenameTrampoline(tls *libc.TLS, pVtab uintptr, zNew uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
name := libc.GoString(zNew)
if r, ok := gt.impl.(interface{ Rename(string) error }); ok {
if err := r.Rename(name); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
}
return sqlite3.SQLITE_OK
}
// vtabUpdateTrampoline is xUpdate. Not supported by default; report read-only.
func vtabUpdateTrampoline(tls *libc.TLS, pVtab uintptr, argc int32, argv uintptr, pRowid uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
upd, ok := gt.impl.(interface {
Insert(cols []vtab.Value, rowid *int64) error
Update(oldRowid int64, cols []vtab.Value, newRowid *int64) error
Delete(oldRowid int64) error
})
if !ok {
return sqlite3.SQLITE_READONLY
}
// DELETE: argc == 1; argv[0]=oldRowid
if argc == 1 {
valPtr := *(*uintptr)(unsafe.Pointer(argv))
oldRowid := int64(0)
if sqlite3.Xsqlite3_value_type(tls, valPtr) != sqlite3.SQLITE_NULL {
oldRowid = int64(sqlite3.Xsqlite3_value_int64(tls, valPtr))
}
if err := upd.Delete(oldRowid); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
return sqlite3.SQLITE_OK
}
// INSERT or UPDATE: argc == N+2. argv[0]=oldRowid (NULL for insert),
// argv[1]=newRowid (or desired rowid for insert, may be NULL),
// argv[2..N+1]=column values.
if argc < 3 {
return sqlite3.SQLITE_MISUSE
}
nCols := argc - 2
// Extract column values starting from argv[2]
colsPtr := argv + uintptr(2)*sqliteValPtrSize
cols := functionArgs(tls, nCols, colsPtr)
// Determine old/new rowid
oldPtr := *(*uintptr)(unsafe.Pointer(argv + uintptr(0)*sqliteValPtrSize))
newPtr := *(*uintptr)(unsafe.Pointer(argv + uintptr(1)*sqliteValPtrSize))
oldIsNull := sqlite3.Xsqlite3_value_type(tls, oldPtr) == sqlite3.SQLITE_NULL
newIsNull := sqlite3.Xsqlite3_value_type(tls, newPtr) == sqlite3.SQLITE_NULL
if oldIsNull {
// INSERT
var rid int64
if !newIsNull {
rid = int64(sqlite3.Xsqlite3_value_int64(tls, newPtr))
}
if err := upd.Insert(cols, &rid); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
if pRowid != 0 {
*(*int64)(unsafe.Pointer(pRowid)) = rid
}
return sqlite3.SQLITE_OK
}
// UPDATE
oldRowid := int64(sqlite3.Xsqlite3_value_int64(tls, oldPtr))
var newRid int64
if !newIsNull {
newRid = int64(sqlite3.Xsqlite3_value_int64(tls, newPtr))
}
if err := upd.Update(oldRowid, cols, &newRid); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
if pRowid != 0 && newRid != 0 {
*(*int64)(unsafe.Pointer(pRowid)) = newRid
}
return sqlite3.SQLITE_OK
}
// Transactional callbacks
func vtabBeginTrampoline(tls *libc.TLS, pVtab uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
if tr, ok := gt.impl.(interface{ Begin() error }); ok {
if err := tr.Begin(); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
}
return sqlite3.SQLITE_OK
}
func vtabSyncTrampoline(tls *libc.TLS, pVtab uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
if tr, ok := gt.impl.(interface{ Sync() error }); ok {
if err := tr.Sync(); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
}
return sqlite3.SQLITE_OK
}
func vtabCommitTrampoline(tls *libc.TLS, pVtab uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
if tr, ok := gt.impl.(interface{ Commit() error }); ok {
if err := tr.Commit(); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
}
return sqlite3.SQLITE_OK
}
func vtabRollbackTrampoline(tls *libc.TLS, pVtab uintptr) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
if tr, ok := gt.impl.(interface{ Rollback() error }); ok {
if err := tr.Rollback(); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
}
return sqlite3.SQLITE_OK
}
func vtabSavepointTrampoline(tls *libc.TLS, pVtab uintptr, i int32) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
if tr, ok := gt.impl.(interface{ Savepoint(int) error }); ok {
if err := tr.Savepoint(int(i)); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
}
return sqlite3.SQLITE_OK
}
func vtabReleaseTrampoline(tls *libc.TLS, pVtab uintptr, i int32) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
if tr, ok := gt.impl.(interface{ Release(int) error }); ok {
if err := tr.Release(int(i)); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
}
return sqlite3.SQLITE_OK
}
func vtabRollbackToTrampoline(tls *libc.TLS, pVtab uintptr, i int32) int32 {
vtabTables.mu.RLock()
gt := vtabTables.m[pVtab]
vtabTables.mu.RUnlock()
if gt == nil {
return sqlite3.SQLITE_ERROR
}
if tr, ok := gt.impl.(interface{ RollbackTo(int) error }); ok {
if err := tr.RollbackTo(int(i)); err != nil {
setVtabZErrMsg(tls, pVtab, err.Error())
return sqlite3.SQLITE_ERROR
}
}
return sqlite3.SQLITE_OK
}