-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathvtab.zig
1315 lines (1050 loc) · 45.7 KB
/
vtab.zig
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const debug = std.debug;
const fmt = std.fmt;
const heap = std.heap;
const mem = std.mem;
const meta = std.meta;
const testing = std.testing;
const c = @import("c.zig").c;
const versionGreaterThanOrEqualTo = @import("c.zig").versionGreaterThanOrEqualTo;
const getTestDb = @import("test.zig").getTestDb;
const Diagnostics = @import("sqlite.zig").Diagnostics;
const Blob = @import("sqlite.zig").Blob;
const Text = @import("sqlite.zig").Text;
const helpers = @import("helpers.zig");
const logger = std.log.scoped(.vtab);
fn hasDecls(comptime T: type, comptime names: anytype) bool {
inline for (names) |name| {
if (!@hasDecl(T, name)) {
return false;
}
}
return true;
}
/// ModuleContext contains state that is needed by all implementations of virtual tables.
///
/// Currently there's only an allocator.
pub const ModuleContext = struct {
allocator: mem.Allocator,
};
fn dupeToSQLiteString(s: []const u8) [*c]u8 {
var buffer: [*c]u8 = @ptrCast(c.sqlite3_malloc(@intCast(s.len + 1)));
mem.copyForwards(u8, buffer[0..s.len], s);
buffer[s.len] = 0;
return buffer;
}
/// VTabDiagnostics is used by the user to report error diagnostics to the virtual table.
pub const VTabDiagnostics = struct {
const Self = @This();
allocator: mem.Allocator,
error_message: []const u8 = "unknown error",
pub fn setErrorMessage(self: *Self, comptime format_string: []const u8, values: anytype) void {
self.error_message = fmt.allocPrint(self.allocator, format_string, values) catch |err| switch (err) {
error.OutOfMemory => "can't set diagnostic message, out of memory",
};
}
};
pub const BestIndexBuilder = struct {
const Self = @This();
/// Constraint operator codes.
/// See https://sqlite.org/c3ref/c_index_constraint_eq.html
pub const ConstraintOp = if (versionGreaterThanOrEqualTo(3, 38, 0))
enum {
eq,
gt,
le,
lt,
ge,
match,
like,
glob,
regexp,
ne,
is_not,
is_not_null,
is_null,
is,
limit,
offset,
}
else
enum {
eq,
gt,
le,
lt,
ge,
match,
like,
glob,
regexp,
ne,
is_not,
is_not_null,
is_null,
is,
};
const ConstraintOpFromCodeError = error{
InvalidCode,
};
fn constraintOpFromCode(code: u8) ConstraintOpFromCodeError!ConstraintOp {
if (comptime versionGreaterThanOrEqualTo(3, 38, 0)) {
switch (code) {
c.SQLITE_INDEX_CONSTRAINT_LIMIT => return .limit,
c.SQLITE_INDEX_CONSTRAINT_OFFSET => return .offset,
else => {},
}
}
switch (code) {
c.SQLITE_INDEX_CONSTRAINT_EQ => return .eq,
c.SQLITE_INDEX_CONSTRAINT_GT => return .gt,
c.SQLITE_INDEX_CONSTRAINT_LE => return .le,
c.SQLITE_INDEX_CONSTRAINT_LT => return .lt,
c.SQLITE_INDEX_CONSTRAINT_GE => return .ge,
c.SQLITE_INDEX_CONSTRAINT_MATCH => return .match,
c.SQLITE_INDEX_CONSTRAINT_LIKE => return .like,
c.SQLITE_INDEX_CONSTRAINT_GLOB => return .glob,
c.SQLITE_INDEX_CONSTRAINT_REGEXP => return .regexp,
c.SQLITE_INDEX_CONSTRAINT_NE => return .ne,
c.SQLITE_INDEX_CONSTRAINT_ISNOT => return .is_not,
c.SQLITE_INDEX_CONSTRAINT_ISNOTNULL => return .is_not_null,
c.SQLITE_INDEX_CONSTRAINT_ISNULL => return .is_null,
c.SQLITE_INDEX_CONSTRAINT_IS => return .is,
else => return error.InvalidCode,
}
}
// WHERE clause constraint
pub const Constraint = struct {
// Column constrained. -1 for ROWID
column: isize,
op: ConstraintOp,
usable: bool,
usage: struct {
// If >0, constraint is part of argv to xFilter
argv_index: i32 = 0,
// Id >0, do not code a test for this constraint
omit: bool = false,
},
};
// ORDER BY clause
pub const OrderBy = struct {
column: usize,
order: enum {
desc,
asc,
},
};
/// Internal state
allocator: mem.Allocator,
id_str_buffer: std.ArrayList(u8),
index_info: *c.sqlite3_index_info,
/// List of WHERE clause constraints
///
/// Similar to `aConstraint` in the Inputs section of sqlite3_index_info except we embed the constraint usage in there too.
/// This makes it nicer to use for the user.
constraints: []Constraint,
/// Indicate which columns of the virtual table are actually used by the statement.
/// If the lowest bit of colUsed is set, that means that the first column is used.
/// The second lowest bit corresponds to the second column. And so forth.
///
/// Maps to the `colUsed` field.
columns_used: u64,
/// Index identifier.
/// This is passed to the filtering function to identify which index to use.
///
/// Maps to the `idxNum` and `idxStr` field in sqlite3_index_info.
/// Id id.id_str is non empty the string will be copied to a SQLite-allocated buffer and `needToFreeIdxStr` will be 1.
id: IndexIdentifier,
/// If the virtual table will output its rows already in the order specified by the ORDER BY clause then this can be set to true.
/// This will indicate to SQLite that it doesn't need to do a sorting pass.
///
/// Maps to the `orderByConsumed` field.
already_ordered: bool = false,
/// Estimated number of "disk access operations" required to execute this query.
///
/// Maps to the `estimatedCost` field.
estimated_cost: ?f64 = null,
/// Estimated number of rows returned by this query.
///
/// Maps to the `estimatedRows` field.
///
/// ODO(vincent): implement this
estimated_rows: ?i64 = null,
/// Additiounal flags for this index.
///
/// Maps to the `idxFlags` field.
flags: struct {
unique: bool = false,
} = .{},
const InitError = error{} || mem.Allocator.Error || ConstraintOpFromCodeError;
fn init(allocator: mem.Allocator, index_info: *c.sqlite3_index_info) InitError!Self {
const res = Self{
.allocator = allocator,
.index_info = index_info,
.id_str_buffer = std.ArrayList(u8).init(allocator),
.constraints = try allocator.alloc(Constraint, @intCast(index_info.nConstraint)),
.columns_used = @intCast(index_info.colUsed),
.id = .{},
};
for (res.constraints, 0..) |*constraint, i| {
const raw_constraint = index_info.aConstraint[i];
constraint.column = @intCast(raw_constraint.iColumn);
constraint.op = try constraintOpFromCode(raw_constraint.op);
constraint.usable = if (raw_constraint.usable == 1) true else false;
constraint.usage = .{};
}
return res;
}
/// Returns true if the column is used, false otherwise.
pub fn isColumnUsed(self: *Self, column: u6) bool {
const mask = @as(u64, 1) << column - 1;
return self.columns_used & mask == mask;
}
/// Builds the final index data.
///
/// Internally it populates the sqlite3_index_info "Outputs" fields using the information set by the user.
pub fn build(self: *Self) void {
var index_info = self.index_info;
// Populate the constraint usage
var constraint_usage = index_info.aConstraintUsage[0..self.constraints.len];
for (self.constraints, 0..) |constraint, i| {
constraint_usage[i].argvIndex = constraint.usage.argv_index;
constraint_usage[i].omit = if (constraint.usage.omit) 1 else 0;
}
// Identifiers
index_info.idxNum = @intCast(self.id.num);
if (self.id.str.len > 0) {
// Must always be NULL-terminated so add 1
const tmp: [*c]u8 = @ptrCast(c.sqlite3_malloc(@intCast(self.id.str.len + 1)));
mem.copyForwards(u8, tmp[0..self.id.str.len], self.id.str);
tmp[self.id.str.len] = 0;
index_info.idxStr = tmp;
index_info.needToFreeIdxStr = 1;
}
index_info.orderByConsumed = if (self.already_ordered) 1 else 0;
if (self.estimated_cost) |estimated_cost| {
index_info.estimatedCost = estimated_cost;
}
if (self.estimated_rows) |estimated_rows| {
index_info.estimatedRows = estimated_rows;
}
// Flags
index_info.idxFlags = 0;
if (self.flags.unique) {
index_info.idxFlags |= c.SQLITE_INDEX_SCAN_UNIQUE;
}
}
};
/// Identifies an index for a virtual table.
///
/// The user-provided buildBestIndex functions sets the index identifier.
/// These fields are meaningless for SQLite so they can be whatever you want as long as
/// both buildBestIndex and filter functions agree on what they mean.
pub const IndexIdentifier = struct {
num: i32 = 0,
str: []const u8 = "",
fn fromC(idx_num: c_int, idx_str: [*c]const u8) IndexIdentifier {
return IndexIdentifier{
.num = @intCast(idx_num),
.str = if (idx_str != null) mem.sliceTo(idx_str, 0) else "",
};
}
};
pub const FilterArg = struct {
value: ?*c.sqlite3_value,
pub fn as(self: FilterArg, comptime Type: type) Type {
var result: Type = undefined;
helpers.setTypeFromValue(Type, &result, self.value.?);
return result;
}
};
/// Validates that a type implements everything required to be a cursor for a virtual table.
fn validateCursorType(comptime Table: type) void {
const Cursor = Table.Cursor;
// Validate the `init` function
{
if (!comptime hasDecls(Cursor, .{"InitError"})) {
@compileError("the Cursor type must declare a InitError error set for the init function");
}
const error_message =
\\the Cursor.init function must have the signature `fn init(allocator: std.mem.Allocator, parent: *Table) InitError!*Cursor`
;
if (!comptime helpers.hasFn(Cursor, "init")) {
@compileError("the Cursor type must have an init function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Cursor.init)).@"fn";
if (info.params.len != 2) @compileError(error_message);
if (info.params[0].type.? != mem.Allocator) @compileError(error_message);
if (info.params[1].type.? != *Table) @compileError(error_message);
if (info.return_type.? != Cursor.InitError!*Cursor) @compileError(error_message);
}
// Validate the `deinit` function
{
const error_message =
\\the Cursor.deinit function must have the signature `fn deinit(cursor: *Cursor) void`
;
if (!comptime helpers.hasFn(Cursor, "deinit")) {
@compileError("the Cursor type must have a deinit function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Cursor.deinit)).@"fn";
if (info.params.len != 1) @compileError(error_message);
if (info.params[0].type.? != *Cursor) @compileError(error_message);
if (info.return_type.? != void) @compileError(error_message);
}
// Validate the `next` function
{
if (!comptime hasDecls(Cursor, .{"NextError"})) {
@compileError("the Cursor type must declare a NextError error set for the next function");
}
const error_message =
\\the Cursor.next function must have the signature `fn next(cursor: *Cursor, diags: *sqlite.vtab.VTabDiagnostics) NextError!void`
;
if (!comptime helpers.hasFn(Cursor, "next")) {
@compileError("the Cursor type must have a next function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Cursor.next)).@"fn";
if (info.params.len != 2) @compileError(error_message);
if (info.params[0].type.? != *Cursor) @compileError(error_message);
if (info.params[1].type.? != *VTabDiagnostics) @compileError(error_message);
if (info.return_type.? != Cursor.NextError!void) @compileError(error_message);
}
// Validate the `hasNext` function
{
if (!comptime hasDecls(Cursor, .{"HasNextError"})) {
@compileError("the Cursor type must declare a HasNextError error set for the hasNext function");
}
const error_message =
\\the Cursor.hasNext function must have the signature `fn hasNext(cursor: *Cursor, diags: *sqlite.vtab.VTabDiagnostics) HasNextError!bool`
;
if (!comptime helpers.hasFn(Cursor, "hasNext")) {
@compileError("the Cursor type must have a hasNext function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Cursor.hasNext)).@"fn";
if (info.params.len != 2) @compileError(error_message);
if (info.params[0].type.? != *Cursor) @compileError(error_message);
if (info.params[1].type.? != *VTabDiagnostics) @compileError(error_message);
if (info.return_type.? != Cursor.HasNextError!bool) @compileError(error_message);
}
// Validate the `filter` function
{
if (!comptime hasDecls(Cursor, .{"FilterError"})) {
@compileError("the Cursor type must declare a FilterError error set for the filter function");
}
const error_message =
\\the Cursor.filter function must have the signature `fn filter(cursor: *Cursor, diags: *sqlite.vtab.VTabDiagnostics, index: sqlite.vtab.IndexIdentifier, args: []FilterArg) FilterError!bool`
;
if (!comptime helpers.hasFn(Cursor, "filter")) {
@compileError("the Cursor type must have a filter function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Cursor.filter)).@"fn";
if (info.params.len != 4) @compileError(error_message);
if (info.params[0].type.? != *Cursor) @compileError(error_message);
if (info.params[1].type.? != *VTabDiagnostics) @compileError(error_message);
if (info.params[2].type.? != IndexIdentifier) @compileError(error_message);
if (info.params[3].type.? != []FilterArg) @compileError(error_message);
if (info.return_type.? != Cursor.FilterError!void) @compileError(error_message);
}
// Validate the `column` function
{
if (!comptime hasDecls(Cursor, .{"ColumnError"})) {
@compileError("the Cursor type must declare a ColumnError error set for the column function");
}
if (!comptime hasDecls(Cursor, .{"Column"})) {
@compileError("the Cursor type must declare a Column type for the return type of the column function");
}
const error_message =
\\the Cursor.column function must have the signature `fn column(cursor: *Cursor, diags: *sqlite.vtab.VTabDiagnostics, column_number: i32) ColumnError!Column`
;
if (!comptime helpers.hasFn(Cursor, "column")) {
@compileError("the Cursor type must have a column function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Cursor.column)).@"fn";
if (info.params.len != 3) @compileError(error_message);
if (info.params[0].type.? != *Cursor) @compileError(error_message);
if (info.params[1].type.? != *VTabDiagnostics) @compileError(error_message);
if (info.params[2].type.? != i32) @compileError(error_message);
if (info.return_type.? != Cursor.ColumnError!Cursor.Column) @compileError(error_message);
}
// Validate the `rowId` function
{
if (!comptime hasDecls(Cursor, .{"RowIDError"})) {
@compileError("the Cursor type must declare a RowIDError error set for the rowId function");
}
const error_message =
\\the Cursor.rowId function must have the signature `fn rowId(cursor: *Cursor, diags: *sqlite.vtab.VTabDiagnostics) RowIDError!i64`
;
if (!comptime helpers.hasFn(Cursor, "rowId")) {
@compileError("the Cursor type must have a rowId function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Cursor.rowId)).@"fn";
if (info.params.len != 2) @compileError(error_message);
if (info.params[0].type.? != *Cursor) @compileError(error_message);
if (info.params[1].type.? != *VTabDiagnostics) @compileError(error_message);
if (info.return_type.? != Cursor.RowIDError!i64) @compileError(error_message);
}
}
/// Validates that a type implements everything required to be a virtual table.
fn validateTableType(comptime Table: type) void {
// Validate the `init` function
{
if (!comptime hasDecls(Table, .{"InitError"})) {
@compileError("the Table type must declare a InitError error set for the init function");
}
const error_message =
\\the Table.init function must have the signature `fn init(allocator: std.mem.Allocator, diags: *sqlite.vtab.VTabDiagnostics, args: []const ModuleArgument) InitError!*Table`
;
if (!comptime helpers.hasFn(Table, "init")) {
@compileError("the Table type must have a init function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Table.init)).@"fn";
if (info.params.len != 3) @compileError(error_message);
if (info.params[0].type.? != mem.Allocator) @compileError(error_message);
if (info.params[1].type.? != *VTabDiagnostics) @compileError(error_message);
// TODO(vincent): maybe allow a signature without the params since a table can do withoout them
if (info.params[2].type.? != []const ModuleArgument) @compileError(error_message);
if (info.return_type.? != Table.InitError!*Table) @compileError(error_message);
}
// Validate the `deinit` function
{
const error_message =
\\the Table.deinit function must have the signature `fn deinit(table: *Table, allocator: std.mem.Allocator) void`
;
if (!comptime helpers.hasFn(Table, "deinit")) {
@compileError("the Table type must have a deinit function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Table.deinit)).@"fn";
if (info.params.len != 2) @compileError(error_message);
if (info.params[0].type.? != *Table) @compileError(error_message);
if (info.params[1].type.? != mem.Allocator) @compileError(error_message);
if (info.return_type.? != void) @compileError(error_message);
}
// Validate the `buildBestIndex` function
{
if (!comptime hasDecls(Table, .{"BuildBestIndexError"})) {
@compileError("the Cursor type must declare a BuildBestIndexError error set for the buildBestIndex function");
}
const error_message =
\\the Table.buildBestIndex function must have the signature `fn buildBestIndex(table: *Table, diags: *sqlite.vtab.VTabDiagnostics, builder: *sqlite.vtab.BestIndexBuilder) BuildBestIndexError!void`
;
if (!comptime helpers.hasFn(Table, "buildBestIndex")) {
@compileError("the Table type must have a buildBestIndex function, " ++ error_message);
}
const info = @typeInfo(@TypeOf(Table.buildBestIndex)).@"fn";
if (info.params.len != 3) @compileError(error_message);
if (info.params[0].type.? != *Table) @compileError(error_message);
if (info.params[1].type.? != *VTabDiagnostics) @compileError(error_message);
if (info.params[2].type.? != *BestIndexBuilder) @compileError(error_message);
if (info.return_type.? != Table.BuildBestIndexError!void) @compileError(error_message);
}
if (!comptime hasDecls(Table, .{"Cursor"})) {
@compileError("the Table type must declare a Cursor type");
}
}
pub const ModuleArgument = union(enum) {
kv: struct {
key: []const u8,
value: []const u8,
},
plain: []const u8,
};
const ParseModuleArgumentsError = error{} || mem.Allocator.Error;
fn parseModuleArguments(allocator: mem.Allocator, argc: c_int, argv: [*c]const [*c]const u8) ParseModuleArgumentsError![]ModuleArgument {
const res = try allocator.alloc(ModuleArgument, @intCast(argc));
errdefer allocator.free(res);
for (res, 0..) |*marg, i| {
// The documentation of sqlite says each string in argv is null-terminated
const arg = mem.sliceTo(argv[i], 0);
if (mem.indexOfScalar(u8, arg, '=')) |pos| {
marg.* = ModuleArgument{
.kv = .{
.key = arg[0..pos],
.value = arg[pos + 1 ..],
},
};
} else {
marg.* = ModuleArgument{ .plain = arg };
}
}
return res;
}
pub fn VirtualTable(
comptime table_name: [:0]const u8,
comptime Table: type,
) type {
// Validate the Table type
comptime {
validateTableType(Table);
validateCursorType(Table);
}
const State = struct {
const Self = @This();
/// vtab must come first !
/// The different functions receive a pointer to a vtab so we have to use @fieldParentPtr to get our state.
vtab: c.sqlite3_vtab,
/// The module context contains state that's the same for _all_ implementations of virtual tables.
module_context: *ModuleContext,
/// The table is the actual virtual table implementation.
table: *Table,
const InitError = error{} || mem.Allocator.Error || Table.InitError;
fn init(module_context: *ModuleContext, table: *Table) InitError!*Self {
const res = try module_context.allocator.create(Self);
res.* = .{
.vtab = mem.zeroes(c.sqlite3_vtab),
.module_context = module_context,
.table = table,
};
return res;
}
fn deinit(self: *Self) void {
self.table.deinit(self.module_context.allocator);
self.module_context.allocator.destroy(self);
}
};
const CursorState = struct {
const Self = @This();
/// vtab_cursor must come first !
/// The different functions receive a pointer to a vtab_cursor so we have to use @fieldParentPtr to get our state.
vtab_cursor: c.sqlite3_vtab_cursor,
/// The module context contains state that's the same for _all_ implementations of virtual tables.
module_context: *ModuleContext,
/// The table is the actual virtual table implementation.
table: *Table,
cursor: *Table.Cursor,
const InitError = error{} || mem.Allocator.Error || Table.Cursor.InitError;
fn init(module_context: *ModuleContext, table: *Table) InitError!*Self {
const res = try module_context.allocator.create(Self);
errdefer module_context.allocator.destroy(res);
res.* = .{
.vtab_cursor = mem.zeroes(c.sqlite3_vtab_cursor),
.module_context = module_context,
.table = table,
.cursor = try Table.Cursor.init(module_context.allocator, table),
};
return res;
}
fn deinit(self: *Self) void {
self.cursor.deinit();
self.module_context.allocator.destroy(self);
}
};
return struct {
const Self = @This();
pub const name = table_name;
pub const module = if (versionGreaterThanOrEqualTo(3, 26, 0))
c.sqlite3_module{
.iVersion = 0,
.xCreate = xConnect, // TODO(vincent): implement xCreate and use it
.xConnect = xConnect,
.xBestIndex = xBestIndex,
.xDisconnect = xDisconnect,
.xDestroy = xDisconnect, // TODO(vincent): implement xDestroy and use it
.xOpen = xOpen,
.xClose = xClose,
.xFilter = xFilter,
.xNext = xNext,
.xEof = xEof,
.xColumn = xColumn,
.xRowid = xRowid,
.xUpdate = null,
.xBegin = null,
.xSync = null,
.xCommit = null,
.xRollback = null,
.xFindFunction = null,
.xRename = null,
.xSavepoint = null,
.xRelease = null,
.xRollbackTo = null,
.xShadowName = null,
}
else
c.sqlite3_module{
.iVersion = 0,
.xCreate = xConnect, // TODO(vincent): implement xCreate and use it
.xConnect = xConnect,
.xBestIndex = xBestIndex,
.xDisconnect = xDisconnect,
.xDestroy = xDisconnect, // TODO(vincent): implement xDestroy and use it
.xOpen = xOpen,
.xClose = xClose,
.xFilter = xFilter,
.xNext = xNext,
.xEof = xEof,
.xColumn = xColumn,
.xRowid = xRowid,
.xUpdate = null,
.xBegin = null,
.xSync = null,
.xCommit = null,
.xRollback = null,
.xFindFunction = null,
.xRename = null,
.xSavepoint = null,
.xRelease = null,
.xRollbackTo = null,
};
table: Table,
fn getModuleContext(ptr: ?*anyopaque) *ModuleContext {
return @ptrCast(@alignCast(ptr.?));
}
fn createState(allocator: mem.Allocator, diags: *VTabDiagnostics, module_context: *ModuleContext, args: []const ModuleArgument) !*State {
// The Context holds the complete of the virtual table and lives for its entire lifetime.
// Context.deinit() will be called when xDestroy is called.
var table = try Table.init(allocator, diags, args);
errdefer table.deinit(allocator);
return try State.init(module_context, table);
}
fn xCreate(db: ?*c.sqlite3, module_context_ptr: ?*anyopaque, argc: c_int, argv: [*c]const [*c]const u8, vtab: [*c][*c]c.sqlite3_vtab, err_str: [*c][*c]const u8) callconv(.C) c_int {
_ = db;
_ = module_context_ptr;
_ = argc;
_ = argv;
_ = vtab;
_ = err_str;
debug.print("xCreate\n", .{});
return c.SQLITE_ERROR;
}
fn xConnect(db: ?*c.sqlite3, module_context_ptr: ?*anyopaque, argc: c_int, argv: [*c]const [*c]const u8, vtab: [*c][*c]c.sqlite3_vtab, err_str: [*c][*c]u8) callconv(.C) c_int {
const module_context = getModuleContext(module_context_ptr);
var arena = heap.ArenaAllocator.init(module_context.allocator);
defer arena.deinit();
// Convert the C-like args to more idiomatic types.
const args = parseModuleArguments(arena.allocator(), argc, argv) catch {
err_str.* = dupeToSQLiteString("out of memory");
return c.SQLITE_ERROR;
};
//
// Create the context and state, assign it to the vtab and declare the vtab.
//
var diags = VTabDiagnostics{ .allocator = arena.allocator() };
const state = createState(module_context.allocator, &diags, module_context, args) catch {
err_str.* = dupeToSQLiteString(diags.error_message);
return c.SQLITE_ERROR;
};
vtab.* = @ptrCast(state);
const res = c.sqlite3_declare_vtab(db, @ptrCast(state.table.schema));
if (res != c.SQLITE_OK) {
return c.SQLITE_ERROR;
}
return c.SQLITE_OK;
}
fn xBestIndex(vtab: [*c]c.sqlite3_vtab, index_info_ptr: [*c]c.sqlite3_index_info) callconv(.C) c_int {
const index_info: *c.sqlite3_index_info = index_info_ptr orelse unreachable;
//
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab);
const state = nullable_state orelse unreachable;
var arena = heap.ArenaAllocator.init(state.module_context.allocator);
defer arena.deinit();
// Create an index builder and let the user build the index.
var builder = BestIndexBuilder.init(arena.allocator(), index_info) catch |err| {
logger.err("unable to create best index builder, err: {!}", .{err});
return c.SQLITE_ERROR;
};
var diags = VTabDiagnostics{ .allocator = arena.allocator() };
state.table.buildBestIndex(&diags, &builder) catch |err| {
logger.err("unable to build best index, err: {!}", .{err});
return c.SQLITE_ERROR;
};
return c.SQLITE_OK;
}
fn xDisconnect(vtab: [*c]c.sqlite3_vtab) callconv(.C) c_int {
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab);
const state = nullable_state orelse unreachable;
state.deinit();
return c.SQLITE_OK;
}
fn xDestroy(vtab: [*c]c.sqlite3_vtab) callconv(.C) c_int {
_ = vtab;
debug.print("xDestroy\n", .{});
return c.SQLITE_ERROR;
}
fn xOpen(vtab: [*c]c.sqlite3_vtab, vtab_cursor: [*c][*c]c.sqlite3_vtab_cursor) callconv(.C) c_int {
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab);
const state = nullable_state orelse unreachable;
const cursor_state = CursorState.init(state.module_context, state.table) catch |err| {
logger.err("unable to create cursor state, err: {!}", .{err});
return c.SQLITE_ERROR;
};
vtab_cursor.* = @ptrCast(cursor_state);
return c.SQLITE_OK;
}
fn xClose(vtab_cursor: [*c]c.sqlite3_vtab_cursor) callconv(.C) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const cursor_state = nullable_cursor_state orelse unreachable;
cursor_state.deinit();
return c.SQLITE_OK;
}
fn xEof(vtab_cursor: [*c]c.sqlite3_vtab_cursor) callconv(.C) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
var arena = heap.ArenaAllocator.init(cursor_state.module_context.allocator);
defer arena.deinit();
//
var diags = VTabDiagnostics{ .allocator = arena.allocator() };
const has_next = cursor.hasNext(&diags) catch {
logger.err("unable to call Table.Cursor.hasNext: {s}", .{diags.error_message});
return 1;
};
if (has_next) {
return 0;
} else {
return 1;
}
}
const FilterArgsFromCPointerError = error{} || mem.Allocator.Error;
fn filterArgsFromCPointer(allocator: mem.Allocator, argc: c_int, argv: [*c]?*c.sqlite3_value) FilterArgsFromCPointerError![]FilterArg {
const size: usize = @intCast(argc);
const res = try allocator.alloc(FilterArg, size);
for (res, 0..) |*item, i| {
item.* = .{
.value = argv[i],
};
}
return res;
}
fn xFilter(vtab_cursor: [*c]c.sqlite3_vtab_cursor, idx_num: c_int, idx_str: [*c]const u8, argc: c_int, argv: [*c]?*c.sqlite3_value) callconv(.C) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
var arena = heap.ArenaAllocator.init(cursor_state.module_context.allocator);
defer arena.deinit();
//
const id = IndexIdentifier.fromC(idx_num, idx_str);
const args = filterArgsFromCPointer(arena.allocator(), argc, argv) catch |err| {
logger.err("unable to create filter args, err: {!}", .{err});
return c.SQLITE_ERROR;
};
var diags = VTabDiagnostics{ .allocator = arena.allocator() };
cursor.filter(&diags, id, args) catch {
logger.err("unable to call Table.Cursor.filter: {s}", .{diags.error_message});
return c.SQLITE_ERROR;
};
return c.SQLITE_OK;
}
fn xNext(vtab_cursor: [*c]c.sqlite3_vtab_cursor) callconv(.C) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
var arena = heap.ArenaAllocator.init(cursor_state.module_context.allocator);
defer arena.deinit();
//
var diags = VTabDiagnostics{ .allocator = arena.allocator() };
cursor.next(&diags) catch {
logger.err("unable to call Table.Cursor.next: {s}", .{diags.error_message});
return c.SQLITE_ERROR;
};
return c.SQLITE_OK;
}
fn xColumn(vtab_cursor: [*c]c.sqlite3_vtab_cursor, ctx: ?*c.sqlite3_context, n: c_int) callconv(.C) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
var arena = heap.ArenaAllocator.init(cursor_state.module_context.allocator);
defer arena.deinit();
//
var diags = VTabDiagnostics{ .allocator = arena.allocator() };
const column = cursor.column(&diags, @intCast(n)) catch {
logger.err("unable to call Table.Cursor.column: {s}", .{diags.error_message});
return c.SQLITE_ERROR;
};
// TODO(vincent): does it make sense to put this in setResult ? Functions could also return a union.
const ColumnType = @TypeOf(column);
switch (@typeInfo(ColumnType)) {
.@"union" => |info| {
if (info.tag_type) |UnionTagType| {
inline for (info.fields) |u_field| {
// This wasn't entirely obvious when I saw code like this elsewhere, it works because of type coercion.
// See https://ziglang.org/documentation/master/#Type-Coercion-unions-and-enums
const column_tag: std.meta.Tag(ColumnType) = column;
const this_tag: std.meta.Tag(ColumnType) = @field(UnionTagType, u_field.name);
if (column_tag == this_tag) {
const column_value = @field(column, u_field.name);
helpers.setResult(ctx, column_value);
}
}
} else {
@compileError("cannot use bare unions as a column");
}
},
else => helpers.setResult(ctx, column),
}
return c.SQLITE_OK;
}
fn xRowid(vtab_cursor: [*c]c.sqlite3_vtab_cursor, row_id_ptr: [*c]c.sqlite3_int64) callconv(.C) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
var arena = heap.ArenaAllocator.init(cursor_state.module_context.allocator);
defer arena.deinit();
//
var diags = VTabDiagnostics{ .allocator = arena.allocator() };
const row_id = cursor.rowId(&diags) catch {
logger.err("unable to call Table.Cursor.rowId: {s}", .{diags.error_message});
return c.SQLITE_ERROR;
};
row_id_ptr.* = row_id;
return c.SQLITE_OK;
}
};
}
const TestVirtualTable = struct {
pub const Cursor = TestVirtualTableCursor;
const Row = struct {
foo: []const u8,
bar: []const u8,
baz: isize,
};
arena_state: heap.ArenaAllocator.State,
rows: []Row,
schema: [:0]const u8,
pub const InitError = error{} || mem.Allocator.Error || fmt.ParseIntError;
pub fn init(gpa: mem.Allocator, diags: *VTabDiagnostics, args: []const ModuleArgument) InitError!*TestVirtualTable {
var arena = heap.ArenaAllocator.init(gpa);
const allocator = arena.allocator();
var res = try allocator.create(TestVirtualTable);