diff options
| -rw-r--r-- | sqlite.zig | 64 |
1 files changed, 43 insertions, 21 deletions
| @@ -159,6 +159,30 @@ pub const Db = struct { | |||
| 159 | return getLastDetailedErrorFromDb(self.db); | 159 | return getLastDetailedErrorFromDb(self.db); |
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | fn getPragmaQuery(comptime buf: []u8, comptime name: []const u8, comptime arg: anytype) []const u8 { | ||
| 163 | return if (arg.len == 1) blk: { | ||
| 164 | break :blk try std.fmt.bufPrint(buf, "PRAGMA {} = {}", .{ name, arg[0] }); | ||
| 165 | } else blk: { | ||
| 166 | break :blk try std.fmt.bufPrint(buf, "PRAGMA {}", .{name}); | ||
| 167 | }; | ||
| 168 | } | ||
| 169 | |||
| 170 | /// pragmaAlloc is like `pragma` but can allocate memory. | ||
| 171 | /// | ||
| 172 | /// Useful when the pragma command returns text, for example: | ||
| 173 | /// | ||
| 174 | /// const journal_mode = try db.pragma([]const u8, allocator, .{}, "journal_mode", .{}); | ||
| 175 | /// | ||
| 176 | pub fn pragmaAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: anytype, comptime name: []const u8, comptime arg: anytype) !?Type { | ||
| 177 | comptime var buf: [1024]u8 = undefined; | ||
| 178 | comptime var query = getPragmaQuery(&buf, name, arg); | ||
| 179 | |||
| 180 | var stmt = try self.prepare(query); | ||
| 181 | defer stmt.deinit(); | ||
| 182 | |||
| 183 | return try stmt.oneAlloc(Type, allocator, options, .{}); | ||
| 184 | } | ||
| 185 | |||
| 162 | /// pragma is a convenience function to use the PRAGMA statement. | 186 | /// pragma is a convenience function to use the PRAGMA statement. |
| 163 | /// | 187 | /// |
| 164 | /// Here is how to set a pragma value: | 188 | /// Here is how to set a pragma value: |
| @@ -167,21 +191,14 @@ pub const Db = struct { | |||
| 167 | /// | 191 | /// |
| 168 | /// Here is how to query a pragama value: | 192 | /// Here is how to query a pragama value: |
| 169 | /// | 193 | /// |
| 170 | /// const journal_mode = try db.pragma( | 194 | /// const journal_mode = try db.pragma([128:0]const u8, .{}, "journal_mode", .{}); |
| 171 | /// []const u8, | ||
| 172 | /// "journal_mode", | ||
| 173 | /// .{ .allocator = allocator }, | ||
| 174 | /// .{}, | ||
| 175 | /// ); | ||
| 176 | /// | 195 | /// |
| 177 | /// The pragma name must be known at comptime. | 196 | /// The pragma name must be known at comptime. |
| 178 | pub fn pragma(self: *Self, comptime Type: type, comptime name: []const u8, options: anytype, arg: anytype) !?Type { | 197 | /// |
| 198 | /// This cannot allocate memory. If your pragma command returns text you must use an array or call `pragmaAlloc`. | ||
| 199 | pub fn pragma(self: *Self, comptime Type: type, options: anytype, comptime name: []const u8, arg: anytype) !?Type { | ||
| 179 | comptime var buf: [1024]u8 = undefined; | 200 | comptime var buf: [1024]u8 = undefined; |
| 180 | comptime var query = if (arg.len == 1) blk: { | 201 | comptime var query = getPragmaQuery(&buf, name, arg); |
| 181 | break :blk try std.fmt.bufPrint(&buf, "PRAGMA {} = {}", .{ name, arg[0] }); | ||
| 182 | } else blk: { | ||
| 183 | break :blk try std.fmt.bufPrint(&buf, "PRAGMA {}", .{name}); | ||
| 184 | }; | ||
| 185 | 202 | ||
| 186 | var stmt = try self.prepare(query); | 203 | var stmt = try self.prepare(query); |
| 187 | defer stmt.deinit(); | 204 | defer stmt.deinit(); |
| @@ -920,19 +937,24 @@ test "sqlite: db pragma" { | |||
| 920 | var db: Db = undefined; | 937 | var db: Db = undefined; |
| 921 | try db.init(initOptions()); | 938 | try db.init(initOptions()); |
| 922 | 939 | ||
| 923 | const foreign_keys = try db.pragma(usize, "foreign_keys", .{}, .{}); | 940 | const foreign_keys = try db.pragma(usize, .{}, "foreign_keys", .{}); |
| 924 | testing.expect(foreign_keys != null); | 941 | testing.expect(foreign_keys != null); |
| 925 | testing.expectEqual(@as(usize, 0), foreign_keys.?); | 942 | testing.expectEqual(@as(usize, 0), foreign_keys.?); |
| 926 | 943 | ||
| 944 | const arg = .{"wal"}; | ||
| 945 | |||
| 927 | if (build_options.in_memory) { | 946 | if (build_options.in_memory) { |
| 928 | const journal_mode = try db.pragma( | 947 | { |
| 929 | [128:0]u8, | 948 | const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", arg); |
| 930 | "journal_mode", | 949 | testing.expect(journal_mode != null); |
| 931 | .{}, | 950 | testing.expectEqualStrings("memory", mem.spanZ(&journal_mode.?)); |
| 932 | .{"wal"}, | 951 | } |
| 933 | ); | 952 | |
| 934 | testing.expect(journal_mode != null); | 953 | { |
| 935 | testing.expectEqualStrings("memory", journal_mode.?); | 954 | const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", arg); |
| 955 | testing.expect(journal_mode != null); | ||
| 956 | testing.expectEqualStrings("memory", journal_mode.?); | ||
| 957 | } | ||
| 936 | } else { | 958 | } else { |
| 937 | { | 959 | { |
| 938 | const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", arg); | 960 | const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", arg); |