Here is the project files:
// main.zig
const std = @import("std");
const pg = @import("pg");
pub fn main() !void {
var pool = try pg.Pool.init(std.heap.c_allocator, .{ .size = 5, .connect = .{
.port = 5432,
.host = "...",
}, .auth = .{
.username = "postgres",
.database = "...",
.password = "...",
.timeout = 10_000,
} });
defer pool.deinit();
var result = try pool.query("SELECT tag from params_table LIMIT 100", .{});
defer result.deinit();
while (try result.next()) |row| {
const tag_name = row.get([]u8, 0);
std.debug.print("tag: {s}\n", .{tag_name});
}
}
// build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "test_pg",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "pg", .module = b.dependency("pg", .{}).module("pg") },
},
}),
});
b.installArtifact(exe);
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
}
The example runs on native build but with an error during the build (how is that possible btw?):
$ zig build run
run
└─ run exe test_pg
└─ install
└─ install test_pg
└─ compile exe test_pg Debug native failure
error: warning: unable to open library directory '/opt/openssl/lib': FileNotFound
tag: ... (correct output)
When I cross-compile for arm-linux-gnueabihf, I get (see next message):