diff options
| -rw-r--r-- | sqlite.zig | 28 |
1 files changed, 11 insertions, 17 deletions
| @@ -377,30 +377,30 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 377 | Text, | 377 | Text, |
| 378 | }; | 378 | }; |
| 379 | 379 | ||
| 380 | fn readBytes(self: *Self, allocator: *mem.Allocator, mode: ReadBytesMode, _i: usize) !?[]const u8 { | 380 | fn readBytes(self: *Self, allocator: *mem.Allocator, mode: ReadBytesMode, _i: usize, ptr: *[]const u8) !void { |
| 381 | const i = @intCast(c_int, _i); | 381 | const i = @intCast(c_int, _i); |
| 382 | switch (mode) { | 382 | switch (mode) { |
| 383 | .Blob => { | 383 | .Blob => { |
| 384 | const data = c.sqlite3_column_blob(self.stmt, i); | 384 | const data = c.sqlite3_column_blob(self.stmt, i); |
| 385 | if (data == null) return null; | 385 | if (data == null) ptr.* = ""; |
| 386 | 386 | ||
| 387 | const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); | 387 | const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); |
| 388 | 388 | ||
| 389 | var tmp = try allocator.alloc(u8, size); | 389 | var tmp = try allocator.alloc(u8, size); |
| 390 | mem.copy(u8, tmp, @ptrCast([*c]const u8, data)[0..size]); | 390 | mem.copy(u8, tmp, @ptrCast([*c]const u8, data)[0..size]); |
| 391 | 391 | ||
| 392 | return tmp; | 392 | ptr.* = tmp; |
| 393 | }, | 393 | }, |
| 394 | .Text => { | 394 | .Text => { |
| 395 | const data = c.sqlite3_column_text(self.stmt, i); | 395 | const data = c.sqlite3_column_text(self.stmt, i); |
| 396 | if (data == null) return null; | 396 | if (data == null) ptr.* = ""; |
| 397 | 397 | ||
| 398 | const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); | 398 | const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); |
| 399 | 399 | ||
| 400 | var tmp = try allocator.alloc(u8, size); | 400 | var tmp = try allocator.alloc(u8, size); |
| 401 | mem.copy(u8, tmp, @ptrCast([*c]const u8, data)[0..size]); | 401 | mem.copy(u8, tmp, @ptrCast([*c]const u8, data)[0..size]); |
| 402 | 402 | ||
| 403 | return tmp; | 403 | ptr.* = tmp; |
| 404 | }, | 404 | }, |
| 405 | } | 405 | } |
| 406 | } | 406 | } |
| @@ -413,20 +413,14 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 413 | const field_type_info = @typeInfo(field.field_type); | 413 | const field_type_info = @typeInfo(field.field_type); |
| 414 | 414 | ||
| 415 | switch (field.field_type) { | 415 | switch (field.field_type) { |
| 416 | []const u8, []u8 => if (try self.readBytes(options.allocator, .Blob, i)) |tmp| { | 416 | []const u8, []u8 => { |
| 417 | @field(value, field.name) = tmp; | 417 | try self.readBytes(options.allocator, .Blob, i, &@field(value, field.name)); |
| 418 | } else { | ||
| 419 | @field(value, field.name) = ""; | ||
| 420 | }, | 418 | }, |
| 421 | Blob => if (try self.readBytes(options.allocator, .Blob, i)) |tmp| { | 419 | Blob => { |
| 422 | @field(value, field.name).data = tmp; | 420 | try self.readBytes(options.allocator, .Blob, i, &@field(value, field.name).data); |
| 423 | } else { | ||
| 424 | @field(value, field.name).data = ""; | ||
| 425 | }, | 421 | }, |
| 426 | Text => if (try self.readBytes(options.allocator, .Text, i)) |tmp| { | 422 | Text => { |
| 427 | @field(value, field.name).data = tmp; | 423 | try self.readBytes(options.allocator, .Text, i, &@field(value, field.name).data); |
| 428 | } else { | ||
| 429 | @field(value, field.name).data = ""; | ||
| 430 | }, | 424 | }, |
| 431 | else => switch (field_type_info) { | 425 | else => switch (field_type_info) { |
| 432 | .Int => { | 426 | .Int => { |