A SpaceTraders Agent
1const std = @import("std");
2const zqlite = @import("zqlite");
3
4const mod = @import("root.zig");
5
6const log = std.log.scoped(.@"space db");
7
8pub 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};