IrenejMarc/dpq

Result iteration issues

Closed this issue · 2 comments

I am unable to iterate on a set of results, this is my query code:

auto result = database.Query("
    SELECT * FROM player
    WHERE guild_id = $1
    ORDER BY last_message DESC
    LIMIT 2
").run(guildID);

If I manually perform an identical query to the database, I get one result.
In the code, if I do writeln(result.rows) I get 1, which matches expectations.

However, if I attempt to .map! on the result, the callback isn't evaluated even a single time. I'm not sure why.

As a workaround, I tried to loop using while, .empty and .popFront:

Player[] players;
while (!result.empty) {

    players ~= [result.front.deserialise!Player];
    result.popFront;

}

This won't evaluate either. In this case, the bug occurs on this line, in particular, the -1 shouldn't be here (0 >= 1-1 is true, not what we wanted).

o3o commented

Try:

Query q = database.Query("
    SELECT * FROM player
    WHERE guild_id = $1
    ORDER BY last_message DESC
    LIMIT 2
");
q.addParam(guildID); // or q << guildID
Player[] players;
foreach (r; q.run()) {
   Player p = deserialise!Player(r);
   players ~= p;
}

I guess you are right about what causes this bug. It seems I had the same issue quite long time ago and even did a commit to fix it in my fork, but it didn't get to the original repo. Hopefully, I'll readdress this issue in the weekend as well as some other minor fixes.