Skip to content

Commit

Permalink
query: add count(), add/update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cztomsik committed Aug 22, 2024
1 parent 708263d commit 6316a8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/query.zig
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ pub fn Query(comptime T: type, comptime R: type) type {
return stmt.value(V, self.session.arena);
}

pub fn exists(self: Q) !bool {
return try self.valueRaw(bool, "1") orelse false;
}

pub fn count(self: Q, comptime col: Col) !u64 {
return (try self.valueRaw(u64, "COUNT(" ++ @tagName(col) ++ ")")).?;
}
Expand Down
37 changes: 22 additions & 15 deletions src/session.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,43 @@ const Person = struct {
};

fn open() !Session {
var conn = try Connection.open(@import("sqlite.zig").SQLite3, .{ .filename = ":memory:" });
errdefer conn.deinit();
var db = try Session.open(@import("sqlite.zig").SQLite3, t.allocator, .{ .filename = ":memory:" });
errdefer db.deinit();

try conn.execAll(
try db.conn.execAll(
\\CREATE TABLE Person (id INTEGER PRIMARY KEY, name TEXT);
\\INSERT INTO Person (name) VALUES ('Alice');
\\INSERT INTO Person (name) VALUES ('Bob');
);

return Session.init(t.allocator, conn);
}

fn close(db: *Session) void {
db.deinit();
db.conn.deinit();
return db;
}

test "db.prepare()" {
var db = try open();
defer close(&db);
defer db.deinit();

var stmt = try db.prepare("SELECT 1 + ?", .{1});
defer stmt.deinit();

try t.expectEqual(2, try stmt.value(u32, db.arena));
}

test "db.query(T).xxx() value methods" {
var db = try open();
defer db.deinit();

var q = db.query(Person);

try t.expectEqual(true, q.exists());
try t.expectEqual(2, q.count(.id));
try t.expectEqual(1, q.min(.id));
try t.expectEqual(2, q.max(.id));
}

test "db.query(T).findAll()" {
var db = try open();
defer close(&db);
defer db.deinit();

try t.expectEqualDeep(&[_]Person{
.{ .id = 1, .name = "Alice" },
Expand All @@ -131,7 +138,7 @@ test "db.query(T).findAll()" {

test "db.find(T, id)" {
var db = try open();
defer close(&db);
defer db.deinit();

try t.expectEqualDeep(
Person{ .id = 1, .name = "Alice" },
Expand All @@ -141,7 +148,7 @@ test "db.find(T, id)" {

test "db.insert(T, data)" {
var db = try open();
defer close(&db);
defer db.deinit();

_ = try db.insert(Person, .{ .name = "Charlie" });
try t.expectEqualDeep(3, db.conn.lastInsertRowId());
Expand All @@ -150,7 +157,7 @@ test "db.insert(T, data)" {

test "db.update(T, id, data)" {
var db = try open();
defer close(&db);
defer db.deinit();

try db.update(Person, 1, .{ .name = "Sarah" });
try t.expectEqual(1, db.conn.rowsAffected());
Expand All @@ -159,7 +166,7 @@ test "db.update(T, id, data)" {

test "db.delete(T, id)" {
var db = try open();
defer close(&db);
defer db.deinit();

try db.delete(Person, 1);
try t.expectEqual(1, db.conn.rowsAffected());
Expand Down

0 comments on commit 6316a8b

Please sign in to comment.