From d4890ee55b3d5efc4a789069dc59f6e6e0f42506 Mon Sep 17 00:00:00 2001 From: Altagos Date: Wed, 28 Jan 2026 15:16:27 +0100 Subject: [PATCH] add sqlite3 database --- .gitignore | 2 ++ build.zig | 39 +++++++++++++++++++++++++++++++++++++++ build.zig.zon | 8 ++++++++ src/agent/config.zig | 10 ++++++++++ src/agent/data.zig | 32 ++++++++++++++++++++++++++++++++ src/agent/root.zig | 1 + src/main.zig | 26 ++++++++++++-------------- 7 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 src/agent/data.zig diff --git a/.gitignore b/.gitignore index d8c8979..fd65636 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .zig-cache zig-out + +*.db diff --git a/build.zig b/build.zig index 7358140..46f28bb 100644 --- a/build.zig +++ b/build.zig @@ -7,6 +7,43 @@ pub fn build(b: *Build) void { const known_folders = b.dependency("known_folders", .{}).module("known-folders"); const pretty = b.dependency("pretty", .{}).module("pretty"); + const zqlite = b.dependency("zqlite", .{}).module("zqlite"); + + const sqlite3 = b.dependency("sqlite3", .{}); + + const mod_sqlite3 = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + }); + mod_sqlite3.addIncludePath(sqlite3.path("")); + mod_sqlite3.addCSourceFile(.{ + .file = sqlite3.path("sqlite3.c"), + .flags = &[_][]const u8{ + "-DSQLITE_DQS=0", + "-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1", + "-DSQLITE_USE_ALLOCA=1", + "-DSQLITE_THREADSAFE=1", + "-DSQLITE_TEMP_STORE=3", + "-DSQLITE_ENABLE_API_ARMOR=1", + "-DSQLITE_ENABLE_UNLOCK_NOTIFY", + "-DSQLITE_DEFAULT_FILE_PERMISSIONS=0600", + "-DSQLITE_OMIT_DECLTYPE=1", + "-DSQLITE_OMIT_DEPRECATED=1", + "-DSQLITE_OMIT_LOAD_EXTENSION=1", + "-DSQLITE_OMIT_PROGRESS_CALLBACK=1", + "-DSQLITE_OMIT_SHARED_CACHE", + "-DSQLITE_OMIT_TRACE=1", + "-DSQLITE_OMIT_UTF16=1", + "-DHAVE_USLEEP=0", + }, + }); + + const libsqlite = b.addLibrary(.{ + .name = "sqlite3", + .linkage = .static, + .root_module = mod_sqlite3, + }); const meta = b.createModule(.{ .root_source_file = b.path("src/meta/root.zig"), @@ -29,11 +66,13 @@ pub fn build(b: *Build) void { .optimize = optimize, .imports = &.{ .{ .name = "known-folders", .module = known_folders }, + .{ .name = "zqlite", .module = zqlite }, // Modules .{ .name = "st", .module = st }, .{ .name = "meta", .module = meta }, }, }); + agent.linkLibrary(libsqlite); const space = b.createModule(.{ .root_source_file = b.path("src/main.zig"), diff --git a/build.zig.zon b/build.zig.zon index 8b3de74..aa90b01 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -10,6 +10,14 @@ .url = "git+https://git.sr.ht/~altagos/pretty?ref=main#a025f81f4b9a1a6be483a4c33fafc6e0ef5cae6f", .hash = "pretty-0.1.0--Y3D26JKAAABPfRqFy6oUakwiYAawqqbU6xF0_5ziP-l", }, + .zqlite = .{ + .url = "git+https://github.com/karlseguin/zqlite.zig?ref=master#b44ed5cdc64859b08446c246f307e65ebe2b2d9c", + .hash = "zqlite-0.0.0-RWLaY_y_mADh2LdbDrG_2HT2dBAcsAR8Jig_7-dOJd0B", + }, + .sqlite3 = .{ + .url = "https://www.sqlite.org/2026/sqlite-amalgamation-3510200.zip", + .hash = "N-V-__8AALNmqgBz73I7J8GlQS_kCPTRgxS0rm8gXcEM8Evk", + }, }, .minimum_zig_version = "0.16.0-dev.1246+4b593a6c2", .paths = .{ diff --git a/src/agent/config.zig b/src/agent/config.zig index fa99cbf..4dd8c7a 100644 --- a/src/agent/config.zig +++ b/src/agent/config.zig @@ -9,11 +9,21 @@ pub const Config = struct { .agent = "Bearer ", }, + db: DBConfig = .{ + .path = "./space.db", + .pool_size = 5, + }, + pub fn updateAgentToken(self: *Config, token: []const u8) void { self.auth.agent = "Bearer " ++ token; } }; +pub const DBConfig = struct { + path: []const u8, + pool_size: u32, +}; + const log = std.log.scoped(.agent); pub fn load(io: std.Io, allocator: std.mem.Allocator, environ: *std.process.Environ.Map) !Config { diff --git a/src/agent/data.zig b/src/agent/data.zig new file mode 100644 index 0000000..20f7e27 --- /dev/null +++ b/src/agent/data.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const zqlite = @import("zqlite"); + +const mod = @import("root.zig"); + +const log = std.log.scoped(.@"space db"); + +pub const DB = struct { + pool: *zqlite.Pool, + + pub fn open(allocator: std.mem.Allocator, config: mod.config.DBConfig) !DB { + log.debug("Creating DB Pool size={} path={s}", .{ config.pool_size, config.path }); + + const path = try allocator.dupeZ(u8, config.path); + defer allocator.free(path); + + const pool = try zqlite.Pool.init(allocator, .{ + .path = path, + .size = config.pool_size, + .on_first_connection = &DB.onFirstConnection, + }); + return .{ .pool = pool }; + } + + pub fn deinit(this: *DB) void { + this.pool.deinit(); + } + + fn onFirstConnection(_: zqlite.Conn, _: ?*anyopaque) !void { + log.info("Connected to database", .{}); + } +}; diff --git a/src/agent/root.zig b/src/agent/root.zig index 30fc341..4a2cabf 100644 --- a/src/agent/root.zig +++ b/src/agent/root.zig @@ -1,4 +1,5 @@ pub const config = @import("config.zig"); +pub const data = @import("data.zig"); test { const testing = @import("std").testing; diff --git a/src/main.zig b/src/main.zig index 5522045..a7b8be2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,26 +21,24 @@ fn juicyMain( config: *const Config, client: *Client, ) !void { - _ = gpa; - _ = config; - // _ = io; - // _ = client; + var db = try agent.data.DB.open(gpa, config.db); + defer db.deinit(); - var register_f = try api.agent.register(client, "ALTAGOS", .ANCIENTS); - defer _ = register_f.cancel(io) catch {}; + // var register_f = try api.agent.register(client, "ALTAGOS", .ANCIENTS); + // defer _ = register_f.cancel(io) catch {}; - const register = try register_f.await(io); - defer register.deinit(); + // const register = try register_f.await(io); + // defer register.deinit(); - std.log.info("Registered agent\n{f}", .{pretty(register.value)}); + // std.log.info("Registered agent\n{f}", .{pretty(register.value)}); - // var fleet_f = try api.fleet.listShips(client, .{}); - // defer _ = fleet_f.cancel(io) catch {}; + var fleet_f = try api.fleet.listShips(client, .{}); + defer _ = fleet_f.cancel(io) catch {}; - // const fleet = try fleet_f.await(io); - // defer fleet.deinit(); + const fleet = try fleet_f.await(io); + defer fleet.deinit(); - // std.log.info("{f}", .{pretty(fleet.value)}); + std.log.info("{f}", .{pretty(fleet.value)}); } pub fn main(init: std.process.Init) !void { -- 2.51.2