A SpaceTraders Agent
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add sqlite3 database

Altagos d4890ee5 3156930b

+104 -14
+2
.gitignore
··· 1 1 .zig-cache 2 2 zig-out 3 + 4 + *.db
+39
build.zig
··· 7 7 8 8 const known_folders = b.dependency("known_folders", .{}).module("known-folders"); 9 9 const pretty = b.dependency("pretty", .{}).module("pretty"); 10 + const zqlite = b.dependency("zqlite", .{}).module("zqlite"); 11 + 12 + const sqlite3 = b.dependency("sqlite3", .{}); 13 + 14 + const mod_sqlite3 = b.createModule(.{ 15 + .target = target, 16 + .optimize = optimize, 17 + .link_libc = true, 18 + }); 19 + mod_sqlite3.addIncludePath(sqlite3.path("")); 20 + mod_sqlite3.addCSourceFile(.{ 21 + .file = sqlite3.path("sqlite3.c"), 22 + .flags = &[_][]const u8{ 23 + "-DSQLITE_DQS=0", 24 + "-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1", 25 + "-DSQLITE_USE_ALLOCA=1", 26 + "-DSQLITE_THREADSAFE=1", 27 + "-DSQLITE_TEMP_STORE=3", 28 + "-DSQLITE_ENABLE_API_ARMOR=1", 29 + "-DSQLITE_ENABLE_UNLOCK_NOTIFY", 30 + "-DSQLITE_DEFAULT_FILE_PERMISSIONS=0600", 31 + "-DSQLITE_OMIT_DECLTYPE=1", 32 + "-DSQLITE_OMIT_DEPRECATED=1", 33 + "-DSQLITE_OMIT_LOAD_EXTENSION=1", 34 + "-DSQLITE_OMIT_PROGRESS_CALLBACK=1", 35 + "-DSQLITE_OMIT_SHARED_CACHE", 36 + "-DSQLITE_OMIT_TRACE=1", 37 + "-DSQLITE_OMIT_UTF16=1", 38 + "-DHAVE_USLEEP=0", 39 + }, 40 + }); 41 + 42 + const libsqlite = b.addLibrary(.{ 43 + .name = "sqlite3", 44 + .linkage = .static, 45 + .root_module = mod_sqlite3, 46 + }); 10 47 11 48 const meta = b.createModule(.{ 12 49 .root_source_file = b.path("src/meta/root.zig"), ··· 29 66 .optimize = optimize, 30 67 .imports = &.{ 31 68 .{ .name = "known-folders", .module = known_folders }, 69 + .{ .name = "zqlite", .module = zqlite }, 32 70 // Modules 33 71 .{ .name = "st", .module = st }, 34 72 .{ .name = "meta", .module = meta }, 35 73 }, 36 74 }); 75 + agent.linkLibrary(libsqlite); 37 76 38 77 const space = b.createModule(.{ 39 78 .root_source_file = b.path("src/main.zig"),
+8
build.zig.zon
··· 10 10 .url = "git+https://git.sr.ht/~altagos/pretty?ref=main#a025f81f4b9a1a6be483a4c33fafc6e0ef5cae6f", 11 11 .hash = "pretty-0.1.0--Y3D26JKAAABPfRqFy6oUakwiYAawqqbU6xF0_5ziP-l", 12 12 }, 13 + .zqlite = .{ 14 + .url = "git+https://github.com/karlseguin/zqlite.zig?ref=master#b44ed5cdc64859b08446c246f307e65ebe2b2d9c", 15 + .hash = "zqlite-0.0.0-RWLaY_y_mADh2LdbDrG_2HT2dBAcsAR8Jig_7-dOJd0B", 16 + }, 17 + .sqlite3 = .{ 18 + .url = "https://www.sqlite.org/2026/sqlite-amalgamation-3510200.zip", 19 + .hash = "N-V-__8AALNmqgBz73I7J8GlQS_kCPTRgxS0rm8gXcEM8Evk", 20 + }, 13 21 }, 14 22 .minimum_zig_version = "0.16.0-dev.1246+4b593a6c2", 15 23 .paths = .{
+10
src/agent/config.zig
··· 9 9 .agent = "Bearer <TOKEN>", 10 10 }, 11 11 12 + db: DBConfig = .{ 13 + .path = "./space.db", 14 + .pool_size = 5, 15 + }, 16 + 12 17 pub fn updateAgentToken(self: *Config, token: []const u8) void { 13 18 self.auth.agent = "Bearer " ++ token; 14 19 } 20 + }; 21 + 22 + pub const DBConfig = struct { 23 + path: []const u8, 24 + pool_size: u32, 15 25 }; 16 26 17 27 const log = std.log.scoped(.agent);
+32
src/agent/data.zig
··· 1 + const std = @import("std"); 2 + const zqlite = @import("zqlite"); 3 + 4 + const mod = @import("root.zig"); 5 + 6 + const log = std.log.scoped(.@"space db"); 7 + 8 + pub const DB = struct { 9 + pool: *zqlite.Pool, 10 + 11 + pub fn open(allocator: std.mem.Allocator, config: mod.config.DBConfig) !DB { 12 + log.debug("Creating DB Pool size={} path={s}", .{ config.pool_size, config.path }); 13 + 14 + const path = try allocator.dupeZ(u8, config.path); 15 + defer allocator.free(path); 16 + 17 + const pool = try zqlite.Pool.init(allocator, .{ 18 + .path = path, 19 + .size = config.pool_size, 20 + .on_first_connection = &DB.onFirstConnection, 21 + }); 22 + return .{ .pool = pool }; 23 + } 24 + 25 + pub fn deinit(this: *DB) void { 26 + this.pool.deinit(); 27 + } 28 + 29 + fn onFirstConnection(_: zqlite.Conn, _: ?*anyopaque) !void { 30 + log.info("Connected to database", .{}); 31 + } 32 + };
+1
src/agent/root.zig
··· 1 1 pub const config = @import("config.zig"); 2 + pub const data = @import("data.zig"); 2 3 3 4 test { 4 5 const testing = @import("std").testing;
+12 -14
src/main.zig
··· 21 21 config: *const Config, 22 22 client: *Client, 23 23 ) !void { 24 - _ = gpa; 25 - _ = config; 26 - // _ = io; 27 - // _ = client; 24 + var db = try agent.data.DB.open(gpa, config.db); 25 + defer db.deinit(); 28 26 29 - var register_f = try api.agent.register(client, "ALTAGOS", .ANCIENTS); 30 - defer _ = register_f.cancel(io) catch {}; 27 + // var register_f = try api.agent.register(client, "ALTAGOS", .ANCIENTS); 28 + // defer _ = register_f.cancel(io) catch {}; 31 29 32 - const register = try register_f.await(io); 33 - defer register.deinit(); 30 + // const register = try register_f.await(io); 31 + // defer register.deinit(); 34 32 35 - std.log.info("Registered agent\n{f}", .{pretty(register.value)}); 33 + // std.log.info("Registered agent\n{f}", .{pretty(register.value)}); 36 34 37 - // var fleet_f = try api.fleet.listShips(client, .{}); 38 - // defer _ = fleet_f.cancel(io) catch {}; 35 + var fleet_f = try api.fleet.listShips(client, .{}); 36 + defer _ = fleet_f.cancel(io) catch {}; 39 37 40 - // const fleet = try fleet_f.await(io); 41 - // defer fleet.deinit(); 38 + const fleet = try fleet_f.await(io); 39 + defer fleet.deinit(); 42 40 43 - // std.log.info("{f}", .{pretty(fleet.value)}); 41 + std.log.info("{f}", .{pretty(fleet.value)}); 44 42 } 45 43 46 44 pub fn main(init: std.process.Init) !void {