diff options
| author | 2021-04-17 00:53:58 +0200 | |
|---|---|---|
| committer | 2021-04-17 00:53:58 +0200 | |
| commit | be3d4dd6e449e6073c1da5240446d637653dc6e5 (patch) | |
| tree | 50be17235a1dab2a2a8a8df68e3e226ed90c2a0e /sqlite.zig | |
| parent | fix documentation (diff) | |
| download | zig-sqlite-be3d4dd6e449e6073c1da5240446d637653dc6e5.tar.gz zig-sqlite-be3d4dd6e449e6073c1da5240446d637653dc6e5.tar.xz zig-sqlite-be3d4dd6e449e6073c1da5240446d637653dc6e5.zip | |
replace the pragma argument with a nullable string
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 33 |
1 files changed, 15 insertions, 18 deletions
| @@ -365,12 +365,11 @@ pub const Db = struct { | |||
| 365 | return getLastDetailedErrorFromDb(self.db); | 365 | return getLastDetailedErrorFromDb(self.db); |
| 366 | } | 366 | } |
| 367 | 367 | ||
| 368 | fn getPragmaQuery(comptime buf: []u8, comptime name: []const u8, comptime arg: anytype) []const u8 { | 368 | fn getPragmaQuery(comptime buf: []u8, comptime name: []const u8, comptime arg: ?[]const u8) []const u8 { |
| 369 | return if (arg.len == 1) blk: { | 369 | if (arg) |a| { |
| 370 | break :blk try std.fmt.bufPrint(buf, "PRAGMA {s} = {s}", .{ name, arg[0] }); | 370 | return try std.fmt.bufPrint(buf, "PRAGMA {s} = {s}", .{ name, a }); |
| 371 | } else blk: { | 371 | } |
| 372 | break :blk try std.fmt.bufPrint(buf, "PRAGMA {s}", .{name}); | 372 | return try std.fmt.bufPrint(buf, "PRAGMA {s}", .{name}); |
| 373 | }; | ||
| 374 | } | 373 | } |
| 375 | 374 | ||
| 376 | /// getLastInsertRowID returns the last inserted rowid. | 375 | /// getLastInsertRowID returns the last inserted rowid. |
| @@ -383,9 +382,9 @@ pub const Db = struct { | |||
| 383 | /// | 382 | /// |
| 384 | /// Useful when the pragma command returns text, for example: | 383 | /// Useful when the pragma command returns text, for example: |
| 385 | /// | 384 | /// |
| 386 | /// const journal_mode = try db.pragma([]const u8, allocator, .{}, "journal_mode", .{}); | 385 | /// const journal_mode = try db.pragma([]const u8, allocator, .{}, "journal_mode", null); |
| 387 | /// | 386 | /// |
| 388 | pub fn pragmaAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: anytype, comptime name: []const u8, comptime arg: anytype) !?Type { | 387 | pub fn pragmaAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: anytype, comptime name: []const u8, comptime arg: ?[]const u8) !?Type { |
| 389 | comptime var buf: [1024]u8 = undefined; | 388 | comptime var buf: [1024]u8 = undefined; |
| 390 | comptime var query = getPragmaQuery(&buf, name, arg); | 389 | comptime var query = getPragmaQuery(&buf, name, arg); |
| 391 | 390 | ||
| @@ -399,16 +398,16 @@ pub const Db = struct { | |||
| 399 | /// | 398 | /// |
| 400 | /// Here is how to set a pragma value: | 399 | /// Here is how to set a pragma value: |
| 401 | /// | 400 | /// |
| 402 | /// try db.pragma(void, .{}, "foreign_keys", .{1}); | 401 | /// try db.pragma(void, .{}, "foreign_keys", "1"); |
| 403 | /// | 402 | /// |
| 404 | /// Here is how to query a pragama value: | 403 | /// Here is how to query a pragama value: |
| 405 | /// | 404 | /// |
| 406 | /// const journal_mode = try db.pragma([128:0]const u8, .{}, "journal_mode", .{}); | 405 | /// const journal_mode = try db.pragma([128:0]const u8, .{}, "journal_mode", null); |
| 407 | /// | 406 | /// |
| 408 | /// The pragma name must be known at comptime. | 407 | /// The pragma name must be known at comptime. |
| 409 | /// | 408 | /// |
| 410 | /// This cannot allocate memory. If your pragma command returns text you must use an array or call `pragmaAlloc`. | 409 | /// This cannot allocate memory. If your pragma command returns text you must use an array or call `pragmaAlloc`. |
| 411 | pub fn pragma(self: *Self, comptime Type: type, options: anytype, comptime name: []const u8, arg: anytype) !?Type { | 410 | pub fn pragma(self: *Self, comptime Type: type, options: anytype, comptime name: []const u8, comptime arg: ?[]const u8) !?Type { |
| 412 | comptime var buf: [1024]u8 = undefined; | 411 | comptime var buf: [1024]u8 = undefined; |
| 413 | comptime var query = getPragmaQuery(&buf, name, arg); | 412 | comptime var query = getPragmaQuery(&buf, name, arg); |
| 414 | 413 | ||
| @@ -1234,33 +1233,31 @@ test "sqlite: db pragma" { | |||
| 1234 | 1233 | ||
| 1235 | var db = try getTestDb(); | 1234 | var db = try getTestDb(); |
| 1236 | 1235 | ||
| 1237 | const foreign_keys = try db.pragma(usize, .{}, "foreign_keys", .{}); | 1236 | const foreign_keys = try db.pragma(usize, .{}, "foreign_keys", null); |
| 1238 | testing.expect(foreign_keys != null); | 1237 | testing.expect(foreign_keys != null); |
| 1239 | testing.expectEqual(@as(usize, 0), foreign_keys.?); | 1238 | testing.expectEqual(@as(usize, 0), foreign_keys.?); |
| 1240 | 1239 | ||
| 1241 | const arg = .{"wal"}; | ||
| 1242 | |||
| 1243 | if (build_options.in_memory) { | 1240 | if (build_options.in_memory) { |
| 1244 | { | 1241 | { |
| 1245 | const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", arg); | 1242 | const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", "wal"); |
| 1246 | testing.expect(journal_mode != null); | 1243 | testing.expect(journal_mode != null); |
| 1247 | testing.expectEqualStrings("memory", mem.spanZ(&journal_mode.?)); | 1244 | testing.expectEqualStrings("memory", mem.spanZ(&journal_mode.?)); |
| 1248 | } | 1245 | } |
| 1249 | 1246 | ||
| 1250 | { | 1247 | { |
| 1251 | const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", arg); | 1248 | const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", "wal"); |
| 1252 | testing.expect(journal_mode != null); | 1249 | testing.expect(journal_mode != null); |
| 1253 | testing.expectEqualStrings("memory", journal_mode.?); | 1250 | testing.expectEqualStrings("memory", journal_mode.?); |
| 1254 | } | 1251 | } |
| 1255 | } else { | 1252 | } else { |
| 1256 | { | 1253 | { |
| 1257 | const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", arg); | 1254 | const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", "wal"); |
| 1258 | testing.expect(journal_mode != null); | 1255 | testing.expect(journal_mode != null); |
| 1259 | testing.expectEqualStrings("wal", mem.spanZ(&journal_mode.?)); | 1256 | testing.expectEqualStrings("wal", mem.spanZ(&journal_mode.?)); |
| 1260 | } | 1257 | } |
| 1261 | 1258 | ||
| 1262 | { | 1259 | { |
| 1263 | const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", arg); | 1260 | const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", "wal"); |
| 1264 | testing.expect(journal_mode != null); | 1261 | testing.expect(journal_mode != null); |
| 1265 | testing.expectEqualStrings("wal", journal_mode.?); | 1262 | testing.expectEqualStrings("wal", journal_mode.?); |
| 1266 | } | 1263 | } |