summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sqlite.zig32
1 files changed, 13 insertions, 19 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 817cff8..81c9e43 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -196,20 +196,18 @@ pub fn Iterator(comptime Type: type) type {
196 switch (Type) { 196 switch (Type) {
197 []const u8, []u8 => { 197 []const u8, []u8 => {
198 debug.assert(columns == 1); 198 debug.assert(columns == 1);
199 var ret: Type = undefined; 199 return try self.readBytes(Type, options, .Text, 0);
200 try self.readBytes(options, .Text, 0, &ret);
201 return ret;
202 }, 200 },
203 Blob => { 201 Blob => {
204 debug.assert(columns == 1); 202 debug.assert(columns == 1);
205 var ret: Type = undefined; 203 var ret: Type = undefined;
206 try self.readBytes(options, .Blob, 0, &ret.data); 204 ret.data = try self.readBytes([]const u8, options, .Blob, 0);
207 return ret; 205 return ret;
208 }, 206 },
209 Text => { 207 Text => {
210 debug.assert(columns == 1); 208 debug.assert(columns == 1);
211 var ret: Type = undefined; 209 var ret: Type = undefined;
212 try self.readBytes(options, .Text, 0, &ret.data); 210 ret.data = try self.readBytes([]const u8, options, .Text, 0);
213 return ret; 211 return ret;
214 }, 212 },
215 else => {}, 213 else => {},
@@ -294,30 +292,26 @@ pub fn Iterator(comptime Type: type) type {
294 Text, 292 Text,
295 }; 293 };
296 294
297 fn readBytes(self: *Self, options: anytype, mode: ReadBytesMode, _i: usize, ptr: *[]const u8) !void { 295 fn readBytes(self: *Self, comptime BytesType: type, options: anytype, mode: ReadBytesMode, _i: usize) !BytesType {
298 const i = @intCast(c_int, _i); 296 const i = @intCast(c_int, _i);
299 switch (mode) { 297 switch (mode) {
300 .Blob => { 298 .Blob => {
301 const data = c.sqlite3_column_blob(self.stmt, i); 299 const data = c.sqlite3_column_blob(self.stmt, i);
302 if (data == null) ptr.* = ""; 300 if (data == null) return "";
303 301
304 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); 302 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i));
303 const ptr = @ptrCast([*c]const u8, data)[0..size];
305 304
306 var tmp = try options.allocator.alloc(u8, size); 305 return options.allocator.dupe(u8, ptr);
307 mem.copy(u8, tmp, @ptrCast([*c]const u8, data)[0..size]);
308
309 ptr.* = tmp;
310 }, 306 },
311 .Text => { 307 .Text => {
312 const data = c.sqlite3_column_text(self.stmt, i); 308 const data = c.sqlite3_column_text(self.stmt, i);
313 if (data == null) ptr.* = ""; 309 if (data == null) return "";
314 310
315 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); 311 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i));
312 const ptr = @ptrCast([*c]const u8, data)[0..size];
316 313
317 var tmp = try options.allocator.alloc(u8, size); 314 return options.allocator.dupe(u8, ptr);
318 mem.copy(u8, tmp, @ptrCast([*c]const u8, data)[0..size]);
319
320 ptr.* = tmp;
321 }, 315 },
322 } 316 }
323 } 317 }
@@ -331,13 +325,13 @@ pub fn Iterator(comptime Type: type) type {
331 325
332 switch (field.field_type) { 326 switch (field.field_type) {
333 []const u8, []u8 => { 327 []const u8, []u8 => {
334 try self.readBytes(options, .Blob, i, &@field(value, field.name)); 328 @field(value, field.name) = try self.readBytes(field.field_type, options, .Blob, i);
335 }, 329 },
336 Blob => { 330 Blob => {
337 try self.readBytes(options, .Blob, i, &@field(value, field.name).data); 331 @field(value, field.name).data = try self.readBytes([]const u8, options, .Blob, i);
338 }, 332 },
339 Text => { 333 Text => {
340 try self.readBytes(options, .Text, i, &@field(value, field.name).data); 334 @field(value, field.name).data = try self.readBytes([]const u8, options, .Text, i);
341 }, 335 },
342 else => switch (field_type_info) { 336 else => switch (field_type_info) {
343 .Int => { 337 .Int => {