summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2020-11-13 17:13:59 +0100
committerGravatar Vincent Rischmann2020-11-13 17:13:59 +0100
commitcc0c6adb429cdb065b0f5b3d797bfe41985ddf87 (patch)
treeb7dbb78b9a0347b173b517fcb43d9ad5088fe0ef /sqlite.zig
parentallow reading a single string in one() and all() (diff)
parentfix the pragma test when using -Dis_ci (diff)
downloadzig-sqlite-cc0c6adb429cdb065b0f5b3d797bfe41985ddf87.tar.gz
zig-sqlite-cc0c6adb429cdb065b0f5b3d797bfe41985ddf87.tar.xz
zig-sqlite-cc0c6adb429cdb065b0f5b3d797bfe41985ddf87.zip
Merge branch 'pragma' into master
Diffstat (limited to 'sqlite.zig')
-rw-r--r--sqlite.zig62
1 files changed, 62 insertions, 0 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 1b50d36..fd5b606 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -88,6 +88,36 @@ pub const Db = struct {
88 _ = c.sqlite3_close(self.db); 88 _ = c.sqlite3_close(self.db);
89 } 89 }
90 90
91 /// pragma is a convenience function to use the PRAGMA statement.
92 ///
93 /// Here is how to set a pragma value:
94 ///
95 /// try db.pragma(void, "foreign_keys", .{}, .{1});
96 ///
97 /// Here is how to query a pragama value:
98 ///
99 /// const journal_mode = try db.pragma(
100 /// []const u8,
101 /// "journal_mode",
102 /// .{ .allocator = allocator },
103 /// .{},
104 /// );
105 ///
106 /// The pragma name must be known at comptime.
107 pub fn pragma(self: *Self, comptime Type: type, comptime name: []const u8, options: anytype, arg: anytype) !?Type {
108 comptime var buf: [1024]u8 = undefined;
109 comptime var query = if (arg.len == 1) blk: {
110 break :blk try std.fmt.bufPrint(&buf, "PRAGMA {} = {}", .{ name, arg[0] });
111 } else blk: {
112 break :blk try std.fmt.bufPrint(&buf, "PRAGMA {}", .{name});
113 };
114
115 var stmt = try self.prepare(query);
116 defer stmt.deinit();
117
118 return try stmt.one(Type, options, .{});
119 }
120
91 /// exec is a convenience function which prepares a statement and executes it directly. 121 /// exec is a convenience function which prepares a statement and executes it directly.
92 pub fn exec(self: *Self, comptime query: []const u8, values: anytype) !void { 122 pub fn exec(self: *Self, comptime query: []const u8, values: anytype) !void {
93 var stmt = try self.prepare(query); 123 var stmt = try self.prepare(query);
@@ -618,6 +648,38 @@ test "sqlite: db init" {
618 try db.init(testing.allocator, .{}); 648 try db.init(testing.allocator, .{});
619} 649}
620 650
651test "sqlite: db pragma" {
652 var arena = std.heap.ArenaAllocator.init(testing.allocator);
653 defer arena.deinit();
654
655 var db: Db = undefined;
656 try db.init(testing.allocator, .{ .mode = dbMode() });
657
658 const foreign_keys = try db.pragma(usize, "foreign_keys", .{}, .{});
659 testing.expect(foreign_keys != null);
660 testing.expectEqual(@as(usize, 0), foreign_keys.?);
661
662 if (build_options.is_ci) {
663 const journal_mode = try db.pragma(
664 []const u8,
665 "journal_mode",
666 .{ .allocator = &arena.allocator },
667 .{"wal"},
668 );
669 testing.expect(journal_mode != null);
670 testing.expectEqualStrings("memory", journal_mode.?);
671 } else {
672 const journal_mode = try db.pragma(
673 []const u8,
674 "journal_mode",
675 .{ .allocator = &arena.allocator },
676 .{"wal"},
677 );
678 testing.expect(journal_mode != null);
679 testing.expectEqualStrings("wal", journal_mode.?);
680 }
681}
682
621test "sqlite: statement exec" { 683test "sqlite: statement exec" {
622 var db: Db = undefined; 684 var db: Db = undefined;
623 try db.init(testing.allocator, .{ .mode = dbMode() }); 685 try db.init(testing.allocator, .{ .mode = dbMode() });