I'm trying to work with the c postgres library. I can create a connection correctly, however I'm not able to create a struct around it:
const Pg = struct {
conn: *pq.PGconn,
pub fn init(addr: []const u8) !*Pg {
var conn = pq.PQconnectdb(addr.ptr);
if (pq.PQstatus(conn) != pq.CONNECTION_OK) {
pq.PQfinish(conn);
std.log.err("{s}", .{pq.PQerrorMessage(conn)});
return error.ConnectionError;
}
var pg = Pg{ .conn = conn.? };
return &pg;
}
pub fn exec(self: *Pg, query: []const u8) !*pq.PGresult {
var res = pq.PQexec(self.conn, query.ptr);
switch (pq.PQresultStatus(res)) {
pq.PGRES_EMPTY_QUERY => return error.ExecEmpty,
else => return error.ExecUnknown,
}
return res;
}
The init function works correctly, but the exec function throws a segfault.
If I move the code from exec inside init, everything seems to be working ok
The segfault happens at the var res = pq.PQexec(self.conn, query.ptr); line.
Any ideas??
Thanks!:)