Closed Krymancer closed 8 months ago
I've thinking about this and for now I'm "solving" this parsing the data direclty.
var result = try global.pool.query("select criartransacao($1, $2, $3, $4);", .{ id, valor, payload.tipo, payload.descricao });
defer result.deinit();
const row = try result.next();
const raw = row.?.get([]u8, 0);
// First byte indicates how many items we have in the record
const len = std.mem.readInt(i32, raw[0..4], .big);
if (len == 1) {
// Don't really care for the next two bytes, the value is in [12..16]
const status_value = std.mem.readInt(i32, raw[12..16], .big);
const status: CreateTransactionReturn = @enumFromInt(status_value);
switch (status) { ... }
}
// If len is not 1 we expect two values so...
const saldo = std.mem.readInt(i32, raw[12..16], .big);
// We have more two bytes that we don't care here so...
const limite = std.mem.readInt(i32, raw[24..28], .big);
Maybe when I more familiar with zig and postgres I can make a PR to try getting records directly
There's support for the record type now. The API is simple and, to avoid allocations, I opted to force reading the columns in order (rather than being able to specify an ordinal position).
FWIW, since your query is only expecting a single row, you can use pool.row(...)
which wraps result
+ result.next
. deinit
has to be called on the row, since it's a special row (internally it holds the result)
var row = (try pool.row(....)).?
defer row.deinit()
I'm not sure if this is possible but I have a function in postgress that return a record.
Basicly it will return (x) if something wrong happen and if is ok will return (a,b)
there is the function:
My first guess was that I cloud get the raw data and if the len of the data was 1, I had an error, otherwhite I would have on a on data[0] and b on data[1]. Not what I am seeing here.
This is a snnipet of what I'm trying to do
right now I'm working with this
I'm guessing that the 4 first values represents the quantity of the data, like, when I expect a error to return like (2) the frist record is 00001, I'm not sure what 23 and 4 means here, and 2 is the value I expect
in the middle one seems similar, 2 for the two results, 23, and 4 (I guess is shows that is an integer or smothing) and them the value.
I'm sorry if this is a dumb question but I'm lost into this, have no Idea how to convert this to the data I expect, hope someone can help me or show me an easy way to do this?