diff options
| author | 2021-12-01 19:07:05 +0100 | |
|---|---|---|
| committer | 2021-12-01 19:07:05 +0100 | |
| commit | 4964f195bd411d39eefc6bbabb8b675371894cc3 (patch) | |
| tree | a7f465f668e3a22052e6da01a8ff6023261a84e5 /sqlite.zig | |
| parent | dynamic statement: fix documentation (diff) | |
| download | zig-sqlite-4964f195bd411d39eefc6bbabb8b675371894cc3.tar.gz zig-sqlite-4964f195bd411d39eefc6bbabb8b675371894cc3.tar.xz zig-sqlite-4964f195bd411d39eefc6bbabb8b675371894cc3.zip | |
all: fix for latest Allocator interface refactor
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 105 |
1 files changed, 61 insertions, 44 deletions
| @@ -422,7 +422,7 @@ pub const Db = struct { | |||
| 422 | /// | 422 | /// |
| 423 | /// const journal_mode = try db.pragma([]const u8, allocator, .{}, "journal_mode", null); | 423 | /// const journal_mode = try db.pragma([]const u8, allocator, .{}, "journal_mode", null); |
| 424 | /// | 424 | /// |
| 425 | pub fn pragmaAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, comptime name: []const u8, comptime arg: ?[]const u8) !?Type { | 425 | pub fn pragmaAlloc(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, comptime name: []const u8, comptime arg: ?[]const u8) !?Type { |
| 426 | comptime var query = getPragmaQuery(name, arg); | 426 | comptime var query = getPragmaQuery(name, arg); |
| 427 | 427 | ||
| 428 | var stmt = try self.prepare(query); | 428 | var stmt = try self.prepare(query); |
| @@ -482,14 +482,14 @@ pub const Db = struct { | |||
| 482 | } | 482 | } |
| 483 | 483 | ||
| 484 | /// oneAlloc is like `one` but can allocate memory. | 484 | /// oneAlloc is like `one` but can allocate memory. |
| 485 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, comptime query: []const u8, options: QueryOptions, values: anytype) !?Type { | 485 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: mem.Allocator, comptime query: []const u8, options: QueryOptions, values: anytype) !?Type { |
| 486 | var stmt = try self.prepareWithDiags(query, options); | 486 | var stmt = try self.prepareWithDiags(query, options); |
| 487 | defer stmt.deinit(); | 487 | defer stmt.deinit(); |
| 488 | return try stmt.oneAlloc(Type, allocator, options, values); | 488 | return try stmt.oneAlloc(Type, allocator, options, values); |
| 489 | } | 489 | } |
| 490 | 490 | ||
| 491 | /// oneDynamicAlloc is like `oneDynamic` but can allocate memory. | 491 | /// oneDynamicAlloc is like `oneDynamic` but can allocate memory. |
| 492 | pub fn oneDynamicAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, query: []const u8, options: QueryOptions, values: anytype) !?Type { | 492 | pub fn oneDynamicAlloc(self: *Self, comptime Type: type, allocator: mem.Allocator, query: []const u8, options: QueryOptions, values: anytype) !?Type { |
| 493 | var stmt = try self.prepareDynamicWithDiags(query, options); | 493 | var stmt = try self.prepareDynamicWithDiags(query, options); |
| 494 | defer stmt.deinit(); | 494 | defer stmt.deinit(); |
| 495 | return try stmt.oneAlloc(Type, allocator, options, values); | 495 | return try stmt.oneAlloc(Type, allocator, options, values); |
| @@ -662,9 +662,10 @@ pub const Savepoint = struct { | |||
| 662 | 662 | ||
| 663 | var buffer: [256]u8 = undefined; | 663 | var buffer: [256]u8 = undefined; |
| 664 | var fba = std.heap.FixedBufferAllocator.init(&buffer); | 664 | var fba = std.heap.FixedBufferAllocator.init(&buffer); |
| 665 | var allocator = fba.allocator(); | ||
| 665 | 666 | ||
| 666 | const commit_query = try std.fmt.allocPrint(&fba.allocator, "RELEASE SAVEPOINT {s}", .{name}); | 667 | const commit_query = try std.fmt.allocPrint(allocator, "RELEASE SAVEPOINT {s}", .{name}); |
| 667 | const rollback_query = try std.fmt.allocPrint(&fba.allocator, "ROLLBACK TRANSACTION TO SAVEPOINT {s}", .{name}); | 668 | const rollback_query = try std.fmt.allocPrint(allocator, "ROLLBACK TRANSACTION TO SAVEPOINT {s}", .{name}); |
| 668 | 669 | ||
| 669 | var res = Self{ | 670 | var res = Self{ |
| 670 | .db = db, | 671 | .db = db, |
| @@ -674,7 +675,7 @@ pub const Savepoint = struct { | |||
| 674 | }; | 675 | }; |
| 675 | 676 | ||
| 676 | try res.db.execDynamic( | 677 | try res.db.execDynamic( |
| 677 | try std.fmt.allocPrint(&fba.allocator, "SAVEPOINT {s}", .{name}), | 678 | try std.fmt.allocPrint(allocator, "SAVEPOINT {s}", .{name}), |
| 678 | .{}, | 679 | .{}, |
| 679 | .{}, | 680 | .{}, |
| 680 | ); | 681 | ); |
| @@ -802,7 +803,7 @@ pub fn Iterator(comptime Type: type) type { | |||
| 802 | } | 803 | } |
| 803 | 804 | ||
| 804 | // nextAlloc is like `next` but can allocate memory. | 805 | // nextAlloc is like `next` but can allocate memory. |
| 805 | pub fn nextAlloc(self: *Self, allocator: *mem.Allocator, options: QueryOptions) !?Type { | 806 | pub fn nextAlloc(self: *Self, allocator: mem.Allocator, options: QueryOptions) !?Type { |
| 806 | var dummy_diags = Diagnostics{}; | 807 | var dummy_diags = Diagnostics{}; |
| 807 | var diags = options.diags orelse &dummy_diags; | 808 | var diags = options.diags orelse &dummy_diags; |
| 808 | 809 | ||
| @@ -952,7 +953,7 @@ pub fn Iterator(comptime Type: type) type { | |||
| 952 | }; | 953 | }; |
| 953 | 954 | ||
| 954 | // dupeWithSentinel is like dupe/dupeZ but allows for any sentinel value. | 955 | // dupeWithSentinel is like dupe/dupeZ but allows for any sentinel value. |
| 955 | fn dupeWithSentinel(comptime SliceType: type, allocator: *mem.Allocator, data: []const u8) !SliceType { | 956 | fn dupeWithSentinel(comptime SliceType: type, allocator: mem.Allocator, data: []const u8) !SliceType { |
| 956 | switch (@typeInfo(SliceType)) { | 957 | switch (@typeInfo(SliceType)) { |
| 957 | .Pointer => |ptr_info| { | 958 | .Pointer => |ptr_info| { |
| 958 | if (ptr_info.sentinel) |sentinel| { | 959 | if (ptr_info.sentinel) |sentinel| { |
| @@ -979,7 +980,7 @@ pub fn Iterator(comptime Type: type) type { | |||
| 979 | // When using .Text you can only read into either []const u8, []u8 or Text. | 980 | // When using .Text you can only read into either []const u8, []u8 or Text. |
| 980 | // | 981 | // |
| 981 | // The options must contain an `allocator` field which will be used to create a copy of the data. | 982 | // The options must contain an `allocator` field which will be used to create a copy of the data. |
| 982 | fn readBytes(self: *Self, comptime BytesType: type, allocator: *mem.Allocator, _i: usize, comptime mode: ReadBytesMode) !BytesType { | 983 | fn readBytes(self: *Self, comptime BytesType: type, allocator: mem.Allocator, _i: usize, comptime mode: ReadBytesMode) !BytesType { |
| 983 | const i = @intCast(c_int, _i); | 984 | const i = @intCast(c_int, _i); |
| 984 | 985 | ||
| 985 | switch (mode) { | 986 | switch (mode) { |
| @@ -1498,7 +1499,7 @@ pub const DynamicStatement = struct { | |||
| 1498 | /// | 1499 | /// |
| 1499 | /// Possible errors: | 1500 | /// Possible errors: |
| 1500 | /// - SQLiteError.SQLiteNotFound if some fields not found | 1501 | /// - SQLiteError.SQLiteNotFound if some fields not found |
| 1501 | pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: *std.mem.Allocator, values: anytype) !Iterator(Type) { | 1502 | pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: mem.Allocator, values: anytype) !Iterator(Type) { |
| 1502 | try self.bind(.{ .allocator = allocator }, values); | 1503 | try self.bind(.{ .allocator = allocator }, values); |
| 1503 | 1504 | ||
| 1504 | var res: Iterator(Type) = undefined; | 1505 | var res: Iterator(Type) = undefined; |
| @@ -1541,7 +1542,7 @@ pub const DynamicStatement = struct { | |||
| 1541 | } | 1542 | } |
| 1542 | 1543 | ||
| 1543 | /// oneAlloc is like `one` but can allocate memory. | 1544 | /// oneAlloc is like `one` but can allocate memory. |
| 1544 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) !?Type { | 1545 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, values: anytype) !?Type { |
| 1545 | var iter = try self.iteratorAlloc(Type, allocator, values); | 1546 | var iter = try self.iteratorAlloc(Type, allocator, values); |
| 1546 | 1547 | ||
| 1547 | const row = (try iter.nextAlloc(allocator, options)) orelse return null; | 1548 | const row = (try iter.nextAlloc(allocator, options)) orelse return null; |
| @@ -1574,7 +1575,7 @@ pub const DynamicStatement = struct { | |||
| 1574 | /// in the input query string. | 1575 | /// in the input query string. |
| 1575 | /// | 1576 | /// |
| 1576 | /// Note that this allocates all rows into a single slice: if you read a lot of data this can use a lot of memory. | 1577 | /// Note that this allocates all rows into a single slice: if you read a lot of data this can use a lot of memory. |
| 1577 | pub fn all(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) ![]Type { | 1578 | pub fn all(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, values: anytype) ![]Type { |
| 1578 | var iter = try self.iterator(Type, values); | 1579 | var iter = try self.iterator(Type, values); |
| 1579 | 1580 | ||
| 1580 | var rows = std.ArrayList(Type).init(allocator); | 1581 | var rows = std.ArrayList(Type).init(allocator); |
| @@ -1700,7 +1701,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1700 | } | 1701 | } |
| 1701 | 1702 | ||
| 1702 | /// execAlloc is like `exec` but can allocate memory. | 1703 | /// execAlloc is like `exec` but can allocate memory. |
| 1703 | pub fn execAlloc(self: *Self, allocator: *std.mem.Allocator, options: QueryOptions, values: anytype) !void { | 1704 | pub fn execAlloc(self: *Self, allocator: mem.Allocator, options: QueryOptions, values: anytype) !void { |
| 1704 | try self.bind(.{ .allocator = allocator }, values); | 1705 | try self.bind(.{ .allocator = allocator }, values); |
| 1705 | 1706 | ||
| 1706 | var dummy_diags = Diagnostics{}; | 1707 | var dummy_diags = Diagnostics{}; |
| @@ -1768,7 +1769,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1768 | } | 1769 | } |
| 1769 | 1770 | ||
| 1770 | /// iteratorAlloc is like `iterator` but can allocate memory. | 1771 | /// iteratorAlloc is like `iterator` but can allocate memory. |
| 1771 | pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: *std.mem.Allocator, values: anytype) !Iterator(Type) { | 1772 | pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: mem.Allocator, values: anytype) !Iterator(Type) { |
| 1772 | try self.bind(.{ .allocator = allocator }, values); | 1773 | try self.bind(.{ .allocator = allocator }, values); |
| 1773 | 1774 | ||
| 1774 | var res: Iterator(Type) = undefined; | 1775 | var res: Iterator(Type) = undefined; |
| @@ -1811,7 +1812,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1811 | } | 1812 | } |
| 1812 | 1813 | ||
| 1813 | /// oneAlloc is like `one` but can allocate memory. | 1814 | /// oneAlloc is like `one` but can allocate memory. |
| 1814 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) !?Type { | 1815 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, values: anytype) !?Type { |
| 1815 | var iter = try self.iteratorAlloc(Type, allocator, values); | 1816 | var iter = try self.iteratorAlloc(Type, allocator, values); |
| 1816 | 1817 | ||
| 1817 | const row = (try iter.nextAlloc(allocator, options)) orelse return null; | 1818 | const row = (try iter.nextAlloc(allocator, options)) orelse return null; |
| @@ -1844,7 +1845,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1844 | /// in the input query string. | 1845 | /// in the input query string. |
| 1845 | /// | 1846 | /// |
| 1846 | /// Note that this allocates all rows into a single slice: if you read a lot of data this can use a lot of memory. | 1847 | /// Note that this allocates all rows into a single slice: if you read a lot of data this can use a lot of memory. |
| 1847 | pub fn all(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) ![]Type { | 1848 | pub fn all(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, values: anytype) ![]Type { |
| 1848 | var iter = try self.iteratorAlloc(Type, allocator, values); | 1849 | var iter = try self.iteratorAlloc(Type, allocator, values); |
| 1849 | 1850 | ||
| 1850 | var rows = std.ArrayList(Type).init(allocator); | 1851 | var rows = std.ArrayList(Type).init(allocator); |
| @@ -1935,6 +1936,7 @@ test "sqlite: db init" { | |||
| 1935 | test "sqlite: db pragma" { | 1936 | test "sqlite: db pragma" { |
| 1936 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 1937 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 1937 | defer arena.deinit(); | 1938 | defer arena.deinit(); |
| 1939 | var allocator = arena.allocator(); | ||
| 1938 | 1940 | ||
| 1939 | var db = try getTestDb(); | 1941 | var db = try getTestDb(); |
| 1940 | 1942 | ||
| @@ -1950,7 +1952,7 @@ test "sqlite: db pragma" { | |||
| 1950 | } | 1952 | } |
| 1951 | 1953 | ||
| 1952 | { | 1954 | { |
| 1953 | const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", "wal"); | 1955 | const journal_mode = try db.pragmaAlloc([]const u8, allocator, .{}, "journal_mode", "wal"); |
| 1954 | try testing.expect(journal_mode != null); | 1956 | try testing.expect(journal_mode != null); |
| 1955 | try testing.expectEqualStrings("memory", journal_mode.?); | 1957 | try testing.expectEqualStrings("memory", journal_mode.?); |
| 1956 | } | 1958 | } |
| @@ -1962,7 +1964,7 @@ test "sqlite: db pragma" { | |||
| 1962 | } | 1964 | } |
| 1963 | 1965 | ||
| 1964 | { | 1966 | { |
| 1965 | const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", "wal"); | 1967 | const journal_mode = try db.pragmaAlloc([]const u8, allocator, .{}, "journal_mode", "wal"); |
| 1966 | try testing.expect(journal_mode != null); | 1968 | try testing.expect(journal_mode != null); |
| 1967 | try testing.expectEqualStrings("wal", journal_mode.?); | 1969 | try testing.expectEqualStrings("wal", journal_mode.?); |
| 1968 | } | 1970 | } |
| @@ -2034,6 +2036,7 @@ test "sqlite: statement execDynamic" { | |||
| 2034 | test "sqlite: read a single user into a struct" { | 2036 | test "sqlite: read a single user into a struct" { |
| 2035 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2037 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2036 | defer arena.deinit(); | 2038 | defer arena.deinit(); |
| 2039 | var allocator = arena.allocator(); | ||
| 2037 | 2040 | ||
| 2038 | var db = try getTestDb(); | 2041 | var db = try getTestDb(); |
| 2039 | try addTestData(&db); | 2042 | try addTestData(&db); |
| @@ -2041,7 +2044,7 @@ test "sqlite: read a single user into a struct" { | |||
| 2041 | var stmt = try db.prepare("SELECT * FROM user WHERE id = ?{usize}"); | 2044 | var stmt = try db.prepare("SELECT * FROM user WHERE id = ?{usize}"); |
| 2042 | defer stmt.deinit(); | 2045 | defer stmt.deinit(); |
| 2043 | 2046 | ||
| 2044 | var rows = try stmt.all(TestUser, &arena.allocator, .{}, .{ | 2047 | var rows = try stmt.all(TestUser, allocator, .{}, .{ |
| 2045 | .id = @as(usize, 20), | 2048 | .id = @as(usize, 20), |
| 2046 | }); | 2049 | }); |
| 2047 | for (rows) |row| { | 2050 | for (rows) |row| { |
| @@ -2078,7 +2081,7 @@ test "sqlite: read a single user into a struct" { | |||
| 2078 | id: usize, | 2081 | id: usize, |
| 2079 | age: usize, | 2082 | age: usize, |
| 2080 | }, | 2083 | }, |
| 2081 | &arena.allocator, | 2084 | allocator, |
| 2082 | "SELECT name, id, age FROM user WHERE id = ?{usize}", | 2085 | "SELECT name, id, age FROM user WHERE id = ?{usize}", |
| 2083 | .{}, | 2086 | .{}, |
| 2084 | .{@as(usize, 20)}, | 2087 | .{@as(usize, 20)}, |
| @@ -2095,6 +2098,7 @@ test "sqlite: read a single user into a struct" { | |||
| 2095 | test "sqlite: read all users into a struct" { | 2098 | test "sqlite: read all users into a struct" { |
| 2096 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2099 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2097 | defer arena.deinit(); | 2100 | defer arena.deinit(); |
| 2101 | var allocator = arena.allocator(); | ||
| 2098 | 2102 | ||
| 2099 | var db = try getTestDb(); | 2103 | var db = try getTestDb(); |
| 2100 | try addTestData(&db); | 2104 | try addTestData(&db); |
| @@ -2102,7 +2106,7 @@ test "sqlite: read all users into a struct" { | |||
| 2102 | var stmt = try db.prepare("SELECT * FROM user"); | 2106 | var stmt = try db.prepare("SELECT * FROM user"); |
| 2103 | defer stmt.deinit(); | 2107 | defer stmt.deinit(); |
| 2104 | 2108 | ||
| 2105 | var rows = try stmt.all(TestUser, &arena.allocator, .{}, .{}); | 2109 | var rows = try stmt.all(TestUser, allocator, .{}, .{}); |
| 2106 | try testing.expectEqual(@as(usize, 3), rows.len); | 2110 | try testing.expectEqual(@as(usize, 3), rows.len); |
| 2107 | for (rows) |row, i| { | 2111 | for (rows) |row, i| { |
| 2108 | const exp = test_users[i]; | 2112 | const exp = test_users[i]; |
| @@ -2116,6 +2120,7 @@ test "sqlite: read all users into a struct" { | |||
| 2116 | test "sqlite: read in an anonymous struct" { | 2120 | test "sqlite: read in an anonymous struct" { |
| 2117 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2121 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2118 | defer arena.deinit(); | 2122 | defer arena.deinit(); |
| 2123 | var allocator = arena.allocator(); | ||
| 2119 | 2124 | ||
| 2120 | var db = try getTestDb(); | 2125 | var db = try getTestDb(); |
| 2121 | try addTestData(&db); | 2126 | try addTestData(&db); |
| @@ -2132,7 +2137,7 @@ test "sqlite: read in an anonymous struct" { | |||
| 2132 | is_id: bool, | 2137 | is_id: bool, |
| 2133 | weight: f64, | 2138 | weight: f64, |
| 2134 | }, | 2139 | }, |
| 2135 | &arena.allocator, | 2140 | allocator, |
| 2136 | .{}, | 2141 | .{}, |
| 2137 | .{ .id = @as(usize, 20) }, | 2142 | .{ .id = @as(usize, 20) }, |
| 2138 | ); | 2143 | ); |
| @@ -2150,6 +2155,7 @@ test "sqlite: read in an anonymous struct" { | |||
| 2150 | test "sqlite: read in a Text struct" { | 2155 | test "sqlite: read in a Text struct" { |
| 2151 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2156 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2152 | defer arena.deinit(); | 2157 | defer arena.deinit(); |
| 2158 | var allocator = arena.allocator(); | ||
| 2153 | 2159 | ||
| 2154 | var db = try getTestDb(); | 2160 | var db = try getTestDb(); |
| 2155 | try addTestData(&db); | 2161 | try addTestData(&db); |
| @@ -2163,7 +2169,7 @@ test "sqlite: read in a Text struct" { | |||
| 2163 | id: usize, | 2169 | id: usize, |
| 2164 | age: usize, | 2170 | age: usize, |
| 2165 | }, | 2171 | }, |
| 2166 | &arena.allocator, | 2172 | allocator, |
| 2167 | .{}, | 2173 | .{}, |
| 2168 | .{@as(usize, 20)}, | 2174 | .{@as(usize, 20)}, |
| 2169 | ); | 2175 | ); |
| @@ -2178,6 +2184,7 @@ test "sqlite: read in a Text struct" { | |||
| 2178 | test "sqlite: read a single text value" { | 2184 | test "sqlite: read a single text value" { |
| 2179 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2185 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2180 | defer arena.deinit(); | 2186 | defer arena.deinit(); |
| 2187 | var allocator = arena.allocator(); | ||
| 2181 | 2188 | ||
| 2182 | var db = try getTestDb(); | 2189 | var db = try getTestDb(); |
| 2183 | try addTestData(&db); | 2190 | try addTestData(&db); |
| @@ -2204,7 +2211,7 @@ test "sqlite: read a single text value" { | |||
| 2204 | var stmt: StatementType(.{}, query) = try db.prepare(query); | 2211 | var stmt: StatementType(.{}, query) = try db.prepare(query); |
| 2205 | defer stmt.deinit(); | 2212 | defer stmt.deinit(); |
| 2206 | 2213 | ||
| 2207 | const name = try stmt.oneAlloc(typ, &arena.allocator, .{}, .{ | 2214 | const name = try stmt.oneAlloc(typ, allocator, .{}, .{ |
| 2208 | .id = @as(usize, 20), | 2215 | .id = @as(usize, 20), |
| 2209 | }); | 2216 | }); |
| 2210 | try testing.expect(name != null); | 2217 | try testing.expect(name != null); |
| @@ -2263,6 +2270,7 @@ test "sqlite: read a single integer value" { | |||
| 2263 | test "sqlite: read a single value into an enum backed by an integer" { | 2270 | test "sqlite: read a single value into an enum backed by an integer" { |
| 2264 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2271 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2265 | defer arena.deinit(); | 2272 | defer arena.deinit(); |
| 2273 | var allocator = arena.allocator(); | ||
| 2266 | 2274 | ||
| 2267 | var db = try getTestDb(); | 2275 | var db = try getTestDb(); |
| 2268 | try createTestTables(&db); | 2276 | try createTestTables(&db); |
| @@ -2297,7 +2305,7 @@ test "sqlite: read a single value into an enum backed by an integer" { | |||
| 2297 | var stmt: StatementType(.{}, query) = try db.prepare(query); | 2305 | var stmt: StatementType(.{}, query) = try db.prepare(query); |
| 2298 | defer stmt.deinit(); | 2306 | defer stmt.deinit(); |
| 2299 | 2307 | ||
| 2300 | const b = try stmt.oneAlloc(IntColor, &arena.allocator, .{}, .{ | 2308 | const b = try stmt.oneAlloc(IntColor, allocator, .{}, .{ |
| 2301 | .id = @as(usize, 10), | 2309 | .id = @as(usize, 10), |
| 2302 | }); | 2310 | }); |
| 2303 | try testing.expect(b != null); | 2311 | try testing.expect(b != null); |
| @@ -2308,6 +2316,7 @@ test "sqlite: read a single value into an enum backed by an integer" { | |||
| 2308 | test "sqlite: read a single value into an enum backed by a string" { | 2316 | test "sqlite: read a single value into an enum backed by a string" { |
| 2309 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2317 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2310 | defer arena.deinit(); | 2318 | defer arena.deinit(); |
| 2319 | var allocator = arena.allocator(); | ||
| 2311 | 2320 | ||
| 2312 | var db = try getTestDb(); | 2321 | var db = try getTestDb(); |
| 2313 | try createTestTables(&db); | 2322 | try createTestTables(&db); |
| @@ -2322,7 +2331,7 @@ test "sqlite: read a single value into an enum backed by a string" { | |||
| 2322 | var stmt: StatementType(.{}, query) = try db.prepare(query); | 2331 | var stmt: StatementType(.{}, query) = try db.prepare(query); |
| 2323 | defer stmt.deinit(); | 2332 | defer stmt.deinit(); |
| 2324 | 2333 | ||
| 2325 | const b = try stmt.oneAlloc(TestUser.Color, &arena.allocator, .{}, .{ | 2334 | const b = try stmt.oneAlloc(TestUser.Color, allocator, .{}, .{ |
| 2326 | .id = @as(usize, 10), | 2335 | .id = @as(usize, 10), |
| 2327 | }); | 2336 | }); |
| 2328 | try testing.expect(b != null); | 2337 | try testing.expect(b != null); |
| @@ -2403,6 +2412,7 @@ test "sqlite: bind string literal" { | |||
| 2403 | test "sqlite: bind pointer" { | 2412 | test "sqlite: bind pointer" { |
| 2404 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2413 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2405 | defer arena.deinit(); | 2414 | defer arena.deinit(); |
| 2415 | var allocator = arena.allocator(); | ||
| 2406 | 2416 | ||
| 2407 | var db = try getTestDb(); | 2417 | var db = try getTestDb(); |
| 2408 | try addTestData(&db); | 2418 | try addTestData(&db); |
| @@ -2415,7 +2425,7 @@ test "sqlite: bind pointer" { | |||
| 2415 | for (test_users) |test_user, i| { | 2425 | for (test_users) |test_user, i| { |
| 2416 | stmt.reset(); | 2426 | stmt.reset(); |
| 2417 | 2427 | ||
| 2418 | const name = try stmt.oneAlloc([]const u8, &arena.allocator, .{}, .{&test_user.id}); | 2428 | const name = try stmt.oneAlloc([]const u8, allocator, .{}, .{&test_user.id}); |
| 2419 | try testing.expect(name != null); | 2429 | try testing.expect(name != null); |
| 2420 | try testing.expectEqualStrings(test_users[i].name, name.?); | 2430 | try testing.expectEqualStrings(test_users[i].name, name.?); |
| 2421 | } | 2431 | } |
| @@ -2424,6 +2434,7 @@ test "sqlite: bind pointer" { | |||
| 2424 | test "sqlite: read pointers" { | 2434 | test "sqlite: read pointers" { |
| 2425 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2435 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2426 | defer arena.deinit(); | 2436 | defer arena.deinit(); |
| 2437 | var allocator = arena.allocator(); | ||
| 2427 | 2438 | ||
| 2428 | var db = try getTestDb(); | 2439 | var db = try getTestDb(); |
| 2429 | try addTestData(&db); | 2440 | try addTestData(&db); |
| @@ -2440,7 +2451,7 @@ test "sqlite: read pointers" { | |||
| 2440 | age: *u32, | 2451 | age: *u32, |
| 2441 | weight: *f32, | 2452 | weight: *f32, |
| 2442 | }, | 2453 | }, |
| 2443 | &arena.allocator, | 2454 | allocator, |
| 2444 | .{}, | 2455 | .{}, |
| 2445 | .{}, | 2456 | .{}, |
| 2446 | ); | 2457 | ); |
| @@ -2510,7 +2521,7 @@ test "sqlite: statement reset" { | |||
| 2510 | test "sqlite: statement iterator" { | 2521 | test "sqlite: statement iterator" { |
| 2511 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2522 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2512 | defer arena.deinit(); | 2523 | defer arena.deinit(); |
| 2513 | var allocator = &arena.allocator; | 2524 | var allocator = arena.allocator(); |
| 2514 | 2525 | ||
| 2515 | var db = try getTestDb(); | 2526 | var db = try getTestDb(); |
| 2516 | try addTestData(&db); | 2527 | try addTestData(&db); |
| @@ -2595,7 +2606,7 @@ test "sqlite: statement iterator" { | |||
| 2595 | test "sqlite: blob open, reopen" { | 2606 | test "sqlite: blob open, reopen" { |
| 2596 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2607 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2597 | defer arena.deinit(); | 2608 | defer arena.deinit(); |
| 2598 | var allocator = &arena.allocator; | 2609 | var allocator = arena.allocator(); |
| 2599 | 2610 | ||
| 2600 | var db = try getTestDb(); | 2611 | var db = try getTestDb(); |
| 2601 | defer db.deinit(); | 2612 | defer db.deinit(); |
| @@ -2747,6 +2758,7 @@ test "sqlite: exec with diags, failing statement" { | |||
| 2747 | test "sqlite: savepoint with no failures" { | 2758 | test "sqlite: savepoint with no failures" { |
| 2748 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2759 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2749 | defer arena.deinit(); | 2760 | defer arena.deinit(); |
| 2761 | var allocator = arena.allocator(); | ||
| 2750 | 2762 | ||
| 2751 | var db = try getTestDb(); | 2763 | var db = try getTestDb(); |
| 2752 | try addTestData(&db); | 2764 | try addTestData(&db); |
| @@ -2779,7 +2791,7 @@ test "sqlite: savepoint with no failures" { | |||
| 2779 | data: []const u8, | 2791 | data: []const u8, |
| 2780 | author_id: usize, | 2792 | author_id: usize, |
| 2781 | }, | 2793 | }, |
| 2782 | &arena.allocator, | 2794 | allocator, |
| 2783 | .{}, | 2795 | .{}, |
| 2784 | .{}, | 2796 | .{}, |
| 2785 | ); | 2797 | ); |
| @@ -2794,6 +2806,7 @@ test "sqlite: savepoint with no failures" { | |||
| 2794 | test "sqlite: two nested savepoints with inner failure" { | 2806 | test "sqlite: two nested savepoints with inner failure" { |
| 2795 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2807 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2796 | defer arena.deinit(); | 2808 | defer arena.deinit(); |
| 2809 | var allocator = arena.allocator(); | ||
| 2797 | 2810 | ||
| 2798 | var db = try getTestDb(); | 2811 | var db = try getTestDb(); |
| 2799 | try addTestData(&db); | 2812 | try addTestData(&db); |
| @@ -2831,7 +2844,7 @@ test "sqlite: two nested savepoints with inner failure" { | |||
| 2831 | data: []const u8, | 2844 | data: []const u8, |
| 2832 | author_id: usize, | 2845 | author_id: usize, |
| 2833 | }, | 2846 | }, |
| 2834 | &arena.allocator, | 2847 | allocator, |
| 2835 | .{}, | 2848 | .{}, |
| 2836 | .{}, | 2849 | .{}, |
| 2837 | ); | 2850 | ); |
| @@ -2843,6 +2856,7 @@ test "sqlite: two nested savepoints with inner failure" { | |||
| 2843 | test "sqlite: two nested savepoints with outer failure" { | 2856 | test "sqlite: two nested savepoints with outer failure" { |
| 2844 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2857 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2845 | defer arena.deinit(); | 2858 | defer arena.deinit(); |
| 2859 | var allocator = arena.allocator(); | ||
| 2846 | 2860 | ||
| 2847 | var db = try getTestDb(); | 2861 | var db = try getTestDb(); |
| 2848 | try addTestData(&db); | 2862 | try addTestData(&db); |
| @@ -2869,7 +2883,7 @@ test "sqlite: two nested savepoints with outer failure" { | |||
| 2869 | var stmt = try db.prepare("SELECT 1 FROM article"); | 2883 | var stmt = try db.prepare("SELECT 1 FROM article"); |
| 2870 | defer stmt.deinit(); | 2884 | defer stmt.deinit(); |
| 2871 | 2885 | ||
| 2872 | var rows = try stmt.all(usize, &arena.allocator, .{}, .{}); | 2886 | var rows = try stmt.all(usize, allocator, .{}, .{}); |
| 2873 | try testing.expectEqual(@as(usize, 0), rows.len); | 2887 | try testing.expectEqual(@as(usize, 0), rows.len); |
| 2874 | } | 2888 | } |
| 2875 | 2889 | ||
| @@ -2877,7 +2891,7 @@ fn getTestDb() !Db { | |||
| 2877 | var buf: [1024]u8 = undefined; | 2891 | var buf: [1024]u8 = undefined; |
| 2878 | var fba = std.heap.FixedBufferAllocator.init(&buf); | 2892 | var fba = std.heap.FixedBufferAllocator.init(&buf); |
| 2879 | 2893 | ||
| 2880 | var mode = dbMode(&fba.allocator); | 2894 | var mode = dbMode(fba.allocator()); |
| 2881 | 2895 | ||
| 2882 | return try Db.init(.{ | 2896 | return try Db.init(.{ |
| 2883 | .open_flags = .{ | 2897 | .open_flags = .{ |
| @@ -2888,7 +2902,7 @@ fn getTestDb() !Db { | |||
| 2888 | }); | 2902 | }); |
| 2889 | } | 2903 | } |
| 2890 | 2904 | ||
| 2891 | fn tmpDbPath(allocator: *mem.Allocator) ![:0]const u8 { | 2905 | fn tmpDbPath(allocator: mem.Allocator) ![:0]const u8 { |
| 2892 | const tmp_dir = testing.tmpDir(.{}); | 2906 | const tmp_dir = testing.tmpDir(.{}); |
| 2893 | 2907 | ||
| 2894 | const path = try std.fs.path.join(allocator, &[_][]const u8{ | 2908 | const path = try std.fs.path.join(allocator, &[_][]const u8{ |
| @@ -2902,7 +2916,7 @@ fn tmpDbPath(allocator: *mem.Allocator) ![:0]const u8 { | |||
| 2902 | return allocator.dupeZ(u8, path); | 2916 | return allocator.dupeZ(u8, path); |
| 2903 | } | 2917 | } |
| 2904 | 2918 | ||
| 2905 | fn dbMode(allocator: *mem.Allocator) Db.Mode { | 2919 | fn dbMode(allocator: mem.Allocator) Db.Mode { |
| 2906 | return if (build_options.in_memory) blk: { | 2920 | return if (build_options.in_memory) blk: { |
| 2907 | break :blk .{ .Memory = {} }; | 2921 | break :blk .{ .Memory = {} }; |
| 2908 | } else blk: { | 2922 | } else blk: { |
| @@ -2922,11 +2936,11 @@ const MyData = struct { | |||
| 2922 | 2936 | ||
| 2923 | const BaseType = []const u8; | 2937 | const BaseType = []const u8; |
| 2924 | 2938 | ||
| 2925 | pub fn bindField(self: MyData, allocator: *std.mem.Allocator) !BaseType { | 2939 | pub fn bindField(self: MyData, allocator: mem.Allocator) !BaseType { |
| 2926 | return try std.fmt.allocPrint(allocator, "{}", .{std.fmt.fmtSliceHexLower(&self.data)}); | 2940 | return try std.fmt.allocPrint(allocator, "{}", .{std.fmt.fmtSliceHexLower(&self.data)}); |
| 2927 | } | 2941 | } |
| 2928 | 2942 | ||
| 2929 | pub fn readField(alloc: *std.mem.Allocator, value: BaseType) !MyData { | 2943 | pub fn readField(alloc: mem.Allocator, value: BaseType) !MyData { |
| 2930 | _ = alloc; | 2944 | _ = alloc; |
| 2931 | 2945 | ||
| 2932 | var result = [_]u8{0} ** 16; | 2946 | var result = [_]u8{0} ** 16; |
| @@ -2942,6 +2956,7 @@ const MyData = struct { | |||
| 2942 | test "sqlite: bind custom type" { | 2956 | test "sqlite: bind custom type" { |
| 2943 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2957 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2944 | defer arena.deinit(); | 2958 | defer arena.deinit(); |
| 2959 | var allocator = arena.allocator(); | ||
| 2945 | 2960 | ||
| 2946 | var db = try getTestDb(); | 2961 | var db = try getTestDb(); |
| 2947 | try addTestData(&db); | 2962 | try addTestData(&db); |
| @@ -2953,7 +2968,7 @@ test "sqlite: bind custom type" { | |||
| 2953 | { | 2968 | { |
| 2954 | // insertion | 2969 | // insertion |
| 2955 | var stmt = try db.prepare("INSERT INTO article(data) VALUES(?)"); | 2970 | var stmt = try db.prepare("INSERT INTO article(data) VALUES(?)"); |
| 2956 | try stmt.execAlloc(&arena.allocator, .{}, .{my_data}); | 2971 | try stmt.execAlloc(allocator, .{}, .{my_data}); |
| 2957 | } | 2972 | } |
| 2958 | { | 2973 | { |
| 2959 | // reading back | 2974 | // reading back |
| @@ -2967,7 +2982,7 @@ test "sqlite: bind custom type" { | |||
| 2967 | is_published: bool, | 2982 | is_published: bool, |
| 2968 | }; | 2983 | }; |
| 2969 | 2984 | ||
| 2970 | const row = try stmt.oneAlloc(Article, &arena.allocator, .{}, .{}); | 2985 | const row = try stmt.oneAlloc(Article, allocator, .{}, .{}); |
| 2971 | 2986 | ||
| 2972 | try testing.expect(row != null); | 2987 | try testing.expect(row != null); |
| 2973 | try testing.expectEqualSlices(u8, &my_data.data, &row.?.data.data); | 2988 | try testing.expectEqualSlices(u8, &my_data.data, &row.?.data.data); |
| @@ -2977,6 +2992,7 @@ test "sqlite: bind custom type" { | |||
| 2977 | test "sqlite: prepareDynamic" { | 2992 | test "sqlite: prepareDynamic" { |
| 2978 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 2993 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 2979 | defer arena.deinit(); | 2994 | defer arena.deinit(); |
| 2995 | var allocator = arena.allocator(); | ||
| 2980 | 2996 | ||
| 2981 | var db = try getTestDb(); | 2997 | var db = try getTestDb(); |
| 2982 | try addTestData(&db); | 2998 | try addTestData(&db); |
| @@ -2996,7 +3012,7 @@ test "sqlite: prepareDynamic" { | |||
| 2996 | stmt.reset(); | 3012 | stmt.reset(); |
| 2997 | 3013 | ||
| 2998 | { | 3014 | { |
| 2999 | var iter = try stmt.iteratorAlloc(usize, &arena.allocator, .{ .age = 33 }); | 3015 | var iter = try stmt.iteratorAlloc(usize, allocator, .{ .age = 33 }); |
| 3000 | 3016 | ||
| 3001 | const id = try iter.next(.{}); | 3017 | const id = try iter.next(.{}); |
| 3002 | try testing.expect(id != null); | 3018 | try testing.expect(id != null); |
| @@ -3006,7 +3022,7 @@ test "sqlite: prepareDynamic" { | |||
| 3006 | stmt.reset(); | 3022 | stmt.reset(); |
| 3007 | 3023 | ||
| 3008 | { | 3024 | { |
| 3009 | var iter = try stmt.iteratorAlloc(usize, &arena.allocator, .{33}); | 3025 | var iter = try stmt.iteratorAlloc(usize, allocator, .{33}); |
| 3010 | 3026 | ||
| 3011 | const id = try iter.next(.{}); | 3027 | const id = try iter.next(.{}); |
| 3012 | try testing.expect(id != null); | 3028 | try testing.expect(id != null); |
| @@ -3017,6 +3033,7 @@ test "sqlite: prepareDynamic" { | |||
| 3017 | test "sqlite: oneDynamic" { | 3033 | test "sqlite: oneDynamic" { |
| 3018 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 3034 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 3019 | defer arena.deinit(); | 3035 | defer arena.deinit(); |
| 3036 | var allocator = arena.allocator(); | ||
| 3020 | 3037 | ||
| 3021 | var db = try getTestDb(); | 3038 | var db = try getTestDb(); |
| 3022 | try addTestData(&db); | 3039 | try addTestData(&db); |
| @@ -3050,7 +3067,7 @@ test "sqlite: oneDynamic" { | |||
| 3050 | { | 3067 | { |
| 3051 | const id = try db.oneDynamicAlloc( | 3068 | const id = try db.oneDynamicAlloc( |
| 3052 | usize, | 3069 | usize, |
| 3053 | &arena.allocator, | 3070 | allocator, |
| 3054 | "SELECT id FROM user WHERE age = ?", | 3071 | "SELECT id FROM user WHERE age = ?", |
| 3055 | .{ .diags = &diags }, | 3072 | .{ .diags = &diags }, |
| 3056 | .{ .age = 33 }, | 3073 | .{ .age = 33 }, |
| @@ -3062,7 +3079,7 @@ test "sqlite: oneDynamic" { | |||
| 3062 | { | 3079 | { |
| 3063 | const id = try db.oneDynamicAlloc( | 3080 | const id = try db.oneDynamicAlloc( |
| 3064 | usize, | 3081 | usize, |
| 3065 | &arena.allocator, | 3082 | allocator, |
| 3066 | "SELECT id FROM user WHERE age = ?", | 3083 | "SELECT id FROM user WHERE age = ?", |
| 3067 | .{ .diags = &diags }, | 3084 | .{ .diags = &diags }, |
| 3068 | .{33}, | 3085 | .{33}, |