Skip to content

Commit

Permalink
query: rename all/first/firstWhere back to findXxx()
Browse files Browse the repository at this point in the history
  • Loading branch information
cztomsik committed Aug 14, 2024
1 parent e069327 commit 138c810
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/migrate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn migrate(allocator: std.mem.Allocator, filename: [:0]const u8, ddl: []cons
fn migrateObjects(db: *Session, pristine: *Session, kind: []const u8) !void {
for (try sqlite_master.objects(pristine, kind)) |obj| {
// Check if object exists
const curr = try db.query(sqlite_master).where(.type, kind).firstWhere(.name, obj.name) orelse {
const curr = try db.query(sqlite_master).where(.type, kind).findBy(.name, obj.name) orelse {
logObject(obj, .create);
try db.conn.execAll(obj.sql);
continue;
Expand Down Expand Up @@ -94,7 +94,7 @@ fn migrateObjects(db: *Session, pristine: *Session, kind: []const u8) !void {
// Now we can check for extraneous objects and drop them

for (try sqlite_master.objects(db, kind)) |obj| {
if (try pristine.query(sqlite_master).where(.type, kind).firstWhere(.name, obj.name) == null) {
if (try pristine.query(sqlite_master).where(.type, kind).findBy(.name, obj.name) == null) {
logObject(obj, .drop);

const drop_sql = try std.fmt.allocPrint(db.arena, "DROP {s} {s}", .{ kind, obj.name });
Expand All @@ -116,6 +116,6 @@ const sqlite_master = struct {
return db.query(sqlite_master)
.where(.type, kind)
.whereRaw("name NOT LIKE ?", .{"sqlite_%"})
.all();
.findAll();
}
};
16 changes: 10 additions & 6 deletions src/query.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,22 @@ pub fn Query(comptime T: type, comptime R: type) type {
try stmt.exec();
}

pub fn first(self: Q) !?R {
pub fn find(self: Q, id: std.meta.FieldType(T, .id)) !?R {
return self.findBy(.id, id);
}

pub fn findBy(self: Q, comptime col: Col, val: std.meta.FieldType(T, col)) !?R {
return self.where(col, val).findFirst();
}

pub fn findFirst(self: Q) !?R {
var stmt = try self.limit(1).prepare();
defer stmt.deinit();

return stmt.row(R);
}

pub fn firstWhere(self: Q, comptime col: Col, val: std.meta.FieldType(T, col)) !?R {
return self.where(col, val).first();
}

pub fn all(self: Q) ![]const R {
pub fn findAll(self: Q) ![]const R {
var stmt = try self.prepare();
defer stmt.deinit();

Expand Down
4 changes: 2 additions & 2 deletions src/session.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub const Session = struct {

/// Find a record by its primary key.
pub fn find(self: *Session, comptime T: type, id: std.meta.FieldType(T, .id)) !?T {
return self.query(T).firstWhere(.id, id);
return self.query(T).find(id);
}

/// Insert a new record.
Expand Down Expand Up @@ -131,7 +131,7 @@ test "db.query(T).findAll()" {
try t.expectEqualDeep(&[_]Person{
.{ .id = 1, .name = "Alice" },
.{ .id = 2, .name = "Bob" },
}, db.query(Person).all());
}, db.query(Person).findAll());
}

test "db.find(T, id)" {
Expand Down

0 comments on commit 138c810

Please sign in to comment.