summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2023-07-02 15:29:47 +0200
committerGravatar Vincent Rischmann2023-07-02 15:29:47 +0200
commit034d4232be8c9b7f402d09bebd8d1e72c5e93d2c (patch)
treeba788b9916d5d879737959d4823af9fbd104b4df
parentfuzz: fix for latest zig (diff)
parentupdate for latest zig (diff)
downloadzig-sqlite-034d4232be8c9b7f402d09bebd8d1e72c5e93d2c.tar.gz
zig-sqlite-034d4232be8c9b7f402d09bebd8d1e72c5e93d2c.tar.xz
zig-sqlite-034d4232be8c9b7f402d09bebd8d1e72c5e93d2c.zip
Merge branch 'update-latest-zig'
-rw-r--r--errors.zig4
-rw-r--r--helpers.zig14
-rw-r--r--sqlite.zig95
-rw-r--r--vtab.zig36
4 files changed, 72 insertions, 77 deletions
diff --git a/errors.zig b/errors.zig
index add4b37..59293c4 100644
--- a/errors.zig
+++ b/errors.zig
@@ -286,7 +286,7 @@ pub const DetailedError = struct {
286 286
287pub fn getDetailedErrorFromResultCode(code: c_int) DetailedError { 287pub fn getDetailedErrorFromResultCode(code: c_int) DetailedError {
288 return .{ 288 return .{
289 .code = @intCast(usize, code), 289 .code = @intCast(code),
290 .near = -1, 290 .near = -1,
291 .message = blk: { 291 .message = blk: {
292 const msg = c.sqlite3_errstr(code); 292 const msg = c.sqlite3_errstr(code);
@@ -304,7 +304,7 @@ pub fn getErrorOffset(db: *c.sqlite3) i32 {
304 304
305pub fn getLastDetailedErrorFromDb(db: *c.sqlite3) DetailedError { 305pub fn getLastDetailedErrorFromDb(db: *c.sqlite3) DetailedError {
306 return .{ 306 return .{
307 .code = @intCast(usize, c.sqlite3_extended_errcode(db)), 307 .code = @intCast(c.sqlite3_extended_errcode(db)),
308 .near = getErrorOffset(db), 308 .near = getErrorOffset(db),
309 .message = blk: { 309 .message = blk: {
310 const msg = c.sqlite3_errmsg(db); 310 const msg = c.sqlite3_errmsg(db);
diff --git a/helpers.zig b/helpers.zig
index 7bcbabe..1a56231 100644
--- a/helpers.zig
+++ b/helpers.zig
@@ -13,8 +13,8 @@ pub fn setResult(ctx: ?*c.sqlite3_context, result: anytype) void {
13 const ResultType = @TypeOf(result); 13 const ResultType = @TypeOf(result);
14 14
15 switch (ResultType) { 15 switch (ResultType) {
16 Text => c.sqlite3_result_text(ctx, result.data.ptr, @intCast(c_int, result.data.len), c.SQLITE_TRANSIENT), 16 Text => c.sqlite3_result_text(ctx, result.data.ptr, @intCast(result.data.len), c.SQLITE_TRANSIENT),
17 Blob => c.sqlite3_result_blob(ctx, result.data.ptr, @intCast(c_int, result.data.len), c.SQLITE_TRANSIENT), 17 Blob => c.sqlite3_result_blob(ctx, result.data.ptr, @intCast(result.data.len), c.SQLITE_TRANSIENT),
18 else => switch (@typeInfo(ResultType)) { 18 else => switch (@typeInfo(ResultType)) {
19 .Int => |info| if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 32) { 19 .Int => |info| if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 32) {
20 c.sqlite3_result_int(ctx, result); 20 c.sqlite3_result_int(ctx, result);
@@ -31,7 +31,7 @@ pub fn setResult(ctx: ?*c.sqlite3_context, result: anytype) void {
31 }, 31 },
32 .Pointer => |ptr| switch (ptr.size) { 32 .Pointer => |ptr| switch (ptr.size) {
33 .Slice => switch (ptr.child) { 33 .Slice => switch (ptr.child) {
34 u8 => c.sqlite3_result_text(ctx, result.ptr, @intCast(c_int, result.len), c.SQLITE_TRANSIENT), 34 u8 => c.sqlite3_result_text(ctx, result.ptr, @intCast(result.len), c.SQLITE_TRANSIENT),
35 else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), 35 else => @compileError("cannot use a result of type " ++ @typeName(ResultType)),
36 }, 36 },
37 else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), 37 else => @compileError("cannot use a result of type " ++ @typeName(ResultType)),
@@ -51,16 +51,16 @@ pub fn setTypeFromValue(comptime ArgType: type, arg: *ArgType, sqlite_value: *c.
51 else => switch (@typeInfo(ArgType)) { 51 else => switch (@typeInfo(ArgType)) {
52 .Int => |info| if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 32) { 52 .Int => |info| if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 32) {
53 const value = c.sqlite3_value_int(sqlite_value); 53 const value = c.sqlite3_value_int(sqlite_value);
54 arg.* = @intCast(ArgType, value); 54 arg.* = @intCast(value);
55 } else if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 64) { 55 } else if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 64) {
56 const value = c.sqlite3_value_int64(sqlite_value); 56 const value = c.sqlite3_value_int64(sqlite_value);
57 arg.* = @intCast(ArgType, value); 57 arg.* = @intCast(value);
58 } else { 58 } else {
59 @compileError("integer " ++ @typeName(ArgType) ++ " is not representable in sqlite"); 59 @compileError("integer " ++ @typeName(ArgType) ++ " is not representable in sqlite");
60 }, 60 },
61 .Float => { 61 .Float => {
62 const value = c.sqlite3_value_double(sqlite_value); 62 const value = c.sqlite3_value_double(sqlite_value);
63 arg.* = @floatCast(ArgType, value); 63 arg.* = @floatCast(value);
64 }, 64 },
65 .Bool => { 65 .Bool => {
66 const value = c.sqlite3_value_int(sqlite_value); 66 const value = c.sqlite3_value_int(sqlite_value);
@@ -79,7 +79,7 @@ pub fn setTypeFromValue(comptime ArgType: type, arg: *ArgType, sqlite_value: *c.
79} 79}
80 80
81fn sliceFromValue(sqlite_value: *c.sqlite3_value) []const u8 { 81fn sliceFromValue(sqlite_value: *c.sqlite3_value) []const u8 {
82 const size = @intCast(usize, c.sqlite3_value_bytes(sqlite_value)); 82 const size: usize = @intCast(c.sqlite3_value_bytes(sqlite_value));
83 83
84 const value = c.sqlite3_value_text(sqlite_value); 84 const value = c.sqlite3_value_text(sqlite_value);
85 debug.assert(value != null); // TODO(vincent): how do we handle this properly ? 85 debug.assert(value != null); // TODO(vincent): how do we handle this properly ?
diff --git a/sqlite.zig b/sqlite.zig
index 4bd90c9..5c98790 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -107,21 +107,21 @@ pub const Blob = struct {
107 } 107 }
108 108
109 var tmp_buffer = blk: { 109 var tmp_buffer = blk: {
110 const remaining = @intCast(usize, self.size) - @intCast(usize, self.offset); 110 const remaining: usize = @as(usize, @intCast(self.size)) - @as(usize, @intCast(self.offset));
111 break :blk if (buffer.len > remaining) buffer[0..remaining] else buffer; 111 break :blk if (buffer.len > remaining) buffer[0..remaining] else buffer;
112 }; 112 };
113 113
114 const result = c.sqlite3_blob_read( 114 const result = c.sqlite3_blob_read(
115 self.handle, 115 self.handle,
116 tmp_buffer.ptr, 116 tmp_buffer.ptr,
117 @intCast(c_int, tmp_buffer.len), 117 @intCast(tmp_buffer.len),
118 self.offset, 118 self.offset,
119 ); 119 );
120 if (result != c.SQLITE_OK) { 120 if (result != c.SQLITE_OK) {
121 return errors.errorFromResultCode(result); 121 return errors.errorFromResultCode(result);
122 } 122 }
123 123
124 self.offset += @intCast(c_int, tmp_buffer.len); 124 self.offset += @intCast(tmp_buffer.len);
125 125
126 return tmp_buffer.len; 126 return tmp_buffer.len;
127 } 127 }
@@ -137,14 +137,14 @@ pub const Blob = struct {
137 const result = c.sqlite3_blob_write( 137 const result = c.sqlite3_blob_write(
138 self.handle, 138 self.handle,
139 data.ptr, 139 data.ptr,
140 @intCast(c_int, data.len), 140 @intCast(data.len),
141 self.offset, 141 self.offset,
142 ); 142 );
143 if (result != c.SQLITE_OK) { 143 if (result != c.SQLITE_OK) {
144 return errors.errorFromResultCode(result); 144 return errors.errorFromResultCode(result);
145 } 145 }
146 146
147 self.offset += @intCast(c_int, data.len); 147 self.offset += @intCast(data.len);
148 148
149 return data.len; 149 return data.len;
150 } 150 }
@@ -191,7 +191,7 @@ pub const Blob = struct {
191 column, 191 column,
192 row, 192 row,
193 open_flags, 193 open_flags,
194 @ptrCast([*c]?*c.sqlite3_blob, &blob.handle), 194 @ptrCast(&blob.handle),
195 ); 195 );
196 if (result == c.SQLITE_MISUSE) debug.panic("sqlite misuse while opening a blob", .{}); 196 if (result == c.SQLITE_MISUSE) debug.panic("sqlite misuse while opening a blob", .{});
197 if (result != c.SQLITE_OK) { 197 if (result != c.SQLITE_OK) {
@@ -528,7 +528,7 @@ pub const Db = struct {
528 528
529 /// rowsAffected returns the number of rows affected by the last statement executed. 529 /// rowsAffected returns the number of rows affected by the last statement executed.
530 pub fn rowsAffected(self: *Self) usize { 530 pub fn rowsAffected(self: *Self) usize {
531 return @intCast(usize, c.sqlite3_changes(self.db)); 531 return @intCast(c.sqlite3_changes(self.db));
532 } 532 }
533 533
534 /// openBlob opens a blob for incremental i/o. 534 /// openBlob opens a blob for incremental i/o.
@@ -825,12 +825,10 @@ pub const FunctionContext = struct {
825 825
826 pub fn userContext(self: FunctionContext, comptime Type: type) ?Type { 826 pub fn userContext(self: FunctionContext, comptime Type: type) ?Type {
827 const Types = splitPtrTypes(Type); 827 const Types = splitPtrTypes(Type);
828 _ = Types;
828 829
829 if (c.sqlite3_user_data(self.ctx)) |value| { 830 if (c.sqlite3_user_data(self.ctx)) |value| {
830 return @ptrCast( 831 return @ptrCast(@alignCast(value));
831 Types.PointerType,
832 @alignCast(@alignOf(Types.ValueType), value),
833 );
834 } 832 }
835 return null; 833 return null;
836 } 834 }
@@ -839,10 +837,7 @@ pub const FunctionContext = struct {
839 const Types = splitPtrTypes(Type); 837 const Types = splitPtrTypes(Type);
840 838
841 if (c.sqlite3_aggregate_context(self.ctx, @sizeOf(Types.ValueType))) |value| { 839 if (c.sqlite3_aggregate_context(self.ctx, @sizeOf(Types.ValueType))) |value| {
842 return @ptrCast( 840 return @ptrCast(@alignCast(value));
843 Types.PointerType,
844 @alignCast(@alignOf(Types.ValueType), value),
845 );
846 } 841 }
847 return null; 842 return null;
848 } 843 }
@@ -1073,7 +1068,7 @@ pub fn Iterator(comptime Type: type) type {
1073 1068
1074 if (@typeInfo(Type.BaseType) == .Int) { 1069 if (@typeInfo(Type.BaseType) == .Int) {
1075 const inner_value = try self.readField(Type.BaseType, options, 0); 1070 const inner_value = try self.readField(Type.BaseType, options, 0);
1076 return @enumFromInt(Type, @intCast(TI.tag_type, inner_value)); 1071 return @enumFromInt(@as(TI.tag_type, @intCast(inner_value)));
1077 } 1072 }
1078 1073
1079 @compileError("enum column " ++ @typeName(Type) ++ " must have a BaseType of either string or int"); 1074 @compileError("enum column " ++ @typeName(Type) ++ " must have a BaseType of either string or int");
@@ -1157,7 +1152,7 @@ pub fn Iterator(comptime Type: type) type {
1157 return std.meta.stringToEnum(Type, inner_value) orelse unreachable; 1152 return std.meta.stringToEnum(Type, inner_value) orelse unreachable;
1158 } 1153 }
1159 if (@typeInfo(Type.BaseType) == .Int) { 1154 if (@typeInfo(Type.BaseType) == .Int) {
1160 return @enumFromInt(Type, @intCast(TI.tag_type, inner_value)); 1155 return @enumFromInt(@as(TI.tag_type, @intCast(inner_value)));
1161 } 1156 }
1162 @compileError("enum column " ++ @typeName(Type) ++ " must have a BaseType of either string or int"); 1157 @compileError("enum column " ++ @typeName(Type) ++ " must have a BaseType of either string or int");
1163 }, 1158 },
@@ -1178,7 +1173,7 @@ pub fn Iterator(comptime Type: type) type {
1178 // 1173 //
1179 // If the array is too small for the data an error will be returned. 1174 // If the array is too small for the data an error will be returned.
1180 fn readArray(self: *Self, comptime ArrayType: type, _i: usize) !ArrayType { 1175 fn readArray(self: *Self, comptime ArrayType: type, _i: usize) !ArrayType {
1181 const i = @intCast(c_int, _i); 1176 const i: c_int = @intCast(_i);
1182 const type_info = @typeInfo(ArrayType); 1177 const type_info = @typeInfo(ArrayType);
1183 1178
1184 var ret: ArrayType = undefined; 1179 var ret: ArrayType = undefined;
@@ -1186,7 +1181,7 @@ pub fn Iterator(comptime Type: type) type {
1186 .Array => |arr| { 1181 .Array => |arr| {
1187 switch (arr.child) { 1182 switch (arr.child) {
1188 u8 => { 1183 u8 => {
1189 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); 1184 const size: usize = @intCast(c.sqlite3_column_bytes(self.stmt, i));
1190 1185
1191 if (arr.sentinel) |sentinel_ptr| { 1186 if (arr.sentinel) |sentinel_ptr| {
1192 // An array with a sentinel need to be as big as the data, + 1 byte for the sentinel. 1187 // An array with a sentinel need to be as big as the data, + 1 byte for the sentinel.
@@ -1195,7 +1190,7 @@ pub fn Iterator(comptime Type: type) type {
1195 } 1190 }
1196 1191
1197 // Set the sentinel in the result at the correct position. 1192 // Set the sentinel in the result at the correct position.
1198 const sentinel = @ptrCast(*const arr.child, sentinel_ptr).*; 1193 const sentinel = @as(*const arr.child, @ptrCast(sentinel_ptr)).*;
1199 ret[size] = sentinel; 1194 ret[size] = sentinel;
1200 } else if (size != arr.len) { 1195 } else if (size != arr.len) {
1201 // An array without a sentinel must have the exact same size as the data because we can't 1196 // An array without a sentinel must have the exact same size as the data because we can't
@@ -1205,7 +1200,7 @@ pub fn Iterator(comptime Type: type) type {
1205 1200
1206 const data = c.sqlite3_column_blob(self.stmt, i); 1201 const data = c.sqlite3_column_blob(self.stmt, i);
1207 if (data != null) { 1202 if (data != null) {
1208 const ptr = @ptrCast([*c]const u8, data)[0..size]; 1203 const ptr = @as([*c]const u8, @ptrCast(data))[0..size];
1209 1204
1210 mem.copy(u8, ret[0..], ptr); 1205 mem.copy(u8, ret[0..], ptr);
1211 } 1206 }
@@ -1220,19 +1215,19 @@ pub fn Iterator(comptime Type: type) type {
1220 1215
1221 // readInt reads a sqlite INTEGER column into an integer. 1216 // readInt reads a sqlite INTEGER column into an integer.
1222 fn readInt(self: *Self, comptime IntType: type, i: usize) error{Workaround}!IntType { // TODO remove the workaround once https://github.com/ziglang/zig/issues/5149 is resolved or if we actually return an error 1217 fn readInt(self: *Self, comptime IntType: type, i: usize) error{Workaround}!IntType { // TODO remove the workaround once https://github.com/ziglang/zig/issues/5149 is resolved or if we actually return an error
1223 const n = c.sqlite3_column_int64(self.stmt, @intCast(c_int, i)); 1218 const n = c.sqlite3_column_int64(self.stmt, @intCast(i));
1224 return @intCast(IntType, n); 1219 return @intCast(n);
1225 } 1220 }
1226 1221
1227 // readFloat reads a sqlite REAL column into a float. 1222 // readFloat reads a sqlite REAL column into a float.
1228 fn readFloat(self: *Self, comptime FloatType: type, i: usize) error{Workaround}!FloatType { // TODO remove the workaround once https://github.com/ziglang/zig/issues/5149 is resolved or if we actually return an error 1223 fn readFloat(self: *Self, comptime FloatType: type, i: usize) error{Workaround}!FloatType { // TODO remove the workaround once https://github.com/ziglang/zig/issues/5149 is resolved or if we actually return an error
1229 const d = c.sqlite3_column_double(self.stmt, @intCast(c_int, i)); 1224 const d = c.sqlite3_column_double(self.stmt, @intCast(i));
1230 return @floatCast(FloatType, d); 1225 return @floatCast(d);
1231 } 1226 }
1232 1227
1233 // readFloat reads a sqlite INTEGER column into a bool (true is anything > 0, false is anything <= 0). 1228 // readFloat reads a sqlite INTEGER column into a bool (true is anything > 0, false is anything <= 0).
1234 fn readBool(self: *Self, i: usize) error{Workaround}!bool { // TODO remove the workaround once https://github.com/ziglang/zig/issues/5149 is resolved or if we actually return an error 1229 fn readBool(self: *Self, i: usize) error{Workaround}!bool { // TODO remove the workaround once https://github.com/ziglang/zig/issues/5149 is resolved or if we actually return an error
1235 const d = c.sqlite3_column_int64(self.stmt, @intCast(c_int, i)); 1230 const d = c.sqlite3_column_int64(self.stmt, @intCast(i));
1236 return d > 0; 1231 return d > 0;
1237 } 1232 }
1238 1233
@@ -1246,7 +1241,7 @@ pub fn Iterator(comptime Type: type) type {
1246 switch (@typeInfo(SliceType)) { 1241 switch (@typeInfo(SliceType)) {
1247 .Pointer => |ptr_info| { 1242 .Pointer => |ptr_info| {
1248 if (ptr_info.sentinel) |sentinel_ptr| { 1243 if (ptr_info.sentinel) |sentinel_ptr| {
1249 const sentinel = @ptrCast(*const ptr_info.child, sentinel_ptr).*; 1244 const sentinel = @as(*const ptr_info.child, @ptrCast(sentinel_ptr)).*;
1250 1245
1251 const slice = try allocator.alloc(u8, data.len + 1); 1246 const slice = try allocator.alloc(u8, data.len + 1);
1252 mem.copy(u8, slice, data); 1247 mem.copy(u8, slice, data);
@@ -1272,7 +1267,7 @@ pub fn Iterator(comptime Type: type) type {
1272 // 1267 //
1273 // The options must contain an `allocator` field which will be used to create a copy of the data. 1268 // The options must contain an `allocator` field which will be used to create a copy of the data.
1274 fn readBytes(self: *Self, comptime BytesType: type, allocator: mem.Allocator, _i: usize, comptime mode: ReadBytesMode) !BytesType { 1269 fn readBytes(self: *Self, comptime BytesType: type, allocator: mem.Allocator, _i: usize, comptime mode: ReadBytesMode) !BytesType {
1275 const i = @intCast(c_int, _i); 1270 const i: c_int = @intCast(_i);
1276 1271
1277 switch (mode) { 1272 switch (mode) {
1278 .Blob => { 1273 .Blob => {
@@ -1284,8 +1279,8 @@ pub fn Iterator(comptime Type: type) type {
1284 }; 1279 };
1285 } 1280 }
1286 1281
1287 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); 1282 const size: usize = @intCast(c.sqlite3_column_bytes(self.stmt, i));
1288 const ptr = @ptrCast([*c]const u8, data)[0..size]; 1283 const ptr = @as([*c]const u8, @ptrCast(data))[0..size];
1289 1284
1290 if (BytesType == Blob) { 1285 if (BytesType == Blob) {
1291 return Blob{ .data = try allocator.dupe(u8, ptr) }; 1286 return Blob{ .data = try allocator.dupe(u8, ptr) };
@@ -1301,8 +1296,8 @@ pub fn Iterator(comptime Type: type) type {
1301 }; 1296 };
1302 } 1297 }
1303 1298
1304 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); 1299 const size: usize = @intCast(c.sqlite3_column_bytes(self.stmt, i));
1305 const ptr = @ptrCast([*c]const u8, data)[0..size]; 1300 const ptr = @as([*c]const u8, @ptrCast(data))[0..size];
1306 1301
1307 if (BytesType == Text) { 1302 if (BytesType == Text) {
1308 return Text{ .data = try allocator.dupe(u8, ptr) }; 1303 return Text{ .data = try allocator.dupe(u8, ptr) };
@@ -1352,7 +1347,7 @@ pub fn Iterator(comptime Type: type) type {
1352 switch (@typeInfo(OptionalType)) { 1347 switch (@typeInfo(OptionalType)) {
1353 .Optional => |opt| { 1348 .Optional => |opt| {
1354 // Easy way to know if the column represents a null value. 1349 // Easy way to know if the column represents a null value.
1355 const value = c.sqlite3_column_value(self.stmt, @intCast(c_int, _i)); 1350 const value = c.sqlite3_column_value(self.stmt, @intCast(_i));
1356 const datatype = c.sqlite3_value_type(value); 1351 const datatype = c.sqlite3_value_type(value);
1357 1352
1358 if (datatype == c.SQLITE_NULL) { 1353 if (datatype == c.SQLITE_NULL) {
@@ -1445,7 +1440,7 @@ pub fn Iterator(comptime Type: type) type {
1445 return std.meta.stringToEnum(FieldType, inner_value) orelse unreachable; 1440 return std.meta.stringToEnum(FieldType, inner_value) orelse unreachable;
1446 } 1441 }
1447 if (@typeInfo(FieldType.BaseType) == .Int) { 1442 if (@typeInfo(FieldType.BaseType) == .Int) {
1448 return @enumFromInt(FieldType, @intCast(TI.tag_type, inner_value)); 1443 return @enumFromInt(@as(TI.tag_type, @intCast(inner_value)));
1449 } 1444 }
1450 @compileError("enum column " ++ @typeName(FieldType) ++ " must have a BaseType of either string or int"); 1445 @compileError("enum column " ++ @typeName(FieldType) ++ " must have a BaseType of either string or int");
1451 }, 1446 },
@@ -1526,7 +1521,7 @@ pub const DynamicStatement = struct {
1526 const result = c.sqlite3_prepare_v3( 1521 const result = c.sqlite3_prepare_v3(
1527 db.db, 1522 db.db,
1528 query.ptr, 1523 query.ptr,
1529 @intCast(c_int, query.len), 1524 @intCast(query.len),
1530 flags, 1525 flags,
1531 &tmp, 1526 &tmp,
1532 options.sql_tail_ptr, 1527 options.sql_tail_ptr,
@@ -1588,11 +1583,11 @@ pub const DynamicStatement = struct {
1588 1583
1589 switch (FieldType) { 1584 switch (FieldType) {
1590 Text => { 1585 Text => {
1591 const result = c.sqlite3_bind_text(self.stmt, column, field.data.ptr, @intCast(c_int, field.data.len), null); 1586 const result = c.sqlite3_bind_text(self.stmt, column, field.data.ptr, @intCast(field.data.len), null);
1592 return convertResultToError(result); 1587 return convertResultToError(result);
1593 }, 1588 },
1594 Blob => { 1589 Blob => {
1595 const result = c.sqlite3_bind_blob(self.stmt, column, field.data.ptr, @intCast(c_int, field.data.len), null); 1590 const result = c.sqlite3_bind_blob(self.stmt, column, field.data.ptr, @intCast(field.data.len), null);
1596 return convertResultToError(result); 1591 return convertResultToError(result);
1597 }, 1592 },
1598 ZeroBlob => { 1593 ZeroBlob => {
@@ -1601,7 +1596,7 @@ pub const DynamicStatement = struct {
1601 }, 1596 },
1602 else => switch (field_type_info) { 1597 else => switch (field_type_info) {
1603 .Int, .ComptimeInt => { 1598 .Int, .ComptimeInt => {
1604 const result = c.sqlite3_bind_int64(self.stmt, column, @intCast(c_longlong, field)); 1599 const result = c.sqlite3_bind_int64(self.stmt, column, @intCast(field));
1605 return convertResultToError(result); 1600 return convertResultToError(result);
1606 }, 1601 },
1607 .Float, .ComptimeFloat => { 1602 .Float, .ComptimeFloat => {
@@ -1619,7 +1614,7 @@ pub const DynamicStatement = struct {
1619 .Slice => switch (ptr.child) { 1614 .Slice => switch (ptr.child) {
1620 u8 => { 1615 u8 => {
1621 // NOTE(vincent): The slice must live until after the prepared statement is finaliuzed, therefore we use SQLITE_STATIC to avoid a copy 1616 // NOTE(vincent): The slice must live until after the prepared statement is finaliuzed, therefore we use SQLITE_STATIC to avoid a copy
1622 const result = c.sqlite3_bind_text(self.stmt, column, field.ptr, @intCast(c_int, field.len), c.SQLITE_STATIC); 1617 const result = c.sqlite3_bind_text(self.stmt, column, field.ptr, @intCast(field.len), c.SQLITE_STATIC);
1623 return convertResultToError(result); 1618 return convertResultToError(result);
1624 }, 1619 },
1625 else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), 1620 else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)),
@@ -1631,7 +1626,7 @@ pub const DynamicStatement = struct {
1631 const data: []const u8 = field[0..field.len]; 1626 const data: []const u8 = field[0..field.len];
1632 1627
1633 // NOTE(vincent): The array is temporary and must be copied, therefore we use SQLITE_TRANSIENT 1628 // NOTE(vincent): The array is temporary and must be copied, therefore we use SQLITE_TRANSIENT
1634 const result = c.sqlite3_bind_text(self.stmt, column, data.ptr, @intCast(c_int, data.len), c.SQLITE_TRANSIENT); 1629 const result = c.sqlite3_bind_text(self.stmt, column, data.ptr, @intCast(data.len), c.SQLITE_TRANSIENT);
1635 return convertResultToError(result); 1630 return convertResultToError(result);
1636 }, 1631 },
1637 else => @compileError("cannot bind field " ++ field_name ++ " of type array of " ++ @typeName(arr.child)), 1632 else => @compileError("cannot bind field " ++ field_name ++ " of type array of " ++ @typeName(arr.child)),
@@ -1738,7 +1733,7 @@ pub const DynamicStatement = struct {
1738 switch (PointerTypeInfo.size) { 1733 switch (PointerTypeInfo.size) {
1739 .Slice => { 1734 .Slice => {
1740 for (values, 0..) |value_to_bind, index| { 1735 for (values, 0..) |value_to_bind, index| {
1741 try self.bindField(PointerTypeInfo.child, options, "unknown", @intCast(c_int, index), value_to_bind); 1736 try self.bindField(PointerTypeInfo.child, options, "unknown", @intCast(index), value_to_bind);
1742 } 1737 }
1743 }, 1738 },
1744 else => @compileError("TODO support pointer size " ++ @tagName(PointerTypeInfo.size)), 1739 else => @compileError("TODO support pointer size " ++ @tagName(PointerTypeInfo.size)),
@@ -1746,7 +1741,7 @@ pub const DynamicStatement = struct {
1746 }, 1741 },
1747 .Array => |ArrayTypeInfo| { 1742 .Array => |ArrayTypeInfo| {
1748 for (values, 0..) |value_to_bind, index| { 1743 for (values, 0..) |value_to_bind, index| {
1749 try self.bindField(ArrayTypeInfo.child, options, "unknown", @intCast(c_int, index), value_to_bind); 1744 try self.bindField(ArrayTypeInfo.child, options, "unknown", @intCast(index), value_to_bind);
1750 } 1745 }
1751 }, 1746 },
1752 else => @compileError("Unsupported type for values: " ++ @typeName(Type)), 1747 else => @compileError("Unsupported type for values: " ++ @typeName(Type)),
@@ -2541,7 +2536,7 @@ test "sqlite: read in an anonymous struct" {
2541 try testing.expectEqualStrings(exp.name, mem.sliceTo(&row.?.name_2, 0xAD)); 2536 try testing.expectEqualStrings(exp.name, mem.sliceTo(&row.?.name_2, 0xAD));
2542 try testing.expectEqual(exp.age, row.?.age); 2537 try testing.expectEqual(exp.age, row.?.age);
2543 try testing.expect(row.?.is_id); 2538 try testing.expect(row.?.is_id);
2544 try testing.expectEqual(exp.weight, @floatCast(f32, row.?.weight)); 2539 try testing.expectEqual(exp.weight, @as(f32, @floatCast(row.?.weight)));
2545} 2540}
2546 2541
2547test "sqlite: read in a Text struct" { 2542test "sqlite: read in a Text struct" {
@@ -2621,7 +2616,7 @@ test "sqlite: read a single text value" {
2621 try testing.expectEqualStrings("Vincent", name.?); 2616 try testing.expectEqualStrings("Vincent", name.?);
2622 }, 2617 },
2623 .Array => |arr| if (arr.sentinel) |sentinel_ptr| { 2618 .Array => |arr| if (arr.sentinel) |sentinel_ptr| {
2624 const sentinel = @ptrCast(*const arr.child, sentinel_ptr).*; 2619 const sentinel = @as(*const arr.child, @ptrCast(sentinel_ptr)).*;
2625 const res = mem.sliceTo(&name.?, sentinel); 2620 const res = mem.sliceTo(&name.?, sentinel);
2626 try testing.expectEqualStrings("Vincent", res); 2621 try testing.expectEqualStrings("Vincent", res);
2627 } else { 2622 } else {
@@ -2969,7 +2964,7 @@ test "sqlite: statement iterator" {
2969 var i: usize = 0; 2964 var i: usize = 0;
2970 while (i < 20) : (i += 1) { 2965 while (i < 20) : (i += 1) {
2971 const name = try std.fmt.allocPrint(allocator, "Vincent {d}", .{i}); 2966 const name = try std.fmt.allocPrint(allocator, "Vincent {d}", .{i});
2972 const user = TestUser{ .id = i, .name = name, .age = i + 200, .weight = @floatFromInt(f32, i + 200), .favorite_color = .indigo }; 2967 const user = TestUser{ .id = i, .name = name, .age = i + 200, .weight = @floatFromInt(i + 200), .favorite_color = .indigo };
2973 2968
2974 try expected_rows.append(user); 2969 try expected_rows.append(user);
2975 2970
@@ -3360,7 +3355,7 @@ test "sqlite: bind custom type" {
3360 var i: usize = 0; 3355 var i: usize = 0;
3361 while (i < 20) : (i += 1) { 3356 while (i < 20) : (i += 1) {
3362 var my_data: MyData = undefined; 3357 var my_data: MyData = undefined;
3363 @memset(&my_data.data, @intCast(u8, i)); 3358 @memset(&my_data.data, @as(u8, @intCast(i)));
3364 3359
3365 var arena = heap.ArenaAllocator.init(testing.allocator); 3360 var arena = heap.ArenaAllocator.init(testing.allocator);
3366 defer arena.deinit(); 3361 defer arena.deinit();
@@ -3390,7 +3385,7 @@ test "sqlite: bind custom type" {
3390 3385
3391 for (rows, 0..) |row, i| { 3386 for (rows, 0..) |row, i| {
3392 var exp_data: MyData = undefined; 3387 var exp_data: MyData = undefined;
3393 @memset(&exp_data.data, @intCast(u8, i)); 3388 @memset(&exp_data.data, @as(u8, @intCast(i)));
3394 3389
3395 try testing.expectEqualSlices(u8, &exp_data.data, &row.data.data); 3390 try testing.expectEqualSlices(u8, &exp_data.data, &row.data.data);
3396 } 3391 }
@@ -3567,7 +3562,7 @@ test "sqlite: create scalar function" {
3567 "myInteger64", 3562 "myInteger64",
3568 struct { 3563 struct {
3569 fn run(input: i64) i64 { 3564 fn run(input: i64) i64 {
3570 return @intCast(i64, input) * 2; 3565 return @as(i64, @intCast(input)) * 2;
3571 } 3566 }
3572 }.run, 3567 }.run,
3573 .{}, 3568 .{},
@@ -3693,7 +3688,7 @@ test "sqlite: create aggregate function with no aggregate context" {
3693 var db = try getTestDb(); 3688 var db = try getTestDb();
3694 defer db.deinit(); 3689 defer db.deinit();
3695 3690
3696 var rand = std.rand.DefaultPrng.init(@intCast(u64, std.time.milliTimestamp())); 3691 var rand = std.rand.DefaultPrng.init(@intCast(std.time.milliTimestamp()));
3697 3692
3698 // Create an aggregate function working with a MyContext 3693 // Create an aggregate function working with a MyContext
3699 3694
@@ -3754,7 +3749,7 @@ test "sqlite: create aggregate function with an aggregate context" {
3754 var db = try getTestDb(); 3749 var db = try getTestDb();
3755 defer db.deinit(); 3750 defer db.deinit();
3756 3751
3757 var rand = std.rand.DefaultPrng.init(@intCast(u64, std.time.milliTimestamp())); 3752 var rand = std.rand.DefaultPrng.init(@intCast(std.time.milliTimestamp()));
3758 3753
3759 try db.createAggregateFunction( 3754 try db.createAggregateFunction(
3760 "mySum", 3755 "mySum",
diff --git a/vtab.zig b/vtab.zig
index a7645c3..d871d01 100644
--- a/vtab.zig
+++ b/vtab.zig
@@ -24,7 +24,7 @@ pub const ModuleContext = struct {
24}; 24};
25 25
26fn dupeToSQLiteString(s: []const u8) [*c]const u8 { 26fn dupeToSQLiteString(s: []const u8) [*c]const u8 {
27 var buffer = @ptrCast([*c]u8, c.sqlite3_malloc(@intCast(c_int, s.len) + 1)); 27 var buffer: [*c]u8 = @ptrCast(c.sqlite3_malloc(@intCast(s.len + 1)));
28 28
29 mem.copy(u8, buffer[0..s.len], s); 29 mem.copy(u8, buffer[0..s.len], s);
30 buffer[s.len] = 0; 30 buffer[s.len] = 0;
@@ -202,15 +202,15 @@ pub const BestIndexBuilder = struct {
202 .allocator = allocator, 202 .allocator = allocator,
203 .index_info = index_info, 203 .index_info = index_info,
204 .id_str_buffer = std.ArrayList(u8).init(allocator), 204 .id_str_buffer = std.ArrayList(u8).init(allocator),
205 .constraints = try allocator.alloc(Constraint, @intCast(usize, index_info.nConstraint)), 205 .constraints = try allocator.alloc(Constraint, @intCast(index_info.nConstraint)),
206 .columns_used = @intCast(u64, index_info.colUsed), 206 .columns_used = @intCast(index_info.colUsed),
207 .id = .{}, 207 .id = .{},
208 }; 208 };
209 209
210 for (res.constraints, 0..) |*constraint, i| { 210 for (res.constraints, 0..) |*constraint, i| {
211 const raw_constraint = index_info.aConstraint[i]; 211 const raw_constraint = index_info.aConstraint[i];
212 212
213 constraint.column = @intCast(isize, raw_constraint.iColumn); 213 constraint.column = @intCast(raw_constraint.iColumn);
214 constraint.op = try constraintOpFromCode(raw_constraint.op); 214 constraint.op = try constraintOpFromCode(raw_constraint.op);
215 constraint.usable = if (raw_constraint.usable == 1) true else false; 215 constraint.usable = if (raw_constraint.usable == 1) true else false;
216 constraint.usage = .{}; 216 constraint.usage = .{};
@@ -239,10 +239,10 @@ pub const BestIndexBuilder = struct {
239 } 239 }
240 240
241 // Identifiers 241 // Identifiers
242 index_info.idxNum = @intCast(c_int, self.id.num); 242 index_info.idxNum = @intCast(self.id.num);
243 if (self.id.str.len > 0) { 243 if (self.id.str.len > 0) {
244 // Must always be NULL-terminated so add 1 244 // Must always be NULL-terminated so add 1
245 const tmp = @ptrCast([*c]u8, c.sqlite3_malloc(@intCast(c_int, self.id.str.len + 1))); 245 const tmp: [*c]u8 = @ptrCast(c.sqlite3_malloc(@intCast(self.id.str.len + 1)));
246 246
247 mem.copy(u8, tmp[0..self.id.str.len], self.id.str); 247 mem.copy(u8, tmp[0..self.id.str.len], self.id.str);
248 tmp[self.id.str.len] = 0; 248 tmp[self.id.str.len] = 0;
@@ -278,7 +278,7 @@ pub const IndexIdentifier = struct {
278 278
279 fn fromC(idx_num: c_int, idx_str: [*c]const u8) IndexIdentifier { 279 fn fromC(idx_num: c_int, idx_str: [*c]const u8) IndexIdentifier {
280 return IndexIdentifier{ 280 return IndexIdentifier{
281 .num = @intCast(i32, idx_num), 281 .num = @intCast(idx_num),
282 .str = if (idx_str != null) mem.sliceTo(idx_str, 0) else "", 282 .str = if (idx_str != null) mem.sliceTo(idx_str, 0) else "",
283 }; 283 };
284 } 284 }
@@ -538,7 +538,7 @@ pub const ModuleArgument = union(enum) {
538const ParseModuleArgumentsError = error{} || mem.Allocator.Error; 538const ParseModuleArgumentsError = error{} || mem.Allocator.Error;
539 539
540fn parseModuleArguments(allocator: mem.Allocator, argc: c_int, argv: [*c]const [*c]const u8) ParseModuleArgumentsError![]ModuleArgument { 540fn parseModuleArguments(allocator: mem.Allocator, argc: c_int, argv: [*c]const [*c]const u8) ParseModuleArgumentsError![]ModuleArgument {
541 var res = try allocator.alloc(ModuleArgument, @intCast(usize, argc)); 541 var res = try allocator.alloc(ModuleArgument, @intCast(argc));
542 errdefer allocator.free(res); 542 errdefer allocator.free(res);
543 543
544 for (res, 0..) |*marg, i| { 544 for (res, 0..) |*marg, i| {
@@ -695,7 +695,7 @@ pub fn VirtualTable(
695 table: Table, 695 table: Table,
696 696
697 fn getModuleContext(ptr: ?*anyopaque) *ModuleContext { 697 fn getModuleContext(ptr: ?*anyopaque) *ModuleContext {
698 return @ptrCast(*ModuleContext, @alignCast(@alignOf(ModuleContext), ptr.?)); 698 return @ptrCast(@alignCast(ptr.?));
699 } 699 }
700 700
701 fn createState(allocator: mem.Allocator, diags: *VTabDiagnostics, module_context: *ModuleContext, args: []const ModuleArgument) !*State { 701 fn createState(allocator: mem.Allocator, diags: *VTabDiagnostics, module_context: *ModuleContext, args: []const ModuleArgument) !*State {
@@ -742,9 +742,9 @@ pub fn VirtualTable(
742 err_str.* = dupeToSQLiteString(diags.error_message); 742 err_str.* = dupeToSQLiteString(diags.error_message);
743 return c.SQLITE_ERROR; 743 return c.SQLITE_ERROR;
744 }; 744 };
745 vtab.* = @ptrCast(*c.sqlite3_vtab, state); 745 vtab.* = @ptrCast(state);
746 746
747 const res = c.sqlite3_declare_vtab(db, @ptrCast([*c]const u8, state.table.schema)); 747 const res = c.sqlite3_declare_vtab(db, @ptrCast(state.table.schema));
748 if (res != c.SQLITE_OK) { 748 if (res != c.SQLITE_OK) {
749 return c.SQLITE_ERROR; 749 return c.SQLITE_ERROR;
750 } 750 }
@@ -800,7 +800,7 @@ pub fn VirtualTable(
800 logger.err("unable to create cursor state, err: {!}", .{err}); 800 logger.err("unable to create cursor state, err: {!}", .{err});
801 return c.SQLITE_ERROR; 801 return c.SQLITE_ERROR;
802 }; 802 };
803 vtab_cursor.* = @ptrCast(*c.sqlite3_vtab_cursor, cursor_state); 803 vtab_cursor.* = @ptrCast(cursor_state);
804 804
805 return c.SQLITE_OK; 805 return c.SQLITE_OK;
806 } 806 }
@@ -837,7 +837,7 @@ pub fn VirtualTable(
837 const FilterArgsFromCPointerError = error{} || mem.Allocator.Error; 837 const FilterArgsFromCPointerError = error{} || mem.Allocator.Error;
838 838
839 fn filterArgsFromCPointer(allocator: mem.Allocator, argc: c_int, argv: [*c]?*c.sqlite3_value) FilterArgsFromCPointerError![]FilterArg { 839 fn filterArgsFromCPointer(allocator: mem.Allocator, argc: c_int, argv: [*c]?*c.sqlite3_value) FilterArgsFromCPointerError![]FilterArg {
840 const size = @intCast(usize, argc); 840 const size: usize = @intCast(argc);
841 841
842 var res = try allocator.alloc(FilterArg, size); 842 var res = try allocator.alloc(FilterArg, size);
843 for (res, 0..) |*item, i| { 843 for (res, 0..) |*item, i| {
@@ -902,7 +902,7 @@ pub fn VirtualTable(
902 // 902 //
903 903
904 var diags = VTabDiagnostics{ .allocator = arena.allocator() }; 904 var diags = VTabDiagnostics{ .allocator = arena.allocator() };
905 const column = cursor.column(&diags, @intCast(i32, n)) catch { 905 const column = cursor.column(&diags, @intCast(n)) catch {
906 logger.err("unable to call Table.Cursor.column: {s}", .{diags.error_message}); 906 logger.err("unable to call Table.Cursor.column: {s}", .{diags.error_message});
907 return c.SQLITE_ERROR; 907 return c.SQLITE_ERROR;
908 }; 908 };
@@ -1214,7 +1214,7 @@ const TestVirtualTableCursor = struct {
1214 pub fn rowId(cursor: *TestVirtualTableCursor, diags: *VTabDiagnostics) RowIDError!i64 { 1214 pub fn rowId(cursor: *TestVirtualTableCursor, diags: *VTabDiagnostics) RowIDError!i64 {
1215 _ = diags; 1215 _ = diags;
1216 1216
1217 return @intCast(i64, cursor.iterator.pos); 1217 return @intCast(cursor.iterator.pos);
1218 } 1218 }
1219}; 1219};
1220 1220
@@ -1278,13 +1278,13 @@ test "parse module arguments" {
1278 var args = try allocator.alloc([*c]const u8, 20); 1278 var args = try allocator.alloc([*c]const u8, 20);
1279 for (args, 0..) |*arg, i| { 1279 for (args, 0..) |*arg, i| {
1280 const tmp = try fmt.allocPrintZ(allocator, "arg={d}", .{i}); 1280 const tmp = try fmt.allocPrintZ(allocator, "arg={d}", .{i});
1281 arg.* = @ptrCast([*c]const u8, tmp); 1281 arg.* = @ptrCast(tmp);
1282 } 1282 }
1283 1283
1284 const res = try parseModuleArguments( 1284 const res = try parseModuleArguments(
1285 allocator, 1285 allocator,
1286 @intCast(c_int, args.len), 1286 @intCast(args.len),
1287 @ptrCast([*c]const [*c]const u8, args), 1287 @ptrCast(args),
1288 ); 1288 );
1289 try testing.expectEqual(@as(usize, 20), res.len); 1289 try testing.expectEqual(@as(usize, 20), res.len);
1290 1290