prefect server in zig
at main 145 lines 4.5 kB view raw
1const std = @import("std"); 2const Allocator = std.mem.Allocator; 3 4const backend = @import("backend.zig"); 5const log = @import("../logging.zig"); 6 7pub const VariableRow = struct { 8 id: []const u8, 9 created: []const u8, 10 updated: []const u8, 11 name: []const u8, 12 value: []const u8, 13 tags: []const u8, 14}; 15 16const Col = struct { 17 const id: usize = 0; 18 const created: usize = 1; 19 const updated: usize = 2; 20 const name: usize = 3; 21 const value: usize = 4; 22 const tags: usize = 5; 23}; 24 25const select_cols = "id, created, updated, name, value, tags"; 26 27fn rowFromResult(alloc: Allocator, r: anytype) !VariableRow { 28 return VariableRow{ 29 .id = try alloc.dupe(u8, r.text(Col.id)), 30 .created = try alloc.dupe(u8, r.text(Col.created)), 31 .updated = try alloc.dupe(u8, r.text(Col.updated)), 32 .name = try alloc.dupe(u8, r.text(Col.name)), 33 .value = try alloc.dupe(u8, r.text(Col.value)), 34 .tags = try alloc.dupe(u8, r.text(Col.tags)), 35 }; 36} 37 38pub fn getById(alloc: Allocator, id: []const u8) !?VariableRow { 39 var r = backend.db.row( 40 "SELECT " ++ select_cols ++ " FROM variable WHERE id = ?", 41 .{id}, 42 ) catch return null; 43 44 if (r) |*row| { 45 defer row.deinit(); 46 return try rowFromResult(alloc, row); 47 } 48 return null; 49} 50 51pub fn getByName(alloc: Allocator, name: []const u8) !?VariableRow { 52 var r = backend.db.row( 53 "SELECT " ++ select_cols ++ " FROM variable WHERE name = ?", 54 .{name}, 55 ) catch return null; 56 57 if (r) |*row| { 58 defer row.deinit(); 59 return try rowFromResult(alloc, row); 60 } 61 return null; 62} 63 64pub fn insert(id: []const u8, name: []const u8, value: []const u8, tags: []const u8, created: []const u8) !void { 65 backend.db.exec( 66 "INSERT INTO variable (id, name, value, tags, created, updated) VALUES (?, ?, ?, ?, ?, ?)", 67 .{ id, name, value, tags, created, created }, 68 ) catch |err| { 69 log.err("database", "insert variable error: {}", .{err}); 70 return err; 71 }; 72} 73 74pub fn updateById(id: []const u8, name: ?[]const u8, value: ?[]const u8, tags: ?[]const u8, updated: []const u8) !bool { 75 const affected = backend.db.execWithRowCount( 76 "UPDATE variable SET name = COALESCE(?, name), value = COALESCE(?, value), tags = COALESCE(?, tags), updated = ? WHERE id = ?", 77 .{ name, value, tags, updated, id }, 78 ) catch |err| { 79 log.err("database", "update variable error: {}", .{err}); 80 return err; 81 }; 82 return affected > 0; 83} 84 85pub fn updateByName(name: []const u8, new_name: ?[]const u8, value: ?[]const u8, tags: ?[]const u8, updated: []const u8) !bool { 86 const affected = backend.db.execWithRowCount( 87 "UPDATE variable SET name = COALESCE(?, name), value = COALESCE(?, value), tags = COALESCE(?, tags), updated = ? WHERE name = ?", 88 .{ new_name, value, tags, updated, name }, 89 ) catch |err| { 90 log.err("database", "update variable error: {}", .{err}); 91 return err; 92 }; 93 return affected > 0; 94} 95 96pub fn deleteById(id: []const u8) !bool { 97 const affected = backend.db.execWithRowCount( 98 "DELETE FROM variable WHERE id = ?", 99 .{id}, 100 ) catch |err| { 101 log.err("database", "delete variable error: {}", .{err}); 102 return err; 103 }; 104 return affected > 0; 105} 106 107pub fn deleteByName(name: []const u8) !bool { 108 const affected = backend.db.execWithRowCount( 109 "DELETE FROM variable WHERE name = ?", 110 .{name}, 111 ) catch |err| { 112 log.err("database", "delete variable error: {}", .{err}); 113 return err; 114 }; 115 return affected > 0; 116} 117 118pub fn list(alloc: Allocator, limit: usize, offset: usize) ![]VariableRow { 119 var results = std.ArrayListUnmanaged(VariableRow){}; 120 errdefer results.deinit(alloc); 121 122 var rows = backend.db.query( 123 "SELECT " ++ select_cols ++ " FROM variable ORDER BY name ASC LIMIT ? OFFSET ?", 124 .{ @as(i64, @intCast(limit)), @as(i64, @intCast(offset)) }, 125 ) catch |err| { 126 log.err("database", "list variables error: {}", .{err}); 127 return err; 128 }; 129 defer rows.deinit(); 130 131 while (rows.next()) |r| { 132 try results.append(alloc, try rowFromResult(alloc, &r)); 133 } 134 135 return results.toOwnedSlice(alloc); 136} 137 138pub fn count() !usize { 139 var r = backend.db.row("SELECT COUNT(*) FROM variable", .{}) catch return 0; 140 if (r) |*row| { 141 defer row.deinit(); 142 return @intCast(row.bigint(0)); 143 } 144 return 0; 145}