const std = @import("std"); const Allocator = std.mem.Allocator; const backend = @import("backend.zig"); const log = @import("../logging.zig"); const Col = enum(u8) { id = 0, created = 1, flow_run_id = 2, type_ = 3, name = 4, timestamp = 5 }; pub const FlowRunStateRow = struct { id: []const u8, created: []const u8, flow_run_id: []const u8, type: []const u8, name: []const u8, timestamp: []const u8, pub fn toJson(self: FlowRunStateRow, alloc: Allocator) ![]const u8 { var output: std.Io.Writer.Allocating = .init(alloc); var jw: std.json.Stringify = .{ .writer = &output.writer }; try jw.beginObject(); try jw.objectField("id"); try jw.write(self.id); try jw.objectField("created"); try jw.write(self.created); try jw.objectField("flow_run_id"); try jw.write(self.flow_run_id); try jw.objectField("type"); try jw.write(self.type); try jw.objectField("name"); try jw.write(self.name); try jw.objectField("timestamp"); try jw.write(self.timestamp); try jw.endObject(); return output.toOwnedSlice(); } }; fn rowFromResult(alloc: Allocator, r: anytype) !FlowRunStateRow { return FlowRunStateRow{ .id = try alloc.dupe(u8, r.text(@intFromEnum(Col.id))), .created = try alloc.dupe(u8, r.text(@intFromEnum(Col.created))), .flow_run_id = try alloc.dupe(u8, r.text(@intFromEnum(Col.flow_run_id))), .type = try alloc.dupe(u8, r.text(@intFromEnum(Col.type_))), .name = try alloc.dupe(u8, r.text(@intFromEnum(Col.name))), .timestamp = try alloc.dupe(u8, r.text(@intFromEnum(Col.timestamp))), }; } pub fn getById(alloc: Allocator, id: []const u8) !?FlowRunStateRow { var r = backend.db.row( "SELECT id, created, flow_run_id, type, name, timestamp FROM flow_run_state WHERE id = ?", .{id}, ) catch return null; if (r) |*row| { defer row.deinit(); return try rowFromResult(alloc, row); } return null; } pub fn listByFlowRunId(alloc: Allocator, flow_run_id: []const u8) ![]FlowRunStateRow { var results = std.ArrayListUnmanaged(FlowRunStateRow){}; errdefer results.deinit(alloc); var rows = backend.db.query( "SELECT id, created, flow_run_id, type, name, timestamp FROM flow_run_state WHERE flow_run_id = ? ORDER BY timestamp ASC", .{flow_run_id}, ) catch |err| { log.err("database", "list flow_run_states error: {}", .{err}); return err; }; defer rows.deinit(); while (rows.next()) |r| { try results.append(alloc, try rowFromResult(alloc, &r)); } return results.toOwnedSlice(alloc); }