latitude icon indicating copy to clipboard operation
latitude copied to clipboard

Make all query building methods append-only

Open shadowhand opened this issue 7 years ago • 2 comments

Refer to changes make in #58; the following methods should be modified:

  • Merge functionality of addColumns() into columns()
  • Merge functionality of addFrom() into from()

shadowhand avatar May 01 '18 16:05 shadowhand

The ability to add a column or overwrite all is good functionality.

I can think of a good case where this wouldn't be a desired change, if you wanted to run a query i.e. a complex search with a limit of 10 results. But you need to show the total number of results too.

So Query A contains the full query:

$queryA = $builder->select(a.*, b*)
               ->from('a'...
               ->join('b'...
               ->join('c'...
               ->where...
               ->limit(10)
               ->offset(0)

Query B is almost entirely the same query, minus the limit / offset and swapping the select for a count

$queryB = $queryA->columns(fn('COUNT', 'a.id'));
               ->limit(null)
               ->offset(null)

This would then allow a streamlined way of displaying "Showing results 10 of 1000".

If the columns functionality only supported appending, the query would be vastly less efficient.

Keeping addColumns and Columns as separate functions makes perfect sense to me

LincolnLex44 avatar May 08 '18 09:05 LincolnLex44

Yes. This is valuable for pagination: Pass the full SelectQuery to the paginator and it generically create the COUNT query from any other.

pavarnos avatar Jun 17 '20 22:06 pavarnos