-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.zig
999 lines (812 loc) · 34.2 KB
/
client.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
const builtin = @import("builtin");
const build_options = @import("build_options");
const std = @import("std");
const big = std.math.big;
const heap = std.heap;
const io = std.io;
const mem = std.mem;
const net = std.net;
const testing = std.testing;
const Connection = @import("connection.zig").Connection;
const PreparedMetadata = @import("metadata.zig").PreparedMetadata;
const RowsMetadata = @import("metadata.zig").RowsMetadata;
const ColumnSpec = @import("metadata.zig").ColumnSpec;
const bigint = @import("bigint.zig");
const protocol = @import("protocol.zig");
const CompressionAlgorithm = protocol.CompressionAlgorithm;
const Consistency = protocol.Consistency;
const NotSet = protocol.NotSet;
const OptionID = protocol.OptionID;
const ProtocolVersion = protocol.ProtocolVersion;
const Value = protocol.Value;
const Values = protocol.Values;
const MessageWriter = protocol.MessageWriter;
const ExecuteMessage = protocol.ExecuteMessage;
const PrepareMessage = protocol.PrepareMessage;
const QueryMessage = protocol.QueryMessage;
const AlreadyExistsError = protocol.AlreadyExistsError;
const FunctionFailureError = protocol.FunctionFailureError;
const ReadError = protocol.ReadError;
const UnavailableReplicasError = protocol.UnavailableReplicasError;
const UnpreparedError = protocol.UnpreparedError;
const WriteError = protocol.WriteError;
const Iterator = @import("iterator.zig").Iterator;
const QueryParameters = @import("QueryParameters.zig");
const testutils = @import("testutils.zig");
const casstest = @import("casstest.zig");
const PreparedStatementMetadataValue = struct {
result_metadata_id: ?[]const u8,
metadata: PreparedMetadata,
rows_metadata: RowsMetadata,
fn deinit(self: *PreparedStatementMetadataValue, allocator: mem.Allocator) void {
if (self.result_metadata_id) |result_metadata_id| {
allocator.free(result_metadata_id);
}
self.metadata.deinit(allocator);
self.rows_metadata.deinit(allocator);
}
};
/// Maps a prepared statement id to the types of the arguments needed when executing it.
const PreparedStatementsMetadata = std.StringHashMap(PreparedStatementMetadataValue);
pub const Client = struct {
const Self = @This();
pub const InitOptions = struct {
/// The default consistency to use for queries.
consistency: Consistency = .One,
};
pub const QueryOptions = struct {
/// If this is provided the client will try to limit the size of the resultset.
/// Note that even if the query is paged, cassandra doesn't guarantee that there will
/// be at most `page_size` rows, it can be slightly smaller or bigger.
///
/// If page_size is not null, after execution the `paging_state` field in this struct will be
/// populated if paging is necessary, otherwise it will be null.
page_size: ?u32 = null,
/// If this is provided it will be used to page the result of the query.
/// Additionally, this will be populated by the client with the next paging state if any.
paging_state: ?[]const u8 = null,
/// If this is provided it will be populated in case of failures.
/// This will provide more detail than an error can.
diags: ?*Diagnostics = null,
pub const Diagnostics = struct {
/// The error message returned by the Cassandra node.
message: []const u8 = "",
unavailable_replicas: ?UnavailableReplicasError = null,
function_failure: ?FunctionFailureError = null,
write_timeout: ?WriteError.Timeout = null,
read_timeout: ?ReadError.Timeout = null,
write_failure: ?WriteError.Failure = null,
read_failure: ?ReadError.Failure = null,
cas_write_unknown: ?WriteError.CASUnknown = null,
already_exists: ?AlreadyExistsError = null,
unprepared: ?UnpreparedError = null,
execute: Execute = .{},
const Execute = struct {
not_enough_args: ?bool = null,
first_incompatible_arg: ?ExecuteIncompatibleArg = null,
const ExecuteIncompatibleArg = struct {
position: usize = 0,
prepared: ColumnSpec,
argument: ?OptionID = null,
};
pub fn format(value: Execute, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
var buf: [1024]u8 = undefined;
var fbs = io.fixedBufferStream(&buf);
const fbw = fbs.writer();
if (value.not_enough_args) |v| {
if (v) {
try std.fmt.format(fbw, "not enough args, ", .{});
}
}
if (value.first_incompatible_arg) |v| {
try std.fmt.format(fbw, "first incompatible arg: {{position: {d}, prepared: (name: {s}, type: {?}), argument: {any}}}", .{
v.position,
v.prepared.name,
v.prepared.option,
v.argument,
});
}
try writer.writeAll(fbs.getWritten());
}
};
};
};
allocator: mem.Allocator,
connection: *Connection,
options: InitOptions,
/// TODO(vincent): need to implement some sort of TLL or size limit for this.
prepared_statements_metadata: PreparedStatementsMetadata,
pub fn initWithConnection(allocator: mem.Allocator, connection: *Connection, options: InitOptions) Self {
var self: Self = undefined;
self.allocator = allocator;
self.connection = connection;
self.options = options;
self.prepared_statements_metadata = PreparedStatementsMetadata.init(self.allocator);
return self;
}
pub fn deinit(self: *Self) void {
{
var iter = self.prepared_statements_metadata.iterator();
while (iter.next()) |entry| {
self.allocator.free(entry.key_ptr.*);
entry.value_ptr.deinit(self.allocator);
}
self.prepared_statements_metadata.deinit();
}
self.connection.deinit();
}
pub fn prepare(self: *Self, allocator: mem.Allocator, options: QueryOptions, comptime query_string: []const u8, args: anytype) ![]const u8 {
var dummy_diags = QueryOptions.Diagnostics{};
var diags = options.diags orelse &dummy_diags;
// Check that the query makes sense for the arguments provided.
comptime {
const bind_markers = countBindMarkers(query_string);
const fields = @typeInfo(@TypeOf(args)).@"struct".fields.len;
if (bind_markers != fields) {
@compileLog("number of arguments = ", fields);
@compileLog("number of bind markers = ", bind_markers);
@compileError("Query string has different number of bind markers than the number of arguments provided");
}
}
var option_ids = OptionIDArrayList{};
try computeValues(allocator, null, &option_ids, args);
// Write PREPARE, expect RESULT
{
try self.connection.writeMessage(
allocator,
.prepare,
PrepareMessage{
.query = query_string,
.keyspace = null,
},
.{},
);
}
const read_message = try self.connection.nextMessage(allocator, .{
.message_allocator = self.allocator,
});
switch (read_message) {
.result => |result_message| switch (result_message.result) {
.Prepared => |prepared| {
// Store the metadata for later use with `execute`.
const gop = try self.prepared_statements_metadata.getOrPut(prepared.query_id);
if (gop.found_existing) {
self.allocator.free(prepared.query_id);
gop.value_ptr.deinit(self.allocator);
}
gop.value_ptr.* = .{
.result_metadata_id = prepared.result_metadata_id,
.metadata = prepared.metadata,
.rows_metadata = prepared.rows_metadata,
};
return gop.key_ptr.*;
},
else => return error.InvalidServerResponse,
},
.@"error" => |err| {
diags.message = err.message;
return error.QueryPreparationFailed;
},
else => return error.InvalidServerResponse,
}
}
// TODO(vincent): maybe add not comptime equivalent ?
pub fn query(self: *Self, allocator: mem.Allocator, options: QueryOptions, comptime query_string: []const u8, args: anytype) !?Iterator {
var dummy_diags = QueryOptions.Diagnostics{};
var diags = options.diags orelse &dummy_diags;
// Check that the query makes sense for the arguments provided.
comptime {
const bind_markers = countBindMarkers(query_string);
const fields = @typeInfo(@TypeOf(args)).@"struct".fields.len;
if (bind_markers != fields) {
@compileLog("number of arguments = ", fields);
@compileLog("number of bind markers = ", bind_markers);
@compileError("Query string has different number of bind markers than the number of arguments provided");
}
}
var values = std.ArrayList(Value).init(allocator);
try computeValues(allocator, &values, null, args);
// TODO(vincent): handle named values
// TODO(vincent): handle skip_metadata (see §4.1.4 in the spec)
var query_parameters = QueryParameters{
.consistency_level = self.options.consistency,
.values = undefined,
.skip_metadata = false,
.page_size = options.page_size,
.paging_state = options.paging_state,
.serial_consistency_level = null,
.timestamp = null,
.keyspace = null,
.now_in_seconds = null,
};
query_parameters.values = Values{ .Normal = try values.toOwnedSlice() };
// Write QUERY
{
try self.connection.writeMessage(
allocator,
.query,
QueryMessage{
.query = query_string,
.query_parameters = query_parameters,
},
.{},
);
}
// Read either RESULT or ERROR
return switch (try self.connection.nextMessage(allocator, .{})) {
.result => |result_message| {
return switch (result_message.result) {
.Rows => |rows| blk: {
break :blk Iterator.init(rows.metadata, rows.data);
},
else => null,
};
},
.@"error" => |err| {
diags.message = err.message;
return error.QueryExecutionFailed;
},
else => return error.InvalidServerResponse,
};
}
pub fn execute(self: *Self, allocator: mem.Allocator, options: QueryOptions, query_id: []const u8, args: anytype) !?Iterator {
var dummy_diags = QueryOptions.Diagnostics{};
var diags = options.diags orelse &dummy_diags;
// If the metadata doesn't exist we can't proceed.
const prepared_statement_metadata_kv = self.prepared_statements_metadata.get(query_id);
if (prepared_statement_metadata_kv == null) {
return error.InvalidPreparedQueryID;
}
const ps_result_metadata_id = prepared_statement_metadata_kv.?.result_metadata_id;
const ps_metadata = prepared_statement_metadata_kv.?.metadata;
const ps_rows_metadata = prepared_statement_metadata_kv.?.rows_metadata;
var values = try std.ArrayList(Value).initCapacity(allocator, 16);
var option_ids = OptionIDArrayList{};
try computeValues(allocator, &values, &option_ids, args);
// Now that we have both prepared and compute option IDs, check that they're compatible
// If not compatible we produce a diagnostic.
{
const prepared = ps_metadata.column_specs;
const computed = option_ids.getItems();
if (prepared.len != computed.len) {
diags.execute.not_enough_args = true;
return error.InvalidPreparedStatementExecuteArgs;
}
for (prepared, 0..) |column_spec, i| {
if (computed[i]) |option| {
if (column_spec.option != option) {
diags.execute.first_incompatible_arg = .{
.position = i,
.prepared = prepared[i],
.argument = computed[i],
};
return error.InvalidPreparedStatementExecuteArgs;
}
}
}
}
// Check that the values provided are compatible with the prepared statement
// TODO(vincent): handle named values
var query_parameters = QueryParameters{
.consistency_level = self.options.consistency,
.values = undefined,
.skip_metadata = true,
.page_size = options.page_size,
.paging_state = options.paging_state,
.serial_consistency_level = null,
.timestamp = null,
.keyspace = null,
.now_in_seconds = null,
};
query_parameters.values = Values{ .Normal = try values.toOwnedSlice() };
// Write EXECUTE
{
try self.connection.writeMessage(
allocator,
.execute,
ExecuteMessage{
.query_id = query_id,
.result_metadata_id = ps_result_metadata_id,
.query_parameters = query_parameters,
},
.{},
);
}
// Read either RESULT or ERROR
return switch (try self.connection.nextMessage(allocator, .{})) {
.result => |result_message| {
return switch (result_message.result) {
.Rows => |rows| blk: {
const metadata = if (rows.metadata.column_specs.len > 0)
rows.metadata
else
ps_rows_metadata;
break :blk Iterator.init(metadata, rows.data);
},
else => null,
};
},
.@"error" => |err| {
diags.message = err.message;
return error.QueryExecutionFailed;
},
else => return error.InvalidServerResponse,
};
}
};
fn testWithCassandra(harness: *casstest.Harness) !void {
// Insert some data
const nb_rows = 2;
try harness.insertTestData(.AgeToIDs, nb_rows);
try harness.insertTestData(.User, nb_rows);
// Read and validate the data for the age_to_ids table
{
const Callback = struct {
pub fn do(h: *casstest.Harness, _: usize, row: *casstest.Row.AgeToIDs) !bool {
try testing.expectEqual(row.age, 0);
try testing.expectEqualSlices(u8, &[_]u8{ 0, 2, 4, 8 }, row.ids);
try testing.expectEqualStrings("Vincent 0", row.name);
try testing.expect(h.positive_varint.toConst().eq(row.balance));
return true;
}
};
const res = try harness.selectAndScan(
casstest.Row.AgeToIDs,
"SELECT age, name, ids, balance FROM foobar.age_to_ids WHERE age = ?",
.{
@as(u32, @intCast(0)),
},
Callback.do,
);
try testing.expect(res);
}
{
const Callback = struct {
pub fn do(h: *casstest.Harness, _: usize, row: *casstest.Row.AgeToIDs) !bool {
try testing.expectEqual(@as(u32, 1), row.age);
try testing.expectEqualSlices(u8, &[_]u8{ 0, 2, 4, 8 }, row.ids);
try testing.expectEqualStrings("", row.name);
try testing.expect(h.negative_varint.toConst().eq(row.balance));
return true;
}
};
const res = try harness.selectAndScan(
casstest.Row.AgeToIDs,
"SELECT age, name, ids, balance FROM foobar.age_to_ids WHERE age = ?",
.{
@as(u32, @intCast(1)),
},
Callback.do,
);
try testing.expect(res);
}
// Read and validate the data for the user table
{
const Callback = struct {
pub fn do(_: *casstest.Harness, i: usize, row: *casstest.Row.User) !bool {
try testing.expectEqual(@as(u64, 2000), row.id);
try testing.expectEqual(i + 25, row.secondary_id);
return true;
}
};
const res = try harness.selectAndScan(
casstest.Row.User,
"SELECT id, secondary_id FROM foobar.user WHERE id = 2000",
.{},
Callback.do,
);
try testing.expect(res);
}
}
test "client: insert then query" {
if (!build_options.with_cassandra) return error.SkipZigTest;
const testParameters = struct {
const Self = @This();
compression: ?CompressionAlgorithm,
protocol_version: ?ProtocolVersion,
pub fn init(s: []const u8) !Self {
var self: Self = undefined;
var it = mem.tokenize(s, ",");
while (true) {
const token = it.next() orelse break;
var it2 = mem.tokenize(token, ":");
const key = it2.next() orelse continue;
const value = it2.next() orelse std.debug.panic("invalid token {s}\n", .{token});
if (mem.eql(u8, "compression", key)) {
self.compression = try CompressionAlgorithm.fromString(value);
} else if (mem.eql(u8, "protocol", key)) {
self.protocol_version = try ProtocolVersion.fromString(value);
}
}
return self;
}
};
const params = try testParameters.init(build_options.with_cassandra.?);
var arena = testutils.arenaAllocator();
defer arena.deinit();
var harness: casstest.Harness = undefined;
try harness.init(
&arena.allocator,
params.compression,
params.protocol_version,
);
defer harness.deinit();
try testWithCassandra(&harness);
}
const OptionIDArrayList = struct {
const Self = @This();
items: [128]?OptionID = undefined,
pos: usize = 0,
pub fn append(self: *Self, option_id: ?OptionID) !void {
self.items[self.pos] = option_id;
self.pos += 1;
}
pub fn getItems(self: *Self) []?OptionID {
return self.items[0..self.pos];
}
};
test "option id array list" {
var option_ids = OptionIDArrayList{};
try option_ids.append(.Tinyint);
try option_ids.append(.Smallint);
const items = option_ids.getItems();
try testing.expectEqual(@as(usize, 2), items.len);
try testing.expectEqual(OptionID.Tinyint, items[0].?);
try testing.expectEqual(OptionID.Smallint, items[1].?);
}
/// Compute a list of Value and OptionID for each field in the tuple or struct args.
/// It resolves the values recursively too.
///
/// TODO(vincent): it's not clear to the caller that data in `args` must outlive `values` because we don't duplicating memory
/// unless absolutely necessary in the case of arrays.
/// Think of a way to communicate that.
fn computeValues(allocator: mem.Allocator, values: ?*std.ArrayList(Value), options: ?*OptionIDArrayList, args: anytype) !void {
if (@typeInfo(@TypeOf(args)) != .@"struct") {
@compileError("Expected tuple or struct argument, found " ++ @typeName(args) ++ " of type " ++ @tagName(@typeInfo(args)));
}
var dummy_vals = try std.ArrayList(Value).initCapacity(allocator, 16);
defer dummy_vals.deinit();
const vals = values orelse &dummy_vals;
var dummy_opts = OptionIDArrayList{};
const opts = options orelse &dummy_opts;
inline for (@typeInfo(@TypeOf(args)).@"struct".fields) |struct_field| {
const Type = struct_field.type;
const arg = @field(args, struct_field.name);
try computeSingleValue(allocator, vals, opts, Type, arg);
}
}
fn resolveOption(comptime Type: type) OptionID {
// Special case [16]u8 since we consider it a UUID.
if (Type == [16]u8) return .UUID;
// Special case []const u8 because it's used for strings.
if (Type == []const u8) return .Varchar;
// Special case big.int types because it's used for varint.
if (Type == big.int.Mutable or Type == big.int.Const) return .Varint;
const type_info = @typeInfo(Type);
switch (type_info) {
.bool => return .Boolean,
.int => |_| switch (Type) {
i8, u8 => return .Tinyint,
i16, u16 => return .Smallint,
i32, u32 => return .Int,
i64, u64 => return .Bigint,
else => @compileError("field type " ++ @typeName(Type) ++ " is not compatible with CQL"),
},
.float => |_| switch (Type) {
f32 => return .Float,
f64 => return .Double,
else => @compileError("field type " ++ @typeName(Type) ++ " is not compatible with CQL"),
},
.pointer => |pointer| switch (pointer.size) {
.One => {
return resolveOption(pointer.child);
},
else => @compileError("invalid pointer size " ++ @tagName(pointer.size)),
},
.optional => |optional| {
return resolveOption(optional.child);
},
else => @compileError("field type " ++ @typeName(Type) ++ " not handled yet (type id: " ++ @tagName(type_info) ++ ")"),
}
}
fn computeSingleValue(allocator: mem.Allocator, values: *std.ArrayList(Value), options: *OptionIDArrayList, comptime Type: type, arg: Type) !void {
const type_info = @typeInfo(Type);
var value: Value = undefined;
// Special case [16]u8 since we consider it a UUID.
if (Type == [16]u8) {
try options.append(.UUID);
value = Value{ .Set = try allocator.dupe(u8, &arg) };
try values.append(value);
return;
}
// Special case []const u8 because it's used for strings.
if (Type == []const u8) {
try options.append(.Varchar);
// TODO(vincent): should we make a copy ?
value = Value{ .Set = arg };
try values.append(value);
return;
}
// Special case big.int types because it's used for varint.
if (Type == big.int.Const) {
try options.append(.Varint);
value = Value{ .Set = try bigint.toBytes(allocator, arg) };
try values.append(value);
return;
}
// The NotSet struct allows the caller to not set a value, which according to the
// protocol will not result in any change to the existing value.
if (Type == NotSet) {
try options.append(resolveOption(arg.type));
value = Value{ .NotSet = {} };
try values.append(value);
return;
}
switch (type_info) {
.bool => {
var buf = try allocator.alloc(u8, 1);
errdefer allocator.free(buf);
buf[0] = if (arg) 0x01 else 0x00;
try options.append(.Boolean);
value = Value{ .Set = buf };
try values.append(value);
},
.int => |info| {
try options.append(resolveOption(Type));
const buf = try allocator.alloc(u8, info.bits / 8);
errdefer allocator.free(buf);
mem.writeInt(Type, @ptrCast(buf), arg, .big);
value = Value{ .Set = buf };
try values.append(value);
},
.float => |info| {
try options.append(resolveOption(Type));
const buf = try allocator.alloc(u8, info.bits / 8);
errdefer allocator.free(buf);
const arg_ptr: *align(1) Type = @ptrCast(buf);
arg_ptr.* = arg;
value = Value{ .Set = buf };
try values.append(value);
},
.pointer => |pointer| switch (pointer.size) {
.One => {
try computeValues(allocator, values, options, .{arg.*});
return;
},
.Slice => {
// Otherwise it's a list or a set, encode a new list of values.
var inner_values = std.ArrayList(Value).init(allocator);
for (arg) |item| {
try computeValues(allocator, &inner_values, null, .{item});
}
try options.append(null);
value = Value{ .Set = try serializeValues(allocator, try inner_values.toOwnedSlice()) };
try values.append(value);
},
else => @compileError("invalid pointer size " ++ @tagName(pointer.size)),
},
.array => |_| {
// Otherwise it's a list or a set, encode a new list of values.
var inner_values = std.ArrayList(Value).init(allocator);
for (arg) |item| {
try computeValues(allocator, &inner_values, null, .{item});
}
try options.append(null);
value = Value{ .Set = try serializeValues(allocator, try inner_values.toOwnedSlice()) };
try values.append(value);
},
.optional => |optional| {
if (arg) |a| {
try computeSingleValue(allocator, values, options, optional.child, a);
} else {
try options.append(resolveOption(optional.child));
value = Value{ .Null = {} };
try values.append(value);
}
},
else => @compileError("field type " ++ @typeName(Type) ++ " not handled yet (type id: " ++ @tagName(type_info) ++ ")"),
}
}
fn serializeValues(allocator: mem.Allocator, values: []const Value) ![]const u8 {
var mw = try MessageWriter.init(allocator);
try mw.writeInt(u32, @intCast(values.len));
for (values) |value| {
switch (value) {
.Set => |v| {
try mw.writeBytes(v);
},
else => {},
}
}
return mw.toOwnedSlice();
}
test "serialize values" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
const v1 = Value{ .Set = "foobar" };
const v2 = Value{ .Set = "barbaz" };
var data = try serializeValues(arena.allocator(), &[_]Value{ v1, v2 });
try testing.expectEqual(@as(usize, 24), data.len);
try testing.expectEqualSlices(u8, "\x00\x00\x00\x02", data[0..4]);
try testing.expectEqualStrings("\x00\x00\x00\x06foobar\x00\x00\x00\x06barbaz", data[4..]);
}
test "compute values: ints" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
const allocator = arena.allocator();
var values = std.ArrayList(Value).init(allocator);
var options = OptionIDArrayList{};
const my_u64 = @as(u64, 20000);
_ = try computeValues(allocator, &values, &options, .{
.i_tinyint = @as(i8, 0x7f),
.u_tinyint = @as(u8, 0xff),
.i_smallint = @as(i16, 0x7fff),
.u_smallint = @as(u16, 0xdedf),
.i_int = @as(i32, 0x7fffffff),
.u_int = @as(u32, 0xabababaf),
.i_bigint = @as(i64, 0x7fffffffffffffff),
.u_bigint = @as(u64, 0xdcdcdcdcdcdcdcdf),
.u_bigint_ptr = &my_u64,
});
const v = values.items;
const o = options.getItems();
try testing.expectEqual(@as(usize, 9), v.len);
try testing.expectEqual(@as(usize, 9), o.len);
try testing.expectEqualSlices(u8, "\x7f", v[0].Set);
try testing.expectEqual(OptionID.Tinyint, o[0].?);
try testing.expectEqualSlices(u8, "\xff", v[1].Set);
try testing.expectEqual(OptionID.Tinyint, o[1].?);
try testing.expectEqualSlices(u8, "\x7f\xff", v[2].Set);
try testing.expectEqual(OptionID.Smallint, o[2].?);
try testing.expectEqualSlices(u8, "\xde\xdf", v[3].Set);
try testing.expectEqual(OptionID.Smallint, o[3].?);
try testing.expectEqualSlices(u8, "\x7f\xff\xff\xff", v[4].Set);
try testing.expectEqual(OptionID.Int, o[4].?);
try testing.expectEqualSlices(u8, "\xab\xab\xab\xaf", v[5].Set);
try testing.expectEqual(OptionID.Int, o[5].?);
try testing.expectEqualSlices(u8, "\x7f\xff\xff\xff\xff\xff\xff\xff", v[6].Set);
try testing.expectEqual(OptionID.Bigint, o[6].?);
try testing.expectEqualSlices(u8, "\xdc\xdc\xdc\xdc\xdc\xdc\xdc\xdf", v[7].Set);
try testing.expectEqual(OptionID.Bigint, o[7].?);
try testing.expectEqualSlices(u8, "\x00\x00\x00\x00\x00\x00\x4e\x20", v[8].Set);
try testing.expectEqual(OptionID.Bigint, o[8].?);
}
test "compute values: floats" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
const allocator = arena.allocator();
var values = std.ArrayList(Value).init(allocator);
var options = OptionIDArrayList{};
const my_f64 = @as(f64, 402.240);
_ = try computeValues(allocator, &values, &options, .{
.f32 = @as(f32, 0.002),
.f64 = @as(f64, 245601.000240305603),
.f64_ptr = &my_f64,
});
const v = values.items;
const o = options.getItems();
try testing.expectEqual(@as(usize, 3), v.len);
try testing.expectEqual(@as(usize, 3), o.len);
try testing.expectEqualSlices(u8, "\x6f\x12\x03\x3b", v[0].Set);
try testing.expectEqual(OptionID.Float, o[0].?);
try testing.expectEqualSlices(u8, "\x46\xfd\x7d\x00\x08\xfb\x0d\x41", v[1].Set);
try testing.expectEqual(OptionID.Double, o[1].?);
try testing.expectEqualSlices(u8, "\xa4\x70\x3d\x0a\xd7\x23\x79\x40", v[2].Set);
try testing.expectEqual(OptionID.Double, o[2].?);
}
test "compute values: strings" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
const allocator = arena.allocator();
var values = std.ArrayList(Value).init(allocator);
var options = OptionIDArrayList{};
_ = try computeValues(allocator, &values, &options, .{
.string = @as([]const u8, try allocator.dupe(u8, "foobar")),
});
const v = values.items;
const o = options.getItems();
try testing.expectEqual(@as(usize, 1), v.len);
try testing.expectEqual(@as(usize, 1), o.len);
try testing.expectEqualStrings("foobar", v[0].Set);
try testing.expectEqual(OptionID.Varchar, o[0].?);
}
test "compute values: bool" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
const allocator = arena.allocator();
var values = std.ArrayList(Value).init(allocator);
var options = OptionIDArrayList{};
_ = try computeValues(allocator, &values, &options, .{
.bool1 = true,
.bool2 = false,
});
const v = values.items;
const o = options.getItems();
try testing.expectEqual(@as(usize, 2), v.len);
try testing.expectEqual(@as(usize, 2), o.len);
try testing.expectEqualSlices(u8, "\x01", v[0].Set);
try testing.expectEqual(OptionID.Boolean, o[0].?);
try testing.expectEqualSlices(u8, "\x00", v[1].Set);
try testing.expectEqual(OptionID.Boolean, o[1].?);
}
test "compute values: set/list" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
const allocator = arena.allocator();
var values = std.ArrayList(Value).init(allocator);
var options = OptionIDArrayList{};
_ = try computeValues(allocator, &values, &options, .{
.string = &[_]u16{ 0x01, 0x2050 },
.string2 = @as([]const u16, &[_]u16{ 0x01, 0x2050 }),
});
const v = values.items;
const o = options.getItems();
try testing.expectEqual(@as(usize, 2), v.len);
try testing.expectEqual(@as(usize, 2), o.len);
try testing.expectEqualSlices(u8, "\x00\x00\x00\x02\x00\x00\x00\x02\x00\x01\x00\x00\x00\x02\x20\x50", v[0].Set);
try testing.expect(o[0] == null);
try testing.expectEqualSlices(u8, "\x00\x00\x00\x02\x00\x00\x00\x02\x00\x01\x00\x00\x00\x02\x20\x50", v[1].Set);
try testing.expect(o[1] == null);
}
test "compute values: uuid" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
const allocator = arena.allocator();
var values = std.ArrayList(Value).init(allocator);
var options = OptionIDArrayList{};
_ = try computeValues(allocator, &values, &options, .{
.uuid = [16]u8{
0x55, 0x94, 0xd5, 0xb1,
0xef, 0x84, 0x41, 0xc4,
0xb2, 0x4e, 0x68, 0x48,
0x8d, 0xcf, 0xa1, 0xc9,
},
});
const v = values.items;
const o = options.getItems();
try testing.expectEqual(@as(usize, 1), v.len);
try testing.expectEqual(@as(usize, 1), o.len);
try testing.expectEqualSlices(u8, "\x55\x94\xd5\xb1\xef\x84\x41\xc4\xb2\x4e\x68\x48\x8d\xcf\xa1\xc9", v[0].Set);
try testing.expectEqual(OptionID.UUID, o[0].?);
}
test "compute values: not set and null" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
const allocator = arena.allocator();
var values = std.ArrayList(Value).init(allocator);
var options = OptionIDArrayList{};
const Args = struct {
not_set: NotSet,
nullable: ?u64,
};
_ = try computeValues(allocator, &values, &options, Args{
.not_set = NotSet{ .type = i32 },
.nullable = null,
});
const v = values.items;
const o = options.getItems();
try testing.expectEqual(@as(usize, 2), v.len);
try testing.expectEqual(@as(usize, 2), o.len);
try testing.expect(v[0] == .NotSet);
try testing.expectEqual(OptionID.Int, o[0].?);
try testing.expect(v[1] == .Null);
try testing.expectEqual(OptionID.Bigint, o[1].?);
}
fn areOptionIDsEqual(_: []const ColumnSpec, _: []const ?OptionID) bool {}
fn countBindMarkers(query_string: []const u8) usize {
var pos: usize = 0;
var count: usize = 0;
while (mem.indexOfScalarPos(u8, query_string, pos, '?')) |i| {
count += 1;
pos = i + 1;
}
return count;
}
test "count bind markers" {
const query_string = "select * from foobar.user where id = ? and name = ? and age < ?";
const count = countBindMarkers(query_string);
try testing.expectEqual(@as(usize, 3), count);
}
test {
_ = @import("bigint.zig");
}