summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2020-11-12 15:45:42 +0100
committerGravatar Vincent Rischmann2020-11-12 15:45:42 +0100
commitb5b228547d16e8d9589062ce69bb5da1b273f52b (patch)
treebedab03da17734ba1880df0c00e034c3f23f2cd3 /sqlite.zig
parentMerge branch 'reset-statement' into master (diff)
downloadzig-sqlite-b5b228547d16e8d9589062ce69bb5da1b273f52b.tar.gz
zig-sqlite-b5b228547d16e8d9589062ce69bb5da1b273f52b.tar.xz
zig-sqlite-b5b228547d16e8d9589062ce69bb5da1b273f52b.zip
allow reading a single float value
Diffstat (limited to '')
-rw-r--r--sqlite.zig44
1 files changed, 37 insertions, 7 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 9e26cd6..d08717b 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -326,6 +326,11 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t
326 c.SQLITE_DONE => null, 326 c.SQLITE_DONE => null,
327 else => std.debug.panic("invalid result {}", .{result}), 327 else => std.debug.panic("invalid result {}", .{result}),
328 }, 328 },
329 .Float => return switch (result) {
330 c.SQLITE_ROW => try self.readFloat(Type, options),
331 c.SQLITE_DONE => null,
332 else => std.debug.panic("invalid result {}", .{result}),
333 },
329 .Struct => return switch (result) { 334 .Struct => return switch (result) {
330 c.SQLITE_ROW => try self.readStruct(Type, options), 335 c.SQLITE_ROW => try self.readStruct(Type, options),
331 c.SQLITE_DONE => null, 336 c.SQLITE_DONE => null,
@@ -382,6 +387,10 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t
382 debug.assert(columns == 1); 387 debug.assert(columns == 1);
383 break :blk try self.readInt(Type, options); 388 break :blk try self.readInt(Type, options);
384 }, 389 },
390 .Float => blk: {
391 debug.assert(columns == 1);
392 break :blk try self.readFloat(Type, options);
393 },
385 .Struct => blk: { 394 .Struct => blk: {
386 std.debug.assert(columns == @typeInfo(Type).Struct.fields.len); 395 std.debug.assert(columns == @typeInfo(Type).Struct.fields.len);
387 break :blk try self.readStruct(Type, options); 396 break :blk try self.readStruct(Type, options);
@@ -405,6 +414,11 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t
405 return @intCast(Type, n); 414 return @intCast(Type, n);
406 } 415 }
407 416
417 fn readFloat(self: *Self, comptime Type: type, options: anytype) !Type {
418 const d = c.sqlite3_column_double(self.stmt, 0);
419 return @floatCast(Type, d);
420 }
421
408 const ReadBytesMode = enum { 422 const ReadBytesMode = enum {
409 Blob, 423 Blob,
410 Text, 424 Text,
@@ -597,18 +611,34 @@ test "sqlite: statement exec" {
597 testing.expectEqual(exp.age, row.?.age); 611 testing.expectEqual(exp.age, row.?.age);
598 } 612 }
599 613
600 // Test with a single integer 614 // Test with a single integer or float
601 615
602 { 616 {
603 const query = "SELECT age FROM user WHERE id = ?{usize}"; 617 const types = &[_]type{
618 u8,
619 u16,
620 u32,
621 u64,
622 u128,
623 usize,
624 f16,
625 f32,
626 f64,
627 f128,
628 };
604 629
605 var stmt: Statement(.{}, ParsedQuery.from(query)) = try db.prepare(query); 630 inline for (types) |typ| {
606 defer stmt.deinit(); 631 const query = "SELECT age FROM user WHERE id = ?{usize}";
607 632
608 var age = try stmt.one(usize, .{}, .{ .id = @as(usize, 20) }); 633 @setEvalBranchQuota(5000);
609 testing.expect(age != null); 634 var stmt: Statement(.{}, ParsedQuery.from(query)) = try db.prepare(query);
635 defer stmt.deinit();
610 636
611 testing.expectEqual(@as(usize, 33), age.?); 637 var age = try stmt.one(typ, .{}, .{ .id = @as(usize, 20) });
638 testing.expect(age != null);
639
640 testing.expectEqual(@as(typ, 33), age.?);
641 }
612 } 642 }
613 643
614 // Test with a Blob struct 644 // Test with a Blob struct