summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--query.zig8
-rw-r--r--sqlite.zig154
2 files changed, 81 insertions, 81 deletions
diff --git a/query.zig b/query.zig
index 2605498..8bd5025 100644
--- a/query.zig
+++ b/query.zig
@@ -169,7 +169,7 @@ test "parsed query: query" {
169 169
170 inline for (testCases) |tc| { 170 inline for (testCases) |tc| {
171 comptime var parsed_query = ParsedQuery.from(tc.query); 171 comptime var parsed_query = ParsedQuery.from(tc.query);
172 testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery()); 172 try testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery());
173 } 173 }
174} 174}
175 175
@@ -201,12 +201,12 @@ test "parsed query: bind markers types" {
201 inline for (testCases) |tc| { 201 inline for (testCases) |tc| {
202 comptime var parsed_query = ParsedQuery.from(tc.query); 202 comptime var parsed_query = ParsedQuery.from(tc.query);
203 203
204 testing.expectEqual(1, parsed_query.nb_bind_markers); 204 try testing.expectEqual(1, parsed_query.nb_bind_markers);
205 205
206 const bind_marker = parsed_query.bind_markers[0]; 206 const bind_marker = parsed_query.bind_markers[0];
207 switch (tc.expected_marker) { 207 switch (tc.expected_marker) {
208 .Typed => |typ| testing.expectEqual(typ, bind_marker.Typed), 208 .Typed => |typ| try testing.expectEqual(typ, bind_marker.Typed),
209 .Untyped => |typ| testing.expectEqual(typ, bind_marker.Untyped), 209 .Untyped => |typ| try testing.expectEqual(typ, bind_marker.Untyped),
210 } 210 }
211 } 211 }
212} 212}
diff --git a/sqlite.zig b/sqlite.zig
index 1c22c83..79286e3 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -1233,7 +1233,7 @@ fn addTestData(db: *Db) !void {
1233 try db.exec("INSERT INTO user(name, id, age, weight) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32})", user); 1233 try db.exec("INSERT INTO user(name, id, age, weight) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32})", user);
1234 1234
1235 const rows_inserted = db.rowsAffected(); 1235 const rows_inserted = db.rowsAffected();
1236 testing.expectEqual(@as(usize, 1), rows_inserted); 1236 try testing.expectEqual(@as(usize, 1), rows_inserted);
1237 } 1237 }
1238} 1238}
1239 1239
@@ -1248,32 +1248,32 @@ test "sqlite: db pragma" {
1248 var db = try getTestDb(); 1248 var db = try getTestDb();
1249 1249
1250 const foreign_keys = try db.pragma(usize, .{}, "foreign_keys", null); 1250 const foreign_keys = try db.pragma(usize, .{}, "foreign_keys", null);
1251 testing.expect(foreign_keys != null); 1251 try testing.expect(foreign_keys != null);
1252 testing.expectEqual(@as(usize, 0), foreign_keys.?); 1252 try testing.expectEqual(@as(usize, 0), foreign_keys.?);
1253 1253
1254 if (build_options.in_memory) { 1254 if (build_options.in_memory) {
1255 { 1255 {
1256 const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", "wal"); 1256 const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", "wal");
1257 testing.expect(journal_mode != null); 1257 try testing.expect(journal_mode != null);
1258 testing.expectEqualStrings("memory", mem.spanZ(&journal_mode.?)); 1258 try testing.expectEqualStrings("memory", mem.spanZ(&journal_mode.?));
1259 } 1259 }
1260 1260
1261 { 1261 {
1262 const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", "wal"); 1262 const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", "wal");
1263 testing.expect(journal_mode != null); 1263 try testing.expect(journal_mode != null);
1264 testing.expectEqualStrings("memory", journal_mode.?); 1264 try testing.expectEqualStrings("memory", journal_mode.?);
1265 } 1265 }
1266 } else { 1266 } else {
1267 { 1267 {
1268 const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", "wal"); 1268 const journal_mode = try db.pragma([128:0]u8, .{}, "journal_mode", "wal");
1269 testing.expect(journal_mode != null); 1269 try testing.expect(journal_mode != null);
1270 testing.expectEqualStrings("wal", mem.spanZ(&journal_mode.?)); 1270 try testing.expectEqualStrings("wal", mem.spanZ(&journal_mode.?));
1271 } 1271 }
1272 1272
1273 { 1273 {
1274 const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", "wal"); 1274 const journal_mode = try db.pragmaAlloc([]const u8, &arena.allocator, .{}, "journal_mode", "wal");
1275 testing.expect(journal_mode != null); 1275 try testing.expect(journal_mode != null);
1276 testing.expectEqualStrings("wal", journal_mode.?); 1276 try testing.expectEqualStrings("wal", journal_mode.?);
1277 } 1277 }
1278 } 1278 }
1279} 1279}
@@ -1288,7 +1288,7 @@ test "sqlite: last insert row id" {
1288 }); 1288 });
1289 1289
1290 const id = db.getLastInsertRowID(); 1290 const id = db.getLastInsertRowID();
1291 testing.expectEqual(@as(i64, 1), id); 1291 try testing.expectEqual(@as(i64, 1), id);
1292} 1292}
1293 1293
1294test "sqlite: statement exec" { 1294test "sqlite: statement exec" {
@@ -1328,9 +1328,9 @@ test "sqlite: read a single user into a struct" {
1328 .id = @as(usize, 20), 1328 .id = @as(usize, 20),
1329 }); 1329 });
1330 for (rows) |row| { 1330 for (rows) |row| {
1331 testing.expectEqual(test_users[0].id, row.id); 1331 try testing.expectEqual(test_users[0].id, row.id);
1332 testing.expectEqualStrings(test_users[0].name, row.name); 1332 try testing.expectEqualStrings(test_users[0].name, row.name);
1333 testing.expectEqual(test_users[0].age, row.age); 1333 try testing.expectEqual(test_users[0].age, row.age);
1334 } 1334 }
1335 1335
1336 // Read a row with db.one() 1336 // Read a row with db.one()
@@ -1345,12 +1345,12 @@ test "sqlite: read a single user into a struct" {
1345 .{}, 1345 .{},
1346 .{@as(usize, 20)}, 1346 .{@as(usize, 20)},
1347 ); 1347 );
1348 testing.expect(row != null); 1348 try testing.expect(row != null);
1349 1349
1350 const exp = test_users[0]; 1350 const exp = test_users[0];
1351 testing.expectEqual(exp.id, row.?.id); 1351 try testing.expectEqual(exp.id, row.?.id);
1352 testing.expectEqualStrings(exp.name, mem.spanZ(&row.?.name)); 1352 try testing.expectEqualStrings(exp.name, mem.spanZ(&row.?.name));
1353 testing.expectEqual(exp.age, row.?.age); 1353 try testing.expectEqual(exp.age, row.?.age);
1354 } 1354 }
1355 1355
1356 // Read a row with db.oneAlloc() 1356 // Read a row with db.oneAlloc()
@@ -1366,12 +1366,12 @@ test "sqlite: read a single user into a struct" {
1366 .{}, 1366 .{},
1367 .{@as(usize, 20)}, 1367 .{@as(usize, 20)},
1368 ); 1368 );
1369 testing.expect(row != null); 1369 try testing.expect(row != null);
1370 1370
1371 const exp = test_users[0]; 1371 const exp = test_users[0];
1372 testing.expectEqual(exp.id, row.?.id); 1372 try testing.expectEqual(exp.id, row.?.id);
1373 testing.expectEqualStrings(exp.name, row.?.name.data); 1373 try testing.expectEqualStrings(exp.name, row.?.name.data);
1374 testing.expectEqual(exp.age, row.?.age); 1374 try testing.expectEqual(exp.age, row.?.age);
1375 } 1375 }
1376} 1376}
1377 1377
@@ -1386,13 +1386,13 @@ test "sqlite: read all users into a struct" {
1386 defer stmt.deinit(); 1386 defer stmt.deinit();
1387 1387
1388 var rows = try stmt.all(TestUser, &arena.allocator, .{}, .{}); 1388 var rows = try stmt.all(TestUser, &arena.allocator, .{}, .{});
1389 testing.expectEqual(@as(usize, 3), rows.len); 1389 try testing.expectEqual(@as(usize, 3), rows.len);
1390 for (rows) |row, i| { 1390 for (rows) |row, i| {
1391 const exp = test_users[i]; 1391 const exp = test_users[i];
1392 testing.expectEqual(exp.id, row.id); 1392 try testing.expectEqual(exp.id, row.id);
1393 testing.expectEqualStrings(exp.name, row.name); 1393 try testing.expectEqualStrings(exp.name, row.name);
1394 testing.expectEqual(exp.age, row.age); 1394 try testing.expectEqual(exp.age, row.age);
1395 testing.expectEqual(exp.weight, row.weight); 1395 try testing.expectEqual(exp.weight, row.weight);
1396 } 1396 }
1397} 1397}
1398 1398
@@ -1419,15 +1419,15 @@ test "sqlite: read in an anonymous struct" {
1419 .{}, 1419 .{},
1420 .{ .id = @as(usize, 20) }, 1420 .{ .id = @as(usize, 20) },
1421 ); 1421 );
1422 testing.expect(row != null); 1422 try testing.expect(row != null);
1423 1423
1424 const exp = test_users[0]; 1424 const exp = test_users[0];
1425 testing.expectEqual(exp.id, row.?.id); 1425 try testing.expectEqual(exp.id, row.?.id);
1426 testing.expectEqualStrings(exp.name, row.?.name); 1426 try testing.expectEqualStrings(exp.name, row.?.name);
1427 testing.expectEqualStrings(exp.name, mem.spanZ(&row.?.name_2)); 1427 try testing.expectEqualStrings(exp.name, mem.spanZ(&row.?.name_2));
1428 testing.expectEqual(exp.age, row.?.age); 1428 try testing.expectEqual(exp.age, row.?.age);
1429 testing.expect(row.?.is_id); 1429 try testing.expect(row.?.is_id);
1430 testing.expectEqual(exp.weight, @floatCast(f32, row.?.weight)); 1430 try testing.expectEqual(exp.weight, @floatCast(f32, row.?.weight));
1431} 1431}
1432 1432
1433test "sqlite: read in a Text struct" { 1433test "sqlite: read in a Text struct" {
@@ -1450,12 +1450,12 @@ test "sqlite: read in a Text struct" {
1450 .{}, 1450 .{},
1451 .{@as(usize, 20)}, 1451 .{@as(usize, 20)},
1452 ); 1452 );
1453 testing.expect(row != null); 1453 try testing.expect(row != null);
1454 1454
1455 const exp = test_users[0]; 1455 const exp = test_users[0];
1456 testing.expectEqual(exp.id, row.?.id); 1456 try testing.expectEqual(exp.id, row.?.id);
1457 testing.expectEqualStrings(exp.name, row.?.name.data); 1457 try testing.expectEqualStrings(exp.name, row.?.name.data);
1458 testing.expectEqual(exp.age, row.?.age); 1458 try testing.expectEqual(exp.age, row.?.age);
1459} 1459}
1460 1460
1461test "sqlite: read a single text value" { 1461test "sqlite: read a single text value" {
@@ -1490,10 +1490,10 @@ test "sqlite: read a single text value" {
1490 const name = try stmt.oneAlloc(typ, &arena.allocator, .{}, .{ 1490 const name = try stmt.oneAlloc(typ, &arena.allocator, .{}, .{
1491 .id = @as(usize, 20), 1491 .id = @as(usize, 20),
1492 }); 1492 });
1493 testing.expect(name != null); 1493 try testing.expect(name != null);
1494 switch (typ) { 1494 switch (typ) {
1495 Text, Blob => { 1495 Text, Blob => {
1496 testing.expectEqualStrings("Vincent", name.?.data); 1496 try testing.expectEqualStrings("Vincent", name.?.data);
1497 }, 1497 },
1498 else => { 1498 else => {
1499 const span = blk: { 1499 const span = blk: {
@@ -1505,7 +1505,7 @@ test "sqlite: read a single text value" {
1505 }; 1505 };
1506 }; 1506 };
1507 1507
1508 testing.expectEqualStrings("Vincent", span); 1508 try testing.expectEqualStrings("Vincent", span);
1509 }, 1509 },
1510 } 1510 }
1511 } 1511 }
@@ -1538,9 +1538,9 @@ test "sqlite: read a single integer value" {
1538 var age = try stmt.one(typ, .{}, .{ 1538 var age = try stmt.one(typ, .{}, .{
1539 .id = @as(usize, 20), 1539 .id = @as(usize, 20),
1540 }); 1540 });
1541 testing.expect(age != null); 1541 try testing.expect(age != null);
1542 1542
1543 testing.expectEqual(@as(typ, 33), age.?); 1543 try testing.expectEqual(@as(typ, 33), age.?);
1544 } 1544 }
1545} 1545}
1546 1546
@@ -1570,8 +1570,8 @@ test "sqlite: read a single value into bool" {
1570 const b = try stmt.one(bool, .{}, .{ 1570 const b = try stmt.one(bool, .{}, .{
1571 .id = @as(usize, 20), 1571 .id = @as(usize, 20),
1572 }); 1572 });
1573 testing.expect(b != null); 1573 try testing.expect(b != null);
1574 testing.expect(b.?); 1574 try testing.expect(b.?);
1575} 1575}
1576 1576
1577test "sqlite: insert bool and bind bool" { 1577test "sqlite: insert bool and bind bool" {
@@ -1592,8 +1592,8 @@ test "sqlite: insert bool and bind bool" {
1592 const b = try stmt.one(bool, .{}, .{ 1592 const b = try stmt.one(bool, .{}, .{
1593 .is_published = true, 1593 .is_published = true,
1594 }); 1594 });
1595 testing.expect(b != null); 1595 try testing.expect(b != null);
1596 testing.expect(b.?); 1596 try testing.expect(b.?);
1597} 1597}
1598 1598
1599test "sqlite: bind string literal" { 1599test "sqlite: bind string literal" {
@@ -1611,8 +1611,8 @@ test "sqlite: bind string literal" {
1611 defer stmt.deinit(); 1611 defer stmt.deinit();
1612 1612
1613 const b = try stmt.one(usize, .{}, .{"foobar"}); 1613 const b = try stmt.one(usize, .{}, .{"foobar"});
1614 testing.expect(b != null); 1614 try testing.expect(b != null);
1615 testing.expectEqual(@as(usize, 10), b.?); 1615 try testing.expectEqual(@as(usize, 10), b.?);
1616} 1616}
1617 1617
1618test "sqlite: bind pointer" { 1618test "sqlite: bind pointer" {
@@ -1631,8 +1631,8 @@ test "sqlite: bind pointer" {
1631 stmt.reset(); 1631 stmt.reset();
1632 1632
1633 const name = try stmt.oneAlloc([]const u8, &arena.allocator, .{}, .{&test_user.id}); 1633 const name = try stmt.oneAlloc([]const u8, &arena.allocator, .{}, .{&test_user.id});
1634 testing.expect(name != null); 1634 try testing.expect(name != null);
1635 testing.expectEqualStrings(test_users[i].name, name.?); 1635 try testing.expectEqualStrings(test_users[i].name, name.?);
1636 } 1636 }
1637} 1637}
1638 1638
@@ -1660,13 +1660,13 @@ test "sqlite: read pointers" {
1660 .{}, 1660 .{},
1661 ); 1661 );
1662 1662
1663 testing.expectEqual(@as(usize, 3), rows.len); 1663 try testing.expectEqual(@as(usize, 3), rows.len);
1664 for (rows) |row, i| { 1664 for (rows) |row, i| {
1665 const exp = test_users[i]; 1665 const exp = test_users[i];
1666 testing.expectEqual(exp.id, row.id.*); 1666 try testing.expectEqual(exp.id, row.id.*);
1667 testing.expectEqualStrings(exp.name, row.name.*); 1667 try testing.expectEqualStrings(exp.name, row.name.*);
1668 testing.expectEqual(exp.age, row.age.*); 1668 try testing.expectEqual(exp.age, row.age.*);
1669 testing.expectEqual(exp.weight, row.weight.*); 1669 try testing.expectEqual(exp.weight, row.weight.*);
1670 } 1670 }
1671} 1671}
1672 1672
@@ -1691,9 +1691,9 @@ test "sqlite: optional" {
1691 .{}, 1691 .{},
1692 ); 1692 );
1693 1693
1694 testing.expect(row != null); 1694 try testing.expect(row != null);
1695 testing.expect(row.?.data == null); 1695 try testing.expect(row.?.data == null);
1696 testing.expectEqual(true, row.?.is_published.?); 1696 try testing.expectEqual(true, row.?.is_published.?);
1697} 1697}
1698 1698
1699test "sqlite: statement reset" { 1699test "sqlite: statement reset" {
@@ -1716,7 +1716,7 @@ test "sqlite: statement reset" {
1716 try stmt.exec(user); 1716 try stmt.exec(user);
1717 1717
1718 const rows_inserted = db.rowsAffected(); 1718 const rows_inserted = db.rowsAffected();
1719 testing.expectEqual(@as(usize, 1), rows_inserted); 1719 try testing.expectEqual(@as(usize, 1), rows_inserted);
1720 } 1720 }
1721} 1721}
1722 1722
@@ -1747,7 +1747,7 @@ test "sqlite: statement iterator" {
1747 try stmt.exec(user); 1747 try stmt.exec(user);
1748 1748
1749 const rows_inserted = db.rowsAffected(); 1749 const rows_inserted = db.rowsAffected();
1750 testing.expectEqual(@as(usize, 1), rows_inserted); 1750 try testing.expectEqual(@as(usize, 1), rows_inserted);
1751 } 1751 }
1752 1752
1753 // Get data with a non-allocating iterator. 1753 // Get data with a non-allocating iterator.
@@ -1768,12 +1768,12 @@ test "sqlite: statement iterator" {
1768 } 1768 }
1769 1769
1770 // Check the data 1770 // Check the data
1771 testing.expectEqual(expected_rows.items.len, rows.items.len); 1771 try testing.expectEqual(expected_rows.items.len, rows.items.len);
1772 1772
1773 for (rows.items) |row, j| { 1773 for (rows.items) |row, j| {
1774 const exp_row = expected_rows.items[j]; 1774 const exp_row = expected_rows.items[j];
1775 testing.expectEqualStrings(exp_row.name, mem.spanZ(&row.name)); 1775 try testing.expectEqualStrings(exp_row.name, mem.spanZ(&row.name));
1776 testing.expectEqual(exp_row.age, row.age); 1776 try testing.expectEqual(exp_row.age, row.age);
1777 } 1777 }
1778 } 1778 }
1779 1779
@@ -1795,12 +1795,12 @@ test "sqlite: statement iterator" {
1795 } 1795 }
1796 1796
1797 // Check the data 1797 // Check the data
1798 testing.expectEqual(expected_rows.items.len, rows.items.len); 1798 try testing.expectEqual(expected_rows.items.len, rows.items.len);
1799 1799
1800 for (rows.items) |row, j| { 1800 for (rows.items) |row, j| {
1801 const exp_row = expected_rows.items[j]; 1801 const exp_row = expected_rows.items[j];
1802 testing.expectEqualStrings(exp_row.name, row.name.data); 1802 try testing.expectEqualStrings(exp_row.name, row.name.data);
1803 testing.expectEqual(exp_row.age, row.age); 1803 try testing.expectEqual(exp_row.age, row.age);
1804 } 1804 }
1805 } 1805 }
1806} 1806}
@@ -1843,7 +1843,7 @@ test "sqlite: blob open, reopen" {
1843 var blob_reader = blob.reader(); 1843 var blob_reader = blob.reader();
1844 const data = try blob_reader.readAllAlloc(allocator, 8192); 1844 const data = try blob_reader.readAllAlloc(allocator, 8192);
1845 1845
1846 testing.expectEqualSlices(u8, blob_data1 ** 2, data); 1846 try testing.expectEqualSlices(u8, blob_data1 ** 2, data);
1847 } 1847 }
1848 1848
1849 // Reopen the blob in the second row 1849 // Reopen the blob in the second row
@@ -1860,7 +1860,7 @@ test "sqlite: blob open, reopen" {
1860 var blob_reader = blob.reader(); 1860 var blob_reader = blob.reader();
1861 const data = try blob_reader.readAllAlloc(allocator, 8192); 1861 const data = try blob_reader.readAllAlloc(allocator, 8192);
1862 1862
1863 testing.expectEqualSlices(u8, blob_data2 ** 2, data); 1863 try testing.expectEqualSlices(u8, blob_data2 ** 2, data);
1864 } 1864 }
1865 1865
1866 try blob.close(); 1866 try blob.close();
@@ -1875,9 +1875,9 @@ test "sqlite: failing open" {
1875 .open_flags = .{}, 1875 .open_flags = .{},
1876 .mode = .{ .File = "/tmp/not_existing.db" }, 1876 .mode = .{ .File = "/tmp/not_existing.db" },
1877 }); 1877 });
1878 testing.expectError(error.SQLiteCantOpen, res); 1878 try testing.expectError(error.SQLiteCantOpen, res);
1879 testing.expectEqual(@as(usize, 14), diags.err.?.code); 1879 try testing.expectEqual(@as(usize, 14), diags.err.?.code);
1880 testing.expectEqualStrings("unable to open database file", diags.err.?.message); 1880 try testing.expectEqualStrings("unable to open database file", diags.err.?.message);
1881} 1881}
1882 1882
1883test "sqlite: failing prepare statement" { 1883test "sqlite: failing prepare statement" {
@@ -1886,11 +1886,11 @@ test "sqlite: failing prepare statement" {
1886 var diags: Diagnostics = undefined; 1886 var diags: Diagnostics = undefined;
1887 1887
1888 const result = db.prepareWithDiags("SELECT id FROM foobar", .{ .diags = &diags }); 1888 const result = db.prepareWithDiags("SELECT id FROM foobar", .{ .diags = &diags });
1889 testing.expectError(error.SQLiteError, result); 1889 try testing.expectError(error.SQLiteError, result);
1890 1890
1891 const detailed_err = db.getDetailedError(); 1891 const detailed_err = db.getDetailedError();
1892 testing.expectEqual(@as(usize, 1), detailed_err.code); 1892 try testing.expectEqual(@as(usize, 1), detailed_err.code);
1893 testing.expectEqualStrings("no such table: foobar", detailed_err.message); 1893 try testing.expectEqualStrings("no such table: foobar", detailed_err.message);
1894} 1894}
1895 1895
1896test "sqlite: diagnostics format" { 1896test "sqlite: diagnostics format" {
@@ -1935,7 +1935,7 @@ test "sqlite: diagnostics format" {
1935 var buf: [1024]u8 = undefined; 1935 var buf: [1024]u8 = undefined;
1936 const str = try std.fmt.bufPrint(&buf, "my diagnostics: {s}", .{tc.input}); 1936 const str = try std.fmt.bufPrint(&buf, "my diagnostics: {s}", .{tc.input});
1937 1937
1938 testing.expectEqualStrings(tc.exp, str); 1938 try testing.expectEqualStrings(tc.exp, str);
1939 } 1939 }
1940} 1940}
1941 1941