Skip to content

Commit

Permalink
example(hostname): error handling, pg_export
Browse files Browse the repository at this point in the history
  • Loading branch information
divyenduz committed Oct 22, 2024
1 parent c4cb75a commit ad33dc4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/pghostname_zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.name = "pg_audit_zig",
.name = "pghostname_zig",
.version = "0.1.0",
.paths = .{
"extension",
Expand Down
1 change: 1 addition & 0 deletions examples/pghostname_zig/expected/pghostname_zig_test.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE EXTENSION pghostname_zig;
SELECT COALESCE(length(pghostname_zig()), 0) > 0;
INFO: hostname: ubuntu buffer:ubuntu
?column?
----------
t
Expand Down
15 changes: 12 additions & 3 deletions examples/pghostname_zig/src/main.zig
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
const std = @import("std");
const builtin = @import("builtin");
const pgzx = @import("pgzx");

comptime {
pgzx.PG_MODULE_MAGIC();

pgzx.PG_FUNCTION_V1("pghostname_zig", pghostname_zig);
pgzx.PG_EXPORT(@This());
}

fn pghostname_zig() ![]const u8 {
pub fn pghostname_zig() ![]const u8 {
var buffer: [std.posix.HOST_NAME_MAX]u8 = undefined;
const hostname = try std.posix.gethostname(&buffer);
const hostname = std.posix.gethostname(&buffer) catch |err| switch (err) {
error.PermissionDenied => {
return "unknown";
},
error.Unexpected => {
return "unknown";
},
};
pgzx.elog.Info(@src(), "hostname: {s} buffer:{s}", .{ hostname, buffer });
return hostname;
}

Expand Down

0 comments on commit ad33dc4

Please sign in to comment.