database icon indicating copy to clipboard operation
database copied to clipboard

Update SelectQuery.php

Open brasizza opened this issue 8 months ago • 2 comments

🔍 What was changed

Adding fetch() simplifies common one-row use cases, makes intent explicit, and avoids the perf overhead of materialising an unused array. image

🤔 Why?

  1. Clearer intent fetch() makes it obvious we expect one record (or null). Code reviewers no longer have to parse an indexing expression to know this.

  2. Avoids unnecessary allocations fetchAll() builds a PHP array for every row returned by the database driver. When the caller only needs the first row, we: allocate and populate an array for all rows, immediately destroy most of it, pay the GC cost later.

fetch() stops reading after the first result, closes the cursor in the finally, and releases the driver buffer early. This is a measurable win on large result sets and in tight loops.

  1. Reduces boilerplate and error-prone code No more [0] ?? null, no more manual casts, no more “did we forget to check the array isn’t empty?” bugs. The new method returns:

an array representing the row (default FETCH_ASSOC), or false when the result set is empty (mirrors PDO).

  • How was this tested:
    • [X] Tested manually
    • [ ] Unit tests added

brasizza avatar Apr 23 '25 13:04 brasizza

I like the idea. Need to think about the best name for the method: fetch(), fetchOne(), first(), fetchFirst().

Still need to add tests

roxblnfk avatar Apr 23 '25 15:04 roxblnfk

I just keep the sema logic with fetchAll and fetch from the pdo

brasizza avatar Apr 23 '25 15:04 brasizza